homeserverdns/homeserverdns-update

105 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# homeserverdns - https://ulrich.earth/code/homeserverdns
#
# Copyright (C) 2018 Christian Ulrich
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script is called by homeserverdns-daemon for updating A/AAAA records.
# These environment variables will be set by homeserverdns-daemon:
#
# PROTOCOL protocol name (gandi|dyndns|httpnet)
# API_ADDRESS address for API requests, required for dnydns protocol
# USER user name for authentication at the API
# AUTH_KEY authentication token for authentication at the API
# DOMAIN fqdn for which A/AAAA records will be set
# L2_DOMAIN second-level domain for which A/AAAA records will be set
# RECORD_NAME name of the record to be set
# IP4 new value of the A record (IP4 or IP6 will be set exclusively)
# IP6 new value of the AAAA record (IP4 or IP6 will be set exclusively)
# TTL new TTL value for the A/AAAA records, required for gandi protocol
### gandi.net LiveDNS API
### () -> status_code
function update_gandi {
local url="https://dns.api.gandi.net/api/v5/domains/${L2_DOMAIN}/records/${RECORD_NAME}"
local ip=""
if [ -n "${IP4}" ]; then
url="${url}/A"
ip=$IP4
elif [ -n "${IP6}" ]; then
url="${url}/AAAA"
ip=$IP6
fi
status=$(curl -o /dev/null -s -w "%{http_code}\n" \
-X PUT \
-H "Content-Type:application/json" \
-H "X-Api-Key:${AUTH_KEY}" \
-d "{\"rrset_ttl\":${TTL},\"rrset_values\":[\"${ip}\"]}" \
$url)
echo "${status}"
}
# FIXME: untested
### http.net DNS API v1
### () -> status_code
function update_httpnet {
local current_ip4=$(dig A +short $DOMAIN)
local current_ip6=$(dig AAAA +short $DOMAIN)
local url="https://partner.http.net/api/dns/v1/json/zoneUpdate"
local items_add=""
local items_delete=""
if [ -n "${IP4}" ]; then
items_add="{\"name\":\"${DOMAIN}\",\"type\":\"A\",\"content\":\"${IP4}\",\"ttl\":\"${TTL}\"}"
elif [ -n "${IP6}" ]; then
items_add="{\"name\":\"${DOMAIN}\",\"type\":\"AAAA\",\"content\":\"${IP6}\",\"ttl\":\"${TTL}\"}"
fi
if [ -n "${current_ip4}" ]; then
items_delete="{\"name\":\"${DOMAIN}\",\"type\":\"A\",\"content\":\"${current_ip4}\"}"
fi
if [ -n "${current_ip6}" ]; then
items_delete="{\"name\":\"${DOMAIN}\",\"type\":\"AAAA\",\"content\":\"${current_ip6}\"}"
fi
status=$(curl -o /dev/null -s -w "%{http_code}\n" \
-X PUT \
-d "{\"authToken\":\"${AUTH_KEY}\"," \
"\"zoneConfig\":{\"name\":\"${L2_DOMAIN}\"}," \
"\"recordsToAdd\":[${items_add}]," \
"\"recordsToDelete\":[${items_delete}]}" \
$url)
echo "${status}"
}
# FIXME: untested
### dyndns API v2/v3 API
### () -> status_code
function update_dyndns {
local hostname=${L2_DOMAIN}
if [ "${RECORD_NAME}" != "@" ]; then
local hostname=$DOMAIN
fi
local address=${IP4:-$IP6}
local url="https://${USER}:${AUTH_KEY}@${API_ADDRESS}?hostname=${hostname}&myip=${address}"
status=$(curl -o /dev/null -s -w "%{http_code}\n" $url)
echo "${url} ${status}"
}
eval "update_${PROTOCOL}"