nss-resolve.c revision 66a16e7e9fc501d371b57cbe2ae5d130fe930c6d
3802a3d3d7af51ddff31943d5514382f01265770Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek This file is part of systemd.
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek (at your option) any later version.
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-SzmekNSS_GETHOSTBYNAME_PROTOTYPES(resolve);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-SzmekNSS_GETHOSTBYADDR_PROTOTYPES(resolve);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek#define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmektypedef void (*voidfunc_t)(void);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmekstatic voidfunc_t find_fallback(const char *module, const char *symbol) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek /* Try to find a fallback NSS module symbol */
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek dl = dlopen(module, RTLD_LAZY|RTLD_NODELETE);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmekstatic bool bus_error_shall_fallback(sd_bus_error *e) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek return sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER) ||
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek sd_bus_error_has_name(e, SD_BUS_ERROR_NO_REPLY) ||
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED);
e8216945a97bc2a2b04bc286e67ab5bba313b83eLennart Poetteringstatic int count_addresses(sd_bus_message *m, int af, const char **canonical) {
e8216945a97bc2a2b04bc286e67ab5bba313b83eLennart Poettering r = sd_bus_message_read(m, "i", &ifindex);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_enter_container(m, 'a', "(iay)");
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek while ((r = sd_bus_message_enter_container(m, 'r', "iay")) > 0) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(m, "i", &family);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_skip(m, "ay");
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek if (af != AF_UNSPEC && family != af)
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_exit_container(m);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(m, "s", canonical);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_rewind(m, true);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmekenum nss_status _nss_resolve_gethostbyname4_r(
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *req = NULL, *reply = NULL;
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek _cleanup_bus_close_unref_ sd_bus *bus = NULL;
e8216945a97bc2a2b04bc286e67ab5bba313b83eLennart Poettering int c, r, i = 0, ifindex;
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_call(
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek "org.freedesktop.resolve1",
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek "org.freedesktop.resolve1.Manager",
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek "ResolveHostname");
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_set_auto_start(req, false);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append(req, "isit", 0, name, AF_UNSPEC, (uint64_t) 0);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_call(bus, req, DNS_CALL_TIMEOUT_USEC, &error, &reply);
e8216945a97bc2a2b04bc286e67ab5bba313b83eLennart Poettering if (sd_bus_error_has_name(&error, _BUS_ERROR_DNS "NXDOMAIN")) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek if (bus_error_shall_fallback(&error)) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek fallback = (enum nss_status (*)(const char *name,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek find_fallback("libnss_dns.so.2", "_nss_dns_gethostbyname4_r");
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek return fallback(name, pat, buffer, buflen, errnop, h_errnop, ttlp);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek c = count_addresses(reply, AF_UNSPEC, &canonical);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * c;
b8bde11658366290521e3d03316378b482600323Jan Engelhardt /* First, append name */
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek /* Second, append addresses */
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(reply, "i", &ifindex);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_enter_container(reply, 'a', "(iay)");
8474b70c3a3842cdf3d51f331dd117ab6421f6d0Zbigniew Jędrzejewski-Szmek while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(reply, "i", &family);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read_array(reply, 'y', &a, &sz);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r = sd_bus_message_exit_container(reply);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek if (!IN_SET(family, AF_INET, AF_INET6))
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek if (sz != FAMILY_ADDRESS_SIZE(family)) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r_tuple = (struct gaih_addrtuple*) (buffer + idx);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek r_tuple->next = i == c-1 ? NULL : (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek idx += ALIGN(sizeof(struct gaih_addrtuple));
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek /* Explicitly reset all error variables */
*errnop = -r;
return NSS_STATUS_UNAVAIL;
const char *name,
int af,
char **canonp) {
const char *canonical;
int c, r, i = 0, ifindex;
r = -EAFNOSUPPORT;
goto fail;
goto fail;
bus,
&req,
"/org/freedesktop/resolve1",
goto fail;
goto fail;
goto fail;
return NSS_STATUS_NOTFOUND;
const char *name,
int af,
char **canonp);
int af,
char **canonp))
if (fallback)
*errnop = -r;
return NSS_STATUS_UNAVAIL;
goto fail;
return NSS_STATUS_NOTFOUND;
return NSS_STATUS_TRYAGAIN;
idx += sizeof(char*);
goto fail;
if (ifindex < 0) {
r = -EINVAL;
goto fail;
goto fail;
int family;
goto fail;
goto fail;
goto fail;
r = -EINVAL;
goto fail;
goto fail;
assert(i == c);
*errnop = 0;
h_errno = 0;
if (ttlp)
*ttlp = 0;
if (canonp)
return NSS_STATUS_SUCCESS;
fail:
*errnop = -r;
return NSS_STATUS_UNAVAIL;
int af,
int r, ifindex;
return NSS_STATUS_UNAVAIL;
return NSS_STATUS_UNAVAIL;
goto fail;
bus,
&req,
"/org/freedesktop/resolve1",
goto fail;
goto fail;
goto fail;
goto fail;
goto fail;
return NSS_STATUS_NOTFOUND;
int af,
int af,
if (fallback)
*errnop = -r;
return NSS_STATUS_UNAVAIL;
goto fail;
if (ifindex < 0) {
r = -EINVAL;
goto fail;
goto fail;
goto fail;
return NSS_STATUS_NOTFOUND;
return NSS_STATUS_TRYAGAIN;
idx += sizeof(char*) * c;
size_t l;
l = strlen(n);
goto fail;
if (ttlp)
*ttlp = 0;
*errnop = 0;
h_errno = 0;
return NSS_STATUS_SUCCESS;
fail:
*errnop = -r;
return NSS_STATUS_UNAVAIL;