freedombone-app-cryptpad 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # cryptpad application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017 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 writer'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  34. CRYPTPAD_ONION_PORT=8119
  35. CRYPTPAD_PORT=9003
  36. CRYPTPAD_REPO="https://github.com/xwiki-labs/cryptpad"
  37. CRYPTPAD_COMMIT='ed5b005216be9b9029c1ccd25a5fdc7908ed8730'
  38. CRYPTPAD_DIR=/etc/cryptpad
  39. cryptpad_variables=(ONION_ONLY)
  40. function remove_user_cryptpad {
  41. remove_username="$1"
  42. }
  43. function add_user_cryptpad {
  44. new_username="$1"
  45. new_user_password="$2"
  46. echo '0'
  47. }
  48. function install_interactive_cryptpad {
  49. echo -n ''
  50. APP_INSTALLED=1
  51. }
  52. function change_password_cryptpad {
  53. curr_username="$1"
  54. new_user_password="$2"
  55. }
  56. function reconfigure_cryptpad {
  57. if [ -d $CRYPTPAD_DIR/datastore ]; then
  58. rm -rf $CRYPTPAD_DIR/datastore
  59. fi
  60. }
  61. function upgrade_cryptpad {
  62. CURR_CRYPTPAD_COMMIT=$(get_completion_param "cryptpad commit")
  63. if [[ "$CURR_CRYPTPAD_COMMIT" == "$CRYPTPAD_COMMIT" ]]; then
  64. return
  65. fi
  66. systemctl stop cryptpad
  67. # update to the next commit
  68. function_check set_repo_commit
  69. set_repo_commit $CRYPTPAD_DIR "cryptpad commit" "$CRYPTPAD_COMMIT" $CRYPTPAD_REPO
  70. cd $CRYPTPAD_DIR
  71. systemctl start cryptpad
  72. }
  73. function backup_local_cryptpad {
  74. source_directory=$CRYPTPAD_DIR/datastore
  75. if [ -d $source_directory ]; then
  76. systemctl stop cryptpad
  77. dest_directory=cryptpad
  78. function_check suspend_site
  79. suspend_site cryptpad
  80. function_check backup_directory_to_usb
  81. backup_directory_to_usb $source_directory $dest_directory
  82. function_check restart_site
  83. restart_site
  84. systemctl start cryptpad
  85. fi
  86. }
  87. function restore_local_cryptpad {
  88. if [ -d $CRYPTPAD_DIR ]; then
  89. systemctl stop cryptpad
  90. temp_restore_dir=/root/tempcryptpad
  91. function_check restore_directory_from_usb
  92. restore_directory_from_usb $temp_restore_dir cryptpad
  93. cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
  94. systemctl start cryptpad
  95. fi
  96. }
  97. function backup_remote_cryptpad {
  98. if grep -q "cryptpad domain" $COMPLETION_FILE; then
  99. temp_backup_dir=$CRYPTPAD_DIR/datastore
  100. if [ -d $temp_backup_dir ]; then
  101. systemctl stop cryptpad
  102. function_check suspend_site
  103. suspend_site cryptpad
  104. echo $"Backing up Cryptpad installation"
  105. function_check backup_directory_to_friend
  106. backup_directory_to_friend $temp_backup_dir cryptpad
  107. function_check restart_site
  108. restart_site
  109. systemctl start cryptpad
  110. else
  111. echo $"cryptpad domain specified but not found in ${temp_backup_dir}"
  112. fi
  113. fi
  114. }
  115. function restore_remote_cryptpad {
  116. if [ -d $CRYPTPAD_DIR ]; then
  117. systemctl stop cryptpad
  118. temp_restore_dir=/root/tempcryptpad
  119. function_check restore_directory_from_usb
  120. restore_directory_from_friend $temp_restore_dir cryptpad
  121. cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
  122. systemctl start cryptpad
  123. fi
  124. }
  125. function remove_cryptpad {
  126. systemctl stop cryptpad
  127. systemctl disable cryptpad
  128. if [ -f /etc/systemd/system/cryptpad.service ]; then
  129. rm /etc/systemd/system/cryptpad.service
  130. fi
  131. systemctl daemon-reload
  132. function_check remove_nodejs
  133. remove_nodejs cryptpad
  134. nginx_dissite cryptpad
  135. if [ -d $CRYPTPAD_DIR ]; then
  136. rm -rf $CRYPTPAD_DIR
  137. fi
  138. if [ -f /etc/nginx/sites-available/cryptpad ]; then
  139. rm /etc/nginx/sites-available/cryptpad
  140. fi
  141. function_check remove_onion_service
  142. remove_onion_service cryptpad ${CRYPTPAD_ONION_PORT}
  143. remove_app cryptpad
  144. remove_completion_param install_cryptpad
  145. sed -i '/cryptpad/d' $COMPLETION_FILE
  146. userdel -r cryptpad
  147. }
  148. function install_cryptpad_main {
  149. if [[ $(app_is_installed cryptpad_main) == "1" ]]; then
  150. return
  151. fi
  152. if [ ! -d /var/www/cryptpad ]; then
  153. mkdir /var/www/cryptpad
  154. fi
  155. if [ -d $CRYPTPAD_DIR ]; then
  156. rm -rf $CRYPTPAD_DIR
  157. fi
  158. function_check git_clone
  159. git_clone $CRYPTPAD_REPO $CRYPTPAD_DIR
  160. if [ ! -d $CRYPTPAD_DIR ]; then
  161. echo $'Unable to clone cryptpad repo'
  162. exit 783251
  163. fi
  164. # an unprivileged user to run as
  165. useradd -d $CRYPTPAD_DIR/ cryptpad
  166. cd $CRYPTPAD_DIR
  167. git checkout $CRYPTPAD_COMMIT -b $CRYPTPAD_COMMIT
  168. set_completion_param "cryptpad commit" "$CRYPTPAD_COMMIT"
  169. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  170. CRYPTPAD_ONION_HOSTNAME=$(add_onion_service cryptpad 80 ${CRYPTPAD_ONION_PORT})
  171. cryptpad_nginx_site=/etc/nginx/sites-available/cryptpad
  172. echo 'server {' > $cryptpad_nginx_site
  173. echo " listen 127.0.0.1:$CRYPTPAD_ONION_PORT default_server;" >> $cryptpad_nginx_site
  174. echo " server_name $CRYPTPAD_ONION_HOSTNAME;" >> $cryptpad_nginx_site
  175. echo '' >> $cryptpad_nginx_site
  176. echo ' # Logs' >> $cryptpad_nginx_site
  177. echo ' access_log /dev/null;' >> $cryptpad_nginx_site
  178. echo ' error_log /dev/null;' >> $cryptpad_nginx_site
  179. echo '' >> $cryptpad_nginx_site
  180. echo ' # Root' >> $cryptpad_nginx_site
  181. echo " root $CRYPTPAD_DIR;" >> $cryptpad_nginx_site
  182. echo '' >> $cryptpad_nginx_site
  183. echo ' index index.html;' >> $cryptpad_nginx_site
  184. echo '' >> $cryptpad_nginx_site
  185. echo ' add_header X-XSS-Protection "1; mode=block";' >> $cryptpad_nginx_site
  186. echo ' add_header X-Content-Type-Options nosniff;' >> $cryptpad_nginx_site
  187. echo ' add_header X-Frame-Options SAMEORIGIN;' >> $cryptpad_nginx_site
  188. echo '' >> $cryptpad_nginx_site
  189. echo ' if ($uri = /pad/inner.html) {' >> $cryptpad_nginx_site
  190. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";" >> $cryptpad_nginx_site
  191. echo ' }' >> $cryptpad_nginx_site
  192. echo '' >> $cryptpad_nginx_site
  193. echo ' location = /cryptpad_websocket {' >> $cryptpad_nginx_site
  194. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;" >> $cryptpad_nginx_site
  195. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $cryptpad_nginx_site
  196. echo ' proxy_set_header Host $host;' >> $cryptpad_nginx_site
  197. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $cryptpad_nginx_site
  198. echo '' >> $cryptpad_nginx_site
  199. echo ' # WebSocket support (nginx 1.4)' >> $cryptpad_nginx_site
  200. echo ' proxy_http_version 1.1;' >> $cryptpad_nginx_site
  201. echo ' proxy_set_header Upgrade $http_upgrade;' >> $cryptpad_nginx_site
  202. echo ' proxy_set_header Connection upgrade;' >> $cryptpad_nginx_site
  203. echo ' }' >> $cryptpad_nginx_site
  204. echo '' >> $cryptpad_nginx_site
  205. echo ' location ^~ /customize.dist/ {' >> $cryptpad_nginx_site
  206. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root' >> $cryptpad_nginx_site
  207. echo ' }' >> $cryptpad_nginx_site
  208. echo ' location ^~ /customize/ {' >> $cryptpad_nginx_site
  209. echo ' rewrite ^/customize/(.*)$ $1 break;' >> $cryptpad_nginx_site
  210. echo ' try_files /customize/$uri /customize.dist/$uri;' >> $cryptpad_nginx_site
  211. echo ' }' >> $cryptpad_nginx_site
  212. echo ' location = /api/config {' >> $cryptpad_nginx_site
  213. echo ' default_type text/javascript;' >> $cryptpad_nginx_site
  214. echo ' rewrite ^.*$ /customize/api/config break;' >> $cryptpad_nginx_site
  215. echo ' }' >> $cryptpad_nginx_site
  216. echo '' >> $cryptpad_nginx_site
  217. echo ' location ^~ /blob/ {' >> $cryptpad_nginx_site
  218. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  219. echo ' }' >> $cryptpad_nginx_site
  220. echo '' >> $cryptpad_nginx_site
  221. echo ' location ^~ /register/ {' >> $cryptpad_nginx_site
  222. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  223. echo ' }' >> $cryptpad_nginx_site
  224. echo '' >> $cryptpad_nginx_site
  225. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {' >> $cryptpad_nginx_site
  226. echo ' rewrite ^(.*)$ $1/ redirect;' >> $cryptpad_nginx_site
  227. echo ' }' >> $cryptpad_nginx_site
  228. echo '' >> $cryptpad_nginx_site
  229. echo ' try_files /www/$uri /www/$uri/index.html /customize/$uri;' >> $cryptpad_nginx_site
  230. echo '}' >> $cryptpad_nginx_site
  231. function_check nginx_ensite
  232. nginx_ensite cryptpad
  233. install_completed cryptpad_main
  234. }
  235. function install_cryptpad {
  236. function_check install_nodejs
  237. install_nodejs cryptpad
  238. install_cryptpad_main
  239. cd $CRYPTPAD_DIR
  240. npm install
  241. npm install -g bower@1.8.0
  242. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  243. su -c 'bower install' - cryptpad
  244. cp config.example.js config.js
  245. if [ ! -f config.js ]; then
  246. echo $'Cryptpad config file not found'
  247. exit 628252
  248. fi
  249. sed -i "s|httpPort:.*|httpPort: $CRYPTPAD_PORT,|g" config.js
  250. sed -i "s|// domain:|domain:|g" config.js
  251. sed -i 's|openFileLimit:.*|openFileLimit: 1024,|g' config.js
  252. sed -i "s|domain:.*|domain: 'http://$CRYPTPAD_ONION_HOSTNAME',|g" config.js
  253. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  254. # daemon
  255. echo '[Unit]' > /etc/systemd/system/cryptpad.service
  256. echo 'Description=Cryptpad' >> /etc/systemd/system/cryptpad.service
  257. echo 'After=syslog.target' >> /etc/systemd/system/cryptpad.service
  258. echo 'After=network.target' >> /etc/systemd/system/cryptpad.service
  259. echo '' >> /etc/systemd/system/cryptpad.service
  260. echo '[Service]' >> /etc/systemd/system/cryptpad.service
  261. echo 'User=cryptpad' >> /etc/systemd/system/cryptpad.service
  262. echo 'Group=cryptpad' >> /etc/systemd/system/cryptpad.service
  263. echo "WorkingDirectory=$CRYPTPAD_DIR" >> /etc/systemd/system/cryptpad.service
  264. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js" >> /etc/systemd/system/cryptpad.service
  265. echo 'Environment=PATH=/usr/bin:/usr/local/bin' >> /etc/systemd/system/cryptpad.service
  266. echo 'Environment=NODE_ENV=production' >> /etc/systemd/system/cryptpad.service
  267. echo 'Restart=on-failure' >> /etc/systemd/system/cryptpad.service
  268. echo '' >> /etc/systemd/system/cryptpad.service
  269. echo '[Install]' >> /etc/systemd/system/cryptpad.service
  270. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/cryptpad.service
  271. systemctl enable cryptpad.service
  272. systemctl daemon-reload
  273. systemctl start cryptpad.service
  274. sleep 6
  275. if [ ! -d $CRYPTPAD_DIR/customize/api ]; then
  276. mkdir -p $CRYPTPAD_DIR/customize/api
  277. fi
  278. wget 127.0.0.1:$CRYPTPAD_PORT/api/config -O $CRYPTPAD_DIR/customize/api/config
  279. if [ ! -f $CRYPTPAD_DIR/customize/api/config ]; then
  280. echo $'Unable to wget api/config'
  281. exit 89252
  282. fi
  283. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  284. # install again
  285. cd $CRYPTPAD_DIR
  286. su -c 'bower install' - cryptpad
  287. systemctl restart nginx
  288. APP_INSTALLED=1
  289. }
  290. # NOTE: deliberately there is no "exit 0"