123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- #!/bin/bash
- #
- # .---. . .
- # | | |
- # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
- # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
- # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
- #
- # Freedom in the Cloud
- #
- # SKS Keyserver
- #
- # 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'
-
- IN_DEFAULT_INSTALL=0
- SHOW_ON_ABOUT=1
-
- KEYSERVER_WEB_REPO="https://github.com/mattrude/pgpkeyserver-lite"
- KEYSERVER_WEB_COMMIT='a038cb79b927c99bf7da62f20d2c6a2f20374339'
- KEYSERVER_PORT=11371
- KEYSERVER_ONION_PORT=8122
- KEYSERVER_DOMAIN_NAME=
- KEYSERVER_CODE=
-
- keyserver_variables=(ONION_ONLY
- MY_USERNAME
- DEFAULT_DOMAIN_NAME
- KEYSERVER_DOMAIN_NAME
- KEYSERVER_CODE)
-
- function configure_firewall_for_keyserver {
- if [[ $ONION_ONLY != "no" ]]; then
- return
- fi
- firewall_add keyserver 11370 tcp
- firewall_add keyserver 11371 tcp
- firewall_add keyserver 11372 tcp
- mark_completed $FUNCNAME
- }
-
- function logging_on_keyserver {
- echo -n ''
- }
-
- function logging_off_keyserver {
- echo -n ''
- }
-
- function reconfigure_keyserver {
- echo -n ''
- }
-
- function upgrade_keyserver {
- CURR_KEYSERVER_WEB_COMMIT=$(get_completion_param "keyserver web commit")
- if [[ "$CURR_KEYSERVER_WEB_COMMIT" == "$KEYSERVER_WEB_COMMIT" ]]; then
- return
- fi
-
- if grep -q "keyserver domain" $COMPLETION_FILE; then
- KEYSERVER_DOMAIN_NAME=$(get_completion_param "keyserver domain")
- fi
-
- # update to the next commit
- function_check set_repo_commit
- set_repo_commit /var/www/$KEYSERVER_DOMAIN_NAME/htdocs "keyserver web commit" "$KEYSERVER_WEB_COMMIT" $KEYSERVER_WEB_REPO
-
- read_config_param MY_USERNAME
- USER_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
- GPG_ID=$(su -m root -c "gpg --list-keys $USER_EMAIL_ADDRESS | sed -n '2p' | sed 's/^[ \t]*//'" - $MY_USERNAME)
- if [ ! $GPG_ID ]; then
- echo $'No GPG ID for admin user'
- exit 846336
- fi
- if [ ${#GPG_ID} -lt 5 ]; then
- echo $'GPG ID not retrieved for admin user'
- exit 835292
- fi
- if [[ "$GPG_ID" == *"error"* ]]; then
- echo $'GPG ID not retrieved for admin user due to error'
- exit 74825
- fi
- sed -i "s|###ENTERPUBLICKEYHERE###|$GPG_ID|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/404.html
- sed -i "s|###ENTERPUBLICKEYHERE###|$GPG_ID|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/index.html
- sed -i "s|###ENTERNAMEHERE###|$USER_EMAIL_ADDRESS|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/404.html
- sed -i "s|###ENTERNAMEHERE###|$USER_EMAIL_ADDRESS|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/index.html
-
- chown -R www-data:www-data /var/www/$KEYSERVER_DOMAIN_NAME/htdocs
- }
-
- function backup_local_keyserver {
- echo -n ''
- }
-
- function restore_local_keyserver {
- echo -n ''
- }
-
- function backup_remote_keyserver {
- echo -n ''
- }
-
- function restore_remote_keyserver {
- echo -n ''
- }
-
- function remove_keyserver {
- systemctl stop sks
- apt-get -qy remove sks
-
- read_config_param "KEYSERVER_DOMAIN_NAME"
- nginx_dissite $KEYSERVER_DOMAIN_NAME
- remove_certs ${KEYSERVER_DOMAIN_NAME}
- if [ -f /etc/nginx/sites-available/$KEYSERVER_DOMAIN_NAME ]; then
- rm -f /etc/nginx/sites-available/$KEYSERVER_DOMAIN_NAME
- fi
- if [ -d /var/www/$KEYSERVER_DOMAIN_NAME ]; then
- rm -rf /var/www/$KEYSERVER_DOMAIN_NAME
- fi
- function_check remove_ddns_domain
- remove_ddns_domain $KEYSERVER_DOMAIN_NAME
-
- remove_config_param KEYSERVER_DOMAIN_NAME
- remove_config_param KEYSERVER_CODE
- function_check remove_onion_service
- remove_onion_service keyserver ${KEYSERVER_ONION_PORT}
- remove_onion_service sks 11370 11371 11372
- remove_completion_param "install_keyserver"
-
- firewall_remove 11370 tcp
- firewall_remove 11371 tcp
- firewall_remove 11372 tcp
-
- sed -i '/keyserver/d' $COMPLETION_FILE
- sed -i '/sks onion/d' $COMPLETION_FILE
- if [ -d /var/lib/sks ]; then
- rm -rf /var/lib/sks
- fi
- }
-
- function install_interactive_keyserver {
- if [ ! $ONION_ONLY ]; then
- ONION_ONLY='no'
- fi
-
- if [[ $ONION_ONLY != "no" ]]; then
- KEYSERVER_DOMAIN_NAME='keyserver.local'
- write_config_param "KEYSERVER_DOMAIN_NAME" "$KEYSERVER_DOMAIN_NAME"
- else
- function_check interactive_site_details
- interactive_site_details "keyserver" "KEYSERVER_DOMAIN_NAME" "KEYSERVER_CODE"
- fi
- APP_INSTALLED=1
- }
-
- function keyserver_import_keys {
- dialog --title $"Import public keys database" \
- --backtitle $"Freedombone Control Panel" \
- --defaultno \
- --yesno $"\nThis will download many gigabytes of data and so depending on your bandwidth it could take several days.\n\nContinue?" 10 60
- sel=$?
- case $sel in
- 1) return;;
- 255) return;;
- esac
- if [ ! -d /var/lib/sks/dump ]; then
- mkdir -p /var/lib/sks/dump
- fi
- cd /var/lib/sks/dump
- echo $'Getting keyserver dump. This may take a few days or longer, so be patient.'
- rm -rf /var/lib/sks/dump/*
- KEYSERVER_DUMP_URL="https://keyserver.mattrude.com/dump/$(date +%F)/"
- wget -crp -e robots=off --level=1 --cut-dirs=3 -nH \
- -A pgp,txt $KEYSERVER_DUMP_URL
-
- cd /var/lib/sks
- echo $'Building the keyserver database from the downloaded dump'
- sks build
- }
-
- function configure_interactive_keyserver {
- while true
- do
- data=$(tempfile 2>/dev/null)
- trap "rm -f $data" 0 1 2 5 15
- dialog --backtitle $"Freedombone Control Panel" \
- --title $"SKS Keyserver" \
- --radiolist $"Choose an operation:" 10 70 2 \
- 1 $"Import public keys database" off \
- 2 $"Exit" on 2> $data
- sel=$?
- case $sel in
- 1) return;;
- 255) return;;
- esac
- case $(cat $data) in
- 1) keyserver_import_keys;;
- 2) break;;
- esac
- done
- }
-
- function install_keyserver {
- apt-get -qy install build-essential gcc ocaml libdb-dev wget sks
- sks build
- chown -Rc debian-sks: /var/lib/sks/DB
- sed -i 's|initstart=.*|initstart=yes|g' /etc/default/sks
- systemctl restart sks
-
- if [ ! -d /var/www/$KEYSERVER_DOMAIN_NAME ]; then
- mkdir /var/www/$KEYSERVER_DOMAIN_NAME
- fi
-
- cd /var/www/$KEYSERVER_DOMAIN_NAME
- if [ -d /var/www/$KEYSERVER_DOMAIN_NAME/htdocs ]; then
- rm -rf /var/www/$KEYSERVER_DOMAIN_NAME/htdocs
- fi
-
- if [ -d /repos/keyserverweb ]; then
- mkdir htdocs
- cp -r -p /repos/keyserverweb/. htdocs
- cd htdocs
- git pull
- else
- git_clone $KEYSERVER_WEB_REPO htdocs
- fi
- if [ ! -d /var/www/$KEYSERVER_DOMAIN_NAME/htdocs ]; then
- echo $"/var/www/$KEYSERVER_DOMAIN_NAME/htdocs not found"
- exit 6539230
- fi
-
- cd /var/www/$KEYSERVER_DOMAIN_NAME/htdocs
- git checkout $KEYSERVER_WEB_COMMIT -b $KEYSERVER_WEB_COMMIT
- set_completion_param "keyserver web commit" "$KEYSERVER_WEB_COMMIT"
-
-
- USER_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
- GPG_ID=$(su -m root -c "gpg --list-keys $USER_EMAIL_ADDRESS | sed -n '2p' | sed 's/^[ \t]*//'" - $MY_USERNAME)
- if [ ! $GPG_ID ]; then
- echo $'No GPG ID for admin user'
- exit 846336
- fi
- if [ ${#GPG_ID} -lt 5 ]; then
- echo $'GPG ID not retrieved for admin user'
- exit 835292
- fi
- if [[ "$GPG_ID" == *"error"* ]]; then
- echo $'GPG ID not retrieved for admin user due to error'
- exit 74825
- fi
- sed -i "s|###ENTERPUBLICKEYHERE###|$GPG_ID|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/404.html
- sed -i "s|###ENTERPUBLICKEYHERE###|$GPG_ID|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/index.html
- sed -i "s|###ENTERNAMEHERE###|$USER_EMAIL_ADDRESS|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/404.html
- sed -i "s|###ENTERNAMEHERE###|$USER_EMAIL_ADDRESS|g" /var/www/$KEYSERVER_DOMAIN_NAME/htdocs/index.html
-
- sksconf_file=/var/lib/sks/sksconf
- echo 'debuglevel: 3' > $sksconf_file
- echo '' >> $sksconf_file
- echo "hostname: $KEYSERVER_DOMAIN_NAME" >> $sksconf_file
- echo '' >> $sksconf_file
- echo 'hkp_address: 127.0.0.1' >> $sksconf_file
- echo "hkp_port: $KEYSERVER_PORT" >> $sksconf_file
- echo 'recon_port: 11370' >> $sksconf_file
- echo '' >> $sksconf_file
- echo "server_contact: $GPG_ID" >> $sksconf_file
- echo '' >> $sksconf_file
- echo 'initial_stat:' >> $sksconf_file
- echo 'disable_mailsync:' >> $sksconf_file
- echo 'membership_reload_interval: 1' >> $sksconf_file
- echo 'stat_hour: 12' >> $sksconf_file
- echo '' >> $sksconf_file
- echo 'max_matches: 500' >> $sksconf_file
- chown debian-sks: $sksconf_file
-
- if ! grep -q "hidden_service_sks" /etc/tor/torrc; then
- echo 'HiddenServiceDir /var/lib/tor/hidden_service_sks/' >> /etc/tor/torrc
- echo "HiddenServicePort 11370 127.0.0.1:11370" >> /etc/tor/torrc
- echo "HiddenServicePort 11371 127.0.0.1:11371" >> /etc/tor/torrc
- echo "HiddenServicePort 11372 127.0.0.1:11372" >> /etc/tor/torrc
- echo $'Added onion site for sks'
- fi
-
- onion_update
- wait_for_onion_service 'sks'
-
- if [ ! -f /var/lib/tor/hidden_service_sks/hostname ]; then
- echo $'sks onion site hostname not found'
- exit 8352982
- fi
- SKS_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_sks/hostname)
-
- KEYSERVER_ONION_HOSTNAME=$(add_onion_service keyserver 80 ${KEYSERVER_ONION_PORT})
-
- keyserver_nginx_site=/etc/nginx/sites-available/$KEYSERVER_DOMAIN_NAME
- if [[ $ONION_ONLY == "no" ]]; then
- function_check nginx_http_redirect
- nginx_http_redirect $KEYSERVER_DOMAIN_NAME
- echo 'server {' >> $keyserver_nginx_site
- echo ' listen 443 ssl;' >> $keyserver_nginx_site
- echo ' listen 11372 ssl;' >> $keyserver_nginx_site
- echo ' listen [::]:443 ssl;' >> $keyserver_nginx_site
- echo " server_name $KEYSERVER_DOMAIN_NAME;" >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' # Security' >> $keyserver_nginx_site
- function_check nginx_ssl
- nginx_ssl $KEYSERVER_DOMAIN_NAME
-
- function_check nginx_disable_sniffing
- nginx_disable_sniffing $KEYSERVER_DOMAIN_NAME
-
- echo ' add_header Strict-Transport-Security max-age=15768000;' >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' # Logs' >> $keyserver_nginx_site
- echo ' access_log /dev/null;' >> $keyserver_nginx_site
- echo ' error_log /dev/null;' >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' # Root' >> $keyserver_nginx_site
- echo " root /var/www/$KEYSERVER_DOMAIN_NAME/htdocs;" >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
-
- echo ' rewrite ^/stats /pks/lookup?op=stats;' >> $keyserver_nginx_site
- echo ' rewrite ^/s/(.*) /pks/lookup?search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/search/(.*) /pks/lookup?search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/g/(.*) /pks/lookup?op=get&search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/get/(.*) /pks/lookup?op=get&search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/d/(.*) /pks/lookup?op=get&options=mr&search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/download/(.*) /pks/lookup?op=get&options=mr&search=$1;' >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' location /pks {' >> $keyserver_nginx_site
- echo " proxy_pass http://127.0.0.1:$KEYSERVER_PORT;" >> $keyserver_nginx_site
- echo ' proxy_pass_header Server;' >> $keyserver_nginx_site
- echo " add_header Via \"1.1 $KEYSERVER_DOMAIN_NAME:$KEYSERVER_PORT (nginx)\";" >> $keyserver_nginx_site
- echo ' proxy_ignore_client_abort on;' >> $keyserver_nginx_site
- echo ' client_max_body_size 8m;' >> $keyserver_nginx_site
- echo ' }' >> $keyserver_nginx_site
- echo '}' >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- else
- echo -n '' > $keyserver_nginx_site
- fi
- echo 'server {' >> $keyserver_nginx_site
- echo " listen 127.0.0.1:$KEYSERVER_ONION_PORT default_server;" >> $keyserver_nginx_site
- echo " listen 127.0.0.1:11371 default_server;" >> $keyserver_nginx_site
- echo " server_name $KEYSERVER_ONION_HOSTNAME;" >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- function_check nginx_disable_sniffing
- nginx_disable_sniffing $KEYSERVER_DOMAIN_NAME
- echo '' >> $keyserver_nginx_site
- echo ' # Logs' >> $keyserver_nginx_site
- echo ' access_log /dev/null;' >> $keyserver_nginx_site
- echo ' error_log /dev/null;' >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' # Root' >> $keyserver_nginx_site
- echo " root /var/www/$KEYSERVER_DOMAIN_NAME/mail;" >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' rewrite ^/stats /pks/lookup?op=stats;' >> $keyserver_nginx_site
- echo ' rewrite ^/s/(.*) /pks/lookup?search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/search/(.*) /pks/lookup?search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/g/(.*) /pks/lookup?op=get&search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/get/(.*) /pks/lookup?op=get&search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/d/(.*) /pks/lookup?op=get&options=mr&search=$1;' >> $keyserver_nginx_site
- echo ' rewrite ^/download/(.*) /pks/lookup?op=get&options=mr&search=$1;' >> $keyserver_nginx_site
- echo '' >> $keyserver_nginx_site
- echo ' location /pks {' >> $keyserver_nginx_site
- echo " proxy_pass http://127.0.0.1:$KEYSERVER_PORT;" >> $keyserver_nginx_site
- echo ' proxy_pass_header Server;' >> $keyserver_nginx_site
- echo " add_header Via \"1.1 $KEYSERVER_DOMAIN_NAME:$KEYSERVER_PORT (nginx)\";" >> $keyserver_nginx_site
- echo ' proxy_ignore_client_abort on;' >> $keyserver_nginx_site
- echo ' client_max_body_size 8m;' >> $keyserver_nginx_site
- echo ' }' >> $keyserver_nginx_site
- echo '}' >> $keyserver_nginx_site
-
- function_check create_site_certificate
- if [ ! -f /etc/ssl/certs/${KEYSERVER_DOMAIN_NAME}.pem ]; then
- create_site_certificate $KEYSERVER_DOMAIN_NAME 'yes'
- fi
-
- if [ -f /etc/ssl/certs/${KEYSERVER_DOMAIN_NAME}.crt ]; then
- mv /etc/ssl/certs/${KEYSERVER_DOMAIN_NAME}.crt /etc/ssl/certs/${KEYSERVER_DOMAIN_NAME}.pem
- fi
- if [ -f /etc/ssl/certs/${KEYSERVER_DOMAIN_NAME}.pem ]; then
- chown root:root /etc/ssl/certs/${KEYSERVER_DOMAIN_NAME}.pem
- sed -i "s|.crt|.pem|g" /etc/nginx/sites-available/${KEYSERVER_DOMAIN_NAME}
- fi
- if [ -f /etc/ssl/private/${KEYSERVER_DOMAIN_NAME}.key ]; then
- chown root:root /etc/ssl/private/${KEYSERVER_DOMAIN_NAME}.key
- fi
-
- chown -R www-data:www-data /var/www/$KEYSERVER_DOMAIN_NAME/htdocs
-
- function_check nginx_ensite
- nginx_ensite $KEYSERVER_DOMAIN_NAME
-
- configure_firewall_for_keyserver
-
- systemctl restart nginx
-
- set_completion_param "keyserver domain" "$KEYSERVER_DOMAIN_NAME"
- set_completion_param "keyserver onion domain" "$KEYSERVER_ONION_HOSTNAME"
- set_completion_param "sks onion domain" "$SKS_ONION_HOSTNAME"
-
- APP_INSTALLED=1
- }
-
- # NOTE: deliberately no exit 0
|