freedombone-app-mailpile 16KB

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