diff --git a/lib/functions.sh b/lib/functions.sh index 47bcb3b..a39df39 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -6,11 +6,16 @@ YELLOW='\033[0;33m' PURPLE='\033[0;35m' DARK='\e[100m' NC='\033[0m' # No Color -TO_BE_DEFINED="TO BE DEFINED" - # BOLD='\033[1m' # DIM='\e[2m\e[0;90m' +TO_BE_DEFINED="TO BE DEFINED" + +FDN_DOMAINNAME=fdn.fr +FDN_NAMESERVER="ns0.$FDN_DOMAINNAME" +FDN_RESOLVER=80.67.169.12 +: $FDN_DOMAINNAME $FDN_NAMESERVER $FDN_RESOLVER + function echo() { [[ -n ${PREFIX:-} ]] && printf "${DARK}%25.25s${NC} " "${PREFIX}" builtin echo "$@" diff --git a/lib/install.sh b/lib/install.sh index f353bb6..6bac93c 100755 --- a/lib/install.sh +++ b/lib/install.sh @@ -1,14 +1,10 @@ #!/bin/bash MIAOU_BASEDIR=$(readlink -f "$(dirname "$0")/..") +readonly MIAOU_BASEDIR + # shellcheck source=/dev/null . "$MIAOU_BASEDIR/lib/functions.sh" - -FDN_DOMAINNAME=fdn.fr -FDN_RESOLVER=80.67.169.12 - -readonly MIAOU_BASEDIR FDN_DOMAINNAME FDN_RESOLVER - miaou_init EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml" diff --git a/lib/registrar/ovh-domain.sh b/lib/registrar/ovh-domain.sh index a8feee8..10c4a29 100755 --- a/lib/registrar/ovh-domain.sh +++ b/lib/registrar/ovh-domain.sh @@ -4,10 +4,10 @@ # https://eu.api.ovh.com/createToken/?GET=/domain*&POST=/domain*&PUT=/domain*&DELETE=/domain* function usage { - echo 'usage: [CNAME] [destination]' + echo 'usage: [CNAME] [TARGET]' echo ' - list ' echo ' - get ' - echo ' - set ' + echo ' - set ' exit 1 } function set_base { @@ -33,6 +33,7 @@ function build_url_list_records { } function list_records { + zone="$1" fieldType="${2:-}" subDomain="${3:-}" @@ -56,7 +57,7 @@ function list_records { echo "" echo "output=$output" - echo "" + echo "ERROR" exit 1 # echo "$output" | yq .[] # values of array @@ -82,6 +83,47 @@ function get_record { "$query" | yq -o=props } +function set_record { + zone="$1" + method="POST" + query="${BASE_URL}/domain/zone/$zone/record" + body="{\"fieldType\":\"CNAME\",\"subDomain\":\"$CNAME\",\"target\":\"$TARGET\",\"ttl\":0}" + tstamp=$(date +%s) + sha=$(echo -n "$AS+$CK+$method+$query+$body+$tstamp" | shasum | cut -d ' ' -f 1) + signature="\$1\$$sha" + + curl -s \ + -X $method \ + -H "Content-type: application/json" \ + -H 'Accept: application/json' \ + -H "X-Ovh-Application: $AK" \ + -H "X-Ovh-Consumer: $CK" \ + -H "X-Ovh-Signature: $signature" \ + -H "X-Ovh-Timestamp: $tstamp" \ + --data-raw "$body" \ + "$query" | yq -o=props +} + +function refresh { + zone="$1" + method="POST" + query="${BASE_URL}/domain/zone/$zone/refresh" + body="" + tstamp=$(date +%s) + sha=$(echo -n "$AS+$CK+$method+$query+$body+$tstamp" | shasum | cut -d ' ' -f 1) + signature="\$1\$$sha" + + curl -s \ + -X $method \ + -H "Content-type: application/json" \ + -H 'Accept: application/json' \ + -H "X-Ovh-Application: $AK" \ + -H "X-Ovh-Consumer: $CK" \ + -H "X-Ovh-Signature: $signature" \ + -H "X-Ovh-Timestamp: $tstamp" \ + "$query" | yq -o=props +} + function action_list { for record_id in $(list_records "$DOMAIN" CNAME "$CNAME"); do get_record "$DOMAIN" "$record_id" @@ -90,12 +132,28 @@ function action_list { function action_get { local record_id + + #TODO: to delete after testing... + list_records "$DOMAIN" CNAME "$CNAME" + record_id=$(list_records "$DOMAIN" CNAME "$CNAME") get_record "$DOMAIN" "$record_id" } +function append_dot_target { + local old_target=$TARGET + [[ $TARGET == *\. ]] || TARGET="$TARGET." + [[ $old_target != "$TARGET" ]] && echo "TARGET has been suffixed with '.' (the dot symbol)!" +} + function action_set { - echo set_record "$DOMAIN" "$CNAME" "$DESTINATION" + append_dot_target + + echo set_record "$DOMAIN" "$CNAME" "$TARGET" + set_record "$DOMAIN" "$CNAME" "$TARGET" + + echo refresh "$DOMAIN" + refresh "$DOMAIN" } ##----------- @@ -109,7 +167,7 @@ function action_set { ACTION="$1" DOMAIN="$2" CNAME="${3:-}" -DESTINATION="${4:-}" +TARGET="${4:-}" set_base diff --git a/scripts/miaou b/scripts/miaou index 65d418e..6ae0d2a 100755 --- a/scripts/miaou +++ b/scripts/miaou @@ -186,8 +186,13 @@ function build_dmz_certbot { #TODO: check public ip available my_ip=$(dig +short myip.opendns.com @resolver1.opendns.com) - if hostname -I | grep -q ${my_ip}; then + public_hostname=$(hostname -f) + public_ip=$(dig +short A "${public_hostname}" "@${FDN_NAMESERVER}") + + if hostname -I | grep -q "$my_ip"; then echo "My PUBLIC IP address is: <$my_ip>" + echo "My PUBLIC hostname is: <$public_hostname>" + [[ $my_ip != "$public_ip" ]] && echoerr "This machine provides wrong public IP: <$public_ip>" && exit 101 else echoerr "This machine can not respond to its PUBLIC IP address: <$my_ip>" #FIXME: && exit 100 fi @@ -201,11 +206,12 @@ function build_dmz_certbot { domain=${item[0]} fqdn=${item[1]} - subDomain=${fqdn%.$domain} + subDomain=${fqdn%".${domain}"} + + echo "TODO: fqdn=${fqdn}, domain=${domain}" + local server_ip public_hostname + server_ip=$(dig +short A "$fqdn") - echo "TODO: fqdn=$fqdn, domain=$domain" - local server_ip - server_ip=$(dig +short A $fqdn) if [[ $server_ip == "$my_ip" ]]; then echo "CNAME <$fqdn> approved successfuly!" else @@ -214,11 +220,13 @@ function build_dmz_certbot { server_name=$(dig +short CNAME "$fqdn") echowarn "CNAME <$fqdn> points to another server: <$server_name>" else - echo TODO:REGISTRAR needed - "$MIAOU_BASEDIR"/lib/registrar/ovh-domain.sh "$domain" "$subDomain" + echo registering "$domain" "$subDomain" to ... "$public_hostname" + "$MIAOU_BASEDIR"/lib/registrar/ovh-domain.sh set "$domain" "$subDomain" "$public_hostname" + + echo "TODO: TEST with dig, wait for reply in 4s then certbot!!!" + exit 5 fi fi - done else echo "bypass certbot certificate generation due to target=<$target>"