summaryrefslogtreecommitdiffstats
path: root/net/ddns-scripts/files/usr/lib/ddns/update_dnsimple_v2.sh
blob: 29e5b10d229de6f20b8317b33a5c905778520de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh
#
# Distributed under the terms of the GNU General Public License (GPL) version 2.0
# 2026 Jorge Gallegos <kad@blegh.net>
#
# Script for DDNS support via DNSimple's v2 API for the OpenWRT ddns-scripts package.
#
# Will attempt to create a new or edit an existing A or AAAA record for the
# given lookup host and domain. "password" configuration should be set to
# DNSimple API token. "username" should be set to the DNSimple account ID.
# "domain" should be set to the DNSimple zone. Optionally, "param_opt" can
# be set to an existing DNSimple record
#
# DNSimple API documentation:
# https://developer.dnsimple.com/v2/zones/records/

# Source JSON parser
. /usr/share/libubox/jshn.sh

# Set API base URL
__API="https://api.dnsimple.com/v2"
__CONNECT_TIMEOUT=5  # cURL connect timeout in seconds

# Check availability of cURL with SSL
[ -z "$CURL" ] && [ -z "$CURL_SSL" ] && write_log 14 "cURL with SSL support required! Please install"

# Validate configuration
[ -z "$lookup_host" ] && write_log 14 "Service section not configured correctly! Missing 'lookup_host' (Hostname)"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username' (Account ID)"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password' (API token)"
[ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain' (DNS Zone)"

__ACCOUNT_ID="$username"
__RECORD_ID="$param_opt"  # if provided, saves 1 API call to retrieve the record ID

# Get record name by substracting zone from lookup host
# if they are equal (i.e. root record) blank it
if [ "$lookup_host" = "$domain" ]; then
	__SUBDOMAIN=""
else
	__SUBDOMAIN="$(echo "$lookup_host" | sed -e "s/\.$domain\$//")"
fi
__DOMAIN="$domain"

# Determine IPv4 or IPv6 address and record type
if [ "$use_ipv6" -eq 1 ]; then
	expand_ipv6 "$__IP" __ADDR
	__RECORD_TYPE="AAAA"
else
	__ADDR="$__IP"
	__RECORD_TYPE="A"
fi


# Make DNSimple API call
# $1 - HTTP method (GET, POST, PATCH, DELETE)
# $2 - DNSimple API endpoint path
# $3 - request JSON payload (optional)
api_call() {
	local response url method payload
	method="$1"
	url="$__API/$2"
	payload="$3"

	write_log 7 "API endpoint URL: $url"
	write_log 7 "API request method: $method"
	[ -n "$payload" ] && write_log 7 "API request JSON payload: $payload"

	if [ -n "$payload" ]; then
		"$CURL" -s -X "$method" "$url" \
			-H "Authorization: Bearer $password" \
			-H "Accept: application/json" \
			-H "Content-Type: application/json" \
			--fail-with-body \
			--connect-timeout "$__CONNECT_TIMEOUT" \
			--data "$payload" \
			-o "$DATFILE" 2>"$ERRFILE"
	else
		"$CURL" -s -X "$method" "$url" \
			-H "Authorization: Bearer $password" \
			-H "Accept: application/json" \
			--fail-with-body \
			--connect-timeout "$__CONNECT_TIMEOUT" \
			-o "$DATFILE" 2>"$ERRFILE"
	fi

	if [ -s "$ERRFILE" ]; then
		write_log 3 "API response error: $(cat "$ERRFILE")"
	fi

	write_log 7 "API response JSON payload: $(cat "$DATFILE")"
	cat "$DATFILE"
}

# Check DNSimple API response for errors
# DNSimple returns HTTP error codes, but we also check for error messages in response
json_check_response() {
	local message
	json_get_var message "message" 2>/dev/null
	[ -n "$message" ] && write_log 3 "API request failed: $message"
}

# Review DNS record and, if it is the record we're looking for, get its id
callback_review_record() {
	local id name type
	json_select "$1"
	json_get_var id "id"
	json_get_var name "name"
	json_get_var type "type"

	[ "$name" = "$__SUBDOMAIN" ] && [ "$type" = "$__RECORD_TYPE" ] && echo "$id"
	json_select ..
}

# Retrieve all DNS records, find the first appropriate A/AAAA record, and get its id
find_existing_record_id() {
	local response

	# Call API with filters
	response=$(api_call "GET" "$__ACCOUNT_ID/zones/$__DOMAIN/records?name=$__SUBDOMAIN&type=$__RECORD_TYPE")
	json_load "$response"
	json_check_response

	# Check if data array exists and iterate through it
	json_select "data" 2>/dev/null || return
	json_get_keys keys
	for key in $keys; do
		local found_id
		found_id=$(callback_review_record "$key")
		if [ -n "$found_id" ]; then
			echo "$found_id"
			return
		fi
	done
}

# Create a new record
create_record() {
	local request response
	json_init
	json_add_string "name" "$__SUBDOMAIN"
	json_add_string "type" "$__RECORD_TYPE"
	json_add_string "content" "$__ADDR"
	request=$(json_dump)
	response=$(api_call "POST" "$__ACCOUNT_ID/zones/$__DOMAIN/records" "$request")
	json_load "$response"
	json_check_response
}

# Retrieve an existing record and get its content
# $1 - record id to retrieve
retrieve_record_content() {
	local content response
	response=$(api_call "GET" "$__ACCOUNT_ID/zones/$__DOMAIN/records/$1")
	json_load "$response"
	json_check_response
	json_select "data"
	json_get_var content "content"
	echo "$content"
}

# Edit an existing A/AAAA record
# $1 - record id to edit
edit_record() {
	local request response
	json_init
	json_add_string "content" "$__ADDR"
	request=$(json_dump)
	response=$(api_call "PATCH" "$__ACCOUNT_ID/zones/$__DOMAIN/records/$1" "$request")
	json_load "$response"
	json_check_response
}


# Try to identify an appropriate existing DNS record to update
if [ -z "$__RECORD_ID" ]; then
	write_log 7 "Retrieving DNS $__RECORD_TYPE record"
	__ID=$(find_existing_record_id)
else
	write_log 7 "Using user-supplied DNS record id: $__RECORD_ID"
	__ID="$__RECORD_ID"
fi

# Create or update DNS record with current IP address
if [ -z "$__ID" ]; then
	write_log 7 "Creating new DNS $__RECORD_TYPE record"
	create_record
else
	write_log 7 "Updating existing DNS $__RECORD_TYPE record"
	if [ "$use_ipv6" -eq 1 ]; then
		__IPV6=$(retrieve_record_content "$__ID")
		expand_ipv6 "$__IPV6" __CURRENT_ADDR
		write_log 7 "Expanded ipv6 from $__IPV6 to $__CURRENT_ADDR"
	else
		__CURRENT_ADDR=$(retrieve_record_content "$__ID")
	fi
	write_log 7 "$__CURRENT_ADDR == $__ADDR ?"
	if [ "$__CURRENT_ADDR" = "$__ADDR" ]; then
		write_log 7 "DNS record already has the correct IP address, skipping update"
	else
		edit_record "$__ID"
	fi
fi