test/lib/roundcube.sh
2024-08-29 17:36:53 +00:00

235 lines
9.1 KiB
Bash

#!/bin/bash
#
# Roundcube Service
PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
get_appvars
config_roundcube() {
echo -ne "\n* Configuring roundcube container.."
if [ ! -d "/federated/apps/roundcube" ]; then
mkdir -p /federated/apps/roundcube/data/var/www/html
fi
cat > /federated/apps/roundcube/docker-compose.yml <<EOF
version: "3.7"
services:
roundcube:
image: roundcube/roundcubemail:\${IMAGE_VERSION}
container_name: roundcube
hostname: roundcube.$DOMAIN
domainname: $DOMAIN
restart: always
networks:
federated:
ipv4_address: 172.99.0.47
extra_hosts:
- "authelia.$DOMAIN:$EXTERNALIP"
env_file:
- ./.env
volumes:
- ./data/var/www/html:/var/www/html
labels:
- "traefik.enable=true"
- "traefik.http.routers.roundcube.rule=Host(\`roundcube.$DOMAIN\`,\`webmail.$DOMAIN\`)"
- "traefik.http.routers.roundcube.entrypoints=websecure"
- "traefik.http.routers.roundcube.tls.certresolver=letsencrypt"
networks:
federated:
external: true
EOF
ROUNDCUBE_SECRET=$(create_password);
cat > /federated/apps/roundcube/.env <<EOF
IMAGE_VERSION="1.6.8-apache"
ROUNDCUBEMAIL_DB_TYPE=mysql
ROUNDCUBEMAIL_DB_HOST=pdnsmysql.$DOMAIN
ROUNDCUBEMAIL_DB_USER=roundcube
ROUNDCUBEMAIL_DB_NAME=roundcube
ROUNDCUBEMAIL_DB_PASSWORD=$ROUNDCUBE_SECRET
ROUNDCUBEMAIL_SKIN=elastic
ROUNDCUBEMAIL_DEFAULT_HOST=tls://mail.$DOMAIN
ROUNDCUBEMAIL_SMTP_SERVER=tls://mail.$DOMAIN
ROUNDCUBEMAIL_USERNAME_DOMAIN=$DOMAIN
ROUNDCUBEMAIL_UPLOAD_MAX_FILESIZE=4G
EOF
chmod 600 /federated/apps/roundcube/.env
# Create database and user in mysql
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'create database roundcube;'" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e \"CREATE USER 'roundcube'@'%' IDENTIFIED BY '$ROUNDCUBE_SECRET';\"" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e \"grant all privileges on roundcube.* to 'roundcube'@'%';\"" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'flush privileges;'" &> /dev/null
echo -ne "done.\n"
}
start_roundcube() {
# Start service with command to make sure it's up before proceeding
start_service "roundcube" "nc -z 172.99.0.47 80 &> /dev/null" "7"
docker exec pdns pdnsutil add-record $DOMAIN roundcube A 86400 $EXTERNALIP &> /dev/null
[ $? -ne 0 ] && fail "Couldn't add dns record for roundcube"
docker exec pdns pdnsutil add-record $DOMAIN webmail A 86400 $EXTERNALIP &> /dev/null
[ $? -ne 0 ] && fail "Couldn't add dns record for roundcube"
kill -9 $SPINPID &> /dev/null
echo -ne "done.\n"
}
email_roundcube() {
echo -ne "* Sending email to customer.."
spin &
SPINPID=$!
cat > /federated/apps/mail/data/root/certs/mailfile <<EOF
<html>
<img src="https://www.federated.computer/wp-content/uploads/2023/11/logo.png" alt="" /><br>
<p>
<h4>Roundcube is now installed on $DOMAIN</h4>
<p>
Here is your applications chart on how to access this service:<br>
<p>
<h4>Applications</h4>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-cul6{border-color:inherit;color:#340096;text-align:left;text-decoration:underline;vertical-align:top}
.tg .tg-acii{background-color:#FFF;border-color:inherit;color:#333;text-align:left;vertical-align:top}
.tg .tg-0hty{background-color:#000000;border-color:inherit;color:#ffffff;font-weight:bold;text-align:left;vertical-align:top}
.tg .tg-kwiq{border-color:inherit;color:#000000;text-align:left;vertical-align:top}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 996px">
<colgroup>
<col style="width: 101.333333px">
<col style="width: 203.333333px">
<col style="width: 282.333333px">
<col style="width: 185.33333px">
<col style="width: 78.333333px">
<col style="width: 220.333333px">
</colgroup>
<thead>
<tr>
<th class="tg-0hty">Service</th>
<th class="tg-0hty">Link</th>
<th class="tg-0hty">User / Pass</th>
<th class="tg-0hty">Access</th>
<th class="tg-0hty">Docs</th>
<th class="tg-0hty">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0pky">Roundcube</td>
<td class="tg-0pky"><a href="https://webmail.$DOMAIN/login" target="_blank" rel="noopener noreferrer"><span style="color:#340096">webmail.$DOMAIN</span></a></td>
<td class="tg-0pky">admin@$DOMAIN<br>Panel password</td>
<td class="tg-0pky">User access using your panel user email and password</td>
<td class="tg-cul6"><a href="https://documentation.federated.computer/docs/getting_started/welcome/" target="_blank" rel="noopener noreferrer"><span style="color:#340096">Click here</span></a></td>
<td class="tg-0pky">Roundcube is a web-based IMAP email client</td>
</tr>
</tbody>
</table>
<h4>Thanks for your support!</h4>
<p>
Thank you for your support of Federated Computer. We really appreciate it and hope you have a very successful
time with Federated Core.
<p>
Again, if we can be of any assistance, please don't hesitate to get in touch.
<p>
Support: https://support.federated.computer<br>
Phone: (970) 722-8715<br>
Email: support@federated.computer<br>
<p>
It's <b>your</b> computer. Let's make it work for you!
</html>
EOF
# Send out e-mail from mail container with details
docker exec mail bash -c "mail -r admin@$DOMAIN -a \"Content-type: text/html\" -s \"Application installed on $DOMAIN\" $EMAIL < /root/certs/mailfile"
rm /federated/apps/mail/data/root/certs/mailfile
kill -9 $SPINPID &> /dev/null
echo -ne "done.\n"
}
uninstall_roundcube() {
echo -ne "* Uninstalling roundcube container.."
spin &
SPINPID=$!
# First stop the service
cd /federated/apps/roundcube && docker-compose -f docker-compose.yml -p roundcube down &> /dev/null
# Delete database and user
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'drop database roundcube;'" &> /dev/null
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'drop user roundcube;'" &> /dev/null
# Delete the app directory
rm -rf /federated/apps/roundcube
# Delete the image
docker image rm roundcube/roundcubemail:$IMAGE_VERSION &> /dev/null
# Delete the DNS record
docker exec pdns pdnsutil delete-rrset $DOMAIN roundcube A
# Uninstall the SSO configuration if it exists in authelia (authelia must exist too)
if [[ $(grep "### Roundcube" /federated/apps/authelia/data/config/idproviders.yml 2>/dev/null) ]]; then
sed -i '/### Roundcube/,/### /{/### PowerDNS/!{/### /!d}}' /federated/apps/authelia/data/config/idproviders.yml
sed -i '/### Roundcube/d' /federated/apps/authelia/data/config/idproviders.yml
/federated/bin/stop authelia
/federated/bin/start authelia
fi
kill -9 $SPINPID &> /dev/null
echo -ne "done.\n"
}
configsso_roundcube() {
[ ! -d "/federated/apps/authelia" ] && failcheck "Authelia is not installed. You need this first before continuing."
[ ! -f "/federated/apps/authelia/data/config/idproviders.yml" ] && failcheck "Authelia idproviders.yml is missing."
[[ $(grep "### Roundcube" /federated/apps/authelia/data/config/idproviders.yml 2>/dev/null) ]] && failcheck "Authelia already has a Roundcube configuration."
ROUNDCUBE_CLIENT_SECRET=$(create_password);
ROUNDCUBE_CLIENT_SECRET_HASH=$(docker run -it --rm authelia/authelia:latest authelia crypto hash generate pbkdf2 --password $ROUNDCUBE_CLIENT_SECRET | awk '{ print $2 }')
cat >> /federated/apps/authelia/data/config/idproviders.yml <<EOF
### Roundcube
- client_id: 'roundcube'
client_name: 'Roundcube'
client_secret: $ROUNDCUBE_CLIENT_SECRET_HASH
consent_mode: 'implicit'
public: false
authorization_policy: 'one_factor'
redirect_uris:
- https://webmail.$DOMAIN/index.php/login/oauth
scopes:
- 'openid'
- 'profile'
- 'email'
token_endpoint_auth_method: 'client_secret_post'
EOF
# Restart Authelia for changes to take the above configuration
/federated/bin/stop authelia
/federated/bin/start authelia
sed -i "/?php/a \ \$config['oauth_provider'] = 'generic'; \n\
\$config['oauth_provider_name'] = 'Authelia'; \n\
\$config['oauth_client_id'] = 'roundcube'; \n\
\$config['oauth_client_secret'] = '$ROUNDCUBE_CLIENT_SECRET'; \n\
\$config['oauth_auth_uri'] = 'https://authelia.$DOMAIN/api/oidc/authorization'; \n\
\$config['oauth_token_uri'] = 'https://authelia.$DOMAIN/api/oidc/token'; \n\
\$config['oauth_identity_uri'] = 'https://authelia.$DOMAIN/api/oidc/userinfo'; \n\
\$config['oauth_identity_fields'] = ['email']; \n\
\$config['use_https'] = true; \n\
\$config['oauth_scope'] = 'email openid profile'; \n\
\$config['oauth_login_redirect'] = false;" /federated/apps/roundcube/data/var/www/html/config/config.inc.php
/federated/bin/stop roundcube
/federated/bin/start roundcube
}