mirror of
https://github.com/mail-in-a-box/mailinabox.git
synced 2026-03-12 17:07:23 +01:00
Fix url redirection when a remote nextcloud is used so that .well-known/caldav and carddav work properly, as well as the redirecting /cloud to the remote Nextcloud. Since the nginx config is replaced by the management daemon whenever a new domain is added, this change adds a hooking mechanism for setup mods.Fix url redirection when a remote nextcloud is in use. This corrects redirection for /.well-known/caldav, /.well-known/carddav and /cloud to send the client to the remote nextcloud. This requires an nginx configuration change, and since the nginx config is replaced by the management daemon whenever a new domain is added, this change adds a hooking mechanism for setup mods allowing them to intercept and modify the resultant nginx config.
This commit is contained in:
@@ -316,3 +316,14 @@ say_verbose() {
|
||||
say() {
|
||||
echo "$@"
|
||||
}
|
||||
|
||||
install_hook_handler() {
|
||||
# this is used by local setup mods to install a hook handler for
|
||||
# the management daemon
|
||||
local handler_file="$1"
|
||||
local dst="${LOCAL_MODS_DIR:-local}/management_hooks_d"
|
||||
mkdir -p "$dst"
|
||||
cp "$handler_file" "$dst"
|
||||
# let the daemon know there's a new hook handler
|
||||
tools/hooks_update >/dev/null
|
||||
}
|
||||
|
||||
@@ -113,8 +113,8 @@ tr -cd '[:xdigit:]' < /dev/urandom | head -c 32 > /var/lib/mailinabox/api.key
|
||||
chmod 640 /var/lib/mailinabox/api.key
|
||||
|
||||
source $venv/bin/activate
|
||||
export PYTHONPATH=$(pwd)/management
|
||||
exec gunicorn -b localhost:10222 -w 1 wsgi:app
|
||||
export PYTHONPATH=$(pwd)/management:${LOCAL_MODS_DIR:-$(pwd)/local}
|
||||
exec gunicorn --log-level ${MGMT_LOG_LEVEL:-info} -b localhost:10222 -w 1 wsgi:app
|
||||
EOF
|
||||
chmod +x $inst_dir/start
|
||||
cp --remove-destination conf/mailinabox.service /lib/systemd/system/mailinabox.service # target was previously a symlink so remove it first
|
||||
|
||||
63
setup/mods.available/hooks/remote-nextcloud-mgmt-hooks.py
Normal file
63
setup/mods.available/hooks/remote-nextcloud-mgmt-hooks.py
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
#
|
||||
# This is a web_update management hook for the remote-nextcloud setup
|
||||
# mod.
|
||||
#
|
||||
# When management/web_update.py creates a new nginx configuration file
|
||||
# "local.conf", this mod will ensure that .well-known/caldav and
|
||||
# .well-known/carddav urls are redirected to the remote nextcloud.
|
||||
#
|
||||
# The hook is enabled by placing the file in directory
|
||||
# LOCAL_MODS_DIR/managment_hooks_d.
|
||||
#
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def do_hook(hook_name, hook_data, mods_env):
|
||||
if hook_name != 'web_update':
|
||||
# we only care about hooking web_update
|
||||
log.debug('hook - ignoring %s' % hook_name)
|
||||
return False
|
||||
|
||||
if 'NC_HOST' not in mods_env or mods_env['NC_HOST'].strip() == '':
|
||||
# not configured for a remote nextcloud
|
||||
log.debug('hook - not configured for a remote nextcloud')
|
||||
return False
|
||||
|
||||
# get the remote nextcloud url and ensure no tailing /
|
||||
nc_url = "%s://%s:%s%s" % (
|
||||
mods_env['NC_PROTO'],
|
||||
mods_env['NC_HOST'],
|
||||
mods_env['NC_PORT'],
|
||||
mods_env['NC_PREFIX'][0:-1] if mods_env['NC_PREFIX'].endswith('/') else mods_env['NC_PREFIX']
|
||||
)
|
||||
|
||||
#
|
||||
# modify nginx_conf
|
||||
#
|
||||
def do_replace(find_str, replace_with):
|
||||
if hook_data['nginx_conf'].find(find_str) == -1:
|
||||
log.warning('remote-nextcloud hook: string "%s" not found in proposed nginx_conf' % (find_str))
|
||||
return False
|
||||
hook_data['nginx_conf'] = hook_data['nginx_conf'].replace(
|
||||
find_str,
|
||||
replace_with
|
||||
)
|
||||
return True
|
||||
|
||||
# 1. change the .well-known/(caldav|carddav) redirects
|
||||
do_replace(
|
||||
'/cloud/remote.php/dav/',
|
||||
'%s/remote.php/dav/' % nc_url
|
||||
)
|
||||
|
||||
# 2. redirect /cloud to the remote nextcloud
|
||||
do_replace(
|
||||
'rewrite ^/cloud/$ /cloud/index.php;',
|
||||
'rewrite ^/cloud/(.*)$ %s/$1 redirect;' % nc_url
|
||||
)
|
||||
|
||||
@@ -89,6 +89,12 @@ EOF
|
||||
}
|
||||
|
||||
|
||||
update_mobileconfig() {
|
||||
local url="$1"
|
||||
sed -i "s|<string>/cloud/remote.php|<string>${url%/}/remote.php|g" /var/lib/mailinabox/mobileconfig.xml
|
||||
}
|
||||
|
||||
|
||||
|
||||
remote_nextcloud_handler() {
|
||||
echo ""
|
||||
@@ -184,6 +190,10 @@ remote_nextcloud_handler() {
|
||||
|
||||
# configure zpush (which links to contacts & calendar)
|
||||
configure_zpush
|
||||
|
||||
# update ios mobileconfig.xml
|
||||
update_mobileconfig "$new_url"
|
||||
|
||||
|
||||
# prevent nginx from serving any miab-installed nextcloud
|
||||
# files and remove owncloud cron job
|
||||
@@ -225,6 +235,16 @@ remote_nextcloud_handler() {
|
||||
"NC_PORT=$NC_PORT" \
|
||||
"NC_PREFIX=$NC_PREFIX" \
|
||||
"NC_HOST_SRC_IP='${NC_HOST_SRC_IP:-}'"
|
||||
|
||||
# Hook the management daemon, even if no remote nextcloud
|
||||
# (NC_HOST==''). Must be done after writing mailinabox_mods.conf
|
||||
|
||||
# 1. install hooking code
|
||||
install_hook_handler "setup/mods.available/hooks/remote-nextcloud-mgmt-hooks.py"
|
||||
# 2. trigger hooking code for a web_update event, which updates
|
||||
# the systems nginx configuration
|
||||
tools/web_update
|
||||
}
|
||||
|
||||
remote_nextcloud_handler
|
||||
|
||||
|
||||
Reference in New Issue
Block a user