From 24b1e25f80fbc688657b3efe97d3ba65ef2040b3 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Sun, 5 May 2019 19:29:59 +0200 Subject: [PATCH] Handle well-known conflict --- scripts/_common.sh | 35 +++++++++++++++++++++++++++++++++++ scripts/install | 8 +++++++- scripts/restore | 8 +++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 scripts/_common.sh diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..0a018dc --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +# Check if an URL is already handled +# usage: is_url_handled URL +is_url_handled() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [u]=url= ) + local url + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Try to get the url with curl, and keep the http code and an eventual redirection url. + local curl_output="$(curl --insecure --silent --output /dev/null \ + --write-out '%{http_code};%{redirect_url}' "$url")" + + # Cut the output and keep only the first part to keep the http code + local http_code="${curl_output%%;*}" + # Do the same thing but keep the second part, the redirection url + local redirection="${curl_output#*;}" + + # Return 1 if the url isn't handled. + # Which means either curl got a 404 (or the admin) or the sso. + # A handled url should redirect to a publicly accessible url. + # Return 1 if the url has returned 404 + if [ "$http_code" = "404" ] || [[ $redirection =~ "/yunohost/admin" ]]; then + return 1 + # Return 1 if the url is redirected to the SSO + elif [[ $redirection =~ "/yunohost/sso" ]]; then + return 1 + fi +} diff --git a/scripts/install b/scripts/install index 075beb9..f027e99 100644 --- a/scripts/install +++ b/scripts/install @@ -6,7 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -#source ./_common.sh +source ./_common.sh source /usr/share/yunohost/helpers #================================================= @@ -40,6 +40,12 @@ path_url=$(ynh_normalize_url_path $path_url) # Register (book) web path ynh_webpath_register $app $domain $path_url +# Check if .well-known is available for this domain. +if is_url_handled "https://$domain/.well-known/caldav" || is_url_handled "https://$domain/.well-known/carddav" +then + ynh_die --message="Another app already uses the domain $domain to serve a caldav/carddav feature. Please use another domain." +fi + #================================================= # STORE SETTINGS FROM MANIFEST #================================================= diff --git a/scripts/restore b/scripts/restore index 02cc555..2f4592a 100644 --- a/scripts/restore +++ b/scripts/restore @@ -6,7 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -# source ../settings/scripts/_common.sh +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers #================================================= @@ -38,6 +38,12 @@ ynh_webpath_available $domain $path_url \ test ! -d $final_path \ || ynh_die "There is already a directory: $final_path " +# Check if .well-known is available for this domain. +if is_url_handled "https://$domain/.well-known/caldav" || is_url_handled "https://$domain/.well-known/carddav" +then + ynh_die --message="Another app already uses the domain $domain to serve a caldav/carddav feature. Please use another domain." +fi + #================================================= # STANDARD RESTORATION STEPS #=================================================