freedombone-app-bludit 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS='full full-vim'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. BLUDIT_DOMAIN_NAME=
  32. BLUDIT_CODE=
  33. BLUDIT_ONION_PORT=9844
  34. BLUDIT_REPO="https://github.com/bludit/bludit"
  35. BLUDIT_COMMIT='0e27e31a84421b3e6bd000a77bc89c2dff3c446a'
  36. bludit_variables=(ONION_ONLY
  37. BLUDIT_DOMAIN_NAME
  38. BLUDIT_CODE
  39. DDNS_PROVIDER
  40. MY_USERNAME)
  41. function logging_on_bludit {
  42. echo -n ''
  43. }
  44. function logging_off_bludit {
  45. echo -n ''
  46. }
  47. function remove_user_bludit {
  48. remove_username="$1"
  49. "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp bludit
  50. }
  51. function add_user_bludit {
  52. new_username="$1"
  53. new_user_password="$2"
  54. "${PROJECT_NAME}-pass" -u "$new_username" -a bludit -p "$new_user_password"
  55. echo '0'
  56. }
  57. function install_interactive_bludit {
  58. if [ ! "$ONION_ONLY" ]; then
  59. ONION_ONLY='no'
  60. fi
  61. if [[ "$ONION_ONLY" != "no" ]]; then
  62. BLUDIT_DOMAIN_NAME='bludit.local'
  63. write_config_param "BLUDIT_DOMAIN_NAME" "$BLUDIT_DOMAIN_NAME"
  64. else
  65. interactive_site_details "bludit" "BLUDIT_DOMAIN_NAME" "BLUDIT_CODE"
  66. fi
  67. APP_INSTALLED=1
  68. }
  69. function change_password_bludit {
  70. curr_username="$1"
  71. new_user_password="$2"
  72. read_config_param 'BLUDIT_DOMAIN_NAME'
  73. "${PROJECT_NAME}-pass" -u "$curr_username" -a bludit -p "$new_user_password"
  74. }
  75. function reconfigure_bludit {
  76. # This is used if you need to switch identity. Dump old keys and generate new ones
  77. echo -n ''
  78. }
  79. function configure_interactive_bludit {
  80. while true
  81. do
  82. data=$(mktemp 2>/dev/null)
  83. dialog --backtitle $"Freedombone Control Panel" \
  84. --title $"bludit" \
  85. --radiolist $"Choose an operation:" 16 70 3 \
  86. 1 $"Option 1" off \
  87. 2 $"Option 2" off \
  88. 3 $"Exit" on 2> "$data"
  89. sel=$?
  90. case $sel in
  91. 1) rm -f "$data"
  92. return;;
  93. 255) rm -f "$data"
  94. return;;
  95. esac
  96. case $(cat "$data") in
  97. 1) # call some function for option 1
  98. ;;
  99. 2) # call some function for option 2
  100. ;;
  101. 3) rm -f "$data"
  102. break;;
  103. esac
  104. rm -f "$data"
  105. done
  106. }
  107. function upgrade_bludit {
  108. CURR_BLUDIT_COMMIT=$(get_completion_param "bludit commit")
  109. if [[ "$CURR_BLUDIT_COMMIT" == "$BLUDIT_COMMIT" ]]; then
  110. return
  111. fi
  112. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  113. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  114. fi
  115. # update to the next commit
  116. set_repo_commit "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" "bludit commit" "$BLUDIT_COMMIT" $BLUDIT_REPO
  117. chown -R www-data:www-data "/var/www/${BLUDIT_DOMAIN_NAME}/htdocs"
  118. }
  119. function backup_local_bludit {
  120. BLUDIT_DOMAIN_NAME='bludit'
  121. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  122. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  123. fi
  124. source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  125. suspend_site "${BLUDIT_DOMAIN_NAME}"
  126. dest_directory=bludit
  127. backup_directory_to_usb "$source_directory" $dest_directory
  128. restart_site
  129. }
  130. function restore_local_bludit {
  131. if ! grep -q "bludit domain" "$COMPLETION_FILE"; then
  132. return
  133. fi
  134. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  135. if [ "$BLUDIT_DOMAIN_NAME" ]; then
  136. temp_restore_dir=/root/tempbludit
  137. bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  138. restore_directory_from_usb $temp_restore_dir bludit
  139. if [ -d $temp_restore_dir ]; then
  140. if [ -d "$temp_restore_dir$bludit_dir" ]; then
  141. cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/
  142. else
  143. if [ ! -d "$bludit_dir" ]; then
  144. mkdir "$bludit_dir"
  145. fi
  146. cp -rp "$temp_restore_dir"/* "$bludit_dir"/
  147. fi
  148. chown -R www-data:www-data "$bludit_dir"
  149. rm -rf $temp_restore_dir
  150. fi
  151. fi
  152. }
  153. function backup_remote_bludit {
  154. BLUDIT_DOMAIN_NAME='bludit'
  155. if grep -q "bludit domain" "$COMPLETION_FILE"; then
  156. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  157. fi
  158. source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  159. suspend_site "${BLUDIT_DOMAIN_NAME}"
  160. dest_directory=bludit
  161. backup_directory_to_friend "$source_directory" $dest_directory
  162. restart_site
  163. }
  164. function restore_remote_bludit {
  165. if ! grep -q "bludit domain" "$COMPLETION_FILE"; then
  166. return
  167. fi
  168. BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain")
  169. if [ "$BLUDIT_DOMAIN_NAME" ]; then
  170. temp_restore_dir=/root/tempbludit
  171. bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs
  172. restore_directory_from_friend $temp_restore_dir bludit
  173. if [ -d $temp_restore_dir ]; then
  174. if [ -d "$temp_restore_dir$bludit_dir" ]; then
  175. cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/
  176. else
  177. if [ ! -d "$bludit_dir" ]; then
  178. mkdir "$bludit_dir"
  179. fi
  180. cp -rp $temp_restore_dir/* "$bludit_dir"/
  181. fi
  182. chown -R www-data:www-data "$bludit_dir"
  183. rm -rf $temp_restore_dir
  184. fi
  185. fi
  186. }
  187. function remove_bludit {
  188. nginx_dissite "$BLUDIT_DOMAIN_NAME"
  189. remove_certs "$BLUDIT_DOMAIN_NAME"
  190. if [ -d "/var/www/$BLUDIT_DOMAIN_NAME" ]; then
  191. rm -rf "/var/www/$BLUDIT_DOMAIN_NAME"
  192. fi
  193. if [ -f "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME" ]; then
  194. rm "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME"
  195. fi
  196. remove_onion_service bludit ${BLUDIT_ONION_PORT}
  197. if grep -q "bludit" /etc/crontab; then
  198. sed -i "/bludit/d" /etc/crontab
  199. fi
  200. remove_app bludit
  201. remove_completion_param install_bludit
  202. sed -i '/bludit/d' "$COMPLETION_FILE"
  203. remove_ddns_domain "$BLUDIT_DOMAIN_NAME"
  204. }
  205. function install_bludit {
  206. apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
  207. apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
  208. if [ ! "$BLUDIT_DOMAIN_NAME" ]; then
  209. echo $'No domain name was given'
  210. exit 3568356
  211. fi
  212. if [ -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then
  213. rm -rf "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  214. fi
  215. if [ -d /repos/bludit ]; then
  216. mkdir "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  217. cp -r -p /repos/bludit/. "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  218. cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 324687356
  219. git pull
  220. else
  221. git_clone $BLUDIT_REPO "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  222. fi
  223. if [ ! -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then
  224. echo $'Unable to clone bludit repo'
  225. exit 87525
  226. fi
  227. cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 36587356
  228. git checkout $BLUDIT_COMMIT -b $BLUDIT_COMMIT
  229. set_completion_param "bludit commit" "$BLUDIT_COMMIT"
  230. chmod g+w "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  231. chown -R www-data:www-data "/var/www/$BLUDIT_DOMAIN_NAME/htdocs"
  232. add_ddns_domain "$BLUDIT_DOMAIN_NAME"
  233. BLUDIT_ONION_HOSTNAME=$(add_onion_service bludit 80 ${BLUDIT_ONION_PORT})
  234. bludit_nginx_site=/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME
  235. if [[ "$ONION_ONLY" == "no" ]]; then
  236. nginx_http_redirect "$BLUDIT_DOMAIN_NAME" "index index.php"
  237. { echo 'server {';
  238. echo ' listen 443 ssl;';
  239. echo ' #listen [::]:443 ssl;';
  240. echo " server_name $BLUDIT_DOMAIN_NAME;";
  241. echo ''; } >> "$bludit_nginx_site"
  242. nginx_compress "$BLUDIT_DOMAIN_NAME"
  243. echo '' >> "$bludit_nginx_site"
  244. echo ' # Security' >> "$bludit_nginx_site"
  245. nginx_ssl "$BLUDIT_DOMAIN_NAME"
  246. nginx_security_options "$BLUDIT_DOMAIN_NAME"
  247. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  248. echo '';
  249. echo ' # Logs';
  250. echo ' access_log /dev/null;';
  251. echo ' error_log /dev/null;';
  252. echo '';
  253. echo ' # Root';
  254. echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;";
  255. echo '';
  256. echo ' index index.php;';
  257. echo ' location ~ \.php {';
  258. echo ' include snippets/fastcgi-php.conf;';
  259. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  260. echo ' fastcgi_read_timeout 30;';
  261. echo ' }';
  262. echo '';
  263. echo ' # Location';
  264. echo ' location / {'; } >> "$bludit_nginx_site"
  265. nginx_limits "$BLUDIT_DOMAIN_NAME" '15m'
  266. { echo " try_files \$uri \$uri/ /index.php?\$args;";
  267. echo ' }';
  268. echo '}'; } >> "$bludit_nginx_site"
  269. else
  270. echo -n '' > "$bludit_nginx_site"
  271. fi
  272. { echo 'server {';
  273. echo " listen 127.0.0.1:$BLUDIT_ONION_PORT default_server;";
  274. echo " server_name $BLUDIT_ONION_HOSTNAME;";
  275. echo ''; } >> "$bludit_nginx_site"
  276. nginx_compress "$BLUDIT_DOMAIN_NAME"
  277. echo '' >> "$bludit_nginx_site"
  278. nginx_security_options "$BLUDIT_DOMAIN_NAME"
  279. { echo '';
  280. echo ' # Logs';
  281. echo ' access_log /dev/null;';
  282. echo ' error_log /dev/null;';
  283. echo '';
  284. echo ' # Root';
  285. echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;";
  286. echo '';
  287. echo ' index index.php;';
  288. echo ' location ~ \.php {';
  289. echo ' include snippets/fastcgi-php.conf;';
  290. echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
  291. echo ' fastcgi_read_timeout 30;';
  292. echo ' }';
  293. echo '';
  294. echo ' # Location';
  295. echo ' location / {'; } >> "$bludit_nginx_site"
  296. nginx_limits "$BLUDIT_DOMAIN_NAME" '15m'
  297. { echo " try_files \$uri \$uri/ index.php?\$args;";
  298. echo ' }';
  299. echo '}'; } >> "$bludit_nginx_site"
  300. configure_php
  301. create_site_certificate "$BLUDIT_DOMAIN_NAME" 'yes'
  302. nginx_ensite "$BLUDIT_DOMAIN_NAME"
  303. systemctl restart php7.0-fpm
  304. systemctl restart nginx
  305. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bludit -p "$BLUDIT_ADMIN_PASSWORD"
  306. set_completion_param "bludit domain" "$BLUDIT_DOMAIN_NAME"
  307. APP_INSTALLED=1
  308. }
  309. # NOTE: deliberately there is no "exit 0"