#!/bin/bash # # .---. . . # | | | # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-' # ' ' --' --' -' - -' ' ' -' -' -' ' - --' # # Freedom in the Cloud # # Databaseless blogging system # # License # ======= # # Copyright (C) 2018 Bob Mottram # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . VARIANTS='full full-vim' IN_DEFAULT_INSTALL=0 SHOW_ON_ABOUT=1 BLUDIT_DOMAIN_NAME= BLUDIT_CODE= BLUDIT_ONION_PORT=9361 BLUDIT_REPO="https://github.com/bludit/bludit" BLUDIT_COMMIT='0e27e31a84421b3e6bd000a77bc89c2dff3c446a' bludit=(ONION_ONLY BLUDIT_DOMAIN_NAME BLUDIT_CODE DDNS_PROVIDER MY_USERNAME) function logging_on_bludit { echo -n '' } function logging_off_bludit { echo -n '' } function remove_user_bludit { remove_username="$1" "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp bludit } function add_user_bludit { new_username="$1" new_user_password="$2" "${PROJECT_NAME}-pass" -u "$new_username" -a bludit -p "$new_user_password" echo '0' } function install_interactive_bludit { if [ ! "$ONION_ONLY" ]; then ONION_ONLY='no' fi if [[ "$ONION_ONLY" != "no" ]]; then BLUDIT_DOMAIN_NAME='bludit.local' write_config_param "BLUDIT_DOMAIN_NAME" "$BLUDIT_DOMAIN_NAME" else interactive_site_details "bludit" "BLUDIT_DOMAIN_NAME" "bludit_CODE" fi APP_INSTALLED=1 } function change_password_bludit { curr_username="$1" new_user_password="$2" read_config_param 'BLUDIT_DOMAIN_NAME' "${PROJECT_NAME}-pass" -u "$curr_username" -a bludit -p "$new_user_password" } function reconfigure_bludit { # This is used if you need to switch identity. Dump old keys and generate new ones echo -n '' } function upgrade_bludit { CURR_BLUDIT_COMMIT=$(get_completion_param "bludit commit") if [[ "$CURR_BLUDIT_COMMIT" == "$BLUDIT_COMMIT" ]]; then return fi if grep -q "bludit domain" "$COMPLETION_FILE"; then BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain") fi # update to the next commit set_repo_commit "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" "bludit commit" "$BLUDIT_COMMIT" $BLUDIT_REPO chown -R www-data:www-data "/var/www/${BLUDIT_DOMAIN_NAME}/htdocs" } function backup_local_bludit { BLUDIT_DOMAIN_NAME='bludit' if grep -q "bludit domain" "$COMPLETION_FILE"; then BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain") fi source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs suspend_site "${BLUDIT_DOMAIN_NAME}" dest_directory=bludit backup_directory_to_usb "$source_directory" $dest_directory restart_site } function restore_local_bludit { if ! grep -q "bludit domain" "$COMPLETION_FILE"; then return fi BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain") if [ "$BLUDIT_DOMAIN_NAME" ]; then temp_restore_dir=/root/tempbludit bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs restore_directory_from_usb $temp_restore_dir bludit if [ -d $temp_restore_dir ]; then if [ -d "$temp_restore_dir$bludit_dir" ]; then cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/ else if [ ! -d "$bludit_dir" ]; then mkdir "$bludit_dir" fi cp -rp "$temp_restore_dir"/* "$bludit_dir"/ fi chown -R www-data:www-data "$bludit_dir" rm -rf $temp_restore_dir fi fi } function backup_remote_bludit { BLUDIT_DOMAIN_NAME='bludit' if grep -q "bludit domain" "$COMPLETION_FILE"; then BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain") fi source_directory=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs suspend_site "${BLUDIT_DOMAIN_NAME}" dest_directory=bludit backup_directory_to_friend "$source_directory" $dest_directory restart_site } function restore_remote_bludit { if ! grep -q "bludit domain" "$COMPLETION_FILE"; then return fi BLUDIT_DOMAIN_NAME=$(get_completion_param "bludit domain") if [ "$BLUDIT_DOMAIN_NAME" ]; then temp_restore_dir=/root/tempbludit bludit_dir=/var/www/${BLUDIT_DOMAIN_NAME}/htdocs restore_directory_from_friend $temp_restore_dir bludit if [ -d $temp_restore_dir ]; then if [ -d "$temp_restore_dir$bludit_dir" ]; then cp -rp "$temp_restore_dir$bludit_dir"/* "$bludit_dir"/ else if [ ! -d "$bludit_dir" ]; then mkdir "$bludit_dir" fi cp -rp $temp_restore_dir/* "$bludit_dir"/ fi chown -R www-data:www-data "$bludit_dir" rm -rf $temp_restore_dir fi fi } function remove_bludit { nginx_dissite "$BLUDIT_DOMAIN_NAME" remove_certs "$BLUDIT_DOMAIN_NAME" if [ -d "/var/www/$BLUDIT_DOMAIN_NAME" ]; then rm -rf "/var/www/$BLUDIT_DOMAIN_NAME" fi if [ -f "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME" ]; then rm "/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME" fi remove_onion_service bludit ${BLUDIT_ONION_PORT} if grep -q "bludit" /etc/crontab; then sed -i "/bludit/d" /etc/crontab fi remove_app bludit remove_completion_param install_bludit sed -i '/bludit/d' "$COMPLETION_FILE" remove_ddns_domain "$BLUDIT_DOMAIN_NAME" } function install_bludit { apt-get -yq install php-gettext php-curl php-gd php-mysql git curl apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl if [ ! -d "/var/www/$BLUDIT_DOMAIN_NAME" ]; then mkdir "/var/www/$BLUDIT_DOMAIN_NAME" fi if [ ! -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then if [ -d /repos/bludit ]; then mkdir "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" cp -r -p /repos/bludit/. "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 324687356 git pull else git_clone $BLUDIT_REPO "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" fi if [ ! -d "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" ]; then echo $'Unable to clone bludit repo' exit 87525 fi fi cd "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" || exit 36587356 git checkout $BLUDIT_COMMIT -b $BLUDIT_COMMIT set_completion_param "bludit commit" "$BLUDIT_COMMIT" chmod g+w "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" chown -R www-data:www-data "/var/www/$BLUDIT_DOMAIN_NAME/htdocs" add_ddns_domain "$BLUDIT_DOMAIN_NAME" BLUDIT_ONION_HOSTNAME=$(add_onion_service bludit 80 ${BLUDIT_ONION_PORT}) bludit_nginx_site=/etc/nginx/sites-available/$BLUDIT_DOMAIN_NAME if [[ "$ONION_ONLY" == "no" ]]; then nginx_http_redirect "$BLUDIT_DOMAIN_NAME" "index index.php" { echo 'server {'; echo ' listen 443 ssl;'; echo ' #listen [::]:443 ssl;'; echo " server_name $BLUDIT_DOMAIN_NAME;"; echo ''; } >> "$bludit_nginx_site" nginx_compress "$BLUDIT_DOMAIN_NAME" echo '' >> "$bludit_nginx_site" echo ' # Security' >> "$bludit_nginx_site" nginx_ssl "$BLUDIT_DOMAIN_NAME" nginx_security_options "$BLUDIT_DOMAIN_NAME" { echo ' add_header Strict-Transport-Security max-age=15768000;'; echo ''; echo ' # Logs'; echo ' access_log /dev/null;'; echo ' error_log /dev/null;'; echo ''; echo ' # Root'; echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;"; echo ''; echo ' index index.php;'; echo ' location ~ \.php {'; echo ' include snippets/fastcgi-php.conf;'; echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;'; echo ' fastcgi_read_timeout 30;'; echo ' }'; echo ''; echo ' # Location'; echo ' location / {'; } >> "$bludit_nginx_site" nginx_limits "$BLUDIT_DOMAIN_NAME" '15m' { echo " try_files \$uri \$uri/ /index.html;"; echo ' }'; echo '}'; } >> "$bludit_nginx_site" else echo -n '' > "$bludit_nginx_site" fi { echo 'server {'; echo " listen 127.0.0.1:$BLUDIT_ONION_PORT default_server;"; echo " server_name $BLUDIT_ONION_HOSTNAME;"; echo ''; } >> "$bludit_nginx_site" nginx_compress "$BLUDIT_DOMAIN_NAME" echo '' >> "$bludit_nginx_site" nginx_security_options "$BLUDIT_DOMAIN_NAME" { echo ''; echo ' # Logs'; echo ' access_log /dev/null;'; echo ' error_log /dev/null;'; echo ''; echo ' # Root'; echo " root /var/www/$BLUDIT_DOMAIN_NAME/htdocs;"; echo ''; echo ' index index.php;'; echo ' location ~ \.php {'; echo ' include snippets/fastcgi-php.conf;'; echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;'; echo ' fastcgi_read_timeout 30;'; echo ' }'; echo ''; echo ' # Location'; echo ' location / {'; } >> "$bludit_nginx_site" nginx_limits "$BLUDIT_DOMAIN_NAME" '15m' { echo " try_files \$uri \$uri/ index.html;"; echo ' }'; echo '}'; } >> "$bludit_nginx_site" configure_php create_site_certificate "$BLUDIT_DOMAIN_NAME" 'yes' nginx_ensite "$BLUDIT_DOMAIN_NAME" systemctl restart php7.0-fpm systemctl restart nginx "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a bludit -p "$BLUDIT_ADMIN_PASSWORD" set_completion_param "bludit domain" "$BLUDIT_DOMAIN_NAME" APP_INSTALLED=1 } # NOTE: deliberately there is no "exit 0"