freedombone-app-fedwiki 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Federated wiki
  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 media"
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. FEDWIKI_DOMAIN_NAME=
  34. FEDWIKI_CODE=
  35. FEDWIKI_VERSION='0.13.0'
  36. FEDWIKI_ONION_PORT=8131
  37. FEDWIKI_PORT=3053
  38. FEDWIKI_DATA=/var/lib/fedwiki
  39. FEDWIKI_COOKIE=
  40. fedwiki_variables=(FEDWIKI_DOMAIN_NAME
  41. FEDWIKI_CODE
  42. FEDWIKI_COOKIE
  43. FEDWIKI_ADMIN_PASSWORD
  44. ONION_ONLY
  45. DDNS_PROVIDER
  46. MY_USERNAME
  47. MY_EMAIL_ADDRESS)
  48. function fedwiki_remove_bad_links {
  49. if [ -f /usr/local/lib/node_modules/wiki/node_modules/wiki-security-friends/client/security.js ]; then
  50. sed -i 's|maxcdn.bootstrapcdn.com|localhost|g' /usr/local/lib/node_modules/wiki/node_modules/wiki-security-friends/client/security.js
  51. fi
  52. if [ -f /usr/local/lib/node_modules/wiki/node_modules/localforage/docs/theme/style.css ]; then
  53. sed -i '/googleapi/d' /usr/local/lib/node_modules/wiki/node_modules/localforage/docs/theme/style.css
  54. fi
  55. if [ -f /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/addAlternativeDialog.html ]; then
  56. sed -i '/googleapi/d' /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/addAlternativeDialog.html
  57. fi
  58. if [ -f /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/done.html ]; then
  59. sed -i '/googleapi/d' /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/done.html
  60. fi
  61. if [ -f /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/personaDialog.html ]; then
  62. sed -i '/googleapi/d' /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/personaDialog.html
  63. fi
  64. if [ -f /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/securityDialog.html ]; then
  65. sed -i '/googleapi/d' /usr/local/lib/node_modules/wiki/node_modules/wiki-security-passportjs/views/securityDialog.html
  66. fi
  67. if [ -d /usr/local/lib/node_modules/wiki/node_modules/passport-google-oauth20 ]; then
  68. rm -rf /usr/local/lib/node_modules/wiki/node_modules/passport-google-oauth20
  69. fi
  70. if [ -d /usr/local/lib/node_modules/wiki/node_modules/passport-oauth2 ]; then
  71. rm -rf /usr/local/lib/node_modules/wiki/node_modules/passport-oauth2
  72. fi
  73. if [ -d /usr/local/lib/node_modules/wiki/node_modules/passport-twitter ]; then
  74. rm -rf /usr/local/lib/node_modules/wiki/node_modules/passport-twitter
  75. fi
  76. if [ -d /usr/local/lib/node_modules/wiki/node_modules/passport-github ]; then
  77. rm -rf /usr/local/lib/node_modules/wiki/node_modules/passport-github
  78. fi
  79. }
  80. function logging_on_fedwiki {
  81. echo -n ''
  82. }
  83. function logging_off_fedwiki {
  84. echo -n ''
  85. }
  86. function remove_user_fedwiki {
  87. remove_username="$1"
  88. }
  89. function add_user_fedwiki {
  90. if [[ $(app_is_installed fedwiki) == "0" ]]; then
  91. echo '0'
  92. return
  93. fi
  94. new_username="$1"
  95. new_user_password="$2"
  96. echo '0'
  97. }
  98. function install_interactive_fedwiki {
  99. if [ ! $ONION_ONLY ]; then
  100. ONION_ONLY='no'
  101. fi
  102. if [[ $ONION_ONLY != "no" ]]; then
  103. FEDWIKI_DOMAIN_NAME='fedwiki.local'
  104. write_config_param "FEDWIKI_DOMAIN_NAME" "$FEDWIKI_DOMAIN_NAME"
  105. else
  106. function_check interactive_site_details
  107. interactive_site_details "fedwiki" "FEDWIKI_DOMAIN_NAME" "FEDWIKI_CODE"
  108. fi
  109. APP_INSTALLED=1
  110. }
  111. function change_password_fedwiki {
  112. FEDWIKI_USERNAME="$1"
  113. FEDWIKI_PASSWORD="$2"
  114. if [ ${#FEDWIKI_PASSWORD} -lt 8 ]; then
  115. echo $'Fedwiki password is too short'
  116. return
  117. fi
  118. #${PROJECT_NAME}-pass -u $FEDWIKI_USERNAME -a fedwiki -p "$FEDWIKI_PASSWORD"
  119. }
  120. function reconfigure_fedwiki {
  121. echo -n ''
  122. }
  123. function upgrade_fedwiki {
  124. CURR_FEDWIKI_VERSION=$(get_completion_param "fedwiki version")
  125. if [[ "$CURR_FEDWIKI_VERSION" == "$FEDWIKI_VERSION" ]]; then
  126. return
  127. fi
  128. systemctl stop fedwiki
  129. npm upgrade -g wiki@$FEDWIKI_VERSION
  130. fedwiki_remove_bad_links
  131. chown -R fedwiki:fedwiki $FEDWIKI_DATA
  132. systemctl start fedwiki
  133. set_completion_param "fedwiki version" "$FEDWIKI_VERSION"
  134. }
  135. function backup_local_fedwiki {
  136. FEDWIKI_DOMAIN_NAME='fedwiki.local'
  137. if grep -q "fedwiki domain" $COMPLETION_FILE; then
  138. FEDWIKI_DOMAIN_NAME=$(get_completion_param "fedwiki domain")
  139. fi
  140. systemctl stop fedwiki
  141. suspend_site ${FEDWIKI_DOMAIN_NAME}
  142. fedwiki_path=$FEDWIKI_DATA
  143. if [ -d $fedwiki_path ]; then
  144. backup_directory_to_usb $fedwiki_path fedwiki
  145. fi
  146. restart_site
  147. systemctl start fedwiki
  148. }
  149. function restore_local_fedwiki {
  150. FEDWIKI_DOMAIN_NAME='fedwiki.local'
  151. if grep -q "fedwiki domain" $COMPLETION_FILE; then
  152. FEDWIKI_DOMAIN_NAME=$(get_completion_param "fedwiki domain")
  153. fi
  154. if [ $FEDWIKI_DOMAIN_NAME ]; then
  155. suspend_site ${FEDWIKI_DOMAIN_NAME}
  156. systemctl stop fedwiki
  157. temp_restore_dir=/root/tempfedwiki
  158. function_check restore_directory_from_usb
  159. restore_directory_from_usb $temp_restore_dir fedwiki
  160. if [ -d $temp_restore_dir ]; then
  161. if [ -d $temp_restore_dir$FEDWIKI_DATA ]; then
  162. cp -r $temp_restore_dir$FEDWIKI_DATA/* $FEDWIKI_DATA/
  163. else
  164. cp -r $temp_restore_dir/* $FEDWIKI_DATA/
  165. fi
  166. chown -R fedwiki:fedwiki $FEDWIKI_DATA
  167. rm -rf $temp_restore_dir
  168. fi
  169. systemctl start fedwiki
  170. restart_site
  171. fi
  172. }
  173. function backup_remote_fedwiki {
  174. FEDWIKI_DOMAIN_NAME='fedwiki.local'
  175. if grep -q "fedwiki domain" $COMPLETION_FILE; then
  176. FEDWIKI_DOMAIN_NAME=$(get_completion_param "fedwiki domain")
  177. fi
  178. systemctl stop fedwiki
  179. suspend_site ${FEDWIKI_DOMAIN_NAME}
  180. temp_backup_dir=$FEDWIKI_DATA
  181. if [ -d $temp_backup_dir ]; then
  182. backup_directory_to_friend $temp_backup_dir fedwiki
  183. else
  184. echo $"Fedwiki domain specified but not found in $temp_backup_dir"
  185. exit 63542852
  186. fi
  187. restart_site
  188. systemctl start fedwiki
  189. }
  190. function restore_remote_fedwiki {
  191. FEDWIKI_DOMAIN_NAME='fedwiki.local'
  192. if grep -q "fedwiki domain" $COMPLETION_FILE; then
  193. FEDWIKI_DOMAIN_NAME=$(get_completion_param "fedwiki domain")
  194. fi
  195. systemctl stop fedwiki
  196. suspend_site ${FEDWIKI_DOMAIN_NAME}
  197. temp_restore_dir=/root/tempfedwiki
  198. function_check restore_directory_from_friend
  199. restore_directory_from_friend $temp_restore_dir fedwiki
  200. if [ -d $temp_restore_dir ]; then
  201. if [ -d $temp_restore_dir$FEDWIKI_DATA ]; then
  202. cp -r $temp_restore_dir$FEDWIKI_DATA/* $FEDWIKI_DATA/
  203. else
  204. cp -r $temp_restore_dir/* $FEDWIKI_DATA/
  205. fi
  206. chown -R fedwiki: $FEDWIKI_DATA
  207. rm -rf $temp_restore_dir
  208. fi
  209. systemctl start fedwiki
  210. restart_site
  211. }
  212. function remove_fedwiki {
  213. if [ ${#FEDWIKI_DOMAIN_NAME} -eq 0 ]; then
  214. return
  215. fi
  216. systemctl stop fedwiki
  217. systemctl disable fedwiki
  218. rm /etc/systemd/system/fedwiki.service
  219. systemctl daemon-reload
  220. npm uninstall -g wiki
  221. function_check remove_nodejs
  222. remove_nodejs fedwiki
  223. read_config_param "FEDWIKI_DOMAIN_NAME"
  224. nginx_dissite $FEDWIKI_DOMAIN_NAME
  225. remove_certs ${FEDWIKI_DOMAIN_NAME}
  226. if [ -f /etc/nginx/sites-available/$FEDWIKI_DOMAIN_NAME ]; then
  227. rm -f /etc/nginx/sites-available/$FEDWIKI_DOMAIN_NAME
  228. fi
  229. if [ -d /var/www/$FEDWIKI_DOMAIN_NAME ]; then
  230. rm -rf /var/www/$FEDWIKI_DOMAIN_NAME
  231. fi
  232. remove_config_param FEDWIKI_DOMAIN_NAME
  233. remove_config_param FEDWIKI_CODE
  234. function_check remove_onion_service
  235. remove_onion_service fedwiki ${FEDWIKI_ONION_PORT}
  236. remove_completion_param "install_fedwiki"
  237. sed -i '/fedwiki/d' $COMPLETION_FILE
  238. groupdel -f fedwiki
  239. userdel -r fedwiki
  240. if [ -d $FEDWIKI_DATA ]; then
  241. rm -rf $FEDWIKI_DATA
  242. fi
  243. function_check remove_ddns_domain
  244. remove_ddns_domain $FEDWIKI_DOMAIN_NAME
  245. }
  246. function fedwiki_setup_web {
  247. fedwiki_nginx_file=/etc/nginx/sites-available/$FEDWIKI_DOMAIN_NAME
  248. if [[ $ONION_ONLY == "no" ]]; then
  249. echo 'server {' > $fedwiki_nginx_file
  250. echo ' listen 80;' >> $fedwiki_nginx_file
  251. echo ' listen [::]:80;' >> $fedwiki_nginx_file
  252. echo " server_name $FEDWIKI_DOMAIN_NAME;" >> $fedwiki_nginx_file
  253. echo ' rewrite ^ https://$server_name$request_uri? permanent;' >> $fedwiki_nginx_file
  254. echo '}' >> $fedwiki_nginx_file
  255. echo '' >> $fedwiki_nginx_file
  256. echo 'server {' >> $fedwiki_nginx_file
  257. echo ' listen 443 ssl;' >> $fedwiki_nginx_file
  258. echo ' listen [::]:443 ssl;' >> $fedwiki_nginx_file
  259. echo " server_name $FEDWIKI_DOMAIN_NAME;" >> $fedwiki_nginx_file
  260. echo '' >> $fedwiki_nginx_file
  261. function_check nginx_ssl
  262. nginx_ssl $FEDWIKI_DOMAIN_NAME mobile
  263. sed -i '/Content-Security-Policy/d' $fedwiki_nginx_file
  264. sed -i '/X-XSS-Protection/d' $fedwiki_nginx_file
  265. sed -i '/X-Robots-Tag/d' $fedwiki_nginx_file
  266. sed -i '/X-Download-Options/d' $fedwiki_nginx_file
  267. sed -i '/X-Permitted-Cross-Domain-Policies/d' $fedwiki_nginx_file
  268. echo ' add_header X-Robots-Tag none;' >> $fedwiki_nginx_file
  269. echo ' add_header X-Download-Options noopen;' >> $fedwiki_nginx_file
  270. echo ' add_header X-Frame-Options DENY;' >> $fedwiki_nginx_file
  271. echo ' add_header X-Content-Type-Options nosniff;' >> $fedwiki_nginx_file
  272. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $fedwiki_nginx_file
  273. echo '' >> $fedwiki_nginx_file
  274. echo ' location / {' >> $fedwiki_nginx_file
  275. echo " proxy_pass http://localhost:${FEDWIKI_PORT};" >> $fedwiki_nginx_file
  276. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $fedwiki_nginx_file
  277. echo ' proxy_set_header Host $host;' >> $fedwiki_nginx_file
  278. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $fedwiki_nginx_file
  279. echo ' client_max_body_size 1M;' >> $fedwiki_nginx_file
  280. echo ' }' >> $fedwiki_nginx_file
  281. echo '}' >> $fedwiki_nginx_file
  282. echo '' >> $fedwiki_nginx_file
  283. else
  284. echo -n '' > $fedwiki_nginx_file
  285. fi
  286. echo 'server {' >> $fedwiki_nginx_file
  287. echo " listen 127.0.0.1:$FEDWIKI_ONION_PORT default_server;" >> $fedwiki_nginx_file
  288. echo " server_name $FEDWIKI_ONION_HOSTNAME;" >> $fedwiki_nginx_file
  289. echo '' >> $fedwiki_nginx_file
  290. echo ' add_header X-Robots-Tag none;' >> $fedwiki_nginx_file
  291. echo ' add_header X-Download-Options noopen;' >> $fedwiki_nginx_file
  292. echo ' add_header X-Frame-Options DENY;' >> $fedwiki_nginx_file
  293. echo ' add_header X-Content-Type-Options nosniff;' >> $fedwiki_nginx_file
  294. echo '' >> $fedwiki_nginx_file
  295. echo ' location / {' >> $fedwiki_nginx_file
  296. echo " proxy_pass http://localhost:${FEDWIKI_PORT};" >> $fedwiki_nginx_file
  297. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $fedwiki_nginx_file
  298. echo ' proxy_set_header Host $host;' >> $fedwiki_nginx_file
  299. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $fedwiki_nginx_file
  300. echo ' client_max_body_size 1M;' >> $fedwiki_nginx_file
  301. echo ' }' >> $fedwiki_nginx_file
  302. echo '}' >> $fedwiki_nginx_file
  303. function_check create_site_certificate
  304. create_site_certificate $FEDWIKI_DOMAIN_NAME 'yes'
  305. function_check nginx_ensite
  306. nginx_ensite $FEDWIKI_DOMAIN_NAME
  307. }
  308. function install_fedwiki {
  309. if [[ $VARIANT == "mesh"* ]]; then
  310. return
  311. fi
  312. if [ ! $ONION_ONLY ]; then
  313. ONION_ONLY='no'
  314. fi
  315. if [ ! $FEDWIKI_DOMAIN_NAME ]; then
  316. echo $'The fedwiki domain name was not specified'
  317. exit 893635
  318. fi
  319. if [ ! -d /var/www/$FEDWIKI_DOMAIN_NAME/htdocs ]; then
  320. mkdir -p /var/www/$FEDWIKI_DOMAIN_NAME/htdocs
  321. fi
  322. if [ ! -d $FEDWIKI_DATA ]; then
  323. mkdir -p $FEDWIKI_DATA
  324. fi
  325. groupadd fedwiki
  326. useradd -c "Fedwiki system account" -d $FEDWIKI_DATA -m -r -g fedwiki fedwiki
  327. if [ -d $FEDWIKI_DATA/Maildir ]; then
  328. rm -rf $FEDWIKI_DATA/Maildir
  329. fi
  330. function_check install_nodejs
  331. install_nodejs fedwiki
  332. npm install -g wiki@$FEDWIKI_VERSION
  333. if [ ! "$?" = "0" ]; then
  334. echo $'Failed to install fedwiki'
  335. exit 6293523
  336. fi
  337. if [ ! -f /usr/local/bin/wiki ]; then
  338. echo $'wiki was not installed'
  339. exit 5293524
  340. fi
  341. if [ ! -d /usr/local/lib/node_modules/wiki ]; then
  342. echo $'wiki directory not found /usr/local/lib/node_modules/wiki'
  343. exit 6285324
  344. fi
  345. FEDWIKI_ONION_HOSTNAME=$(add_onion_service fedwiki 80 ${FEDWIKI_ONION_PORT})
  346. if [ ! $FEDWIKI_COOKIE ]; then
  347. FEDWIKI_COOKIE="$(create_password 30)$(create_password 30)$(create_password 30)"
  348. fi
  349. echo '[Unit]' > /etc/systemd/system/fedwiki.service
  350. echo 'Description=Fedwiki Decentralized video streaming platform' >> /etc/systemd/system/fedwiki.service
  351. echo 'After=syslog.target' >> /etc/systemd/system/fedwiki.service
  352. echo 'After=network.target' >> /etc/systemd/system/fedwiki.service
  353. echo '' >> /etc/systemd/system/fedwiki.service
  354. echo '[Service]' >> /etc/systemd/system/fedwiki.service
  355. echo 'User=fedwiki' >> /etc/systemd/system/fedwiki.service
  356. echo 'Group=fedwiki' >> /etc/systemd/system/fedwiki.service
  357. echo "WorkingDirectory=/usr/local/lib/node_modules/wiki" >> /etc/systemd/system/fedwiki.service
  358. echo "ExecStart=/usr/local/bin/wiki --security_type friends --cookieSecret '${FEDWIKI_COOKIE}' --session_duration 7 --data $FEDWIKI_DATA -p $FEDWIKI_PORT" >> /etc/systemd/system/fedwiki.service
  359. echo 'StandardOutput=syslog' >> /etc/systemd/system/fedwiki.service
  360. echo 'StandardError=syslog' >> /etc/systemd/system/fedwiki.service
  361. echo 'SyslogIdentifier=fedwiki' >> /etc/systemd/system/fedwiki.service
  362. echo 'Restart=always' >> /etc/systemd/system/fedwiki.service
  363. echo "Environment=NODE_ENV=production" >> /etc/systemd/system/fedwiki.service
  364. echo '' >> /etc/systemd/system/fedwiki.service
  365. echo '[Install]' >> /etc/systemd/system/fedwiki.service
  366. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/fedwiki.service
  367. chown -R fedwiki:fedwiki $FEDWIKI_DATA
  368. fedwiki_setup_web
  369. ${PROJECT_NAME}-pass -u $MY_USERNAME -a fedwiki -p "$FEDWIKI_ADMIN_PASSWORD"
  370. function_check add_ddns_domain
  371. add_ddns_domain $FEDWIKI_DOMAIN_NAME
  372. fedwiki_remove_bad_links
  373. systemctl enable fedwiki
  374. systemctl daemon-reload
  375. systemctl start fedwiki
  376. systemctl restart nginx
  377. set_completion_param "fedwiki domain" "$FEDWIKI_DOMAIN_NAME"
  378. set_completion_param "fedwiki version" "$FEDWIKI_VERSION"
  379. APP_INSTALLED=1
  380. }
  381. # NOTE: deliberately no exit 0