freedombone-app-mailpile 16KB

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