freedombone-app-webmail 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. webmail_variables=(WEBMAIL_REPO
  39. WEBMAIL_COMMIT
  40. MY_USERNAME
  41. ONION_ONLY
  42. DEFAULT_DOMAIN_NAME)
  43. function install_interactive_webmail {
  44. echo -n ''
  45. }
  46. function change_password_webmail {
  47. echo -n ''
  48. }
  49. function get_mariadb_webmail_admin_password {
  50. if [ -f /home/$MY_USERNAME/README ]; then
  51. if grep -q "MariaDB webmail admin password" /home/$MY_USERNAME/README; then
  52. WEBMAIL_ADMIN_PASSWORD=$(cat /home/$MY_USERNAME/README | grep "MariaDB webmail admin password" | awk -F ':' '{print $2}' | sed 's/^ *//')
  53. fi
  54. fi
  55. }
  56. function webmail_create_database {
  57. function_check get_mariadb_webmail_admin_password
  58. get_mariadb_webmail_admin_password
  59. if [ ! $WEBMAIL_ADMIN_PASSWORD ]; then
  60. if [ -f $IMAGE_PASSWORD_FILE ]; then
  61. WEBMAIL_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  62. else
  63. WEBMAIL_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  64. fi
  65. fi
  66. if [ ! $WEBMAIL_ADMIN_PASSWORD ]; then
  67. return
  68. fi
  69. function_check create_database
  70. create_database webmail "$WEBMAIL_ADMIN_PASSWORD"
  71. }
  72. function reconfigure_webmail {
  73. echo -n ''
  74. }
  75. function upgrade_webmail {
  76. function_check set_repo_commit
  77. set_repo_commit $WEBMAIL_PATH "webmail commit" "$WEBMAIL_COMMIT" $WEBMAIL_REPO
  78. }
  79. function backup_local_webmail {
  80. if ! grep -q "install_webmail" $COMPLETION_FILE; then
  81. return
  82. fi
  83. echo $"Backing up webmail"
  84. function_check backup_database_to_usb
  85. backup_database_to_usb webmail
  86. echo $"Backing up webmail complete"
  87. }
  88. function restore_local_webmail {
  89. if ! grep -q "install_webmail" $COMPLETION_FILE; then
  90. return
  91. fi
  92. echo $"Restoring webmail"
  93. function_check webmail_create_database
  94. webmail_create_database
  95. function_check restore_database
  96. restore_database webmail
  97. echo $"Restore of webmail complete"
  98. }
  99. function backup_remote_webmail {
  100. echo -n ''
  101. }
  102. function restore_remote_webmail {
  103. echo -n ''
  104. }
  105. function remove_webmail {
  106. if [[ $(app_is_installed webmail) == "0" ]]; then
  107. return
  108. fi
  109. nginx_dissite webmail
  110. if [ -f /etc/nginx/sites-available/webmail ]; then
  111. rm /etc/nginx/sites-available/webmail
  112. fi
  113. function_check remove_onion_service
  114. remove_onion_service webmail ${WEBMAIL_ONION_PORT}
  115. function_check drop_database
  116. drop_database webmail
  117. if [ -d $WEB_PATH/webmail ]; then
  118. rm -rf $WEB_PATH/webmail
  119. fi
  120. sed -i '/Webmail /d' $COMPLETION_FILE
  121. sed -i '/install_webmail/d' $COMPLETION_FILE
  122. }
  123. function install_webmail {
  124. if [ -d /etc/apache2 ]; then
  125. rm -rf /etc/apache2
  126. echo $'Removed Apache installation after Dokuwiki install'
  127. fi
  128. function_check install_mariadb
  129. install_mariadb
  130. function_check get_mariadb_password
  131. get_mariadb_password
  132. function_check repair_databases_script
  133. repair_databases_script
  134. apt-get -y install php5-common php5-cli php5-curl php5-gd php5-mysql php5-mcrypt git
  135. apt-get -y install php5-dev imagemagick php5-imagick php5-sqlite php-auth-sasl php-net-smtp php-mime-type
  136. apt-get -y install php-mail-mime php-mail-mimedecode php-net-ldap3 php5-pspell
  137. pear install Net_IDNA2
  138. if [ ! -f $WEBMAIL_PATH/index.php ]; then
  139. cd $WEB_PATH
  140. git_clone $WEBMAIL_REPO webmail
  141. cd $WEBMAIL_PATH
  142. git checkout $WEBMAIL_COMMIT -b $WEBMAIL_COMMIT
  143. if ! grep -q "webmail commit" $COMPLETION_FILE; then
  144. echo "webmail commit:$WEBMAIL_COMMIT" >> $COMPLETION_FILE
  145. else
  146. sed -i "s/webmail commit.*/webmail commit:$WEBMAIL_COMMIT/g" $COMPLETION_FILE
  147. fi
  148. fi
  149. if [ ! -f $WEBMAIL_PATH/index.php ]; then
  150. echo $'Did not clone webmail repo'
  151. exit 52825
  152. fi
  153. WEBMAIL_ONION_HOSTNAME=$(add_onion_service webmail 80 ${WEBMAIL_ONION_PORT})
  154. webmail_create_database
  155. mysql -u root --password="$MARIADB_PASSWORD" -D webmail < $WEBMAIL_PATH/SQL/mysql.initial.sql
  156. if [ ! -d /var/www/$DEFAULT_DOMAIN_NAME/htdocs ]; then
  157. mkdir -p /var/www/$DEFAULT_DOMAIN_NAME/htdocs
  158. fi
  159. ln -s $WEBMAIL_PATH /var/www/$DEFAULT_DOMAIN_NAME/htdocs/webmail
  160. if [ ! -f /var/www/webmail/config/config.inc.php ]; then
  161. # generate the configuration
  162. echo '<?php' > /var/www/webmail/config/config.inc.php
  163. echo "\$config['db_dsnw'] = 'mysql://root:${MARIADB_PASSWORD}@localhost/webmail';" >> /var/www/webmail/config/config.inc.php
  164. echo "\$config['default_host'] = 'localhost';" >> /var/www/webmail/config/config.inc.php
  165. echo "\$config['support_url'] = '';" >> /var/www/webmail/config/config.inc.php
  166. WEBMAIL_DES_KEY="$(create_password 25)"
  167. echo "\$config['des_key'] = '${WEBMAIL_DES_KEY}';" >> /var/www/webmail/config/config.inc.php
  168. echo "\$config['product_name'] = '${PROJECT_NAME}';" >> /var/www/webmail/config/config.inc.php
  169. echo "\$config['plugins'] = array('enigma');" >> /var/www/webmail/config/config.inc.php
  170. echo "\$config['mime_param_folding'] = 0;" >> /var/www/webmail/config/config.inc.php
  171. echo "\$config['enable_installer'] = false;" >> /var/www/webmail/config/config.inc.php
  172. fi
  173. chown -R www-data:www-data $WEBMAIL_PATH
  174. echo 'server {' > /etc/nginx/sites-available/webmail
  175. echo " listen 127.0.0.1:$WEBMAIL_ONION_PORT default_server;" >> /etc/nginx/sites-available/webmail
  176. echo " server_name $WEBMAIL_ONION_HOSTNAME;" >> /etc/nginx/sites-available/webmail
  177. echo " root ${WEBMAIL_PATH};" >> /etc/nginx/sites-available/webmail
  178. echo ' index index.php index.html index.htm;' >> /etc/nginx/sites-available/webmail
  179. echo ' access_log off;' >> /etc/nginx/sites-available/webmail
  180. echo ' error_log off;' >> /etc/nginx/sites-available/webmail
  181. echo ' location / {' >> /etc/nginx/sites-available/webmail
  182. echo ' try_files $uri $uri/ /index.php?q=$uri&$args;' >> /etc/nginx/sites-available/webmail
  183. echo ' }' >> /etc/nginx/sites-available/webmail
  184. echo ' error_page 404 /404.html;' >> /etc/nginx/sites-available/webmail
  185. echo ' error_page 500 502 503 504 /50x.html;' >> /etc/nginx/sites-available/webmail
  186. echo ' location = /50x.html {' >> /etc/nginx/sites-available/webmail
  187. echo " root ${WEBMAIL_PATH};" >> /etc/nginx/sites-available/webmail
  188. echo ' }' >> /etc/nginx/sites-available/webmail
  189. echo ' location ~ ^/(README.md|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {' >> /etc/nginx/sites-available/webmail
  190. echo ' deny all;' >> /etc/nginx/sites-available/webmail
  191. echo ' }' >> /etc/nginx/sites-available/webmail
  192. echo ' location ~ ^/(config|temp|logs)/ {' >> /etc/nginx/sites-available/webmail
  193. echo ' deny all;' >> /etc/nginx/sites-available/webmail
  194. echo ' }' >> /etc/nginx/sites-available/webmail
  195. echo ' location ~ /\. {' >> /etc/nginx/sites-available/webmail
  196. echo ' deny all;' >> /etc/nginx/sites-available/webmail
  197. echo ' access_log off;' >> /etc/nginx/sites-available/webmail
  198. echo ' log_not_found off;' >> /etc/nginx/sites-available/webmail
  199. echo ' }' >> /etc/nginx/sites-available/webmail
  200. echo ' location ~ \.php$ {' >> /etc/nginx/sites-available/webmail
  201. echo ' try_files $uri =404;' >> /etc/nginx/sites-available/webmail
  202. echo ' fastcgi_pass unix:/var/run/php5-fpm.sock;' >> /etc/nginx/sites-available/webmail
  203. echo ' fastcgi_index index.php;' >> /etc/nginx/sites-available/webmail
  204. echo ' fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;' >> /etc/nginx/sites-available/webmail
  205. echo ' include fastcgi_params;' >> /etc/nginx/sites-available/webmail
  206. echo ' }' >> /etc/nginx/sites-available/webmail
  207. echo ' add_header X-Frame-Options DENY;' >> /etc/nginx/sites-available/webmail
  208. echo ' add_header X-Content-Type-Options nosniff;' >> /etc/nginx/sites-available/webmail
  209. echo ' client_max_body_size 15m;' >> /etc/nginx/sites-available/webmail
  210. echo '}' >> /etc/nginx/sites-available/webmail
  211. nginx_ensite webmail
  212. systemctl restart php5-fpm
  213. systemctl restart nginx
  214. if ! grep -q "Webmail" /home/$MY_USERNAME/README; then
  215. echo '' >> /home/$MY_USERNAME/README
  216. echo '' >> /home/$MY_USERNAME/README
  217. echo 'Webmail' >> /home/$MY_USERNAME/README
  218. echo '=======' >> /home/$MY_USERNAME/README
  219. if [[ $ONION_ONLY == 'no' ]]; then
  220. echo $"Open https://$DEFAULT_DOMAIN_NAME/webmail/index.php" >> /home/$MY_USERNAME/README
  221. else
  222. echo $"Open http://$WEBMAIL_ONION_HOSTNAME" >> /home/$MY_USERNAME/README
  223. fi
  224. echo $"MariaDB webmail admin password: $WEBMAIL_ADMIN_PASSWORD" >> /home/$MY_USERNAME/README
  225. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  226. chmod 600 /home/$MY_USERNAME/README
  227. fi
  228. }
  229. # NOTE: deliberately no exit 0