freedombone-app-cryptpad 14KB

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