#!/usr/bin/env bash

set -e
set -u
set -o pipefail
#set -r 
#set -x

readonly API_KEY=""
readonly DOMAIN_FILE="$(mktemp)"
DOMAIN_NAME=""
REPORT_FILE=""

cleanup(){
    rm -f ${DOMAIN_FILE}
}

print_banner(){
printf "
 ____    ____     ____
/ ___|  |  _ \   / ___|
\___ \  | | | | | |
 ___) | | |_| | | |___
|____/  |____/   \____|

-----------------------
RWTH SubDomain Checker
                                                                                                                                                                                                                                           
"
}

authoritative_domain(){
    local answer="n"
    read -p $'\n'"Is RWTH Aachen authoritative for this domain (y/N): " -n 1 -r answer
    if [[ ! "${answer,,}"  =~ ^y ]]; then
        echo -e "\n\nSorry can't help you here.\n"
	exit 1
    fi
}

check_api_key(){
    if [ -z ${API_KEY} ]; then
        echo -e "\nWe need an API key for the DNS-Admin tool\n"
        exit
    fi
}

what_domain(){
    local domain
    read -p $'\n\n'"Please enter the domain: " domain

    if ! dig -t ns ${domain,,} +short | grep -i rwth-aachen.de &> /dev/null ; then 
	echo -e "\nSorry, you guessed wrong (does not belong to RWTH)";
        exit 1
    else
        DOMAIN_NAME="${domain,,}"
    fi

}

get_records_in(){
    local api_url="https://noc-portal.rz.rwth-aachen.de/dns-admin/api/v1"
    local zone_id=""

    # get zone id
    zone_id="$(curl --silent --request GET --header "Authorization: Basic ${API_KEY}" --data "search=${1}" "${api_url}/list_zones" | jq -r '.[].id')"

    for i in {a..z}; do
       curl --silent --request GET --header "Authorization: Basic ${API_KEY}" --data "zone_id=${zone_id}&search=${i}*" "${api_url}/list_records" | jq -r '.[].content' | grep -E 'IN A |IN AAAA |IN CNAME ' | awk '{ print $1 }' | sort | uniq >> ${DOMAIN_FILE} || true
    done
}

generate_report_for(){
    local report_file="check_entries_in_${1}_$(date +%Y%m%d).txt"
    local ipv4_addr=""
    local ipv6_addr=""
    local status=""
    local location=""
    
    #echo ${report_file}
    printf "%-40s\t%17s\t%10s\t%s\t%s\n" "FQDN" "internal/external" "IPv4/IPv6" "alive" "ports" > ${report_file}

    while read fqdn; do
        ipv4_addr=""
        ipv6_addr=""

        while read line; do
            if [[ "${line}" =~ \. ]]; then
                ipv4_addr="${line}"
		ping -W 1 -c 1 ${line} &> /dev/null && status="ja" || status="nein"
		if [[ ${ipv4_addr} =~ ^134\.61\. ]] || [[ ${ipv4_addr} =~ ^134\.130\. ]] || [[ ${ipv4_addr} =~ ^137\.226\. ]]; then location="internal"; else location="external";fi
                printf "%-40s\t%17s\t%10s\t%s\t%s\n" "${fqdn}" "${location}" "A" "${status}" "" >> ${report_file}
            elif [[ "${line}" =~ : ]]; then
                ipv6_addr="${line}"
		ping -W 1 -c 1 ${line} &> /dev/null && status="ja" || status="nein"
		if [[ ${ipv6_addr} =~ ^2[aA]00:8[aA]60: ]]; then location="internal"; else location="external";fi
                printf "%-40s\t%17s\t%10s\t%s\t%s\n" "${fqdn}" "${location}" "AAAA" "${status}" "" >> ${report_file}
            fi
        done < <(dig ${fqdn} a ${fqdn} aaaa +short)

    done < ${3}

    cat ${report_file}
}

###################
# main

trap cleanup SIGHUP SIGINT SIGILL SIGKILL SIGTERM EXIT

print_banner

check_api_key

authoritative_domain

what_domain

get_records_in ${DOMAIN_NAME}

generate_report_for ${DOMAIN_NAME} using ${DOMAIN_FILE}

exit

echo "${DOMAIN_FILE}"
cat ${DOMAIN_FILE}


##########
## EOF
