| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 | #!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# mailpile app
#
# License
# =======
#
# Copyright (C) 2016-2018 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 mailbox"
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
MAILPILE_DOMAIN_NAME=
MAILPILE_CODE=
MAILPILE_ONION_PORT=8103
MAILPILE_REPO="https://github.com/mailpile/Mailpile"
MAILPILE_COMMIT='f82074d2ab5ccd65d14a6b3c6cd65aeb132831d7'
MAILPILE_PORT=33411
mailpile_variables=(MAILPILE_REPO
                    MAILPILE_DOMAIN_NAME
                    MAILPILE_CODE
                    ONION_ONLY
                    DDNS_PROVIDER
                    DEFAULT_DOMAIN_NAME
                    MY_USERNAME)
function logging_on_mailpile {
    echo -n ''
}
function logging_off_mailpile {
    echo -n ''
}
function remove_user_mailpile {
    remove_username="$1"
    "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp mailpile
}
function add_user_mailpile {
    "${PROJECT_NAME}-pass" -u "$1" -a mailpile -p "$2"
}
function install_interactive_mailpile {
    if [ ! "$ONION_ONLY" ]; then
        ONION_ONLY='no'
    fi
    if [[ $ONION_ONLY != "no" ]]; then
        MAILPILE_DOMAIN_NAME='mailpile.local'
        write_config_param "MAILPILE_DOMAIN_NAME" "$MAILPILE_DOMAIN_NAME"
    else
        function_check interactive_site_details
        interactive_site_details "mailpile" "MAILPILE_DOMAIN_NAME" "MAILPILE_CODE"
    fi
    APP_INSTALLED=1
}
function change_password_mailpile {
    echo -n ''
    #${PROJECT_NAME}-pass -u $1 -a mailpile -p "$2"
}
function reconfigure_mailpile {
    echo -n ''
}
function upgrade_mailpile {
    read_config_param "MAILPILE_DOMAIN_NAME"
    CURR_COMMIT=$MAILPILE_COMMIT
    if grep -q "mailpile commit" "$COMPLETION_FILE"; then
        CURR_COMMIT=$(get_completion_param "mailpile commit")
    fi
    if [[ "$CURR_COMMIT" == "$MAILPILE_COMMIT" ]]; then
        return
    fi
    function_check set_repo_commit
    set_repo_commit /var/www/$MAILPILE_DOMAIN_NAME/mail "mailpile commit" "$MAILPILE_COMMIT" $MAILPILE_REPO
    cd "/var/www/$MAILPILE_DOMAIN_NAME/mail" || exit 2346836535
    pip install -r requirements.txt
    chown -R mailpile:mailpile "/var/www/$MAILPILE_DOMAIN_NAME/mail"
}
function backup_local_mailpile {
    echo -n ''
}
function restore_local_mailpile {
    echo -n ''
}
function backup_remote_mailpile {
    echo -n ''
}
function restore_remote_mailpile {
    echo -n ''
}
function remove_mailpile {
    if [ ${#MAILPILE_DOMAIN_NAME} -eq 0 ]; then
        return
    fi
    systemctl stop mailpile
    systemctl disable mailpile
    rm /etc/systemd/system/mailpile.service
    systemctl daemon-reload
    read_config_param "MAILPILE_DOMAIN_NAME"
    nginx_dissite "$MAILPILE_DOMAIN_NAME"
    remove_certs "${MAILPILE_DOMAIN_NAME}"
    if [ -f "/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME" ]; then
        rm -f "/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME"
    fi
    if [ -d "/var/www/$MAILPILE_DOMAIN_NAME" ]; then
        rm -rf "/var/www/$MAILPILE_DOMAIN_NAME"
    fi
    function_check remove_ddns_domain
    remove_ddns_domain "$MAILPILE_DOMAIN_NAME"
    groupdel -f mailpile
    userdel -r mailpile
    remove_config_param MAILPILE_DOMAIN_NAME
    remove_config_param MAILPILE_CODE
    function_check remove_onion_service
    remove_onion_service mailpile ${MAILPILE_ONION_PORT}
    remove_completion_param "install_mailpile"
    enable_email_encryption_at_rest
    sed -i '/Mailpile/d' "$COMPLETION_FILE"
    sed -i '/mailpile/d' "$COMPLETION_FILE"
}
function install_mailpile {
    if [ ! $ONION_ONLY ]; then
        ONION_ONLY='no'
    fi
    if [ ! $MAILPILE_DOMAIN_NAME ]; then
        echo $'The mailpile domain name was not specified'
        exit 63824
    fi
    apt-get -yq install python-pip python-lxml python-dev libjpeg-dev
    if [ ! -d /var/www/$MAILPILE_DOMAIN_NAME ]; then
        mkdir /var/www/$MAILPILE_DOMAIN_NAME
    fi
    cd "/var/www/$MAILPILE_DOMAIN_NAME" || exit 264826484
    if [ -d /var/www/$MAILPILE_DOMAIN_NAME/mail ]; then
        rm -rf /var/www/$MAILPILE_DOMAIN_NAME/mail
    fi
    if [ -d /repos/mailpile ]; then
        mkdir mail
        cp -r -p /repos/mailpile/. mail
        cd mail || exit 245728482
        git pull
    else
        git_clone $MAILPILE_REPO mail
    fi
    cd mail || exit 246872468
    git checkout $MAILPILE_COMMIT -b $MAILPILE_COMMIT
    set_completion_param "mailpile commit" "$MAILPILE_COMMIT"
    if [ ! -f requirements-dev.txt ]; then
        echo $'No python requirements file found'
        exit 62382
    fi
    pip install -r requirements.txt
    adduser --system --home=/var/www/$MAILPILE_DOMAIN_NAME/mail/ --group mailpile
    adduser mailpile debian-tor
    adduser mailpile www-data
    adduser mailpile mail
    adduser mailpile "$MY_USERNAME"
    if [[ "$ONION_ONLY" == 'no' ]]; then
        chgrp -R ssl-cert /etc/letsencrypt
        chmod -R g=rX /etc/letsencrypt
        usermod -a -G ssl-cert mailpile
    fi
    chown -R mailpile: /var/www/$MAILPILE_DOMAIN_NAME/mail/
    # create folders and tags
    su -c "cd /var/www/$MAILPILE_DOMAIN_NAME/mail && ./mp --setup" - mailpile
    MAILPILE_ONION_HOSTNAME=$(add_onion_service mailpile 80 ${MAILPILE_ONION_PORT})
    { echo '[Unit]';
      echo 'Description=Mailpile Email Client';
      echo 'After=syslog.target network.target nginx.target';
      echo '';
      echo '[Service]';
      echo 'User=mailpile';
      echo 'Group=mailpile';
      echo "WorkingDirectory=/var/www/$MAILPILE_DOMAIN_NAME/mail";
      echo "ExecStart=/var/www/$MAILPILE_DOMAIN_NAME/mail/mp --www=127.0.0.1:${MAILPILE_PORT} --wait";
      echo 'Restart=always';
      echo 'RestartSec=10';
      echo '';
      echo '[Install]';
      echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/mailpile.service
    chmod +x /etc/systemd/system/mailpile.service
    mailpile_nginx_site=/etc/nginx/sites-available/$MAILPILE_DOMAIN_NAME
    if [[ $ONION_ONLY == "no" ]]; then
        function_check nginx_http_redirect
        nginx_http_redirect $MAILPILE_DOMAIN_NAME
        { echo 'server {';
          echo '  listen 443 ssl;';
          echo '  #listen [::]:443 ssl;';
          echo "  server_name $MAILPILE_DOMAIN_NAME;";
          echo '';
          echo '  # Security'; } >> $mailpile_nginx_site
        function_check nginx_ssl
        nginx_ssl $MAILPILE_DOMAIN_NAME
        function_check nginx_security_options
        nginx_security_options $MAILPILE_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/$MAILPILE_DOMAIN_NAME/mail;";
          echo '';
          echo '  location / {'; } >> $mailpile_nginx_site
        function_check nginx_limits
        nginx_limits $MAILPILE_DOMAIN_NAME '15m'
        { echo "    rewrite /(.*) /\$1 break;";
          echo "    proxy_set_header X-Real-IP \$remote_addr;";
          echo "    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
          echo "    proxy_set_header Host \$http_host;";
          echo '    proxy_set_header X-NginX-Proxy true;';
          echo "    proxy_pass http://localhost:${MAILPILE_PORT};";
          echo '    proxy_redirect off;';
          echo '  }';
          echo '}';
          echo ''; } >> $mailpile_nginx_site
    else
        echo -n '' > $mailpile_nginx_site
    fi
    { echo 'server {';
      echo "    listen 127.0.0.1:$MAILPILE_ONION_PORT default_server;";
      echo "    server_name $MAILPILE_ONION_HOSTNAME;";
      echo ''; } >> $mailpile_nginx_site
    function_check nginx_security_options
    nginx_security_options $MAILPILE_DOMAIN_NAME
    { echo '';
      echo '  # Logs';
      echo '  access_log /dev/null;';
      echo '  error_log /dev/null;';
      echo '';
      echo '  # Root';
      echo "  root /var/www/$MAILPILE_DOMAIN_NAME/mail;";
      echo '';
      echo '  location / {'; } >> $mailpile_nginx_site
    function_check nginx_limits
    nginx_limits $MAILPILE_DOMAIN_NAME '15m'
    { echo "    rewrite /(.*) /\$1 break;";
      echo "    proxy_set_header X-Real-IP \$remote_addr;";
      echo "    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
      echo "    proxy_set_header Host \$http_host;";
      echo '    proxy_set_header X-NginX-Proxy true;';
      echo "    proxy_pass http://localhost:${MAILPILE_PORT};";
      echo '    proxy_redirect off;';
      echo '  }';
      echo '}'; } >> $mailpile_nginx_site
    function_check create_site_certificate
    if [ ! -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem ]; then
        create_site_certificate $MAILPILE_DOMAIN_NAME 'yes'
    fi
    if [ -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.crt ]; then
        mv /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.crt /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
    fi
    if [ -f /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem ]; then
        chown root:root /etc/ssl/certs/${MAILPILE_DOMAIN_NAME}.pem
        sed -i "s|.crt|.pem|g" /etc/nginx/sites-available/${MAILPILE_DOMAIN_NAME}
    fi
    if [ -f /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key ]; then
        chown root:root /etc/ssl/private/${MAILPILE_DOMAIN_NAME}.key
    fi
    function_check nginx_ensite
    nginx_ensite $MAILPILE_DOMAIN_NAME
    "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a mailpile -p "*"
    function_check add_ddns_domain
    add_ddns_domain $MAILPILE_DOMAIN_NAME
    disable_email_encryption_at_rest
    # if strict https is enforced then buttons don't work. This is probably a security bug
    sed -i "/Content-Security-Policy/d" $mailpile_nginx_site
    # link to the administrator's keyring
    if [ -d /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg ]; then
        mv /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg_orig
    fi
    cp -r "/home/$MY_USERNAME/.gnupg" "/var/www/$MAILPILE_DOMAIN_NAME/mail/"
    chown -R mailpile:mailpile /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg
    chmod +x /var/www/$MAILPILE_DOMAIN_NAME/mail/.gnupg
    pip install jinja2==2.9.6
    pip install pgpdump==1.5
    # turn off ssl in dovecot
    sed -i 's|#ssl =.*|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
    sed -i 's|ssl =.*|ssl = no|g' /etc/dovecot/conf.d/10-ssl.conf
    # set ssl certs, just in case we want to use them later
    if [[ "$ONION_ONLY" == "no" ]]; then
        if [ -f "/etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem" ]; then
            sed -i "s|#ssl_cert =.*|ssl_cert = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem|g" /etc/dovecot/conf.d/10-ssl.conf
            sed -i "s|ssl_cert =.*|ssl_cert = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/fullchain.pem|g" /etc/dovecot/conf.d/10-ssl.conf
            sed -i "s|#ssl_key =.*|ssl_key = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/privkey.pem|g" /etc/dovecot/conf.d/10-ssl.conf
            sed -i "s|ssl_key =.*|ssl_key = </etc/letsencrypt/live/${DEFAULT_DOMAIN_NAME}/privkey.pem|g" /etc/dovecot/conf.d/10-ssl.conf
        fi
        groupadd ssl-cert
        usermod -a -G ssl-cert dovecot
    fi
    systemctl restart dovecot
    systemctl enable mailpile
    systemctl daemon-reload
    systemctl start mailpile
    systemctl restart nginx
    set_completion_param "mailpile domain" "$MAILPILE_DOMAIN_NAME"
    APP_INSTALLED=1
}
# NOTE: deliberately no exit 0
 |