]> git.ipfire.org Git - thirdparty/pdns.git/blob - docs/lua-records/functions.rst
3b30327dab42ed0a7d821cbd03c99cdfe3af5158
[thirdparty/pdns.git] / docs / lua-records / functions.rst
1 Preset variables
2 ----------------
3
4 LUA rules run within the same environment as described in
5 :doc:`../modes-of-operation`.
6
7 The Lua snippets can query the following variables:
8
9 ``who``
10 ~~~~~~~
11 IP address of requesting resolver
12
13
14 ``ecswho``
15 ~~~~~~~~~~~
16 The EDNS Client Subnet, should one have been set on the query. Unset
17 otherwise.
18
19 ``bestwho``
20 ~~~~~~~~~~~~
21 In absence of ECS, this is set to the IP address of requesting resolver.
22 Otherwise set to the network part of the EDNS Client Subnet supplied by the
23 resolver.
24
25 Functions available
26 -------------------
27
28 Record creation functions
29 ~~~~~~~~~~~~~~~~~~~~~~~~~
30
31 .. function:: ifportup(portnum, addresses[, options])
32
33 Simplistic test to see if an IP address listens on a certain port. This will
34 attempt a TCP connection on port ``portnum`` and consider it UP if the
35 connection establishes, no data will be sent or read on that connection. Note
36 that both IPv4 and IPv6 addresses can be tested, but that it is an error to
37 list IPv4 addresses on an AAAA record, or IPv6 addresses on an A record.
38
39 Will return a single IP address from the set of available IP addresses. If
40 no IP address is available, will return a random element of the set of
41 addresses supplied for testing.
42
43 :param int portnum: The port number to test connections to.
44 :param {str} addresses: The list of IP addresses to check connectivity for.
45 :param options: Table of options for this specific check, see below.
46
47 Various options can be set in the ``options`` parameter:
48
49 - ``selector``: used to pick the IP address from list of viable candidates. Choices include 'pickclosest', 'random', 'hashed', 'all' (default to 'random').
50 - ``backupSelector``: used to pick the IP address from list of all candidates if all addresses are down. Choices include 'pickclosest', 'random', 'hashed', 'all' (default to 'random').
51 - ``source``: Source IP address to check from
52
53
54 .. function:: ifurlup(url, addresses[, options])
55
56 More sophisticated test that attempts an actual http(s) connection to
57 ``url``. In addition, multiple groups of IP addresses can be supplied. The
58 first set with a working (available) IP address is used. URL is considered up if
59 HTTP response code is 200 and optionally if the content matches ``stringmatch``
60 option.
61
62 :param string url: The url to retrieve.
63 :param addresses: List of lists of IP addresses to check the URL on.
64 :param options: Table of options for this specific check, see below.
65
66 Various options can be set in the ``options`` parameter:
67
68 - ``selector``: used to pick the IP address from list of viable candidates. Choices include 'pickclosest', 'random', 'hashed', 'all' (default to 'random').
69 - ``backupSelector``: used to pick the IP address from list of all candidates if all addresses are down. Choices include 'pickclosest', 'random', 'hashed', 'all' (default to 'random').
70 - ``source``: Source IP address to check from
71 - ``stringmatch``: check ``url`` for this string, only declare 'up' if found
72 - ``useragent``: Set the HTTP "User-Agent" header in the requests. By default it is set to "PowerDNS Authoritative Server"
73
74 An example of IP address sets:
75
76 .. code-block:: lua
77
78 ifurlup("example.com", { {"192.0.2.20", "203.0.113.4"}, {"203.0.113.2"} })
79
80 .. function:: pickrandom(addresses)
81
82 Returns a random IP address from the list supplied.
83
84 :param addresses: A list of strings with the possible IP addresses.
85
86 .. function:: pickclosest(addresses)
87
88 Returns IP address deemed closest to the ``bestwho`` IP address.
89
90 :param addresses: A list of strings with the possible IP addresses.
91
92 .. function:: latlon()
93
94 Returns text listing fractional latitude/longitude associated with the ``bestwho`` IP address.
95
96 .. function:: latlonloc()
97
98 Returns text in LOC record format listing latitude/longitude associated with the ``bestwho`` IP address.
99
100 .. function:: closestMagic()
101
102 Suitable for use as a wildcard LUA A record. Will parse the query name which should be in format::
103
104 192-0-2-1.192-0-2-2.198-51-100-1.magic.v4.powerdns.org
105
106 It will then resolve to an A record with the IP address closest to ``bestwho`` from the list
107 of supplied addresses.
108
109 In the ``magic.v4.powerdns.org`` this looks like::
110
111 *.magic.v4.powerdns.org IN LUA A "closestMagic()"
112
113
114 In another zone, a record is then present like this::
115
116 www-balanced.powerdns.org IN CNAME 192-0-2-1.192-0-2-2.198-51-100-1.magic.v4.powerdns.org
117
118 This effectively opens up your server to being a 'geographical load balancer as a service'.
119
120 Performs no uptime checking.
121
122 .. function:: view(pairs)
123
124 Shorthand function to implement 'views' for all record types.
125
126 :param pairs: A list of netmask/result pairs.
127
128 An example::
129
130 view.v4.powerdns.org IN LUA A ("view({ "
131 "{ {'192.168.0.0/16'}, {'192.168.1.54'}},"
132 "{ {'0.0.0.0/0'}, {'192.0.2.1'}} "
133 " }) " )
134
135 This will return IP address 192.168.1.54 for queries coming from
136 192.168.0.0/16, and 192.0.2.1 for all other queries.
137
138 This function also works for CNAME or TXT records.
139
140 .. function:: pickwhashed(weightparams)
141
142 Based on the hash of ``bestwho``, returns an IP address from the list
143 supplied, as weighted by the various ``weight`` parameters.
144 Performs no uptime checking.
145
146 :param weightparams: table of weight, IP addresses.
147
148 Because of the hash, the same client keeps getting the same answer, but
149 given sufficient clients, the load is still spread according to the weight
150 factors.
151
152 An example::
153
154 mydomain.example.com IN LUA A ("pickwhashed({ "
155 " {15, "192.0.2.1"}, "
156 " {100, "198.51.100.5"} "
157 "}) ")
158
159
160 .. function:: pickwrandom(weightparams)
161
162 Returns a random IP address from the list supplied, as weighted by the
163 various ``weight`` parameters. Performs no uptime checking.
164
165 :param weightparams: table of weight, IP addresses.
166
167 See :func:`pickwhashed` for an example.
168
169 Reverse DNS functions
170 ~~~~~~~~~~~~~~~~~~~~~
171
172 .. warning::
173 The reverse DNS functions are under active development. **They may**
174 **not be safe for production use.** The syntax of these functions may change at any
175 time.
176
177 .. function:: createReverse(format)
178
179 Used for generating default hostnames from IPv4 wildcard reverse DNS records, e.g. ``*.0.0.127.in-addr.arpa``
180
181 See :func:`createReverse6` for IPv6 records (ip6.arpa)
182
183 See :func:`createForward` for creating the A records on a wildcard record such as ``*.static.example.com``
184
185 Returns a formatted hostname based on the format string passed.
186
187 :param format: A hostname string to format, for example ``%1%.%2%.%3%.%4%.static.example.com``.
188
189 **Formatting options:**
190
191 - ``%1%`` to ``%4%`` are individual octets
192 - Example record query: ``1.0.0.127.in-addr.arpa``
193 - ``%1%`` = 127
194 - ``%2%`` = 0
195 - ``%3%`` = 0
196 - ``%4%`` = 1
197 - ``%5%`` joins the four decimal octets together with dashes
198 - Example: ``%5%.static.example.com`` is equivalent to ``%1%-%2%-%3%-%4%.static.example.com``
199 - ``%6%`` converts each octet from decimal to hexadecimal and joins them together
200 - Example: A query for ``15.0.0.127.in-addr.arpa``
201 - ``%6`` would be ``7f00000f`` (127 is 7f, and 15 is 0f in hexadecimal)
202
203 **NOTE:** At the current time, only forward dotted format works with :func:`createForward` (i.e. ``127.0.0.1.static.example.com``)
204
205 Example records::
206
207 *.0.0.127.in-addr.arpa IN LUA PTR "createReverse('%1%.%2%.%3%.%4%.static.example.com')"
208 *.1.0.127.in-addr.arpa IN LUA PTR "createReverse('%5%.static.example.com')"
209 *.2.0.127.in-addr.arpa IN LUA PTR "createReverse('%6%.static.example.com')"
210
211 When queried::
212
213 # -x is syntactic sugar to request the PTR record for an IPv4/v6 address such as 127.0.0.5
214 # Equivalent to dig PTR 5.0.0.127.in-addr.arpa
215 $ dig +short -x 127.0.0.5 @ns1.example.com
216 127.0.0.5.static.example.com.
217 $ dig +short -x 127.0.1.5 @ns1.example.com
218 127-0-0-5.static.example.com.
219 $ dig +short -x 127.0.2.5 @ns1.example.com
220 7f000205.static.example.com.
221
222 .. function:: createForward()
223
224 Used to generate the reverse DNS domains made from :func:`createReverse`
225
226 Generates an A record for a dotted or hexadecimal IPv4 domain (e.g. 127.0.0.1.static.example.com)
227
228 It does not take any parameters, it simply interprets the zone record to find the IP address.
229
230 An example record for zone ``static.example.com``::
231
232 *.static.example.com IN LUA A "createForward()"
233
234 **NOTE:** At the current time, only forward dotted format works for this function (i.e. ``127.0.0.1.static.example.com``)
235
236 When queried::
237
238 $ dig +short A 127.0.0.5.static.example.com @ns1.example.com
239 127.0.0.5
240
241 .. function:: createReverse6(format)
242
243 Used for generating default hostnames from IPv6 wildcard reverse DNS records, e.g. ``*.1.0.0.2.ip6.arpa``
244
245 **For simplicity purposes, only small sections of IPv6 rDNS domains are used in most parts of this guide,**
246 **as a full ip6.arpa record is around 80 characters long**
247
248 See :func:`createReverse` for IPv4 records (in-addr.arpa)
249
250 See :func:`createForward6` for creating the AAAA records on a wildcard record such as ``*.static.example.com``
251
252 Returns a formatted hostname based on the format string passed.
253
254 :param format: A hostname string to format, for example ``%33%.static6.example.com``.
255
256 Formatting options:
257
258 - ``%1%`` to ``%32%`` are individual characters (nibbles)
259 - **Example PTR record query:** ``a.0.0.0.1.0.0.2.ip6.arpa``
260 - ``%1%`` = 2
261 - ``%2%`` = 0
262 - ``%3%`` = 0
263 - ``%4%`` = 1
264 - ``%33%`` converts the compressed address format into a dashed format, e.g. ``2001:a::1`` to ``2001-a--1``
265 - ``%34%`` to ``%41%`` represent the 8 uncompressed 2-byte chunks
266 - **Example:** PTR query for ``2001:a:b::123``
267 - ``%34%`` - returns ``2001`` (chunk 1)
268 - ``%35%`` - returns ``000a`` (chunk 2)
269 - ``%41%`` - returns ``0123`` (chunk 8)
270
271 **NOTE:** At the current time, only dashed compressed format works for this function (i.e. ``2001-a-b--1.static6.example.com``)
272
273 Example records::
274
275 *.1.0.0.2.ip6.arpa IN LUA PTR "createReverse('%33%.static6.example.com')"
276 *.2.0.0.2.ip6.arpa IN LUA PTR "createReverse('%34%.%35%.static6.example.com')"
277
278 When queried::
279
280 # -x is syntactic sugar to request the PTR record for an IPv4/v6 address such as 2001::1
281 # Equivalent to dig PTR 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.b.0.0.0.a.0.0.0.1.0.0.2.ip6.arpa
282 # readable version: 1.0.0.0 .0.0.0.0 .0.0.0.0 .0.0.0.0 .0.0.0.0 .b.0.0.0 .a.0.0.0 .1.0.0.2 .ip6.arpa
283
284 $ dig +short -x 2001:a:b::1 @ns1.example.com
285 2001-a-b--1.static6.example.com.
286
287 $ dig +short -x 2002:a:b::1 @ns1.example.com
288 2002.000a.static6.example.com
289
290 .. function:: createForward6()
291
292 Used to generate the reverse DNS domains made from :func:`createReverse6`
293
294 Generates an AAAA record for a dashed compressed IPv6 domain (e.g. ``2001-a-b--1.static6.example.com``)
295
296 It does not take any parameters, it simply interprets the zone record to find the IP address.
297
298 An example record for zone ``static.example.com``::
299
300 *.static6.example.com IN LUA AAAA "createForward6()"
301
302 **NOTE:** At the current time, only dashed compressed format works for this function (i.e. ``2001-a-b--1.static6.example.com``)
303
304 When queried::
305
306 $ dig +short AAAA 2001-a-b--1.static6.example.com @ns1.example.com
307 2001:a:b::1
308
309 Helper functions
310 ~~~~~~~~~~~~~~~~
311
312 .. function:: asnum(number)
313 asnum(numbers)
314
315 Returns true if the ``bestwho`` IP address is determined to be from
316 any of the listed AS numbers.
317
318 :param int number: An AS number
319 :param [int] numbers: A list of AS numbers
320
321 .. function:: country(country)
322 country(countries)
323
324 Returns true if the ``bestwho`` IP address of the client is within the
325 two letter ISO country code passed, as described in :doc:`../backends/geoip`.
326
327 :param string country: A country code like "NL"
328 :param [string] countries: A list of country codes
329
330 .. function:: continent(continent)
331 continent(continents)
332
333 Returns true if the ``bestwho`` IP address of the client is within the
334 continent passed, as described in :doc:`../backends/geoip`.
335
336 :param string continent: A continent code like "EU"
337 :param [string] continents: A list of continent codes
338
339 .. function:: netmask(netmasks)
340
341 Returns true if ``bestwho`` is within any of the listed subnets.
342
343 :param [string] netmasks: The list of IP addresses to check against