Commit Graph

151 Commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Lennart Poettering db4a47e9fe coccinelle: O_NDELAY → O_NONBLOCK
Apparently O_NONBLOCK is the modern name used in most documentation and
for most cases in our sources. Let's hence replace the old alias
O_NDELAY and stick to O_NONBLOCK everywhere.
2018-01-24 11:09:29 +01:00
Lennart Poettering f1d34068ef tree-wide: add DEBUG_LOGGING macro that checks whether debug logging is on (#7645)
This makes things a bit easier to read I think, and also makes sure we
always use the _unlikely_ wrapper around it, which so far we used
sometimes and other times we didn't. Let's clean that up.
2017-12-15 11:09:00 +01:00
Lennart Poettering 0d53667334 tree-wide: use __fsetlocking() instead of fxyz_unlocked()
Let's replace usage of fputc_unlocked() and friends by __fsetlocking(f,
FSETLOCKING_BYCALLER). This turns off locking for the entire FILE*,
instead of doing individual per-call decision whether to use normal
calls or _unlocked() calls.

This has various benefits:

1. It's easier to read and easier not to forget

2. It's more comprehensive, as fprintf() and friends are covered too
   (as these functions have no _unlocked() counterpart)

3. Philosophically, it's a bit more correct, because it's more a
   property of the file handle really whether we ever pass it on to another
   thread, not of the operations we then apply to it.

This patch reworks all pieces of codes that so far used fxyz_unlocked()
calls to use __fsetlocking() instead. It also reworks all places that
use open_memstream(), i.e. use stdio FILE* for string manipulations.

Note that this in some way a revert of 4b61c87511.
2017-12-14 10:42:25 +01:00
Dmitry Rozhkov e7c1b0e456 resolved: resolve possible conflicts for DNS-SD RRs
It might happen that a DNS-SD service doesn't include local host's
name in its RR keys and still conflicts with a remote service.

In this case try to resolve the conflict by changing name for
this particular service.
2017-12-08 14:29:27 +02:00
Dmitry Rozhkov 6db6a4642e resolved: put DNS-SD records to mDNS-enabled zones. 2017-12-08 14:29:27 +02:00
Dmitry Rozhkov 6501dd31a7 resolved: add enablers for DNS-SD
Introduce network services loaded from .dnssd files that
can be used for server-side DNS-SD implementation in systemd-resolved.
2017-12-08 14:29:27 +02:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Lennart Poettering cf84484a56 resolved: include DNS server feature level info in SIGUSR1 status dump
let's make the status dump more useful for tracking down server issues.
2017-10-05 17:02:25 +02:00
Lennart Poettering d55b0463b2 resolved: add support for explicitly forgetting everything we learnt about DNS server feature levels
This adds "systemd-resolve --reset-server-features" for explicitly
forgetting what we learnt. This might be useful for debugging
purposes, and to force systemd-resolved to restart its learning logic
for all DNS servers.
2017-10-05 16:56:50 +02:00
Lennart Poettering 59c0fd0e17 resolved: automatically forget all learnt DNS server information when the network configuration changes
When the network configuration changes we should relearn everything
there is to know about the configured DNS servers, because we might talk
to the same addresses, but there might be different servers behind them.
2017-10-05 16:22:22 +02:00
Lennart Poettering 5102765695 resolved: rework how we handle truncation in the stub resolver
When we a reply message gets longer than the client supports we need to
truncate the response and set the TC bit, and we already do that.
However, we are not supposed to send incomplete RRs in that case, but
instead truncate right at a record boundary. Do that.

This fixes the "Message parser reports malformed message packet."
warning the venerable "host" tool outputs when a very large response is
requested.

See: #6520
2017-10-05 12:22:43 +02:00
Lennart Poettering 4aa1d31c89 Merge pull request #6974 from keszybz/clean-up-defines
Clean up define definitions
2017-10-04 19:25:30 +02:00
Yu Watanabe 4c70109600 tree-wide: use IN_SET macro (#6977) 2017-10-04 16:01:32 +02:00
Zbigniew Jędrzejewski-Szmek 349cc4a507 build-sys: use #if Y instead of #ifdef Y everywhere
The advantage is that is the name is mispellt, cpp will warn us.

$ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/"
$ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;'
$ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g'
$ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g'
+ manual changes to meson.build

squash! build-sys: use #if Y instead of #ifdef Y everywhere

v2:
- fix incorrect setting of HAVE_LIBIDN2
2017-10-04 12:09:29 +02:00
Zbigniew Jędrzejewski-Szmek 5ce497b59a resolved: treat failure to parse config as non-fatal
Fixes #6014.
2017-07-05 00:16:50 -04:00
Zbigniew Jędrzejewski-Szmek 87057e244b resolved: support libidn2 in addition to libidn
libidn2 2.0.0 supports IDNA2008, in contrast to libidn which supports IDNA2003.

https://bugzilla.redhat.com/show_bug.cgi?id=1449145
From that bug report:

Internationalized domain names exist for quite some time (IDNA2003), although
the protocols describing them have evolved in an incompatible way (IDNA2008).
These incompatibilities will prevent applications written for IDNA2003 to
access certain problematic domain names defined with IDNA2008, e.g., faß.de is
translated to domain xn--fa-hia.de with IDNA2008, while in IDNA2003 it is
translated to fass.de domain. That not only causes incompatibility problems,
but may be used as an attack vector to redirect users to different web sites.

v2:
- keep libidn support
- require libidn2 >= 2.0.0
v3:
- keep dns_name_apply_idna caller dumb, and keep the #ifdefs inside of the
  function.
- use both ±IDN and ±IDN2 in the version string
2017-05-11 14:25:01 -04:00
Yu Watanabe c6a8f6f66d resolved: do not start LLMNR or mDNS stack when no network enables them
When no network enables LLMNR or mDNS, it is not necessary to create
LLMNR or mDNS related sockets. So, let's create them only when
LLMNR- or mDNS-enabled network becomes active or at least one network
enables `LLMNR=` or `MulticastDNS=` options.
2017-03-21 13:14:04 +09:00
Lennart Poettering a25b0dc82d resolved: default to the compile-time fallback hostname
This changes resolved to use the compile-time fallback hostname the
configured one is not set. Note that if the local hostname is set to
"localhost" then we'll instead default to "linux" here, as for
mDNS/LLMNR exposing "localhost" is actively dangerous.
2017-02-17 10:19:26 +01:00
Zbigniew Jędrzejewski-Szmek 01c901e257 Merge pull request #4832 from rojkov/mdns 2017-02-12 15:38:51 -05:00
Lennart Poettering e96de0ce47 resolved: also synthesize records for the full local hostname
Previously, we'd only synthesize RRs for the LLMNR and mDNS versions of
the hostnames (i.e. the first label of the kernel hostname, as well as
the first label of the kernel hostname suffixed with .local). With this
change, we also synthesize an RR for the full hostname, which is
relevant in case it has more than one label.

Fixes: #5041
2017-02-09 16:13:07 +01:00
Lennart Poettering 49bfc8774b fs-util: unify code we use to check if dirent's d_name is "." or ".."
We use different idioms at different places. Let's replace this is the
one true new idiom, that is even a bit faster...
2017-02-02 00:06:18 +01:00
Dmitry Rozhkov 7bef8e8e54 resolved: actually enable mDNS support
Signed-off-by: Dmitry Rozhkov <dmitry.rozhkov@linux.intel.com>
2017-01-19 11:51:21 +02:00
Dmitry Rozhkov 400cb36ec6 resolved: populate mDNS scopes' zones with RRs for the host
Signed-off-by: Dmitry Rozhkov <dmitry.rozhkov@linux.intel.com>
2017-01-19 11:51:21 +02:00
Zbigniew Jędrzejewski-Szmek 6b430fdb7c tree-wide: use mfree more 2016-10-16 23:35:39 -04:00
David Michael 1ae4329575 resolved: add an option to control the DNS stub listener 2016-10-07 12:14:38 -07:00
Martin Pitt 94363cbbf3 resolved: add test for route-only domain filtering (#3609)
With commit 6f7da49d00 route-only domains do not get put into resolv.conf's
"search" list any more. Add a comment about the tri-state, to clarify its
semantics and why we are passing a bool parameter into an int type. Also add a
test case for it.
2016-06-28 18:18:27 +02:00
Martin Pitt ceeddf79b8 resolved: add option to disable caching (#3592)
In some cases, caching DNS results locally is not desirable, a it makes DNS
cache poisoning attacks a tad easier and also allows users on the system to
determine whether or not a particular domain got visited by another user. Thus
provide a new "Cache" resolved.conf option to disable it.
2016-06-24 07:54:28 +02:00
Lennart Poettering b30bf55d5c resolved: respond to local resolver requests on 127.0.0.53:53
In order to improve compatibility with local clients that speak DNS directly
(and do not use NSS or our bus API) listen locally on 127.0.0.53:53 and process
any queries made that way.

Note that resolved does not implement a full DNS server on this port, but
simply enough to allow normal, local clients to resolve RRs through resolved.
Specifically it does not implement queries without the RD bit set (these are
requests where recursive lookups are explicitly disabled), and neither queries
with DNSSEC DO set in combination with DNSSEC CD (i.e. DNSSEC lookups with
validation turned off). It also refuses zone transfers and obsolete RR types.
All lookups done this way will be rejected with a clean error code, so that the
client side can repeat the query with a reduced feature set.

The code will set the DNSSEC AD flag however, depending on whether the data
resolved has been validated (or comes from a local, trusted source).

Lookups made via this mechanisms are propagated to LLMNR and mDNS as necessary,
but this is only partially useful as DNS packets cannot carry IP scope data
(i.e. the ifindex), and hence link-local addresses returned cannot be used
properly (and given that LLMNR/mDNS are mostly about link-local communication
this is quite a limitation). Also, given that DNS tends to use IDNA for
non-ASCII names, while LLMNR/mDNS uses UTF-8 lookups cannot be mapped 1:1.

In general this should improve compatibility with clients bypassing NSS but
it is highly recommended for clients to instead use NSS or our native bus API.

This patch also beefs up the DnsStream logic, as it reuses the code for local
TCP listening. DnsStream now provides proper reference counting for its
objects.

In order to avoid feedback loops resolved will no silently ignore 127.0.0.53
specified as DNS server when reading configuration.

resolved listens on 127.0.0.53:53 instead of 127.0.0.1:53 in order to leave
the latter free for local, external DNS servers or forwarders.

This also changes the "etc.conf" tmpfiles snippet to create a symlink from
/etc/resolv.conf to /usr/lib/systemd/resolv.conf by default, thus making this
stub the default mode of operation if /etc is not populated.
2016-06-21 14:15:23 +02:00
Lennart Poettering 943ef07ce0 resolved: make sure DNS configuration pushed in by the user stays around on restarts
Let's make sure that all settings pushed in stay around when systemd-resolved
is restarted.
2016-06-21 13:20:48 +02:00
Lennart Poettering 6f7da49d00 resolved: make sure that route-only domains are never added to /etc/resolv.conf
After all, /etc/resolv.conf doesn't know the concept of "route-only domains",
hence the domains should really not appear there.
2016-06-21 13:20:47 +02:00
Zbigniew Jędrzejewski-Szmek aac57b3539 resolved: use single message for both dbus and signal calls (#3515)
Follow-up for #3502.
2016-06-13 16:24:48 +02:00
Lennart Poettering ba35662fbd resolved: also add a way to flush all caches via the bus
And expose it in "resolve-tool --flush-caches".
2016-06-10 23:26:53 +02:00
Lennart Poettering bc81447ea5 resolved: flush all caches if SIGUSR2 is received 2016-06-10 23:26:53 +02:00
Lennart Poettering 7207052d25 resolved: also rewrite private /etc/resolv.conf when configuration is changed via bus calls
This also moves log message generation into manager_write_resolv_conf(), so
that it is shorter to invoke the function, given that we have to invoke it at a
couple of additional places now.

Fixes: #3225
2016-06-06 19:17:38 +02:00
Lennart Poettering 2817157bb7 resolved: support IPv6 DNS servers on the local link
Make sure we can parse DNS server addresses that use the "zone id" syntax for
local link addresses, i.e. "fe80::c256:27ff:febb:12f%wlp3s0", when reading
/etc/resolv.conf.

Also make sure we spit this out correctly again when writing /etc/resolv.conf
and via the bus.

Fixes: #3359
2016-06-06 19:17:38 +02:00
Evgeny Vereshchagin f134289ac5 resolved: don't stop handle messages after receiving a zero length UDP packet (#3323)
Fixes:

-bash-4.3# ss --udp -l -p
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port
UNCONN     0      0          *:5355                     *:* users:(("systemd-resolve",pid=601,fd=12))
UNCONN     0      0         :::5355                    :::* users:(("systemd-resolve",pid=601,fd=14))

-bash-4.3# nping --udp -p 5355 --data-length 0 -c 1 localhost

-bash-4.3# journalctl -u systemd-resolved -b --no-hostname
...
May 21 14:59:22 systemd-resolved[601]: Event source llmnr-ipv4-udp (type io) returned error, disabling: Input/output error
...

-bash-4.3# nping --udp -p 5355 --data-length 1000 -c 1 localhost

-bash-4.3# ss --udp -l
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port
UNCONN     2304   0          *:5355                     *:*
UNCONN     0      0         :::5355                    :::*
2016-05-23 10:19:14 +02:00
Lennart Poettering 5031c4e21b Merge pull request #2640 from keszybz/dnssec-work-3
resolve: dumping of binary packets
2016-02-17 12:45:31 +01:00
Zbigniew Jędrzejewski-Szmek 202b76ae1a Use provided buffer in dns_resource_key_to_string
When the buffer is allocated on the stack we do not have to check for
failure everywhere. This is especially useful in debug statements, because
we can put dns_resource_key_to_string() call in the debug statement, and
we do not need a seperate if (log_level >= LOG_DEBUG) for the conversion.

dns_resource_key_to_string() is changed not to provide any whitespace
padding. Most callers were stripping the whitespace with strstrip(),
and it did not look to well anyway. systemd-resolve output is not column
aligned anymore.

The result of the conversion is not stored in DnsTransaction object
anymore. It is used only for debugging, so it seems fine to generate it
when needed.

Various debug statements are extended to provide more information.
2016-02-16 19:55:51 -05:00
Zbigniew Jędrzejewski-Szmek 2c45295e47 Merge pull request #2623 from poettering/networkd-fixes
Networkd, resolved, build-sys fixes
2016-02-16 18:36:42 -05:00
Lennart Poettering 61ecb465b1 resolved: turn on DNSSEC by default, unless configured otherwise
Let's make sure DNSSEC gets more testing, by defaulting DNSSEC to
"allow-downgrade" mode. Since distros should probably not ship DNSSEC enabled
by default add a configure switch to disable this again.

DNSSEC in "allow-downgrade" mode should mostly work without affecting user
experience. There's one exception: some captive portal systems rewrite DNS in
order to redirect HTTP traffic to the captive portal. If these systems
implement DNS servers that are otherwise DNSSEC-capable (which in fact is
pretty unlikely, but still...), then this will result in the captive portal
being inaccessible. To fix this support in NetworkManager (or any other network
management solution that does captive portal detection) is required, which
simply turns off DNSSEC during the captive portal detection, and resets it back
to the default (i.e. on) after captive portal authentication is complete.
2016-02-16 15:22:05 +01:00
Lennart Poettering 4edc2c9b6b networkd: FIONREAD is not reliable on some sockets
Fixes: #2457
2016-02-16 13:06:55 +01:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Lennart Poettering 011696f762 resolved: rework what ResolveHostname() with family == AF_UNSPEC means
Previously, if a hostanem is resolved with AF_UNSPEC specified, this would be used as indication to resolve both an
AF_INET and an AF_INET6 address. With this change this logic is altered: an AF_INET address is only resolved if there's
actually a routable IPv4 address on the specific interface, and similar an AF_INET6 address is only resolved if there's
a routable IPv6 address. With this in place, it's ensured that the returned data is actually connectable by
applications. This logic mimics glibc's resolver behaviour.

Note that if the client asks explicitly for AF_INET or AF_INET6 it will get what it asked for.

This also simplifies the logic how it is determined whether a specific lookup shall take place on a scope.
Specifically, the checks with dns_scope_good_key() are now moved out of the transaction code and into the query code,
so that we don't even create a transaction object on a specific scope if we cannot execute the resolution on it anyway.
2016-02-01 22:18:15 +01:00
Zbigniew Jędrzejewski-Szmek 77abf3c115 resolved: emit full path to file we failed to write
Otherwise it's unclear if it's /etc/resolv.conf or some
other file that is meant.
2016-01-28 18:34:38 -05:00
Lennart Poettering 2d8950384f resolved: prorize rtnl and sd-network event sources
Lets make sure we always take notice of network changes before answering client requests.

This way, calls like SetLinkDNS() become race-free as the specified interface index is guarantee to have been processed
if it exists before we make changes to it.
2016-01-26 14:42:04 +01:00
Lennart Poettering dd0bc0f141 resolved: synthesize RRs for data from /etc/hosts
This way the difference between lookups via NSS and our native bus API should become minimal.
2016-01-25 17:19:19 +01:00
Lennart Poettering 59c5b5974d resolved: log each time we increase the DNSSEC verdict counters
Also, don't consider RRs that aren't primary to the lookups we do as relevant to the lookups.
2016-01-25 17:19:19 +01:00
Lennart Poettering c69fa7e3c4 resolved: rework DNSSECSupported property
Not only report whether the server actually supports DNSSEC, but also first check whether DNSSEC is actually enabled
for it in our local configuration.

Also, export a per-link DNSSECSupported property in addition to the existing manager-wide property.
2016-01-19 21:56:54 +01:00
Lennart Poettering aa4a9deb7d resolved: set a description on all our event sources 2016-01-11 19:39:59 +01:00