host-resolve: always return a new GSockAddr instance from resolve_hostname_to_sockaddr()

Instead of trying to _change_ an existing GSockAddr instance, always return
a new one. This makes the API easier to use, as well as makes
the implementation simpler. The original prototype probably dates back
to times when the afsocket driver stored ip/port parameters in an
existing GSockAddr instance and creating a new one was not good.

These times, we basically create the GSockAddr instance in the drivers
just to satisfy this requirement. So this patch simplifies call-sites
as well.

The only reason the input GSockAddr was used to determine the target address
family, but I introduced a new parameter for that.

Also the patch adds coverage for IPv6 in the testsuite.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
host-resolve: empty string in resolve_hostname_to_sockaddr() resolves to the wildcard address

As call-sites had to handle the default bind address on their own,
a better strategy is to handle a specific hostname to resolve to the
wildcard address that can be used to bind a service.

With IPv4, using the empty string or "0" literal works. With IPv6
unfortunately neither does. This patch adds this capability
to resolve_hostname_to_sockaddr(), either the empty string or a NULL
value returns the 0.0.0.0:0 or [::]:0 for IPv4 and IPv6 respectively.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
host-resolve: refactor resolve_hostname_to_sockaddr() to smaller functions

This patch splits this huge function to smaller ones while keeping
its functionality and prototype.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
dnscache: store and return the length of the cached hostname

This is the fastest path in resolving a hostname, thus it makes sense
to also cache the length of the hostname for speed reasons.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
dnscache: add dns_cache_store() wrappers

Instead of two boolean arguments, use two wrappers for the two different
use-cases of the DNS cache.

1) dns_cache_store_persistent() can be used to add persistent entries
populated from /etc/hosts.

2) dns_cache_store_dynamic() can be used to add the normal, expiring
entries.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
dnscache: don't allow NULL hostname

In old times, hostname == NULL indicated a negative match, however this
was superseeded by an explicit "positive" argument to
dns_cache_store()/lookup() functions. Get rid of the remainder of this
old cruft, also updating the unit test in the process.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
configure.ac: readd missing test on "modern" utmp database

There was an incompatible change in struct utmp and the test to validate
if we are using the old or the new format has been removed in the
past decade somehow.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
lib/compat: move getutent/endutent implementation from utils.c

An ancient version of the compat library was present (and now rediscovered)
in utils.c, move it to its new home.

The inet_aton() implementation is coming in a separate patch.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
afuser: factor utmp specific mess from the main body of the queue function()

Instead of doing a lot of ifdef trickery in the queue() function, use
separate static wrappers for that.

Also adds a locking around the use of the inherently non-thread-safe
utmp interface.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
afuser: remove alarm workaround

The Linux kernel used to have a bug that caused the afuser driver
to block even if O_NONBLOCK is set in the open flags. It was fixed
a long time ago, and alarm() is incompatible with multithreaded programs,
thus get rid off the alarm() call to detect blocked ttys.

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
compat: add strtok_r implementation for platforms that lack that
compat: add inet_aton() for compatibility with platforms that lack one

Signed-off-by: Viktor Juhasz <jviktor@balabit.hu>
Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>