freedombone-app-mailpile 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # mailpile app
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS="full full-vim mailbox"
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. MAILPILE_DOMAIN_NAME=
  32. MAILPILE_CODE=
  33. MAILPILE_ONION_PORT=8103
  34. MAILPILE_REPO="https://github.com/mailpile/Mailpile"
  35. MAILPILE_COMMIT='f82074d2ab5ccd65d14a6b3c6cd65aeb132831d7'
  36. MAILPILE_PORT=33411
  37. mailpile_variables=(MAILPILE_REPO
  38. MAILPILE_DOMAIN_NAME
  39. MAILPILE_CODE
  40. ONION_ONLY
  41. DDNS_PROVIDER
  42. DEFAULT_DOMAIN_NAME
  43. MY_USERNAME)
  44. function logging_on_mailpile {
  45. echo -n ''
  46. }
  47. function logging_off_mailpile {
  48. echo -n ''
  49. }
  50. function remove_user_mailpile {
  51. remove_username="$1"
  52. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp mailpile
  53. }
  54. function add_user_mailpile {
  55. "${PROJECT_NAME}-pass" -u "$1" -a mailpile -p "$2"
  56. }
  57. function install_interactive_mailpile {
  58. if [ ! "$ONION_ONLY" ]; then
  59. ONION_ONLY='no'
  60. fi
  61. if [[ $ONION_ONLY != "no" ]]; then
  62. MAILPILE_DOMAIN_NAME='mailpile.local'
  63. write_config_param "MAILPILE_DOMAIN_NAME" "$MAILPILE_DOMAIN_NAME"
  64. else
  65. function_check interactive_site_details
  66. interactive_site_details "mailpile" "MAILPILE_DOMAIN_NAME" "MAILPILE_CODE"
  67. fi
  68. APP_INSTALLED=1
  69. }
  70. function change_password_mailpile {
  71. echo -n ''
  72. #${PROJECT_NAME}-pass -u $1 -a mailpile -p "$2"
  73. }
  74. function reconfigure_mailpile {
  75. echo -n ''
  76. }
  77. function upgrade_mailpile {
  78. read_config_param "MAILPILE_DOMAIN_NAME"
  79. CURR_COMMIT=$MAILPILE_COMMIT
  80. if grep -q "mailpile commit" "$COMPLETION_FILE"; then
  81. CURR_COMMIT=$(get_completion_param "mailpile commit")
  82. fi
  83. if [[ "$CURR_COMMIT" == "$MAILPILE_COMMIT" ]]; then
  84. return
  85. fi
  86. function_check set_repo_commit
  87. set_repo_commit /var/www/$MAILPILE_DOMAIN_NAME/mail "mailpile commit" "$MAILPILE_COMMIT" $MAILPILE_REPO
  88. cd "/var/www/$MAILPILE_DOMAIN_NAME/mail" || exit 2346836535
  89. pip install -r requirements.txt
  90. chown -R mailpile:mailpile "/var/www/$MAILPILE_DOMAIN_NAME/mail"
  91. }
  92. function backup_local_mailpile {
  93. echo -n ''
  94. }
  95. function restore_local_mailpile {
  96. echo -n ''
  97. }
  98. function backup_remote_mailpile {
  99. echo -n ''
  100. }
  101. function restore_remote_mailpile {
  102. echo -n ''
  103. }
  104. function remove_mailpile {
  105. if [ ${#MAILPILE_DOMAIN_NAME} -eq 0 ]; then
  106. return
  107. fi
  108. systemctl stop mailpile
  109. systemctl disable mailpile
  110. rm /etc/systemd/system/mailpile.service
  111. systemctl daemon-reload
  112. read_config_param "MAILPILE_DOMAIN_NAME"
  113. nginx_dissite "$MAILPILE_DOMAIN_NAME"
  114. remove_certs "${MAILPILE_DOMAIN_NAME}"
  115. if [ -f "/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME" ]; then
  116. rm -f "/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME"
  117. fi
  118. if [ -d "/var/www/$MAILPILE_DOMAIN_NAME" ]; then
  119. rm -rf "/var/www/$MAILPILE_DOMAIN_NAME"
  120. fi
  121. function_check remove_ddns_domain
  122. remove_ddns_domain "$MAILPILE_DOMAIN_NAME"
  123. groupdel -f mailpile
  124. userdel -r mailpile
  125. remove_config_param MAILPILE_DOMAIN_NAME
  126. remove_config_param MAILPILE_CODE
  127. function_check remove_onion_service
  128. remove_onion_service mailpile ${MAILPILE_ONION_PORT}
  129. remove_completion_param "install_mailpile"
  130. enable_email_encryption_at_rest
  131. sed -i '/Mailpile/d' "$COMPLETION_FILE"
  132. sed -i '/mailpile/d' "$COMPLETION_FILE"
  133. }
  134. function install_mailpile {
  135. if [ ! $ONION_ONLY ]; then
  136. ONION_ONLY='no'
  137. fi
  138. if [ ! $MAILPILE_DOMAIN_NAME ]; then
  139. echo $'The mailpile domain name was not specified'
  140. exit 63824
  141. fi
  142. apt-get -yq install python-pip python-lxml python-dev libjpeg-dev
  143. if [ ! -d /var/www/$MAILPILE_DOMAIN_NAME ]; then
  144. mkdir /var/www/$MAILPILE_DOMAIN_NAME
  145. fi
  146. cd "/var/www/$MAILPILE_DOMAIN_NAME" || exit 264826484
  147. if [ -d /var/www/$MAILPILE_DOMAIN_NAME/mail ]; then
  148. rm -rf /var/www/$MAILPILE_DOMAIN_NAME/mail
  149. fi
  150. if [ -d /repos/mailpile ]; then
  151. mkdir mail
  152. cp -r -p /repos/mailpile/. mail
  153. cd mail || exit 245728482
  154. git pull
  155. else
  156. git_clone $MAILPILE_REPO mail
  157. fi
  158. cd mail || exit 246872468
  159. git checkout $MAILPILE_COMMIT -b $MAILPILE_COMMIT
  160. set_completion_param "mailpile commit" "$MAILPILE_COMMIT"
  161. if [ ! -f requirements-dev.txt ]; then
  162. echo $'No python requirements file found'
  163. exit 62382
  164. fi
  165. pip install -r requirements.txt
  166. adduser --system --home=/var/www/$MAILPILE_DOMAIN_NAME/mail/ --group mailpile
  167. adduser mailpile debian-tor
  168. adduser mailpile www-data
  169. adduser mailpile mail
  170. adduser mailpile "$MY_USERNAME"
  171. if [[ "$ONION_ONLY" == 'no' ]]; then
  172. chgrp -R ssl-cert /etc/letsencrypt
  173. chmod -R g=rX /etc/letsencrypt
  174. usermod -a -G ssl-cert mailpile
  175. fi
  176. chown -R mailpile: /var/www/$MAILPILE_DOMAIN_NAME/mail/
  177. # create folders and tags
  178. su -c "cd /var/www/$MAILPILE_DOMAIN_NAME/mail && ./mp --setup" - mailpile
  179. MAILPILE_ONION_HOSTNAME=$(add_onion_service mailpile 80 ${MAILPILE_ONION_PORT})
  180. { echo '[Unit]';
  181. echo 'Description=Mailpile Email Client';
  182. echo 'After=syslog.target network.target nginx.target';
  183. echo '';
  184. echo '[Service]';
  185. echo 'User=mailpile';
  186. echo 'Group=mailpile';
  187. echo "WorkingDirectory=/var/www/$MAILPILE_DOMAIN_NAME/mail";
  188. echo "ExecStart=/var/www/$MAILPILE_DOMAIN_NAME/mail/mp --www=127.0.0.1:${MAILPILE_PORT} --wait";
  189. echo 'Restart=always';
  190. echo 'RestartSec=10';
  191. echo '';
  192. echo '[Install]';
  193. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/mailpile.service
  194. chmod +x /etc/systemd/system/mailpile.service
  195. mailpile_nginx_site=/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME
  196. if [[ $ONION_ONLY == "no" ]]; then
  197. function_check nginx_http_redirect
  198. nginx_http_redirect $MAILPILE_DOMAIN_NAME
  199. { echo 'server {';
  200. echo ' listen 443 ssl;';
  201. echo ' #listen [::]:443 ssl;';
  202. echo " server_name $MAILPILE_DOMAIN_NAME;";
  203. echo '';
  204. echo ' # Security'; } >> $mailpile_nginx_site
  205. function_check nginx_ssl
  206. nginx_ssl $MAILPILE_DOMAIN_NAME
  207. function_check nginx_security_options
  208. nginx_security_options $MAILPILE_DOMAIN_NAME
  209. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  210. echo '';
  211. echo ' # Logs';
  212. echo ' access_log /dev/null;';
  213. echo ' error_log /dev/null;';
  214. echo '';
  215. echo ' # Root';
  216. echo " root /var/www/$MAILPILE_DOMAIN_NAME/mail;";
  217. echo '';
  218. echo ' location / {'; } >> $mailpile_nginx_site
  219. function_check nginx_limits
  220. nginx_limits $MAILPILE_DOMAIN_NAME '15m'
  221. { echo " rewrite /(.*) /\$1 break;";
  222. echo " proxy_set_header X-Real-IP \$remote_addr;";
  223. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  224. echo " proxy_set_header Host \$http_host;";
  225. echo ' proxy_set_header X-NginX-Proxy true;';
  226. echo " proxy_pass http://localhost:${MAILPILE_PORT};";
  227. echo ' proxy_redirect off;';
  228. echo ' }';
  229. echo '}';
  230. echo ''; } >> $mailpile_nginx_site
  231. else
  232. echo -n '' > $mailpile_nginx_site
  233. fi
  234. { echo 'server {';
  235. echo " listen 127.0.0.1:$MAILPILE_ONION_PORT default_server;";
  236. echo " server_name $MAILPILE_ONION_HOSTNAME;";
  237. echo ''; } >> $mailpile_nginx_site
  238. function_check nginx_security_options
  239. nginx_security_options $MAILPILE_DOMAIN_NAME
  240. { echo '';
  241. echo ' # Logs';
  242. echo ' access_log /dev/null;';
  243. echo ' error_log /dev/null;';
  244. echo '';
  245. echo ' # Root';
  246. echo " root /var/www/$MAILPILE_DOMAIN_NAME/mail;";
  247. echo '';
  248. echo ' location / {'; } >> $mailpile_nginx_site
  249. function_check nginx_limits
  250. nginx_limits $MAILPILE_DOMAIN_NAME '15m'
  251. { echo " rewrite /(.*) /\$1 break;";
  252. echo " proxy_set_header X-Real-IP \$remote_addr;";
  253. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  254. echo " proxy_set_header Host \$http_host;";
  255. echo ' proxy_set_header X-NginX-Proxy true;';
  256. echo " proxy_pass http://localhost:${MAILPILE_PORT};";
  257. echo ' proxy_redirect off;';
  258. echo ' }';
  259. echo '}'; } >> $mailpile_nginx_site
  260. function_check create_site_certificate
  261. if [ ! -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem ]; then
  262. create_site_certificate $MAILPILE_DOMAIN_NAME 'yes'
  263. fi
  264. if [ -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.crt ]; then
  265. mv /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.crt /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
  266. fi
  267. if [ -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem ]; then
  268. chown root:root /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
  269. sed -i "s|.crt|.pem|g" /etc/nginx/sites-available/${MAILPILE_DOMAIN_NAME}
  270. fi
  271. if [ -f /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key ]; then
  272. chown root:root /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key
  273. fi
  274. function_check nginx_ensite
  275. nginx_ensite $MAILPILE_DOMAIN_NAME
  276. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a mailpile -p "*"
  277. function_check add_ddns_domain
  278. add_ddns_domain $MAILPILE_DOMAIN_NAME
  279. disable_email_encryption_at_rest
  280. # if strict https is enforced then buttons don't work. This is probably a security bug
  281. sed -i "/Content-Security-Policy/d" $mailpile_nginx_site
  282. # link to the administrator's keyring
  283. if [ -d /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg ]; then
  284. mv /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg_orig
  285. fi
  286. cp -r "/home/$MY_USERNAME/.gnupg" "/var/www/$MAILPILE_DOMAIN_NAME/mail/"
  287. chown -R mailpile:mailpile /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg
  288. chmod +x /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg
  289. pip install jinja2==2.9.6
  290. pip install pgpdump==1.5
  291. # turn off ssl in dovecot
  292. sed -i 's|#ssl =.*|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
  293. sed -i 's|ssl =.*|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
  294. # set ssl certs, just in case we want to use them later
  295. if [[ "$ONION_ONLY" == "no" ]]; then
  296. if [ -f "/etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem" ]; then
  297. sed -i "s|#ssl_cert =.*|ssl_cert = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  298. sed -i "s|ssl_cert =.*|ssl_cert = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  299. sed -i "s|#ssl_key =.*|ssl_key = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/privkey.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  300. sed -i "s|ssl_key =.*|ssl_key = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/privkey.pem|g" /etc/dovecot/conf.d/10-ssl.conf
  301. fi
  302. groupadd ssl-cert
  303. usermod -a -G ssl-cert dovecot
  304. fi
  305. systemctl restart dovecot
  306. systemctl enable mailpile
  307. systemctl daemon-reload
  308. systemctl start mailpile
  309. systemctl restart nginx
  310. set_completion_param "mailpile domain" "$MAILPILE_DOMAIN_NAME"
  311. APP_INSTALLED=1
  312. }
  313. # NOTE: deliberately no exit 0