freedombone-app-mailpile 15KB

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