pax_global_header 0000666 0000000 0000000 00000000064 13202063174 0014510 g ustar 00root root 0000000 0000000 52 comment=a176896d563e7d609784b20dafcaedeef7a4de6a
update-systemd-resolved-1.2.7/ 0000775 0000000 0000000 00000000000 13202063174 0016310 5 ustar 00root root 0000000 0000000 update-systemd-resolved-1.2.7/.gitignore 0000664 0000000 0000000 00000000035 13202063174 0020276 0 ustar 00root root 0000000 0000000 .*.sw[a-z0-9]
.sw[a-z0-9]
*~
update-systemd-resolved-1.2.7/.travis.yml 0000664 0000000 0000000 00000000147 13202063174 0020423 0 ustar 00root root 0000000 0000000 ---
# Don't need sudo access or to install anything
sudo: false
install: true
script:
- ./run-tests
update-systemd-resolved-1.2.7/CHANGELOG.md 0000664 0000000 0000000 00000013046 13202063174 0020125 0 ustar 00root root 0000000 0000000 # Changelog
## 1.2.7 (2017.11.12)
### NOTES
Following a request by @JoshDobbin, support has been added for passing
`ADAPTER_DOMAIN_SUFFIX` via `dhcp-options` to work with the Microsoft standard.
Also included some additional notes in README.md about using `down` in dropped
privilege situations for clarification.
### IMPROVEMENTS
- Added support for ADAPTER_DOMAIN_SUFFIX (@jonathanio)
- Added notes in README.md about `down` with dropped privileges (@jonathanio)
## 1.2.6 (2017.07.24)
### NOTES
Improvements made to the `logger` command to prevent issues with privilege
dropping under the assistance of @dermarens, @terminalmage, @guruxu, and @benvh.
Updated some documentation for consistency and clarity. Thanks to @flungo and
@dawansv here.
### IMPROVEMENTS
- Updated to include a full list in PATH, including sbin paths. (@jonathanio)
- Updated documentation regarding DNS leakage. (@jonathanio)
- Updated all script locations to be consistent. (@jonathanio)
- Add some installation instructions to README.md. (@flungo)
- Update command-line parameters needed within Makefile/README.md. (@noraj1337)
- Fix script nmae in command-line path within README.md. (@phR0ze)
## 1.2.5 (2017.03.02)
### IMPROVEMENTS
- Updated to include a full list in PATH, including sbin paths. (@jonathanio)
## 1.2.4 (2017.03.02)
### NOTES
@piotr-dobrogost, @mgu, and @aRkadeFR helped improve the documentation.
### IMPROVEMENTS
- It was noted that the PATH setting used in the documentation doesn't work on
all systems (sorry, my bad), so it has now been updated so it should now work.
(@aRkadeFR)
## 1.2.3 (2016.12.25)
### NOTES
@Nauxuron provided a patch to improve DESTDIR and PREFIX handling in Makefile.
### IMPROVEMENTS
- Improve handling of DESTDIR and PREFIX in the Makefile to follow the GNU
guidelines. (@Nauxuron)
## 1.2.2 (2016.12.13)
### NOTES
This one is a thanks to @mikken and helps support OpenVPN 2.4 as well as fix
an issue with `DNSSEC` handling on the `busctl` call.
### BUG FIXES
- The incorrect usage of `down-pre` which as of OpenVPN 2.4 is now a fatal error
when you pass it an argument (i.e. the script we were originally thought it
should be calling). (@mikken)
- Issues with `busctl` and bash properly handling the "empty string" case to use
the default `DNSSEC` option. (@jonathanio)
- Noise when `busctl` is called on the down case when privileges have been
dropped in the client. (@mikken)
- Added documentation for `allow-downgrade` support in `DNSSEC` option (which
was supported, but not documented). (@jonathanio)
## 1.2.1 (2016.10.06)
### NOTES
Thanks for @arjenschol for spotting this one: An error in the AF_INET value
provided to SetLinkDNS prevented IPv6 DNS servers from being added.
### BUG FIXES
- Fix IPv6 DNS by specifying AF_INET6 value (10) insteadof array size (2)
(@arjenschol)
## 1.2.0 (2016.08.29)
### NOTES
Add support for DNSSEC processing, improve logic around `DOMAIN` and
`DOMAIN-SEARCH` handling, add support for `DOMAIN-ROUTE`, and improve
documentation.
### BACKWARDS INCOMPATIBILITIES
- Due to (probably) an incorrect assumption on my part (@jonathanio) in the
purpose of `DOMAIN-SEARCH` verses `DOMAIN`, domains added via `DOMAIN` were
marked as searchable, and so would be appended to bare domain names, while
those added via `DOMAIN-SEARCH` would not. This was a divergance from how
older OpenVPN handler scripts (such as `update-resolv-conf` and
`update-systemd-network`) processed them (i.e. in all cases they were just
made searchable). Note that both scripts didn't really have the concept of
`domain` in the same way as `/etc/resolv.conf` understood it. This script now
(hopefully) properly handles `DOMAIN` and `DOMAIN-SEARCH` (single of the
former, and is primary, multiple of the latter and secondary).
### FEATURES
- Add support for `DNSSEC` option which allows you to enable or disable (or
leave to system default) the `DNSSEC` setting for any DNS queries made to the
DNS servers provided for this link. (@jonathanio)
- Add support for `DOMAIN-ROUTE` which, through `systemd-resolved`, allows you
to set domain names which should be routed over this link to the DNS servers
provided. (@jonathanio)
### IMPROVEMENTS
- Correct the logic around the handling of `DOMAIN` and `DOMAIN-SEARCH` to be
more compatible with previous versions of these handlers. (@jonathanio)
## 1.1.1 (2016.08.10)
### NOTES
Thanks to the help from @pid1 for this release. The documentation mistakenly
noted to use pre-down for the script now (compared to down originally, which
failed as the tun or tap device would have been removed before the script
ran). However, this should have in fact been down-pre.
### BUG FIXES
- Fix `pre-down` to `down-pre` in the documentation else you'll break your
OpenVPN configuration. (@pid1)
## 1.1.0 (2016.08.08)
### NOTES
Thanks to the work by @BaxterStockman, the script has been refactored, hopefully
making it easier to read and follow, while additional tests around IPv6
processing have been added.
### IMPROVEMENTS
- Refactor the codebase to make it easier to read and expand. (@BaxterStockman)
- Improve run-tests so multiple tests can be run within a file, and can expect
failures within a test. (@BaxterStockman)
- Add tests for invalid IPv6 addresses. (@BaxterStockman)
## 1.0.0 (2016.06.23)
### NOTES
First release of `update-systemd-resolved`. Should fully support the three
standard DHCP options in OpenVPN (`DNS`, `DOMAIN`, and `DOMAIN-SEARCH`) with
integration tests around the code to manage and monitor regressions. Also
supports multiple (and combined) IPv4 and IPv6 DNS addresses.
update-systemd-resolved-1.2.7/LICENSE 0000664 0000000 0000000 00000001144 13202063174 0017315 0 ustar 00root root 0000000 0000000 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 .
update-systemd-resolved-1.2.7/Makefile 0000664 0000000 0000000 00000001453 13202063174 0017753 0 ustar 00root root 0000000 0000000 PREFIX ?= /etc/openvpn/scripts
SRC = update-systemd-resolved
DEST = $(DESTDIR)$(PREFIX)/$(SRC)
.PHONY: all install info
all: install info
install:
@install -Dm750 $(SRC) $(DEST)
info:
@printf 'Successfully installed %s to %s.\n' $(SRC) $(DEST)
@echo
@echo 'Now would be a good time to update /etc/nsswitch.conf:'
@echo ' # Use systemd-resolved first, then fall back to /etc/resolv.conf'
@echo ' hosts: files resolve dns myhostname'
@echo ' # Use /etc/resolv.conf first, then fall back to systemd-resolved'
@echo ' hosts: files dns resolve myhostname'
@echo
@echo 'You should also update your OpenVPN configuration:'
@printf ' setenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n script-security 2\n up %s\n down %s\n down-pre' $(DEST) $(DEST)
test:
@./run-tests
update-systemd-resolved-1.2.7/README.md 0000664 0000000 0000000 00000017105 13202063174 0017573 0 ustar 00root root 0000000 0000000 # update-systemd-resolved
[](https://travis-ci.org/jonathanio/update-systemd-resolved)
This is a helper script designed to integrate OpenVPN with the `systemd-resolved`
service via DBus instead of trying to override `/etc/resolv.conf`, or manipulate
`systemd-networkd` configuration files.
Since systemd-229, the `systemd-resolved` service has an API available via
DBus which allows directly setting the DNS configuration for a link. This script
makes use of `busctl` from systemd to send DBus messages to `systemd-resolved`
to update the DNS for the link created by OpenVPN.
*NOTE*: This is an beta script. So long as you're using OpenVPN 2.1 or greater,
iproute2, and have at least version 229 of systemd, then it should work.
Nonetheless, if you do come across problems, fork and fix, or raise an issue.
All are most welcome.
## Installation
If you are using a distribution of Linux with access to the Arch User Repository,
the simplest way to install is by using the
[openvpn-update-systemd-resolved](https://aur.archlinux.org/packages/openvpn-update-systemd-resolved/)
AUR package as this will take care of any updates through your package manager.
Alternatively, the package can be manually installed by running the following:
```
git clone https://github.com/jonathanio/update-systemd-resolved.git
cd update-systemd-resolved
make
```
## How to Enable
Make sure that you have `systemd-resolved` enabled and running:
```
systemctl enable systemd-resolved.service
systemctl start systemd-resolved.service
```
Then update your `/etc/nsswitch.conf` file to look up DNS via the `resolve`
service:
```
# Use /etc/resolv.conf first, then fall back to systemd-resolved
hosts: files dns resolve myhostname
# Use systemd-resolved first, then fall back to /etc/resolv.conf
hosts: files resolve dns myhostname
# Don't use /etc/resolv.conf at all
hosts: files resolve myhostname
```
*Note*: If you intend on using this script, the latter two are preferred
otherwise the configuration provided by this script will only work on domains
that cannot be resolved by the currently configured DNS servers (i.e. they must
fall back after trying the ones set by your LAN's DHCP server).
Finally, update your OpenVPN configuration file and set the `up` and `down`
options to point to the script, and `down-pre` to ensure that the script is run
before the device is closed:
```
script-security 2
setenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
up /etc/openvpn/scripts/update-systemd-resolved
down /etc/openvpn/scripts/update-systemd-resolved
down-pre
```
*Note*: The `down` and `down-pre` options here will not work as expected where
the `openvpn` daemon drops privileges after establishing the connection (i.e.
when using the `user` and `group` options). This is because only the `root` user
will have the privileges required to talk to `systemd-resolved.service` over
DBus. The `openvpn-plugin-down-root.so` plugin does provide support for
enabling the `down` script to be run as the `root` user, but this has been known
to be unreliable.
Ultimately this shouldn't affect normal operation as `systemd-resolved.service`
will remove all settings associated with the link (and therefore naturally
update `/etc/resolv.conf`, if you have it symlinked) when the TUN or TAP device
is closed. The option for `down` and `down-pre` just make this step explicit
before the device is torn down rather than implicit on the change in
environment.
Alternatively if you don't want to edit your client configuration, you can add
the following options to your openvpn command:
```
openvpn \
--script-security 2 \
--setenv PATH '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' \
--up /etc/openvpn/scripts/update-systemd-resolved \
--down /etc/openvpn/scripts/update-systemd-resolved \
--down-pre
```
## Usage
`update-systemd-resolved` works by processing the `dhcp-option` commands set in
OpenVPN, either through the server, or the client, configuration:
| Option | Examples | Notes |
|--:|---|---|
| `DNS` | `0.0.0.0`
`::1` | This sets the DNS servers for the link and can take any IPv4 or IPv6 address. |
| `DOMAIN` or `ADAPTER_DOMAIN_SUFFIX` | `example.com` | The primary domain for this host. If set multiple times, the last provided is used. Will be the primary search domain for bare hostnames. All requests for this domain as well will be routed to the `DNS` servers provided on this link. |
| `DOMAIN-SEARCH` | `example.com` | Secondary domains which will be used to search for bare hostnames (after any `DOMAIN`, if set) and in the order provided. All requests for this domain will be routed to the `DNS` servers provided on this link. |
| `DOMAIN-ROUTE` | `example.com` | All requests for these domains will be routed to the `DNS` servers provided on this link. They will *not* be used to search for bare hostnames, only routed. A `DOMAIN-ROUTE` option for `.` (single period) will instruct `systemd-resolved` to route the entire namespace through to the `DNS` servers configured for this connection (unless a more specifc route has been offered by another connection for a selected name/namespace). This is useful if you wish to prevent DNS leakage. |
| `DNSSEC` | `yes`
`no``allow-downgrade``default` | Control of DNSSEC should be enabled (`yes`) or disabled (`no`), or `allow-downgrade` to switch off DNSSEC only if the server doesn't support it, for any queries over this link only, or use the system default (`default`). |
*Note*: There are no local or system options to be configured. All configuration
for this script is handled though OpenVPN, including, for example, the name of
the interface to be configured.
### Example
```
push "dhcp-option DNS 10.62.3.2"
push "dhcp-option DNS 10.62.3.3"
push "dhcp-option DNS 2001:db8::a3:c15c:b56e:619a"
push "dhcp-option DNS 2001:db8::a3:ffec:f61c:2e06"
push "dhcp-option DOMAIN example.office"
push "dhcp-option DOMAIN-SEARCH example.com"
push "dhcp-option DOMAIN-ROUTE example.net"
push "dhcp-option DOMAIN-ROUTE example.org"
push "dhcp-option DNSSEC yes"
```
This, added to the OpenVPN server's configuration file will set two IPv4 DNS
servers and two IPv6 and will set the primary domain for the link to be
`example.office`. Therefore if you try to look up the bare address `mail` then
`mail.example.office` will be attempted first. The domain `example.com` is also
added as an additional search domain, so if `mail.example.office` fails, then
`mail.example.com` will be tried next.
Requests for `example.net` and `example.org` will also be routed though to the
four DNS servers listed too, but they will *not* be appended (i.e.
`mail.example.net` will not be attempted, nor `mail.example.org` if
`mail.example.office` or `mail.example.com` do not exist).
Finally, DNSSEC has been enabled for this link (and this link only).
## How to help
If you can help with any of these areas, or have bug fixes, please fork and
raise a Pull Request for me.
I have built a basic test framework around the script which can be used to
monitor and validate the calls made by the script based on the environment
variables available to it at run-time. Please add a test for any new features
you may wish to add, or update any which are wrong, and test your code by
running `./run-tests` from the root of the repository. There are no dependencies
on `run-tests` - it runs 100% bash and doesn't call out ot any other program or
langauge.
TravisCI is enabled on this repository: Click the link at the top of this README
to see the current state of the code and its tests.
## Licence
GPL
## Author
Jonathan Wright
update-systemd-resolved-1.2.7/run-tests 0000775 0000000 0000000 00000012102 13202063174 0020176 0 ustar 00root root 0000000 0000000 #!/bin/bash
#
# Test Framework for update-systemd-resolved.
# Copyright (C) 2016, Jonathan Wright
#
# 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.
# Set colour escape sequences
RED='\033[0;31m'
YELLOW='\033[1;33m'
ORANGE='\033[0;33m'
GREEN='\033[0;32m'
DARK='\033[1;30m'
RESET='\033[0m'
# Set Pass/Fail signatures
PASS="✓"
FAIL="✗"
# Counters
COUNT_PASS=0
COUNT_FAIL=0
# Flag for determining whether a test script called the `runtest' function
RUNTEST_CALLED=0
AUTOMATED_TESTING=1
function busctl {
shift 4
_log "busctl called with: ${@}"
# Set that busctl has been called
busctl_called=1
case "${1}" in
SetLinkDNS)
shift 2
if [[ "${TEST_BUSCTL_DNS}" == "" ]]; then
[[ "${ip_ifindex} ${TEST_BUSCTL_DNS}" == "${@}" ]] || \
_fail "SetLinkDNS was called and should not be: '${@}'"
else
[[ "${ip_ifindex} ${TEST_BUSCTL_DNS}" == "${@}" ]] && \
_pass "SetLinkDNS was called correctly" || \
_fail "SetLinkDNS was not given the correct arguments:\n" \
" Expected: '${ip_ifindex} ${TEST_BUSCTL_DNS}'\n" \
" Received: '${@}'"
fi
;;
SetLinkDomains)
shift 2
if [[ "${TEST_BUSCTL_DOMAINS}" == "" ]]; then
[[ "${ip_ifindex} ${TEST_BUSCTL_DOMAINS}" == "${@}" ]] || \
_fail "SetLinkDomains was called and should not be: '${@}'"
else
[[ "${ip_ifindex} ${TEST_BUSCTL_DOMAINS}" == "${@}" ]] && \
_pass "SetLinkDomains was called correctly" || \
_fail "SetLinkDomains was not given the correct arguments:\n" \
" Expected: '${ip_ifindex} ${TEST_BUSCTL_DOMAINS}'\n" \
" Received: '${@}'"
fi
;;
SetLinkDNSSEC)
shift 2
[[ "${ip_ifindex} ${TEST_BUSCTL_DNSSEC}x" == "${@}x" ]] && \
_pass "SetLinkDNSSEC was called correctly" || \
_fail "SetLinkDNSSEC was not given the correct arguments:\n" \
" Expected: '${ip_ifindex} ${TEST_BUSCTL_DNSSEC}'\n" \
" Received: '${@}'"
;;
*)
_fail "Unknown command called on busctl: ${1}"
;;
esac
}
function ip {
_log "ip called with: ${@}"
[[ "${1} ${2} ${3} ${4}" == "link show dev ${dev}" ]] && \
_pass "ip was called correctly" || \
_fail "ip was called with incorrect or unknown arguments"
# Return fake ip statement
echo -e "${ip_ifindex}: ${dev}: " \
" mtu 1500 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen" \
" 100\n link/none"
}
function logger {
# Remove standard options
local message="$*"
_log "-- ${message##* -- }"
}
function exit {
# Override
_log "exit called with status ${1}"
}
function _log {
( >&2 echo -e " ${DARK}${@}${RESET}" )
}
function _pass {
COUNT_PASS=$((COUNT_PASS+1))
( >&2 echo -e " ${GREEN}${PASS}${RESET} ${@}" )
}
function _fail {
COUNT_FAIL=$((COUNT_FAIL+1))
( >&2 echo -e " ${RED}${FAIL} ${@}${RESET}" )
}
function runtest {
# Increment counter so that we don't double-execute if a test script calls
# this function.
: ${RUNTEST_CALLED:=0}
(( RUNTEST_CALLED += 1 ))
echo -e "${GREEN}- Testing ${TEST_TITLE:-a nameless test}${RESET}"
# Source, don't run, so we don't need to export and internal functions override
# external calls out to system commands
source update-systemd-resolved
exit_status="$?"
exit_message="script exited with a ${exit_status} exit status"
if [[ "$(( exit_status > 0 ))" == "${EXPECT_FAILURE:-0}" ]]; then
_pass "$exit_message"
else
_fail "$exit_message"
fi
if [[ ${TEST_BUSCTL_CALLED} -eq 0 ]]; then
[[ ${busctl_called} -eq 0 ]] && \
_pass "busctl was not called, as expected" || \
_fail "busctl was called, not expected"
else
[[ ${busctl_called} -eq 0 ]] && \
_fail "busctl was not called, not expected"
fi
echo
}
echo "update-systemd-resolved Test Suite"
echo
for TEST in tests/*.sh; do
# Set/Reset loop variables
RUNTEST_CALLED=0
EXPECT_FAILURE=0
busctl_called=0
# Set/Reset expected results
TEST_BUSCTL_DNS=""
TEST_BUSCTL_DOMAINS=""
TEST_BUSCTL_DNSSEC=""
# Keep this random, as we will never know the ifindex up-front
ip_ifindex=$((RANDOM%=64))
# Clear foreign_option_*
foreign_option_1=""
foreign_option_2=""
foreign_option_3=""
foreign_option_4=""
foreign_option_5=""
foreign_option_6=""
# Import the test configuration
source "${TEST}"
(( RUNTEST_CALLED > 0 )) || runtest
done
echo -e " ${GREEN}${PASS} ${COUNT_PASS} Passed${RESET}"
echo -e " ${RED}${FAIL} ${COUNT_FAIL} Failed${RESET}"
# Make sure we fail if there are failed tests
[[ ${COUNT_FAIL} -eq 0 ]]
update-systemd-resolved-1.2.7/tests/ 0000775 0000000 0000000 00000000000 13202063174 0017452 5 ustar 00root root 0000000 0000000 update-systemd-resolved-1.2.7/tests/01_no_updates.sh 0000664 0000000 0000000 00000000151 13202063174 0022444 0 ustar 00root root 0000000 0000000 # Emulate OpenVPN environment
script_type="up"
dev="tun01"
TEST_TITLE="No Updates"
TEST_BUSCTL_CALLED=0
update-systemd-resolved-1.2.7/tests/02_single_ipv4_dns.sh 0000664 0000000 0000000 00000000246 13202063174 0023400 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun02"
foreign_option_1="dhcp-option DNS 1.23.4.56"
TEST_TITLE="Single IPv4 DNS Server"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 2 4 1 23 4 56"
update-systemd-resolved-1.2.7/tests/03a_multiple_ipv4_dns_1.sh 0000664 0000000 0000000 00000000353 13202063174 0024333 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun03"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 5.6.7.89"
TEST_TITLE="Multiple IPv4 DNS Servers (Part 1)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="2 2 4 1 23 4 56 2 4 5 6 7 89"
update-systemd-resolved-1.2.7/tests/03b_multiple_ipv4_dns_2.sh 0000664 0000000 0000000 00000000446 13202063174 0024340 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun03"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 5.6.7.89"
foreign_option_3="dhcp-option DNS 34.5.67.8"
TEST_TITLE="Multiple IPv4 DNS Servers (Part 2)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="3 2 4 1 23 4 56 2 4 5 6 7 89 2 4 34 5 67 8"
update-systemd-resolved-1.2.7/tests/04a_single_dns_domain.sh 0000664 0000000 0000000 00000000256 13202063174 0024131 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun04"
foreign_option_1="dhcp-option DOMAIN example.com"
TEST_TITLE="Single DNS Domain"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="1 example.com false"
update-systemd-resolved-1.2.7/tests/04b_multiple_dns_domains.sh 0000664 0000000 0000000 00000000341 13202063174 0024662 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun04"
foreign_option_1="dhcp-option DOMAIN example.com"
foreign_option_2="dhcp-option DOMAIN example.co"
TEST_TITLE="Multiple DNS Domains"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="1 example.co false"
update-systemd-resolved-1.2.7/tests/05a_dns_domain_and_search_1.sh 0000664 0000000 0000000 00000000413 13202063174 0025153 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun05"
foreign_option_1="dhcp-option DOMAIN example.com"
foreign_option_2="dhcp-option DOMAIN-SEARCH example.org"
TEST_TITLE="DNS Single Domain and Single Search"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="2 example.com false example.org false"
update-systemd-resolved-1.2.7/tests/05b_dns_domain_and_search_2.sh 0000664 0000000 0000000 00000000524 13202063174 0025160 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun05"
foreign_option_1="dhcp-option DOMAIN example.com"
foreign_option_2="dhcp-option DOMAIN-SEARCH example.org"
foreign_option_3="dhcp-option DOMAIN-SEARCH example.net"
TEST_TITLE="DNS Single Domain and Dual Search"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="3 example.com false example.org false example.net false"
update-systemd-resolved-1.2.7/tests/05c_dns_domain_and_search_3.sh 0000664 0000000 0000000 00000000547 13202063174 0025167 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun05"
foreign_option_1="dhcp-option DOMAIN-SEARCH example.org"
foreign_option_2="dhcp-option DOMAIN example.com"
foreign_option_3="dhcp-option DOMAIN-SEARCH example.net"
TEST_TITLE="DNS Single Domain and Dual Search (with Order Check)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="3 example.com false example.org false example.net false"
update-systemd-resolved-1.2.7/tests/05d_dns_domain_and_search_4.sh 0000664 0000000 0000000 00000000626 13202063174 0025167 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun05"
foreign_option_1="dhcp-option DOMAIN-SEARCH example.org"
foreign_option_2="dhcp-option DOMAIN example.co"
foreign_option_3="dhcp-option DOMAIN example.com"
foreign_option_4="dhcp-option DOMAIN-SEARCH example.net"
TEST_TITLE="DNS Dual Domain and Dual Search (with Order Check)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="3 example.com false example.org false example.net false"
update-systemd-resolved-1.2.7/tests/05e_dns_domain_alternate.sh 0000664 0000000 0000000 00000000322 13202063174 0024626 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun05"
foreign_option_1="dhcp-option ADAPTER_DOMAIN_SUFFIX example.org"
TEST_TITLE="DNS Doamin using ADAPTER_DOMAIN_SUFFIX"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="1 example.org false"
update-systemd-resolved-1.2.7/tests/06a_single_domain_route.sh 0000664 0000000 0000000 00000000262 13202063174 0024502 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun06"
foreign_option_1="dhcp-option DOMAIN-ROUTE example.com"
TEST_TITLE="Single DNS Route"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="1 example.com true"
update-systemd-resolved-1.2.7/tests/06b_multiple_domain_routes.sh 0000664 0000000 0000000 00000000506 13202063174 0025241 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun06"
foreign_option_1="dhcp-option DOMAIN-ROUTE example.com"
foreign_option_2="dhcp-option DOMAIN-ROUTE example.co"
foreign_option_3="dhcp-option DOMAIN-ROUTE example.co.uk"
TEST_TITLE="Single DNS Route"
TEST_BUSCTL_DOMAINS="3 example.com true example.co true example.co.uk true"
TEST_BUSCTL_CALLED=1
update-systemd-resolved-1.2.7/tests/07a_dns_domain_search_and_route_1.sh 0000664 0000000 0000000 00000000654 13202063174 0026402 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun07"
foreign_option_1="dhcp-option DOMAIN example.com"
foreign_option_2="dhcp-option DOMAIN-SEARCH example.org"
foreign_option_3="dhcp-option DOMAIN-SEARCH example.co.uk"
foreign_option_4="dhcp-option DOMAIN-ROUTE example.net"
TEST_TITLE="DNS Single Domain, Dual Search, Single Route"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="4 example.com false example.org false example.co.uk false example.net true"
update-systemd-resolved-1.2.7/tests/07b_dns_domain_search_and_route_2.sh 0000664 0000000 0000000 00000001072 13202063174 0026377 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun07"
foreign_option_1="dhcp-option DOMAIN example.com"
foreign_option_2="dhcp-option DOMAIN-SEARCH example.org"
foreign_option_3="dhcp-option DOMAIN-ROUTE example.net"
foreign_option_4="dhcp-option DOMAIN-SEARCH example.co.uk"
foreign_option_5="dhcp-option DOMAIN example.co"
foreign_option_6="dhcp-option DOMAIN-ROUTE example.uk.com"
TEST_TITLE="DNS Dual Domain, Dual Search, Dual Route (with Order Check)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="5 example.co false example.org false example.co.uk false example.net true example.uk.com true"
update-systemd-resolved-1.2.7/tests/08a_dns_ipv4_and_domain.sh 0000664 0000000 0000000 00000000502 13202063174 0024352 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun08"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 2.34.56.7"
foreign_option_3="dhcp-option DOMAIN example.com"
TEST_TITLE="DNS IPv4 Servers and Domain"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="2 2 4 1 23 4 56 2 4 2 34 56 7"
TEST_BUSCTL_DOMAINS="1 example.com false"
update-systemd-resolved-1.2.7/tests/08b_dns_ipv4_domain_and_search.sh 0000664 0000000 0000000 00000000743 13202063174 0025707 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun08"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 2.34.5.67"
foreign_option_3="dhcp-option DOMAIN example.co.uk"
foreign_option_4="dhcp-option DOMAIN-SEARCH example.co"
foreign_option_5="dhcp-option DOMAIN-SEARCH example.com"
TEST_TITLE="DNS IPv4 Servers, Domain, and Search"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="2 2 4 1 23 4 56 2 4 2 34 5 67"
TEST_BUSCTL_DOMAINS="3 example.co.uk false example.co false example.com false"
update-systemd-resolved-1.2.7/tests/08b_dns_ipv4_domain_search_and_route.sh 0000664 0000000 0000000 00000000750 13202063174 0027123 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun08"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 2.34.5.67"
foreign_option_3="dhcp-option DOMAIN example.co.uk"
foreign_option_4="dhcp-option DOMAIN-SEARCH example.co"
foreign_option_5="dhcp-option DOMAIN-ROUTE example.com"
TEST_TITLE="DNS IPv4 Servers, Domain, Search, and Route"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="2 2 4 1 23 4 56 2 4 2 34 5 67"
TEST_BUSCTL_DOMAINS="3 example.co.uk false example.co false example.com true"
update-systemd-resolved-1.2.7/tests/10_single_ipv6_dns_full.sh 0000664 0000000 0000000 00000000374 13202063174 0024425 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun10"
foreign_option_1="dhcp-option DNS 1234:5678:90ab:cdef:4321:8765:ba09:fedc"
TEST_TITLE="Single IPv6 DNS Server (Full)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 10 16 18 52 86 120 144 171 205 239 67 33 135 101 186 9 254 220"
update-systemd-resolved-1.2.7/tests/11_single_ipv6_dns_simple.sh 0000664 0000000 0000000 00000000363 13202063174 0024753 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun11"
foreign_option_1="dhcp-option DNS 1234:567:89:0:ab:cde:f123:4567"
TEST_TITLE="Single IPv6 DNS Server (Full, Simple)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 10 16 18 52 5 103 0 137 0 0 0 171 12 222 241 35 69 103"
update-systemd-resolved-1.2.7/tests/12_single_ipv6_dns_compact_1.sh 0000664 0000000 0000000 00000000367 13202063174 0025335 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun12"
foreign_option_1="dhcp-option DNS 1234:567:89:0:ab:cde:f123:4567"
TEST_TITLE="Single IPv6 DNS Server (Compact) (Part 1)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 10 16 18 52 5 103 0 137 0 0 0 171 12 222 241 35 69 103"
update-systemd-resolved-1.2.7/tests/13_single_ipv6_dns_compact_2.sh 0000664 0000000 0000000 00000000350 13202063174 0025327 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun13"
foreign_option_1="dhcp-option DNS 1234:567:89::ab:cdef"
TEST_TITLE="Single IPv6 DNS Server (Compact) (Part 2)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 10 16 18 52 5 103 0 137 0 0 0 0 0 0 0 171 205 239"
update-systemd-resolved-1.2.7/tests/14_single_ipv6_dns_compact_3.sh 0000664 0000000 0000000 00000000322 13202063174 0025330 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun14"
foreign_option_1="dhcp-option DNS 20a0::1"
TEST_TITLE="Single IPv6 DNS Server (Compact) (Part 3)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 10 16 32 160 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
update-systemd-resolved-1.2.7/tests/15_single_ipv6_dns_localhost.sh 0000664 0000000 0000000 00000000304 13202063174 0025451 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun15"
foreign_option_1="dhcp-option DNS ::1"
TEST_TITLE="Single IPv6 DNS Server (Localhost)"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="1 10 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
update-systemd-resolved-1.2.7/tests/16_dual_ipv6_single_ipv4.sh 0000664 0000000 0000000 00000000441 13202063174 0024507 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun16"
foreign_option_1="dhcp-option DNS 1234:567:89::ab:cdef"
foreign_option_2="dhcp-option DNS 1.23.4.56"
TEST_TITLE="Single IPv6 and Single IPv4 DNS Servers"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="2 10 16 18 52 5 103 0 137 0 0 0 0 0 0 0 171 205 239 2 4 1 23 4 56"
update-systemd-resolved-1.2.7/tests/17_single_ipv6_single_ipv4.sh 0000664 0000000 0000000 00000000565 13202063174 0025053 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun17"
foreign_option_1="dhcp-option DNS 1234:567:89::ab:cdef"
foreign_option_2="dhcp-option DNS 1.23.4.56"
foreign_option_3="dhcp-option DNS 20a0::1"
TEST_TITLE="Single IPv6 and Single IPv4 DNS Servers"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="3 10 16 18 52 5 103 0 137 0 0 0 0 0 0 0 171 205 239 2 4 1 23 4 56 10 16 32 160 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
update-systemd-resolved-1.2.7/tests/18_dns_ipv4_ipv6_domain_and_search.sh 0000664 0000000 0000000 00000001165 13202063174 0026511 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun18"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 2.34.56.7"
foreign_option_3="dhcp-option DNS 1234:567:89::ab:cdef"
foreign_option_4="dhcp-option DNS 1234:567:89::ba:cdef"
foreign_option_5="dhcp-option DOMAIN example.com"
foreign_option_6="dhcp-option DOMAIN-SEARCH example.co"
TEST_TITLE="DNS IPv4 and IPv6 Servers, plus Domain and Search"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DNS="4 2 4 1 23 4 56 2 4 2 34 56 7 10 16 18 52 5 103 0 137 0 0 0 0 0 0 0 171 205 239 10 16 18 52 5 103 0 137 0 0 0 0 0 0 0 186 205 239"
TEST_BUSCTL_DOMAINS="2 example.com false example.co false"
update-systemd-resolved-1.2.7/tests/19_dns_invalid_ipv6.sh 0000664 0000000 0000000 00000001250 13202063174 0023553 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun19"
# busctl should not be called for any test in here
TEST_BUSCTL_CALLED=0
# update-systemd-resolved should exit nonzero for all tests
EXPECT_FAILURE=1
declare -A test_attrs=(
["has more than one \`::'"]='1234::567::89:ab'
['too long']='1234:567:89:a:b:c:d:e:f'
['single 0 shortened']='1234::567:89:ab:c:de:f'
['zero-run in wrong location']='1234:0:0:567:89::ab'
['compressed run not longest zero-run']='1234:0:0:0:567::89'
['not maximally compressed']='2001:db8::0:1'
)
for test_title in "${!test_attrs[@]}"; do
TEST_TITLE="DNS IPv6 address $test_title"
foreign_option_1="dhcp-option DNS ${test_attrs["$test_title"]}"
runtest
done
update-systemd-resolved-1.2.7/tests/20_dnssec_only.sh 0000664 0000000 0000000 00000000755 13202063174 0022636 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun20"
TEST_BUSCTL_CALLED=1
declare -A test_options=(
['default']=''
['Default']=''
['true']='yes'
['True']='yes'
['yes']='yes'
['Yes']='yes'
['false']='no'
['False']='no'
['no']='no'
['No']='no'
['allow-downgrade']='allow-downgrade'
)
for test_option in "${!test_options[@]}"; do
TEST_TITLE="DNSSEC Set to $test_option"
TEST_BUSCTL_DNSSEC="${test_options["$test_option"]}"
foreign_option_1="dhcp-option DNSSEC $test_option"
runtest
done
update-systemd-resolved-1.2.7/tests/21_dnssec_invalid_options.sh 0000664 0000000 0000000 00000000430 13202063174 0025045 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun21"
TEST_BUSCTL_CALLED=0
EXPECT_FAILURE=1
declare -a test_invalids=(
'1'
'0'
'DOWNGRADE'
)
for test_option in "${test_invalids[@]}"; do
TEST_TITLE="DNSSEC Set to $test_option"
foreign_option_1="dhcp-option DNSSEC $test_option"
runtest
done
update-systemd-resolved-1.2.7/tests/22_dns_dnssec_domain_and_search.sh 0000664 0000000 0000000 00000001123 13202063174 0026127 0 ustar 00root root 0000000 0000000 script_type="up"
dev="tun22"
foreign_option_1="dhcp-option DNS 1.23.4.56"
foreign_option_2="dhcp-option DNS 1234:567:89::ab:cdef"
foreign_option_3="dhcp-option DOMAIN example.com"
foreign_option_4="dhcp-option DOMAIN-SEARCH example.org"
foreign_option_5="dhcp-option DOMAIN-ROUTE example.net"
foreign_option_6="dhcp-option DNSSEC yes"
TEST_TITLE="DNS, DNSSEC, Domain, Search, and Route"
TEST_BUSCTL_CALLED=1
TEST_BUSCTL_DOMAINS="3 example.com false example.org false example.net true"
TEST_BUSCTL_DNSSEC="yes"
TEST_BUSCTL_DNS="2 2 4 1 23 4 56 10 16 18 52 5 103 0 137 0 0 0 0 0 0 0 171 205 239"
update-systemd-resolved-1.2.7/update-systemd-resolved 0000775 0000000 0000000 00000026775 13202063174 0023050 0 ustar 00root root 0000000 0000000 #!/usr/bin/env bash
#
# OpenVPN helper to add DHCP information into systemd-resolved via DBus.
# Copyright (C) 2016, Jonathan Wright
#
# 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 .
# This script will parse DHCP options set via OpenVPN (dhcp-option) to update
# systemd-resolved directly via DBus, instead of updating /etc/resolv.conf. To
# install, set as the 'up' and 'down' script in your OpenVPN configuration file
# or via the command-line arguments, alongside setting the 'down-pre' option to
# run the 'down' script before the device is closed. For example:
# up /etc/openvpn/scripts/update-systemd-resolved
# down /etc/openvpn/scripts/update-systemd-resolved
# down-pre
# Define what needs to be called via DBus
DBUS_DEST="org.freedesktop.resolve1"
DBUS_NODE="/org/freedesktop/resolve1"
SCRIPT_NAME="${BASH_SOURCE[0]##*/}"
log() {
logger -s -t "$SCRIPT_NAME" "$@"
}
for level in emerg err warning info debug; do
printf -v functext -- '%s() { log -p user.%s -- "$@" ; }' "$level" "$level"
eval "$functext"
done
usage() {
err "${1:?${1}. }. Usage: ${SCRIPT_NAME} up|down device_name."
}
busctl_call() {
# Preserve busctl's exit status
busctl call "$DBUS_DEST" "$DBUS_NODE" "${DBUS_DEST}.Manager" "$@" || {
local -i status=$?
emerg "'busctl' exited with status $status"
return $status
}
}
get_link_info() {
dev="$1"
shift
link=''
link="$(ip link show dev "$dev")" || return $?
echo "$dev" "${link%%:*}"
}
dhcp_settings() {
for foreign_option in "${!foreign_option_@}"; do
foreign_option_value="${!foreign_option}"
[[ "$foreign_option_value" == *dhcp-option* ]] \
&& echo "${foreign_option_value#dhcp-option }"
done
}
up() {
local link="$1"
shift
local if_index="$1"
shift
info "Link '$link' coming up"
# Preset values for processing -- will be altered in the various process_*
# functions.
local -a dns_servers=() dns_domain=() dns_search=() dns_routed=()
local -i dns_server_count=0 dns_domain_count=0 dns_search_count=0 dns_routed_count=0
local dns_sec=""
while read -r setting; do
setting_type="${setting%% *}"
setting_value="${setting#* }"
process_setting_function="${setting_type,,}"
process_setting_function="process_${process_setting_function//-/_}"
if declare -f "$process_setting_function" &>/dev/null; then
"$process_setting_function" "$setting_value" || return $?
else
warning "Not a recognized DHCP setting: '${setting}'"
fi
done < <(dhcp_settings)
if [[ "${#dns_servers[*]}" -gt 0 ]]; then
busctl_params=("$if_index" "$dns_server_count" "${dns_servers[@]}")
info "SetLinkDNS(${busctl_params[*]})"
busctl_call SetLinkDNS 'ia(iay)' "${busctl_params[@]}" || return $?
fi
if [[ "${#dns_domain[*]}" -gt 0 \
|| "${#dns_search[*]}" -gt 0 \
|| "${#dns_routed[*]}" -gt 0 ]]; then
dns_count=$((dns_domain_count+dns_search_count+dns_routed_count))
busctl_params=("$if_index" "$dns_count")
if [[ "${#dns_domain[*]}" -gt 0 ]]; then
busctl_params+=("${dns_domain[@]}")
fi
if [[ "${#dns_search[*]}" -gt 0 ]]; then
busctl_params+=("${dns_search[@]}")
fi
if [[ "${#dns_routed[*]}" -gt 0 ]]; then
busctl_params+=("${dns_routed[@]}")
fi
info "SetLinkDomains(${busctl_params[*]})"
busctl_call SetLinkDomains 'ia(sb)' "${busctl_params[@]}" || return $?
fi
if [[ -n "${dns_sec}" ]]; then
if [[ "${dns_sec}" == "default" ]]; then
# We need to provide an empty string to use the default settings
info "SetLinkDNSSEC($if_index '')"
busctl_call SetLinkDNSSEC 'is' "$if_index" "" || return $?
else
info "SetLinkDNSSEC($if_index ${dns_sec})"
busctl_call SetLinkDNSSEC 'is' "$if_index" "${dns_sec}" || return $?
fi
fi
}
down() {
local link="$1"
shift
local if_index="$1"
shift
info "Link '$link' going down"
if [[ "$(whoami 2>/dev/null)" != "root" ]]; then
# Cleanly handle the priviledge dropped case by not calling RevertLink
info "Priviledges dropped in the client: Cannot call RevertLink."
else
busctl_call RevertLink i "$if_index"
fi
}
process_dns() {
address="$1"
shift
if looks_like_ipv6 "$address"; then
process_dns_ipv6 "$address" || return $?
elif looks_like_ipv4 "$address"; then
process_dns_ipv4 "$address" || return $?
else
err "Not a valid IPv6 or IPv4 address: '$address'"
return 1
fi
}
looks_like_ipv4() {
[[ -n "$1" ]] && {
local dots="${1//[^.]}"
(( ${#dots} == 3 ))
}
}
looks_like_ipv6() {
[[ -n "$1" ]] && {
local colons="${1//[^:]}"
(( ${#colons} >= 2 ))
}
}
process_dns_ipv4() {
local address="$1"
shift
info "Adding IPv4 DNS Server ${address}"
(( dns_server_count += 1 ))
dns_servers+=(2 4 ${address//./ })
}
# Enforces RFC 5952:
# 1. Don't shorten a single 0 field to '::'
# 2. Only longest run of zeros should be compressed
# 3. If there are multiple longest runs, the leftmost should be compressed
# 4. Address must be maximally compressed, so no all-zero runs next to '::'
#
# ...
#
# Thank goodness we don't have to handle port numbers, though :)
parse_ipv6() {
local raw_address="$1"
log_invalid_ipv6() {
local message="'$raw_address' is not a valid IPv6 address"
emerg "${message}: $*"
}
trap -- 'unset -f log_invalid_ipv6' RETURN
if [[ "$raw_address" == *::*::* ]]; then
log_invalid_ipv6 "address cannot contain more than one '::'"
return 1
elif [[ "$raw_address" =~ :0+:: ]] || [[ "$raw_address" =~ ::0+: ]]; then
log_invalid_ipv6 "address contains a 0-group adjacent to '::' and is not maximally shortened"
return 1
fi
local -i length=8
local -a raw_segments=()
IFS=$':' read -r -a raw_segments <<<"$raw_address"
local -i raw_length="${#raw_segments[@]}"
if (( raw_length > length )); then
log_invalid_ipv6 "expected ${length} segments, got ${raw_length}"
return 1
fi
# Store zero-runs keyed to their sizes, storing all non-zero segments prefixed
# with a token marking them as such.
local nonzero_prefix=$'!'
local -i zero_run_i=0 compressed_i=0
local -a tokenized_segments=()
local decimal_segment='' next_decimal_segment=''
for (( i = 0 ; i < raw_length ; i++ )); do
raw_segment="${raw_segments[i]}"
printf -v decimal_segment -- '%d' "0x${raw_segment:-0}"
# We're in the compressed group. The length of this run should be
# enough to bring the total number of segments to 8.
if [[ -z "$raw_segment" ]]; then
(( compressed_i = zero_run_i ))
# `+ 1' because the length of the current segment is counted in
# `raw_length'.
(( tokenized_segments[zero_run_i] = ((length - raw_length) + 1) ))
# If we have an address like `::1', skip processing the next group to
# avoid double-counting the zero-run, and increment the number of
# 0-groups to add since the second empty group is counted in
# `raw_length'.
if [[ -z "${raw_segments[i + 1]}" ]]; then
(( i++ ))
(( tokenized_segments[zero_run_i]++ ))
fi
(( zero_run_i++ ))
elif (( decimal_segment == 0 )); then
(( tokenized_segments[zero_run_i]++ ))
# The run is over if the next segment is not 0, so increment the
# tracking index.
printf -v next_decimal_segment -- '%d' "0x${raw_segments[i + 1]}"
(( next_decimal_segment != 0 )) && (( zero_run_i++ ))
else
# Prefix the raw segment with `nonzero_prefix' to mark this as a
# non-zero field.
tokenized_segments[zero_run_i]="${nonzero_prefix}${decimal_segment}"
(( zero_run_i++ ))
fi
done
if [[ "$raw_address" == *::* ]]; then
if (( ${#tokenized_segments[*]} == length )); then
log_invalid_ipv6 "single '0' fields should not be compressed"
return 1
else
local -i largest_run_i=0 largest_run=0
for (( i = 0 ; i < ${#tokenized_segments[@]}; i ++ )); do
# Skip groups that aren't zero-runs
[[ "${tokenized_segments[i]:0:1}" == "$nonzero_prefix" ]] && continue
if (( tokenized_segments[i] > largest_run )); then
(( largest_run_i = i ))
largest_run="${tokenized_segments[i]}"
fi
done
local -i compressed_run="${tokenized_segments[compressed_i]}"
if (( largest_run > compressed_run )); then
log_invalid_ipv6 "the compressed run of all-zero fields is smaller than the largest such run"
return 1
elif (( largest_run == compressed_run )) && (( largest_run_i < compressed_i )); then
log_invalid_ipv6 "only the leftmost largest run of all-zero fields should be compressed"
return 1
fi
fi
fi
for segment in "${tokenized_segments[@]}"; do
if [[ "${segment:0:1}" == "$nonzero_prefix" ]]; then
printf -- '%04x\n' "${segment#${nonzero_prefix}}"
else
for (( n = 0 ; n < segment ; n++ )); do
echo 0000
done
fi
done
}
process_dns_ipv6() {
local address="$1"
shift
info "Adding IPv6 DNS Server ${address}"
local -a segments=()
segments=($(parse_ipv6 "$address")) || return $?
# Add AF_INET6 and byte count
dns_servers+=(10 16)
for segment in "${segments[@]}"; do
dns_servers+=("$((16#${segment:0:2}))" "$((16#${segment:2:2}))")
done
(( dns_server_count += 1 ))
}
process_domain() {
local domain="$1"
shift
info "Setting DNS Domain ${domain}"
(( dns_domain_count = 1 ))
dns_domain=("${domain}" false)
}
process_adapter_domain_suffix() {
# This enables support for ADAPTER_DOMAIN_SUFFIX which is a Microsoft standard
# which works in the same way as DOMAIN to set the primary search domain on
# this specific link.
process_domain "$@"
}
process_domain_search() {
local domain="$1"
shift
info "Adding DNS Search Domain ${domain}"
(( dns_search_count += 1 ))
dns_search+=("${domain}" false)
}
process_domain_route() {
local domain="$1"
shift
info "Adding DNS Routed Domain ${domain}"
(( dns_routed_count += 1 ))
dns_routed+=("${domain}" true)
}
process_dnssec() {
local option="$1" setting=""
shift
case "${option,,}" in
yes|true)
setting="yes" ;;
no|false)
setting="no" ;;
default)
setting="default" ;;
allow-downgrade)
setting="allow-downgrade" ;;
*)
local message="'$option' is not a valid DNSSEC option"
emerg "${message}"
return 1 ;;
esac
info "Setting DNSSEC to ${setting}"
dns_sec="${setting}"
}
main() {
local script_type="$1"
shift
local dev="$1"
shift
if [[ -z "$script_type" ]]; then
usage 'No script type specified'
return 1
elif [[ -z "$dev" ]]; then
usage 'No device name specified'
return 1
elif ! declare -f "${script_type}" &>/dev/null; then
usage "Invalid script type: '${script_type}'"
return 1
else
if ! read -r link if_index _ < <(get_link_info "$dev"); then
usage "Invalid device name: '$dev'"
return 1
fi
"$script_type" "$link" "$if_index" "$@"
fi
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]] || [[ "$AUTOMATED_TESTING" == 1 ]]; then
set -o nounset
main "${script_type:-}" "${dev:-}" "$@"
fi