freedomboneeee/src/freedombone-app-cryptpad

342 lines
12 KiB
Plaintext
Raw Normal View History

2017-05-26 15:26:27 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# cryptpad application
#
# License
# =======
#
# Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
#
# 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 <http://www.gnu.org/licenses/>.
VARIANTS='full full-vim writer'
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
2017-05-28 11:34:31 +02:00
SHOW_ICANN_ADDRESS_ON_ABOUT=0
2017-05-26 15:26:27 +02:00
CRYPTPAD_ONION_PORT=8119
CRYPTPAD_PORT=9003
2017-05-26 15:53:32 +02:00
CRYPTPAD_REPO="https://github.com/xwiki-labs/cryptpad"
2017-05-26 18:09:53 +02:00
CRYPTPAD_COMMIT='ed5b005216be9b9029c1ccd25a5fdc7908ed8730'
2017-05-26 15:26:27 +02:00
CRYPTPAD_DIR=/etc/cryptpad
2017-05-28 11:34:31 +02:00
cryptpad_variables=(ONION_ONLY)
2017-05-26 15:26:27 +02:00
function remove_user_cryptpad {
remove_username="$1"
}
function add_user_cryptpad {
new_username="$1"
new_user_password="$2"
echo '0'
}
function install_interactive_cryptpad {
2017-05-28 11:34:31 +02:00
echo -n ''
2017-05-26 15:26:27 +02:00
APP_INSTALLED=1
}
function change_password_cryptpad {
curr_username="$1"
new_user_password="$2"
}
function reconfigure_cryptpad {
if [ -d $CRYPTPAD_DIR/datastore ]; then
rm -rf $CRYPTPAD_DIR/datastore
fi
}
function upgrade_cryptpad {
CURR_CRYPTPAD_COMMIT=$(get_completion_param "cryptpad commit")
if [[ "$CURR_CRYPTPAD_COMMIT" == "$CRYPTPAD_COMMIT" ]]; then
return
fi
systemctl stop cryptpad
# update to the next commit
function_check set_repo_commit
set_repo_commit $CRYPTPAD_DIR "cryptpad commit" "$CRYPTPAD_COMMIT" $CRYPTPAD_REPO
cd $CRYPTPAD_DIR
systemctl start cryptpad
}
function backup_local_cryptpad {
source_directory=$CRYPTPAD_DIR/datastore
if [ -d $source_directory ]; then
systemctl stop cryptpad
dest_directory=cryptpad
function_check suspend_site
2017-05-28 11:34:31 +02:00
suspend_site cryptpad
2017-05-26 15:26:27 +02:00
function_check backup_directory_to_usb
backup_directory_to_usb $source_directory $dest_directory
function_check restart_site
restart_site
systemctl start cryptpad
fi
}
function restore_local_cryptpad {
if [ -d $CRYPTPAD_DIR ]; then
systemctl stop cryptpad
temp_restore_dir=/root/tempcryptpad
function_check restore_directory_from_usb
restore_directory_from_usb $temp_restore_dir cryptpad
cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
systemctl start cryptpad
fi
}
function backup_remote_cryptpad {
if grep -q "cryptpad domain" $COMPLETION_FILE; then
temp_backup_dir=$CRYPTPAD_DIR/datastore
if [ -d $temp_backup_dir ]; then
systemctl stop cryptpad
function_check suspend_site
2017-05-28 11:34:31 +02:00
suspend_site cryptpad
2017-05-26 15:26:27 +02:00
echo $"Backing up Cryptpad installation"
function_check backup_directory_to_friend
backup_directory_to_friend $temp_backup_dir cryptpad
function_check restart_site
restart_site
systemctl start cryptpad
else
echo $"cryptpad domain specified but not found in ${temp_backup_dir}"
fi
fi
}
function restore_remote_cryptpad {
if [ -d $CRYPTPAD_DIR ]; then
systemctl stop cryptpad
temp_restore_dir=/root/tempcryptpad
function_check restore_directory_from_usb
restore_directory_from_friend $temp_restore_dir cryptpad
cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
systemctl start cryptpad
fi
}
function remove_cryptpad {
systemctl stop cryptpad
systemctl disable cryptpad
if [ -f /etc/systemd/system/cryptpad.service ]; then
rm /etc/systemd/system/cryptpad.service
fi
function_check remove_nodejs
remove_nodejs cryptpad
2017-05-28 11:34:31 +02:00
nginx_dissite cryptpad
2017-05-26 15:26:27 +02:00
if [ -d $CRYPTPAD_DIR ]; then
rm -rf $CRYPTPAD_DIR
fi
2017-05-28 11:34:31 +02:00
if [ -f /etc/nginx/sites-available/cryptpad ]; then
rm /etc/nginx/sites-available/cryptpad
2017-05-26 15:26:27 +02:00
fi
function_check remove_onion_service
remove_onion_service cryptpad ${CRYPTPAD_ONION_PORT}
remove_app cryptpad
remove_completion_param install_cryptpad
sed -i '/cryptpad/d' $COMPLETION_FILE
userdel -r cryptpad
}
function install_cryptpad_main {
if [[ $(app_is_installed cryptpad_main) == "1" ]]; then
return
fi
2017-05-28 11:34:31 +02:00
if [ ! -d /var/www/cryptpad ]; then
mkdir /var/www/cryptpad
2017-05-26 15:26:27 +02:00
fi
if [ -d $CRYPTPAD_DIR ]; then
rm -rf $CRYPTPAD_DIR
fi
2017-05-26 15:26:27 +02:00
function_check git_clone
git_clone $CRYPTPAD_REPO $CRYPTPAD_DIR
if [ ! -d $CRYPTPAD_DIR ]; then
echo $'Unable to clone cryptpad repo'
exit 783251
2017-05-26 15:26:27 +02:00
fi
# an unprivileged user to run as
useradd -d $CRYPTPAD_DIR/ cryptpad
cd $CRYPTPAD_DIR
git checkout $CRYPTPAD_COMMIT -b $CRYPTPAD_COMMIT
set_completion_param "cryptpad commit" "$CRYPTPAD_COMMIT"
2017-05-27 20:11:22 +02:00
chown -R cryptpad:cryptpad $CRYPTPAD_DIR
2017-05-26 15:26:27 +02:00
CRYPTPAD_ONION_HOSTNAME=$(add_onion_service cryptpad 80 ${CRYPTPAD_ONION_PORT})
2017-05-28 11:34:31 +02:00
cryptpad_nginx_site=/etc/nginx/sites-available/cryptpad
echo 'server {' > $cryptpad_nginx_site
2017-05-26 23:56:12 +02:00
echo " listen 127.0.0.1:$CRYPTPAD_ONION_PORT default_server;" >> $cryptpad_nginx_site
echo " server_name $CRYPTPAD_ONION_HOSTNAME;" >> $cryptpad_nginx_site
2017-05-26 15:26:27 +02:00
echo '' >> $cryptpad_nginx_site
echo ' # Logs' >> $cryptpad_nginx_site
echo ' access_log /dev/null;' >> $cryptpad_nginx_site
echo ' error_log /dev/null;' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' # Root' >> $cryptpad_nginx_site
echo " root $CRYPTPAD_DIR;" >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' index index.html;' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
2017-05-26 23:56:12 +02:00
echo ' add_header X-XSS-Protection "1; mode=block";' >> $cryptpad_nginx_site
echo ' add_header X-Content-Type-Options nosniff;' >> $cryptpad_nginx_site
echo ' add_header X-Frame-Options SAMEORIGIN;' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' if ($uri = /pad/inner.html) {' >> $cryptpad_nginx_site
echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";" >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
2017-05-26 16:26:51 +02:00
echo ' location = /cryptpad_websocket {' >> $cryptpad_nginx_site
2017-05-26 15:26:27 +02:00
echo " proxy_pass http://localhost:$CRYPTPAD_PORT;" >> $cryptpad_nginx_site
echo ' proxy_set_header X-Real-IP $remote_addr;' >> $cryptpad_nginx_site
echo ' proxy_set_header Host $host;' >> $cryptpad_nginx_site
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' # WebSocket support (nginx 1.4)' >> $cryptpad_nginx_site
echo ' proxy_http_version 1.1;' >> $cryptpad_nginx_site
echo ' proxy_set_header Upgrade $http_upgrade;' >> $cryptpad_nginx_site
echo ' proxy_set_header Connection upgrade;' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' location ^~ /customize.dist/ {' >> $cryptpad_nginx_site
echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo ' location ^~ /customize/ {' >> $cryptpad_nginx_site
echo ' rewrite ^/customize/(.*)$ $1 break;' >> $cryptpad_nginx_site
echo ' try_files /customize/$uri /customize.dist/$uri;' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo ' location = /api/config {' >> $cryptpad_nginx_site
echo ' default_type text/javascript;' >> $cryptpad_nginx_site
echo ' rewrite ^.*$ /customize/api/config break;' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' location ^~ /blob/ {' >> $cryptpad_nginx_site
echo ' try_files $uri =404;' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
echo ' location ^~ /register/ {' >> $cryptpad_nginx_site
echo ' try_files $uri =404;' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
2017-05-26 15:26:27 +02:00
echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {' >> $cryptpad_nginx_site
echo ' rewrite ^(.*)$ $1/ redirect;' >> $cryptpad_nginx_site
echo ' }' >> $cryptpad_nginx_site
echo '' >> $cryptpad_nginx_site
2017-05-26 23:56:12 +02:00
echo ' try_files /www/$uri /www/$uri/index.html /customize/$uri;' >> $cryptpad_nginx_site
2017-05-26 15:26:27 +02:00
echo '}' >> $cryptpad_nginx_site
function_check nginx_ensite
2017-05-28 11:34:31 +02:00
nginx_ensite cryptpad
2017-05-26 15:26:27 +02:00
install_completed cryptpad_main
}
function install_cryptpad {
function_check install_nodejs
install_nodejs cryptpad
install_cryptpad_main
cd $CRYPTPAD_DIR
npm install
2017-05-26 16:10:25 +02:00
npm install -g bower@1.8.0
2017-05-26 16:26:51 +02:00
su -c 'bower install' - cryptpad
2017-05-26 15:26:27 +02:00
cp config.example.js config.js
if [ ! -f config.js ]; then
echo $'Cryptpad config file not found'
exit 628252
fi
sed -i "s|httpPort:.*|httpPort: $CRYPTPAD_PORT,|g" config.js
sed -i "s|// domain:|domain:|g" config.js
sed -i 's|openFileLimit:.*|openFileLimit: 1024,|g' config.js
2017-05-28 11:34:31 +02:00
sed -i "s|domain:.*|domain: 'http://$CRYPTPAD_ONION_HOSTNAME',|g" config.js
2017-05-26 15:26:27 +02:00
chown -R cryptpad:cryptpad $CRYPTPAD_DIR
# daemon
echo '[Unit]' > /etc/systemd/system/cryptpad.service
echo 'Description=Cryptpad' >> /etc/systemd/system/cryptpad.service
echo 'After=syslog.target' >> /etc/systemd/system/cryptpad.service
echo 'After=network.target' >> /etc/systemd/system/cryptpad.service
echo '' >> /etc/systemd/system/cryptpad.service
echo '[Service]' >> /etc/systemd/system/cryptpad.service
echo 'User=cryptpad' >> /etc/systemd/system/cryptpad.service
echo 'Group=cryptpad' >> /etc/systemd/system/cryptpad.service
echo "WorkingDirectory=$CRYPTPAD_DIR" >> /etc/systemd/system/cryptpad.service
2017-05-26 19:12:39 +02:00
echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js" >> /etc/systemd/system/cryptpad.service
2017-05-26 23:56:12 +02:00
echo 'Environment=PATH=/usr/bin:/usr/local/bin' >> /etc/systemd/system/cryptpad.service
echo 'Environment=NODE_ENV=production' >> /etc/systemd/system/cryptpad.service
2017-05-26 15:26:27 +02:00
echo 'Restart=on-failure' >> /etc/systemd/system/cryptpad.service
echo '' >> /etc/systemd/system/cryptpad.service
echo '[Install]' >> /etc/systemd/system/cryptpad.service
echo 'WantedBy=multi-user.target' >> /etc/systemd/system/cryptpad.service
systemctl enable cryptpad.service
systemctl daemon-reload
systemctl start cryptpad.service
2017-05-28 11:43:29 +02:00
sleep 6
2017-05-27 21:49:13 +02:00
if [ ! -d $CRYPTPAD_DIR/customize/api ]; then
mkdir -p $CRYPTPAD_DIR/customize/api
fi
wget 127.0.0.1:$CRYPTPAD_PORT/api/config -O $CRYPTPAD_DIR/customize/api/config
if [ ! -f $CRYPTPAD_DIR/customize/api/config ]; then
echo $'Unable to wget api/config'
exit 89252
fi
chown -R cryptpad:cryptpad $CRYPTPAD_DIR
2017-05-26 15:26:27 +02:00
systemctl restart nginx
APP_INSTALLED=1
}
# NOTE: deliberately there is no "exit 0"