-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdns_pipe.sh
executable file
·280 lines (235 loc) · 6.75 KB
/
pdns_pipe.sh
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
#
# Originally written by Sam Stephenson for xip.io
set -e
shopt -s nocasematch
# Configuration
#
# Increment this timestamp when the contents of the file change.
XIP_TIMESTAMP="2018092000"
# The top-level domain for which the name server is authoritative.
# CHANGEME: change "sslip.io" to your domain
XIP_DOMAIN="sslip.io"
# How long responses should be cached, in seconds.
XIP_TTL=300
# SOA record
XIP_SOA="briancunnie.gmail.com ns-he.nono.io $XIP_TIMESTAMP $XIP_TTL $XIP_TTL $XIP_TTL $XIP_TTL"
# The public IP addresses (e.g. for the web site) of the top-level domain.
# `A` queries for the top-level domain will return this list of addresses.
# CHANGEME: change this to your domain's webserver's address
XIP_ROOT_ADDRESSES=( "78.46.204.247" )
XIP_ROOT_ADDRESSES_AAAA=( "2a01:4f8:c17:b8f::2" )
# The public IP addresses on which this xip-pdns server will run.
# `NS` queries for the top-level domain will return this list of servers.
# Note: [change from xip.io] The NS servers are in a different domain
# (i.e. nono.io) so the addresses don't need to be included.
XIP_NS=( "ns-aws.nono.io" "ns-azure.nono.io" "ns-gce.nono.io" "ns-vultr.nono.io" )
# These are the MX records for your domain. IF YOU'RE NOT SURE,
# don't set it at at all (comment it out)--it defaults to no
# MX records.
# XIP_MX_RECORDS=(
# "10" "mx.zoho.com"
# "20" "mx2.zoho.com"
# )
XIP_MX_RECORDS=(
"10" "mail.protonmail.ch"
)
# These are the TXT records for your domain. IF YOU'RE NOT SURE,
# don't set it at at all (comment it out)--it defaults to no
XIP_TXT_RECORDS=(
"protonmail-verification=ce0ca3f5010aa7a2cf8bcc693778338ffde73e26"
)
# These are the domains which we should not reply with any records
# at all. Normally this can be emtpy. The only purpose this variable
# serves is in the case of multiple PowerDNS backends, in this case
# the BIND backend. It shouldn't return any records for these domains,
# otherwise it can break CNAME/wildcard records for which the BIND
# backend is authoritative.
XIP_EXCLUDED_DOMAINS=(
"nono.io"
"nono.com"
)
if [ -a "$1" ]; then
source "$1"
fi
#
# Protocol helpers
#
read_cmd() {
local IFS=$'\t'
local i=0
local arg
read -ra CMD
for arg; do
eval "$arg=\"\${CMD[$i]}\""
let i=i+1
done
}
send_cmd() {
local IFS=$'\t'
printf "%s\n" "$*"
}
fail() {
send_cmd "FAIL"
log "Exiting"
exit 1
}
read_helo() {
read_cmd HELO VERSION
[ "$HELO" = "HELO" ] && [ "$VERSION" = "1" ]
}
read_query() {
read_cmd TYPE QNAME QCLASS QTYPE ID IP
}
send_answer() {
local type="$1"
shift
send_cmd "DATA" "$QNAME" "$QCLASS" "$type" "$XIP_TTL" "$ID" "$@"
}
log() {
printf "[xip-pdns:$$] %s\n" "$@" >&2
}
#
# xip.io domain helpers
#
IP_PATTERN="(^|\.)(x{0}(x{0}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))($|\.)"
DASHED_IP_PATTERN="(^|-|\.)(x{0}(x{0}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))($|-|\.)"
# https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
# We don't use "dotted" IPv6 because DNS doesn't allow two dots next to each other
# e.g. "::1" -> "1..sslip.io" isn't allowed (dig error: `is not a legal name (empty label)`)
DASHED_IPV6_PATTERN="(^|\.)(x{0}([0-9a-fA-F]{1,4}-){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}-){1,7}-|([0-9a-fA-F]{1,4}-){1,6}-[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}-){1,5}(-[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}-){1,4}(-[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}-){1,3}(-[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}-){1,2}(-[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}-(x{0}(-[0-9a-fA-F]{1,4}){1,6})|-(x{0}(-[0-9a-fA-F]{1,4}){1,7}|-))(\.|$)"
qtype_is() {
[ "$QTYPE" = "$1" ] || [ "$QTYPE" = "ANY" ]
}
qname_is_root_domain() {
[ "$QNAME" = "$XIP_DOMAIN" ]
}
subdomain_is_ip() {
[[ "$QNAME" =~ $IP_PATTERN ]]
}
subdomain_is_dashed_ip() {
[[ "$QNAME" =~ $DASHED_IP_PATTERN ]]
}
subdomain_is_dashed_ipv6() {
[[ "$QNAME" =~ $DASHED_IPV6_PATTERN ]]
}
resolve_ip_subdomain() {
[[ "$QNAME" =~ $IP_PATTERN ]] || true
echo "${BASH_REMATCH[2]}"
}
resolve_dashed_ip_subdomain() {
[[ "$QNAME" =~ $DASHED_IP_PATTERN ]] || true
echo "${BASH_REMATCH[2]//-/.}"
}
resolve_dashed_ipv6_subdomain() {
[[ "$QNAME" =~ $DASHED_IPV6_PATTERN ]] || true
echo "${BASH_REMATCH[2]//-/:}"
}
answer_soa_query() {
send_answer "SOA" "$XIP_SOA"
}
answer_ns_query() {
local i=1
local ns_address
for ns in "${XIP_NS[@]}"; do
send_answer "NS" "$ns"
done
}
answer_root_a_query() {
local address
for address in "${XIP_ROOT_ADDRESSES[@]}"; do
send_answer "A" "$address"
done
}
answer_root_aaaa_query() {
local address
for address in "${XIP_ROOT_ADDRESSES_AAAA[@]}"; do
send_answer "AAAA" "$address"
done
}
answer_localhost_a_query() {
send_answer "A" "127.0.0.1"
}
answer_localhost_aaaa_query() {
send_answer "AAAA" "::1"
}
answer_mx_query() {
set -- "${XIP_MX_RECORDS[@]}"
while [ $# -gt 1 ]; do
send_answer "MX" "$1 $2"
shift 2
done
}
answer_txt_query() {
local address
for text in "${XIP_TXT_RECORDS[@]}"; do
send_answer "TXT" "$text"
done
}
answer_subdomain_a_query_for() {
local type="$1"
local address="$(resolve_${type}_subdomain)"
if [ -n "$address" ]; then
send_answer "A" "$address"
fi
}
answer_subdomain_aaaa_query_for() {
local type="$1"
local address="$(resolve_${type}_subdomain)"
if [ -n "$address" ]; then
send_answer "AAAA" "$address"
fi
}
#
# PowerDNS pipe backend implementation
#
trap fail err
read_helo
send_cmd "OK" "xip.io PowerDNS pipe backend (protocol version 1)"
while read_query; do
log "Query: type=$TYPE qname=$QNAME qclass=$QCLASS qtype=$QTYPE id=$ID ip=$IP"
for excluded_domain in "${XIP_EXCLUDED_DOMAINS[@]}"; do
if [[ $QNAME =~ $excluded_domain$ ]]; then
log "'$QNAME' matched '$excluded_domain'"
send_cmd "END"
continue 2
fi
done
if qtype_is "SOA"; then
answer_soa_query
fi
if qtype_is "NS"; then
answer_ns_query
fi
if qtype_is "TXT"; then
answer_txt_query
fi
if qtype_is "MX"; then
answer_mx_query
fi
if qtype_is "A"; then
LC_QNAME=$(echo $QNAME | tr 'A-Z' 'a-z')
if [ $LC_QNAME == $XIP_DOMAIN ]; then
answer_root_a_query
else
if [ $LC_QNAME == "localhost.$XIP_DOMAIN" ]; then
answer_localhost_a_query
elif subdomain_is_dashed_ip; then
answer_subdomain_a_query_for dashed_ip
elif subdomain_is_ip; then
answer_subdomain_a_query_for ip
fi
fi
fi
if qtype_is "AAAA"; then
LC_QNAME=$(echo $QNAME | tr 'A-Z' 'a-z')
if [ $LC_QNAME == $XIP_DOMAIN ]; then
answer_root_aaaa_query
elif [ $LC_QNAME == "localhost.$XIP_DOMAIN" ]; then
answer_localhost_aaaa_query
elif subdomain_is_dashed_ipv6; then
answer_subdomain_aaaa_query_for dashed_ipv6
fi
fi
send_cmd "END"
done