4 LUA rules run within the same environment as described in
5 :doc:`../modes-of-operation`.
7 The Lua snippets can query the following variables:
11 IP address of requesting resolver
16 The EDNS Client Subnet, should one have been set on the query. Unset
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
28 Record creation functions
29 ~~~~~~~~~~~~~~~~~~~~~~~~~
31 .. function:: ifportup(portnum, addresses[, options])
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.
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.
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.
47 Various options can be set in the ``options`` parameter:
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
54 .. function:: ifurlup(url, addresses[, options])
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``
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.
66 Various options can be set in the ``options`` parameter:
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"
74 An example of IP address sets:
78 ifurlup("example.com", { {"192.0.2.20", "203.0.113.4"}, {"203.0.113.2"} })
80 .. function:: pickrandom(addresses)
82 Returns a random IP address from the list supplied.
84 :param addresses: A list of strings with the possible IP addresses.
86 .. function:: pickclosest(addresses)
88 Returns IP address deemed closest to the ``bestwho`` IP address.
90 :param addresses: A list of strings with the possible IP addresses.
92 .. function:: latlon()
94 Returns text listing fractional latitude/longitude associated with the ``bestwho`` IP address.
96 .. function:: latlonloc()
98 Returns text in LOC record format listing latitude/longitude associated with the ``bestwho`` IP address.
100 .. function:: closestMagic()
102 Suitable for use as a wildcard LUA A record. Will parse the query name which should be in format::
104 192-0-2-1.192-0-2-2.198-51-100-1.magic.v4.powerdns.org
106 It will then resolve to an A record with the IP address closest to ``bestwho`` from the list
107 of supplied addresses.
109 In the ``magic.v4.powerdns.org`` this looks like::
111 *.magic.v4.powerdns.org IN LUA A "closestMagic()"
114 In another zone, a record is then present like this::
116 www-balanced.powerdns.org IN CNAME 192-0-2-1.192-0-2-2.198-51-100-1.magic.v4.powerdns.org
118 This effectively opens up your server to being a 'geographical load balancer as a service'.
120 Performs no uptime checking.
122 .. function:: view(pairs)
124 Shorthand function to implement 'views' for all record types.
126 :param pairs: A list of netmask/result pairs.
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'}} "
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.
138 This function also works for CNAME or TXT records.
140 .. function:: pickwhashed(weightparams)
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.
146 :param weightparams: table of weight, IP addresses.
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
154 mydomain.example.com IN LUA A ("pickwhashed({ "
155 " {15, "192.0.2.1"}, "
156 " {100, "198.51.100.5"} "
160 .. function:: pickwrandom(weightparams)
162 Returns a random IP address from the list supplied, as weighted by the
163 various ``weight`` parameters. Performs no uptime checking.
165 :param weightparams: table of weight, IP addresses.
167 See :func:`pickwhashed` for an example.
169 Reverse DNS functions
170 ~~~~~~~~~~~~~~~~~~~~~
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
177 .. function:: createReverse(format)
179 Used for generating default hostnames from IPv4 wildcard reverse DNS records, e.g. ``*.0.0.127.in-addr.arpa``
181 See :func:`createReverse6` for IPv6 records (ip6.arpa)
183 See :func:`createForward` for creating the A records on a wildcard record such as ``*.static.example.com``
185 Returns a formatted hostname based on the format string passed.
187 :param format: A hostname string to format, for example ``%1%.%2%.%3%.%4%.static.example.com``.
189 **Formatting options:**
191 - ``%1%`` to ``%4%`` are individual octets
192 - Example record query: ``1.0.0.127.in-addr.arpa``
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)
203 **NOTE:** At the current time, only forward dotted format works with :func:`createForward` (i.e. ``127.0.0.1.static.example.com``)
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')"
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.
222 .. function:: createForward()
224 Used to generate the reverse DNS domains made from :func:`createReverse`
226 Generates an A record for a dotted or hexadecimal IPv4 domain (e.g. 127.0.0.1.static.example.com)
228 It does not take any parameters, it simply interprets the zone record to find the IP address.
230 An example record for zone ``static.example.com``::
232 *.static.example.com IN LUA A "createForward()"
234 **NOTE:** At the current time, only forward dotted format works for this function (i.e. ``127.0.0.1.static.example.com``)
238 $ dig +short A 127.0.0.5.static.example.com @ns1.example.com
241 .. function:: createReverse6(format)
243 Used for generating default hostnames from IPv6 wildcard reverse DNS records, e.g. ``*.1.0.0.2.ip6.arpa``
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**
248 See :func:`createReverse` for IPv4 records (in-addr.arpa)
250 See :func:`createForward6` for creating the AAAA records on a wildcard record such as ``*.static.example.com``
252 Returns a formatted hostname based on the format string passed.
254 :param format: A hostname string to format, for example ``%33%.static6.example.com``.
258 - ``%1%`` to ``%32%`` are individual characters (nibbles)
259 - **Example PTR record query:** ``a.0.0.0.1.0.0.2.ip6.arpa``
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)
271 **NOTE:** At the current time, only dashed compressed format works for this function (i.e. ``2001-a-b--1.static6.example.com``)
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')"
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
284 $ dig +short -x 2001:a:b::1 @ns1.example.com
285 2001-a-b--1.static6.example.com.
287 $ dig +short -x 2002:a:b::1 @ns1.example.com
288 2002.000a.static6.example.com
290 .. function:: createForward6()
292 Used to generate the reverse DNS domains made from :func:`createReverse6`
294 Generates an AAAA record for a dashed compressed IPv6 domain (e.g. ``2001-a-b--1.static6.example.com``)
296 It does not take any parameters, it simply interprets the zone record to find the IP address.
298 An example record for zone ``static.example.com``::
300 *.static6.example.com IN LUA AAAA "createForward6()"
302 **NOTE:** At the current time, only dashed compressed format works for this function (i.e. ``2001-a-b--1.static6.example.com``)
306 $ dig +short AAAA 2001-a-b--1.static6.example.com @ns1.example.com
312 .. function:: asnum(number)
315 Returns true if the ``bestwho`` IP address is determined to be from
316 any of the listed AS numbers.
318 :param int number: An AS number
319 :param [int] numbers: A list of AS numbers
321 .. function:: country(country)
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`.
327 :param string country: A country code like "NL"
328 :param [string] countries: A list of country codes
330 .. function:: continent(continent)
331 continent(continents)
333 Returns true if the ``bestwho`` IP address of the client is within the
334 continent passed, as described in :doc:`../backends/geoip`.
336 :param string continent: A continent code like "EU"
337 :param [string] continents: A list of continent codes
339 .. function:: netmask(netmasks)
341 Returns true if ``bestwho`` is within any of the listed subnets.
343 :param [string] netmasks: The list of IP addresses to check against