freedombone-app-mailpile 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 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='6f56fe4ad736c8e385bea658454bed110d08c60d'
  38. MAILPILE_PORT=33411
  39. mailpile_variables=(MAILPILE_REPO
  40. MAILPILE_DOMAIN_NAME
  41. MAILPILE_CODE
  42. ONION_ONLY
  43. DDNS_PROVIDER
  44. MY_USERNAME)
  45. function remove_user_mailpile {
  46. remove_username="$1"
  47. ${PROJECT_NAME}-pass -u $remove_username --rmapp mailpile
  48. }
  49. function add_user_mailpile {
  50. ${PROJECT_NAME}-pass -u $1 -a mailpile -p "$2"
  51. }
  52. function install_interactive_mailpile {
  53. if [ ! $ONION_ONLY ]; then
  54. ONION_ONLY='no'
  55. fi
  56. if [[ $ONION_ONLY != "no" ]]; then
  57. MAILPILE_DOMAIN_NAME='mailpile.local'
  58. write_config_param "MAILPILE_DOMAIN_NAME" "$MAILPILE_DOMAIN_NAME"
  59. else
  60. function_check interactive_site_details
  61. interactive_site_details "mailpile" "MAILPILE_DOMAIN_NAME" "MAILPILE_CODE"
  62. fi
  63. APP_INSTALLED=1
  64. }
  65. function change_password_mailpile {
  66. echo -n ''
  67. #${PROJECT_NAME}-pass -u $1 -a mailpile -p "$2"
  68. }
  69. function reconfigure_mailpile {
  70. echo -n ''
  71. }
  72. function upgrade_mailpile {
  73. read_config_param "MAILPILE_DOMAIN_NAME"
  74. upgrade_mp=
  75. CURR_COMMIT=$MAILPILE_COMMIT
  76. if grep -q "mailpile commit" $COMPLETION_FILE; then
  77. CURR_COMMIT=$(get_completion_param "mailpile commit")
  78. fi
  79. if [[ "$CURR_COMMIT" != "$MAILPILE_COMMIT" ]]; then
  80. upgrade_mp=1
  81. fi
  82. function_check set_repo_commit
  83. set_repo_commit /var/www/$MAILPILE_DOMAIN_NAME/mail "mailpile commit" "$MAILPILE_COMMIT" $MAILPILE_REPO
  84. if [ $upgrade_mp ]; then
  85. cd /var/www/$MAILPILE_DOMAIN_NAME/mail
  86. pip install -r requirements.txt
  87. chown -R mailpile:mailpile /var/www/$MAILPILE_DOMAIN_NAME/mail
  88. fi
  89. }
  90. function backup_local_mailpile {
  91. if [ ! -f /etc/systemd/system/mailpile.service ]; then
  92. return
  93. fi
  94. MAILPILE_DOMAIN_NAME='mailpile.local'
  95. if grep -q "mailpile domain" $COMPLETION_FILE; then
  96. MAILPILE_DOMAIN_NAME=$(get_completion_param "mailpile domain")
  97. fi
  98. source_directory=/var/www/${MAILPILE_DOMAIN_NAME}/mail/.local
  99. if [ -d $source_directory ]; then
  100. systemctl stop mailpile
  101. function_check backup_directory_to_usb
  102. backup_directory_to_usb $source_directory mailpile
  103. systemctl start mailpile
  104. fi
  105. }
  106. function restore_local_mailpile {
  107. if [ ! -f /etc/systemd/system/mailpile.service ]; then
  108. return
  109. fi
  110. MAILPILE_DOMAIN_NAME='mailpile.local'
  111. if grep -q "mailpile domain" $COMPLETION_FILE; then
  112. MAILPILE_DOMAIN_NAME=$(get_completion_param "mailpile domain")
  113. fi
  114. if [ $MAILPILE_DOMAIN_NAME ]; then
  115. systemctl stop mailpile
  116. temp_restore_dir=/root/tempmailpile
  117. restore_directory_from_usb $temp_restore_dir mailpile
  118. if [ -d /var/www/${MAILPILE_DOMAIN_NAME}/mail/.local ]; then
  119. mv /var/www/${MAILPILE_DOMAIN_NAME}/mail/.local /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous
  120. fi
  121. temp_source_dir=$(find ${temp_restore_dir} -name ".local")
  122. cp -r ${temp_source_dir} /var/www/${MAILPILE_DOMAIN_NAME}/mail/
  123. if [ ! "$?" = "0" ]; then
  124. if [ -d mv /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous ]; then
  125. if [ -d /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous ]; then
  126. rm -rf /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous
  127. fi
  128. mv /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous /var/www/${MAILPILE_DOMAIN_NAME}/mail/.local
  129. fi
  130. backup_unmount_drive
  131. exit 3685
  132. fi
  133. rm -rf ${temp_restore_dir}
  134. chown -R mailpile: /var/www/$MAILPILE_DOMAIN_NAME/mail/
  135. if [ -d /etc/letsencrypt/live/${MAILPILE_DOMAIN_NAME} ]; then
  136. ln -s /etc/letsencrypt/live/${MAILPILE_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key
  137. ln -s /etc/letsencrypt/live/${MAILPILE_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
  138. fi
  139. systemctl start mailpile
  140. fi
  141. }
  142. function backup_remote_mailpile {
  143. if [ ! -f /etc/systemd/system/mailpile.service ]; then
  144. return
  145. fi
  146. MAILPILE_DOMAIN_NAME='mailpile.local'
  147. if grep -q "mailpile domain" $COMPLETION_FILE; then
  148. MAILPILE_DOMAIN_NAME=$(get_completion_param "mailpile domain")
  149. fi
  150. source_directory=/var/www/${MAILPILE_DOMAIN_NAME}/mail/.local
  151. if [ -d $source_directory ]; then
  152. systemctl stop mailpile
  153. function_check backup_directory_to_usb
  154. backup_directory_to_friend $source_directory mailpile
  155. systemctl start mailpile
  156. fi
  157. }
  158. function restore_remote_mailpile {
  159. if [ ! -f /etc/systemd/system/mailpile.service ]; then
  160. return
  161. fi
  162. MAILPILE_DOMAIN_NAME='mailpile.local'
  163. if grep -q "mailpile domain" $COMPLETION_FILE; then
  164. MAILPILE_DOMAIN_NAME=$(get_completion_param "mailpile domain")
  165. fi
  166. if [ $MAILPILE_DOMAIN_NAME ]; then
  167. systemctl stop mailpile
  168. temp_restore_dir=/root/tempmailpile
  169. restore_directory_from_friend $temp_restore_dir mailpile
  170. if [ -d /var/www/${MAILPILE_DOMAIN_NAME}/mail/.local ]; then
  171. mv /var/www/${MAILPILE_DOMAIN_NAME}/mail/.local /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous
  172. fi
  173. temp_source_dir=$(find ${temp_restore_dir} -name ".local")
  174. cp -r ${temp_source_dir} /var/www/${MAILPILE_DOMAIN_NAME}/mail/
  175. if [ ! "$?" = "0" ]; then
  176. if [ -d mv /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous ]; then
  177. mv /var/www/${MAILPILE_DOMAIN_NAME}/mail/.previous /var/www/${MAILPILE_DOMAIN_NAME}/mail/.local
  178. fi
  179. backup_unmount_drive
  180. exit 36732
  181. fi
  182. rm -rf ${temp_restore_dir}
  183. chown -R mailpile: /var/www/$MAILPILE_DOMAIN_NAME/mail/
  184. if [ -d /etc/letsencrypt/live/${MAILPILE_DOMAIN_NAME} ]; then
  185. ln -s /etc/letsencrypt/live/${MAILPILE_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key
  186. ln -s /etc/letsencrypt/live/${MAILPILE_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
  187. fi
  188. systemctl start mailpile
  189. fi
  190. }
  191. function remove_mailpile {
  192. if [ ${#MAILPILE_DOMAIN_NAME} -eq 0 ]; then
  193. return
  194. fi
  195. systemctl stop mailpile
  196. systemctl disable mailpile
  197. rm /etc/systemd/system/mailpile.service
  198. read_config_param "MAILPILE_DOMAIN_NAME"
  199. nginx_dissite $MAILPILE_DOMAIN_NAME
  200. remove_certs ${MAILPILE_DOMAIN_NAME}
  201. if [ -f /etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME ]; then
  202. rm -f /etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME
  203. fi
  204. if [ -d /var/www/$MAILPILE_DOMAIN_NAME ]; then
  205. rm -rf /var/www/$MAILPILE_DOMAIN_NAME
  206. fi
  207. function_check remove_ddns_domain
  208. remove_ddns_domain $MAILPILE_DOMAIN_NAME
  209. deluser --remove-all-files mailpile
  210. remove_config_param MAILPILE_DOMAIN_NAME
  211. remove_config_param MAILPILE_CODE
  212. function_check remove_onion_service
  213. remove_onion_service mailpile ${MAILPILE_ONION_PORT}
  214. remove_completion_param "install_mailpile"
  215. enable_email_encryption_at_rest
  216. sed -i '/Mailpile/d' $COMPLETION_FILE
  217. sed -i '/mailpile/d' $COMPLETION_FILE
  218. }
  219. function install_mailpile {
  220. if [ ! $ONION_ONLY ]; then
  221. ONION_ONLY='no'
  222. fi
  223. if [ ! $MAILPILE_DOMAIN_NAME ]; then
  224. echo $'The mailpile domain name was not specified'
  225. exit 63824
  226. fi
  227. apt-get -yq install python-pip python-lxml python-dev libjpeg-dev
  228. if [ ! -d /var/www/$MAILPILE_DOMAIN_NAME ]; then
  229. mkdir /var/www/$MAILPILE_DOMAIN_NAME
  230. fi
  231. cd /var/www/$MAILPILE_DOMAIN_NAME
  232. if [ -d /var/www/$MAILPILE_DOMAIN_NAME/mail ]; then
  233. rm -rf /var/www/$MAILPILE_DOMAIN_NAME/mail
  234. fi
  235. git_clone $MAILPILE_REPO mail
  236. cd mail
  237. git checkout $MAILPILE_COMMIT -b $MAILPILE_COMMIT
  238. set_completion_param "mailpile commit" "$MAILPILE_COMMIT"
  239. if [ ! -f requirements-dev.txt ]; then
  240. echo $'No python requirements file found'
  241. exit 62382
  242. fi
  243. pip install -r requirements.txt
  244. adduser --system --home=/var/www/$MAILPILE_DOMAIN_NAME/mail/ --group mailpile
  245. adduser mailpile debian-tor
  246. adduser mailpile www-data
  247. adduser mailpile mail
  248. adduser mailpile $MY_USERNAME
  249. chown -R mailpile: /var/www/$MAILPILE_DOMAIN_NAME/mail/
  250. # create folders and tags
  251. su -c "cd /var/www/$MAILPILE_DOMAIN_NAME/mail && ./mp --setup" - mailpile
  252. MAILPILE_ONION_HOSTNAME=$(add_onion_service mailpile 80 ${MAILPILE_ONION_PORT})
  253. echo '[Unit]' > /etc/systemd/system/mailpile.service
  254. echo 'Description=Mailpile Email Client' >> /etc/systemd/system/mailpile.service
  255. echo 'After=syslog.target network.target nginx.target' >> /etc/systemd/system/mailpile.service
  256. echo '' >> /etc/systemd/system/mailpile.service
  257. echo '[Service]' >> /etc/systemd/system/mailpile.service
  258. echo 'User=mailpile' >> /etc/systemd/system/mailpile.service
  259. echo 'Group=mailpile' >> /etc/systemd/system/mailpile.service
  260. echo "WorkingDirectory=/var/www/$MAILPILE_DOMAIN_NAME/mail" >> /etc/systemd/system/mailpile.service
  261. echo "ExecStart=/var/www/$MAILPILE_DOMAIN_NAME/mail/mp --www=0.0.0.0:${MAILPILE_PORT} --wait" >> /etc/systemd/system/mailpile.service
  262. echo 'Restart=always' >> /etc/systemd/system/mailpile.service
  263. echo 'RestartSec=10' >> /etc/systemd/system/mailpile.service
  264. echo '' >> /etc/systemd/system/mailpile.service
  265. echo '[Install]' >> /etc/systemd/system/mailpile.service
  266. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/mailpile.service
  267. chmod +x /etc/systemd/system/mailpile.service
  268. mailpile_nginx_site=/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME
  269. if [[ $ONION_ONLY == "no" ]]; then
  270. function_check nginx_http_redirect
  271. nginx_http_redirect $MAILPILE_DOMAIN_NAME
  272. echo 'server {' >> $mailpile_nginx_site
  273. echo ' listen 443 ssl;' >> $mailpile_nginx_site
  274. echo ' listen [::]:443 ssl;' >> $mailpile_nginx_site
  275. echo " server_name $MAILPILE_DOMAIN_NAME;" >> $mailpile_nginx_site
  276. echo '' >> $mailpile_nginx_site
  277. echo ' # Security' >> $mailpile_nginx_site
  278. function_check nginx_ssl
  279. nginx_ssl $MAILPILE_DOMAIN_NAME
  280. function_check nginx_disable_sniffing
  281. nginx_disable_sniffing $MAILPILE_DOMAIN_NAME
  282. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $mailpile_nginx_site
  283. echo '' >> $mailpile_nginx_site
  284. echo ' # Logs' >> $mailpile_nginx_site
  285. echo ' access_log /dev/null;' >> $mailpile_nginx_site
  286. echo ' error_log /dev/null;' >> $mailpile_nginx_site
  287. echo '' >> $mailpile_nginx_site
  288. echo ' # Root' >> $mailpile_nginx_site
  289. echo " root /var/www/$MAILPILE_DOMAIN_NAME/mail;" >> $mailpile_nginx_site
  290. echo '' >> $mailpile_nginx_site
  291. echo ' location / {' >> $mailpile_nginx_site
  292. function_check nginx_limits
  293. nginx_limits $MAILPILE_DOMAIN_NAME '15m'
  294. echo ' rewrite /(.*) /$1 break;' >> $mailpile_nginx_site
  295. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $mailpile_nginx_site
  296. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $mailpile_nginx_site
  297. echo ' proxy_set_header Host $http_host;' >> $mailpile_nginx_site
  298. echo ' proxy_set_header X-NginX-Proxy true;' >> $mailpile_nginx_site
  299. echo " proxy_pass http://localhost:${MAILPILE_PORT};" >> $mailpile_nginx_site
  300. echo ' proxy_redirect off;' >> $mailpile_nginx_site
  301. echo ' }' >> $mailpile_nginx_site
  302. echo '' >> $mailpile_nginx_site
  303. nginx_keybase ${MAILPILE_DOMAIN_NAME}
  304. echo '}' >> $mailpile_nginx_site
  305. echo '' >> $mailpile_nginx_site
  306. else
  307. echo -n '' > $mailpile_nginx_site
  308. fi
  309. echo 'server {' >> $mailpile_nginx_site
  310. echo " listen 127.0.0.1:$MAILPILE_ONION_PORT default_server;" >> $mailpile_nginx_site
  311. echo " server_name $MAILPILE_ONION_HOSTNAME;" >> $mailpile_nginx_site
  312. echo '' >> $mailpile_nginx_site
  313. function_check nginx_disable_sniffing
  314. nginx_disable_sniffing $MAILPILE_DOMAIN_NAME
  315. echo '' >> $mailpile_nginx_site
  316. echo ' # Logs' >> $mailpile_nginx_site
  317. echo ' access_log /dev/null;' >> $mailpile_nginx_site
  318. echo ' error_log /dev/null;' >> $mailpile_nginx_site
  319. echo '' >> $mailpile_nginx_site
  320. echo ' # Root' >> $mailpile_nginx_site
  321. echo " root /var/www/$MAILPILE_DOMAIN_NAME/mail;" >> $mailpile_nginx_site
  322. echo '' >> $mailpile_nginx_site
  323. echo ' location / {' >> $mailpile_nginx_site
  324. function_check nginx_limits
  325. nginx_limits $MAILPILE_DOMAIN_NAME '15m'
  326. echo ' rewrite /(.*) /$1 break;' >> $mailpile_nginx_site
  327. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $mailpile_nginx_site
  328. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $mailpile_nginx_site
  329. echo ' proxy_set_header Host $http_host;' >> $mailpile_nginx_site
  330. echo ' proxy_set_header X-NginX-Proxy true;' >> $mailpile_nginx_site
  331. echo " proxy_pass http://localhost:${MAILPILE_PORT};" >> $mailpile_nginx_site
  332. echo ' proxy_redirect off;' >> $mailpile_nginx_site
  333. echo ' }' >> $mailpile_nginx_site
  334. echo '' >> $mailpile_nginx_site
  335. nginx_keybase ${MAILPILE_DOMAIN_NAME}
  336. echo '}' >> $mailpile_nginx_site
  337. function_check create_site_certificate
  338. if [ ! -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem ]; then
  339. create_site_certificate $MAILPILE_DOMAIN_NAME 'yes'
  340. fi
  341. if [ -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.crt ]; then
  342. mv /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.crt /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
  343. fi
  344. if [ -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem ]; then
  345. chown root:root /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
  346. sed -i "s|.crt|.pem|g" /etc/nginx/sites-available/${MAILPILE_DOMAIN_NAME}
  347. fi
  348. if [ -f /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key ]; then
  349. chown root:root /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key
  350. fi
  351. function_check nginx_ensite
  352. nginx_ensite $MAILPILE_DOMAIN_NAME
  353. ${PROJECT_NAME}-pass -u $MY_USERNAME -a mailpile -p "*"
  354. function_check add_ddns_domain
  355. add_ddns_domain $MAILPILE_DOMAIN_NAME
  356. disable_email_encryption_at_rest
  357. # if strict https is enforced then buttons don't work. This is probably a security bug
  358. sed -i "/Content-Security-Policy/d" $mailpile_nginx_site
  359. # link to the administrator's keyring
  360. if [ -d /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg ]; then
  361. mv /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg_orig
  362. fi
  363. cp -r /home/$MY_USERNAME/.gnupg /var/www/$MAILPILE_DOMAIN_NAME/mail/
  364. chown -R mailpile:mailpile /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg
  365. chmod +x /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg
  366. pip install jinja2==2.9.6
  367. pip install pgpdump==1.5
  368. systemctl enable mailpile
  369. systemctl daemon-reload
  370. systemctl start mailpile
  371. systemctl restart nginx
  372. set_completion_param "mailpile domain" "$MAILPILE_DOMAIN_NAME"
  373. APP_INSTALLED=1
  374. }
  375. # NOTE: deliberately no exit 0