freedombone-app-mailpile 12KB

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