2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <nss.h>
|
2014-07-16 03:31:30 +02:00
|
|
|
#include <stdlib.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
#include "sd-bus.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2014-12-10 19:00:46 +01:00
|
|
|
#include "bus-common-errors.h"
|
2020-06-28 15:59:37 +02:00
|
|
|
#include "bus-locator.h"
|
2019-03-14 12:24:39 +01:00
|
|
|
#include "errno-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "in-addr-util.h"
|
2014-07-16 03:31:30 +02:00
|
|
|
#include "macro.h"
|
|
|
|
#include "nss-util.h"
|
2018-01-23 01:53:31 +01:00
|
|
|
#include "resolved-def.h"
|
2016-01-26 22:34:46 +01:00
|
|
|
#include "signal-util.h"
|
2019-03-14 12:24:39 +01:00
|
|
|
#include "string-util.h"
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
NSS_GETHOSTBYNAME_PROTOTYPES(resolve);
|
|
|
|
NSS_GETHOSTBYADDR_PROTOTYPES(resolve);
|
|
|
|
|
2014-08-01 01:55:15 +02:00
|
|
|
static bool bus_error_shall_fallback(sd_bus_error *e) {
|
|
|
|
return sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
|
|
|
|
sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER) ||
|
|
|
|
sd_bus_error_has_name(e, SD_BUS_ERROR_NO_REPLY) ||
|
2019-04-11 11:06:40 +02:00
|
|
|
sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED) ||
|
|
|
|
sd_bus_error_has_name(e, SD_BUS_ERROR_DISCONNECTED) ||
|
|
|
|
sd_bus_error_has_name(e, SD_BUS_ERROR_TIMEOUT);
|
2014-08-01 01:55:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 16:09:30 +02:00
|
|
|
static int count_addresses(sd_bus_message *m, int af, const char **canonical) {
|
2015-08-17 23:54:08 +02:00
|
|
|
int c = 0, r;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
assert(m);
|
2014-07-16 22:50:41 +02:00
|
|
|
assert(canonical);
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
r = sd_bus_message_enter_container(m, 'a', "(iiay)");
|
2014-07-16 22:50:41 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
while ((r = sd_bus_message_enter_container(m, 'r', "iiay")) > 0) {
|
|
|
|
int family, ifindex;
|
2014-08-14 01:00:15 +02:00
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
assert_cc(sizeof(int32_t) == sizeof(int));
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
r = sd_bus_message_read(m, "ii", &ifindex, &family);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-08-14 01:00:15 +02:00
|
|
|
r = sd_bus_message_skip(m, "ay");
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_exit_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (af != AF_UNSPEC && family != af)
|
|
|
|
continue;
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
c++;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
r = sd_bus_message_exit_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_read(m, "s", canonical);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
r = sd_bus_message_rewind(m, true);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return c;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 19:40:48 +01:00
|
|
|
static uint32_t ifindex_to_scopeid(int family, const void *a, int ifindex) {
|
|
|
|
struct in6_addr in6;
|
|
|
|
|
|
|
|
if (family != AF_INET6)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Some apps can't deal with the scope ID attached to non-link-local addresses. Hence, let's suppress that. */
|
|
|
|
|
2017-02-16 11:00:49 +01:00
|
|
|
assert(sizeof(in6) == FAMILY_ADDRESS_SIZE(AF_INET6));
|
2017-02-08 19:40:48 +01:00
|
|
|
memcpy(&in6, a, sizeof(struct in6_addr));
|
|
|
|
|
|
|
|
return IN6_IS_ADDR_LINKLOCAL(&in6) ? ifindex : 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 15:37:03 +02:00
|
|
|
static bool avoid_deadlock(void) {
|
|
|
|
|
|
|
|
/* Check whether this lookup might have a chance of deadlocking because we are called from the service manager
|
|
|
|
* code activating systemd-resolved.service. After all, we shouldn't synchronously do lookups to
|
|
|
|
* systemd-resolved if we are required to finish before it can be started. This of course won't detect all
|
|
|
|
* possible dead locks of this kind, but it should work for the most obvious cases. */
|
|
|
|
|
|
|
|
if (geteuid() != 0) /* Ignore the env vars unless we are privileged. */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return streq_ptr(getenv("SYSTEMD_ACTIVATION_UNIT"), "systemd-resolved.service") &&
|
|
|
|
streq_ptr(getenv("SYSTEMD_ACTIVATION_SCOPE"), "system");
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
enum nss_status _nss_resolve_gethostbyname4_r(
|
|
|
|
const char *name,
|
|
|
|
struct gaih_addrtuple **pat,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp) {
|
|
|
|
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2014-07-16 03:31:30 +02:00
|
|
|
struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2014-07-16 22:50:41 +02:00
|
|
|
const char *canonical = NULL;
|
2014-07-16 03:31:30 +02:00
|
|
|
size_t l, ms, idx;
|
|
|
|
char *r_name;
|
2015-08-17 23:54:08 +02:00
|
|
|
int c, r, i = 0;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
PROTECT_ERRNO;
|
2016-01-26 22:34:46 +01:00
|
|
|
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
assert(name);
|
|
|
|
assert(pat);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
|
|
|
|
2018-07-04 15:37:03 +02:00
|
|
|
if (avoid_deadlock()) {
|
|
|
|
r = -EDEADLK;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
r = sd_bus_open_system(&bus);
|
|
|
|
if (r < 0)
|
2017-05-12 14:31:46 +02:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2020-05-03 08:23:50 +02:00
|
|
|
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname");
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_set_auto_start(req, false);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2014-08-14 01:00:15 +02:00
|
|
|
r = sd_bus_message_append(req, "isit", 0, name, AF_UNSPEC, (uint64_t) 0);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2018-01-23 01:53:31 +01:00
|
|
|
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0) {
|
2019-04-11 11:00:22 +02:00
|
|
|
if (!bus_error_shall_fallback(&error))
|
2018-07-15 16:00:00 +02:00
|
|
|
goto not_found;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2017-05-12 14:31:46 +02:00
|
|
|
/* Return NSS_STATUS_UNAVAIL when communication with systemd-resolved fails,
|
|
|
|
allowing falling back to other nss modules. Treat all other error conditions as
|
|
|
|
NOTFOUND. This includes DNSSEC errors and suchlike. (We don't use UNAVAIL in this
|
2019-04-27 02:22:40 +02:00
|
|
|
case so that the nsswitch.conf configuration can distinguish such executed but
|
2017-05-12 14:31:46 +02:00
|
|
|
negative replies from complete failure to talk to resolved). */
|
2016-01-26 23:40:59 +01:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
c = count_addresses(reply, AF_UNSPEC, &canonical);
|
|
|
|
if (c < 0) {
|
|
|
|
r = c;
|
2014-07-16 03:31:30 +02:00
|
|
|
goto fail;
|
2014-07-16 22:50:41 +02:00
|
|
|
}
|
2018-07-15 16:00:00 +02:00
|
|
|
if (c == 0)
|
|
|
|
goto not_found;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
if (isempty(canonical))
|
|
|
|
canonical = name;
|
|
|
|
|
|
|
|
l = strlen(canonical);
|
2014-07-16 03:31:30 +02:00
|
|
|
ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
|
|
|
|
if (buflen < ms) {
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = ERANGE;
|
2017-02-16 10:52:04 +01:00
|
|
|
*h_errnop = NETDB_INTERNAL;
|
2014-07-16 03:31:30 +02:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First, append name */
|
|
|
|
r_name = buffer;
|
2014-07-16 22:50:41 +02:00
|
|
|
memcpy(r_name, canonical, l+1);
|
2014-07-16 03:31:30 +02:00
|
|
|
idx = ALIGN(l+1);
|
|
|
|
|
|
|
|
/* Second, append addresses */
|
|
|
|
r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
|
2014-07-16 22:50:41 +02:00
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
|
2014-07-16 22:50:41 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
|
|
|
|
int family, ifindex;
|
2014-07-16 03:31:30 +02:00
|
|
|
const void *a;
|
|
|
|
size_t sz;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
assert_cc(sizeof(int32_t) == sizeof(int));
|
|
|
|
|
|
|
|
r = sd_bus_message_read(reply, "ii", &ifindex, &family);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
if (ifindex < 0) {
|
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
r = sd_bus_message_read_array(reply, 'y', &a, &sz);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_exit_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!IN_SET(family, AF_INET, AF_INET6))
|
|
|
|
continue;
|
|
|
|
|
2014-07-18 02:36:10 +02:00
|
|
|
if (sz != FAMILY_ADDRESS_SIZE(family)) {
|
2014-07-16 03:31:30 +02:00
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_tuple = (struct gaih_addrtuple*) (buffer + idx);
|
|
|
|
r_tuple->next = i == c-1 ? NULL : (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
|
|
|
|
r_tuple->name = r_name;
|
|
|
|
r_tuple->family = family;
|
2017-02-08 19:40:48 +01:00
|
|
|
r_tuple->scopeid = ifindex_to_scopeid(family, a, ifindex);
|
2014-07-16 03:31:30 +02:00
|
|
|
memcpy(r_tuple->addr, a, sz);
|
|
|
|
|
|
|
|
idx += ALIGN(sizeof(struct gaih_addrtuple));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
assert(i == c);
|
2014-07-16 03:31:30 +02:00
|
|
|
assert(idx == ms);
|
|
|
|
|
|
|
|
if (*pat)
|
|
|
|
**pat = *r_tuple_first;
|
|
|
|
else
|
|
|
|
*pat = r_tuple_first;
|
|
|
|
|
|
|
|
if (ttlp)
|
|
|
|
*ttlp = 0;
|
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
/* Explicitly reset both *h_errnop and h_errno to work around
|
|
|
|
* https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
|
2014-08-01 16:01:12 +02:00
|
|
|
*h_errnop = NETDB_SUCCESS;
|
|
|
|
h_errno = 0;
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
return NSS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
fail:
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = -r;
|
2016-01-26 23:40:59 +01:00
|
|
|
*h_errnop = NO_RECOVERY;
|
2019-04-10 21:26:46 +02:00
|
|
|
return NSS_STATUS_UNAVAIL;
|
2018-07-15 16:00:00 +02:00
|
|
|
|
|
|
|
not_found:
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum nss_status _nss_resolve_gethostbyname3_r(
|
|
|
|
const char *name,
|
|
|
|
int af,
|
|
|
|
struct hostent *result,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp,
|
|
|
|
char **canonp) {
|
|
|
|
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2014-07-16 03:31:30 +02:00
|
|
|
char *r_name, *r_aliases, *r_addr, *r_addr_list;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2014-07-16 03:31:30 +02:00
|
|
|
size_t l, idx, ms, alen;
|
2014-07-16 22:50:41 +02:00
|
|
|
const char *canonical;
|
2015-08-17 23:54:08 +02:00
|
|
|
int c, r, i = 0;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
PROTECT_ERRNO;
|
2016-01-26 22:34:46 +01:00
|
|
|
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
assert(name);
|
|
|
|
assert(result);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
|
|
|
|
|
|
|
if (af == AF_UNSPEC)
|
|
|
|
af = AF_INET;
|
|
|
|
|
2017-10-04 16:01:32 +02:00
|
|
|
if (!IN_SET(af, AF_INET, AF_INET6)) {
|
2014-07-16 03:31:30 +02:00
|
|
|
r = -EAFNOSUPPORT;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-07-04 15:37:03 +02:00
|
|
|
if (avoid_deadlock()) {
|
|
|
|
r = -EDEADLK;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
r = sd_bus_open_system(&bus);
|
|
|
|
if (r < 0)
|
2017-05-12 14:31:46 +02:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2020-05-03 08:23:50 +02:00
|
|
|
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname");
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_set_auto_start(req, false);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2014-08-14 01:00:15 +02:00
|
|
|
r = sd_bus_message_append(req, "isit", 0, name, af, (uint64_t) 0);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2018-01-23 01:53:31 +01:00
|
|
|
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0) {
|
2019-04-11 11:00:22 +02:00
|
|
|
if (!bus_error_shall_fallback(&error))
|
2018-07-15 16:00:00 +02:00
|
|
|
goto not_found;
|
2014-08-01 01:55:15 +02:00
|
|
|
|
2016-01-26 23:40:59 +01:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
c = count_addresses(reply, af, &canonical);
|
|
|
|
if (c < 0) {
|
|
|
|
r = c;
|
2014-07-16 03:31:30 +02:00
|
|
|
goto fail;
|
2014-07-16 22:50:41 +02:00
|
|
|
}
|
2018-07-15 16:00:00 +02:00
|
|
|
if (c == 0)
|
|
|
|
goto not_found;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
if (isempty(canonical))
|
|
|
|
canonical = name;
|
|
|
|
|
2014-07-18 02:36:10 +02:00
|
|
|
alen = FAMILY_ADDRESS_SIZE(af);
|
2014-07-16 22:50:41 +02:00
|
|
|
l = strlen(canonical);
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2014-09-18 23:55:46 +02:00
|
|
|
ms = ALIGN(l+1) + c * ALIGN(alen) + (c+2) * sizeof(char*);
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
if (buflen < ms) {
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = ERANGE;
|
2017-02-16 10:52:04 +01:00
|
|
|
*h_errnop = NETDB_INTERNAL;
|
2014-07-16 03:31:30 +02:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First, append name */
|
|
|
|
r_name = buffer;
|
2014-07-16 22:50:41 +02:00
|
|
|
memcpy(r_name, canonical, l+1);
|
2014-07-16 03:31:30 +02:00
|
|
|
idx = ALIGN(l+1);
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
/* Second, create empty aliases array */
|
2014-07-16 03:31:30 +02:00
|
|
|
r_aliases = buffer + idx;
|
|
|
|
((char**) r_aliases)[0] = NULL;
|
|
|
|
idx += sizeof(char*);
|
|
|
|
|
|
|
|
/* Third, append addresses */
|
|
|
|
r_addr = buffer + idx;
|
2014-07-16 22:50:41 +02:00
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
|
2014-08-14 01:00:15 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
|
|
|
|
int ifindex, family;
|
2014-07-16 03:31:30 +02:00
|
|
|
const void *a;
|
|
|
|
size_t sz;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
r = sd_bus_message_read(reply, "ii", &ifindex, &family);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
if (ifindex < 0) {
|
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
r = sd_bus_message_read_array(reply, 'y', &a, &sz);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_exit_container(reply);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (family != af)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sz != alen) {
|
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(r_addr + i*ALIGN(alen), a, alen);
|
|
|
|
i++;
|
|
|
|
}
|
2014-07-16 22:50:41 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
assert(i == c);
|
|
|
|
idx += c * ALIGN(alen);
|
|
|
|
|
2014-07-16 22:50:41 +02:00
|
|
|
/* Fourth, append address pointer array */
|
2014-07-16 03:31:30 +02:00
|
|
|
r_addr_list = buffer + idx;
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
|
|
|
|
|
|
|
|
((char**) r_addr_list)[i] = NULL;
|
|
|
|
idx += (c+1) * sizeof(char*);
|
|
|
|
|
|
|
|
assert(idx == ms);
|
|
|
|
|
|
|
|
result->h_name = r_name;
|
|
|
|
result->h_aliases = (char**) r_aliases;
|
|
|
|
result->h_addrtype = af;
|
|
|
|
result->h_length = alen;
|
|
|
|
result->h_addr_list = (char**) r_addr_list;
|
|
|
|
|
|
|
|
if (ttlp)
|
|
|
|
*ttlp = 0;
|
|
|
|
|
|
|
|
if (canonp)
|
|
|
|
*canonp = r_name;
|
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
/* Explicitly reset both *h_errnop and h_errno to work around
|
|
|
|
* https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
|
|
|
|
*h_errnop = NETDB_SUCCESS;
|
|
|
|
h_errno = 0;
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
return NSS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
fail:
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = -r;
|
2016-01-26 23:40:59 +01:00
|
|
|
*h_errnop = NO_RECOVERY;
|
2019-04-10 21:26:46 +02:00
|
|
|
return NSS_STATUS_UNAVAIL;
|
2018-07-15 16:00:00 +02:00
|
|
|
|
|
|
|
not_found:
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum nss_status _nss_resolve_gethostbyaddr2_r(
|
|
|
|
const void* addr, socklen_t len,
|
|
|
|
int af,
|
|
|
|
struct hostent *result,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp) {
|
|
|
|
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2014-07-16 03:31:30 +02:00
|
|
|
char *r_name, *r_aliases, *r_addr, *r_addr_list;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2014-07-16 03:31:30 +02:00
|
|
|
unsigned c = 0, i = 0;
|
|
|
|
size_t ms = 0, idx;
|
|
|
|
const char *n;
|
2014-08-14 01:00:15 +02:00
|
|
|
int r, ifindex;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
PROTECT_ERRNO;
|
2016-01-26 22:34:46 +01:00
|
|
|
BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
assert(addr);
|
|
|
|
assert(result);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
|
|
|
|
|
|
|
if (!IN_SET(af, AF_INET, AF_INET6)) {
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = EAFNOSUPPORT;
|
2014-07-16 03:31:30 +02:00
|
|
|
*h_errnop = NO_DATA;
|
|
|
|
return NSS_STATUS_UNAVAIL;
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:36:10 +02:00
|
|
|
if (len != FAMILY_ADDRESS_SIZE(af)) {
|
2019-04-10 21:57:44 +02:00
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 15:37:03 +02:00
|
|
|
if (avoid_deadlock()) {
|
|
|
|
r = -EDEADLK;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
r = sd_bus_open_system(&bus);
|
|
|
|
if (r < 0)
|
2017-05-12 14:31:46 +02:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2020-05-03 08:23:50 +02:00
|
|
|
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveAddress");
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_set_auto_start(req, false);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2014-08-14 01:00:15 +02:00
|
|
|
r = sd_bus_message_append(req, "ii", 0, af);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_append_array(req, 'y', addr, len);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2014-08-14 01:00:15 +02:00
|
|
|
r = sd_bus_message_append(req, "t", (uint64_t) 0);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2018-01-23 01:53:31 +01:00
|
|
|
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
|
2014-07-16 03:31:30 +02:00
|
|
|
if (r < 0) {
|
2019-04-11 11:00:22 +02:00
|
|
|
if (!bus_error_shall_fallback(&error))
|
2018-07-15 16:00:00 +02:00
|
|
|
goto not_found;
|
2014-08-01 01:55:15 +02:00
|
|
|
|
2016-09-16 10:57:06 +02:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
r = sd_bus_message_enter_container(reply, 'a', "(is)");
|
2014-08-14 01:00:15 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
while ((r = sd_bus_message_read(reply, "(is)", &ifindex, &n)) > 0) {
|
2014-08-14 01:00:15 +02:00
|
|
|
|
2015-08-17 23:54:08 +02:00
|
|
|
if (ifindex < 0) {
|
|
|
|
r = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
c++;
|
|
|
|
ms += ALIGN(strlen(n) + 1);
|
|
|
|
}
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
r = sd_bus_message_rewind(reply, false);
|
|
|
|
if (r < 0)
|
2019-04-10 21:27:16 +02:00
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
if (c <= 0)
|
|
|
|
goto not_found;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
ms += ALIGN(len) + /* the address */
|
|
|
|
2 * sizeof(char*) + /* pointers to the address, plus trailing NULL */
|
|
|
|
c * sizeof(char*); /* pointers to aliases, plus trailing NULL */
|
|
|
|
|
|
|
|
if (buflen < ms) {
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = ERANGE;
|
2017-02-16 10:52:04 +01:00
|
|
|
*h_errnop = NETDB_INTERNAL;
|
2014-07-16 03:31:30 +02:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First, place address */
|
|
|
|
r_addr = buffer;
|
|
|
|
memcpy(r_addr, addr, len);
|
|
|
|
idx = ALIGN(len);
|
|
|
|
|
|
|
|
/* Second, place address list */
|
|
|
|
r_addr_list = buffer + idx;
|
|
|
|
((char**) r_addr_list)[0] = r_addr;
|
|
|
|
((char**) r_addr_list)[1] = NULL;
|
|
|
|
idx += sizeof(char*) * 2;
|
|
|
|
|
|
|
|
/* Third, reserve space for the aliases array */
|
|
|
|
r_aliases = buffer + idx;
|
|
|
|
idx += sizeof(char*) * c;
|
|
|
|
|
|
|
|
/* Fourth, place aliases */
|
|
|
|
i = 0;
|
|
|
|
r_name = buffer + idx;
|
2015-08-17 23:54:08 +02:00
|
|
|
while ((r = sd_bus_message_read(reply, "(is)", &ifindex, &n)) > 0) {
|
2014-07-16 03:31:30 +02:00
|
|
|
char *p;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(n);
|
|
|
|
p = buffer + idx;
|
|
|
|
memcpy(p, n, l+1);
|
|
|
|
|
2016-01-26 18:26:48 +01:00
|
|
|
if (i > 0)
|
2014-07-16 03:31:30 +02:00
|
|
|
((char**) r_aliases)[i-1] = p;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
idx += ALIGN(l+1);
|
|
|
|
}
|
2014-07-16 22:50:41 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
2014-07-16 03:31:30 +02:00
|
|
|
|
|
|
|
((char**) r_aliases)[c-1] = NULL;
|
|
|
|
assert(idx == ms);
|
|
|
|
|
|
|
|
result->h_name = r_name;
|
|
|
|
result->h_aliases = (char**) r_aliases;
|
|
|
|
result->h_addrtype = af;
|
|
|
|
result->h_length = len;
|
|
|
|
result->h_addr_list = (char**) r_addr_list;
|
|
|
|
|
|
|
|
if (ttlp)
|
|
|
|
*ttlp = 0;
|
|
|
|
|
2018-07-15 16:00:00 +02:00
|
|
|
/* Explicitly reset both *h_errnop and h_errno to work around
|
|
|
|
* https://bugzilla.redhat.com/show_bug.cgi?id=1125975 */
|
2014-08-01 16:01:12 +02:00
|
|
|
*h_errnop = NETDB_SUCCESS;
|
|
|
|
h_errno = 0;
|
|
|
|
|
2014-07-16 03:31:30 +02:00
|
|
|
return NSS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
fail:
|
2019-01-18 20:13:55 +01:00
|
|
|
UNPROTECT_ERRNO;
|
2019-01-10 16:09:52 +01:00
|
|
|
*errnop = -r;
|
2016-01-26 23:40:59 +01:00
|
|
|
*h_errnop = NO_RECOVERY;
|
2019-04-10 21:26:46 +02:00
|
|
|
return NSS_STATUS_UNAVAIL;
|
2018-07-15 16:00:00 +02:00
|
|
|
|
|
|
|
not_found:
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
2014-07-16 03:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NSS_GETHOSTBYNAME_FALLBACKS(resolve);
|
|
|
|
NSS_GETHOSTBYADDR_FALLBACKS(resolve);
|