nss-resolve: fallback to the class NSS "dns" module if we cannot contact resolved

That way DNS resolution works both with and without resolved running.
This commit is contained in:
Lennart Poettering 2014-08-01 01:55:15 +02:00
parent 95dd6257a6
commit 7c2a5e264c
1 changed files with 87 additions and 0 deletions

View File

@ -29,6 +29,7 @@
#include <net/if.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include "sd-bus.h"
#include "bus-util.h"
@ -43,6 +44,27 @@ NSS_GETHOSTBYADDR_PROTOTYPES(resolve);
#define DNS_CALL_TIMEOUT_USEC (45*USEC_PER_SEC)
typedef void (*voidfunc_t)(void);
static voidfunc_t find_fallback(const char *module, const char *symbol) {
void *dl;
/* Try to find a fallback NSS module symbol */
dl = dlopen(module, RTLD_LAZY|RTLD_NODELETE);
if (!dl)
return NULL;
return dlsym(dl, symbol);
}
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) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED);
}
static int count_addresses(sd_bus_message *m, int af, const char **canonical) {
int c = 0, r;
@ -143,6 +165,25 @@ enum nss_status _nss_resolve_gethostbyname4_r(
return NSS_STATUS_NOTFOUND;
}
if (bus_error_shall_fallback(&error)) {
enum nss_status (*fallback)(
const char *name,
struct gaih_addrtuple **pat,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp);
fallback = (enum nss_status (*)(const char *name,
struct gaih_addrtuple **pat,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp))
find_fallback("libnss_dns.so.2", "_nss_dns_gethostbyname4_r");
if (fallback)
return fallback(name, pat, buffer, buflen, errnop, h_errnop, ttlp);
}
*errnop = -r;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;
@ -309,6 +350,29 @@ enum nss_status _nss_resolve_gethostbyname3_r(
return NSS_STATUS_NOTFOUND;
}
if (bus_error_shall_fallback(&error)) {
enum nss_status (*fallback)(
const char *name,
int af,
struct hostent *result,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp,
char **canonp);
fallback = (enum nss_status (*)(const char *name,
int af,
struct hostent *result,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp,
char **canonp))
find_fallback("libnss_dns.so.2", "_nss_dns_gethostbyname3_r");
if (fallback)
return fallback(name, af, result, buffer, buflen, errnop, h_errnop, ttlp, canonp);
}
*errnop = -r;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;
@ -505,6 +569,29 @@ enum nss_status _nss_resolve_gethostbyaddr2_r(
return NSS_STATUS_NOTFOUND;
}
if (bus_error_shall_fallback(&error)) {
enum nss_status (*fallback)(
const void* addr, socklen_t len,
int af,
struct hostent *result,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp);
fallback = (enum nss_status (*)(
const void* addr, socklen_t len,
int af,
struct hostent *result,
char *buffer, size_t buflen,
int *errnop, int *h_errnop,
int32_t *ttlp))
find_fallback("libnss_dns.so.2", "_nss_dns_gethostbyaddr2_r");
if (fallback)
return fallback(addr, len, af, result, buffer, buflen, errnop, h_errnop, ttlp);
}
*errnop = -r;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;