X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnss-myhostname%2Fnss-myhostname.c;h=aa92cc96e4a1d46f772f85c3ee4012be20b15b1c;hb=1b8951e5bd9b2bf1722098a861055cae0bb52088;hp=f5dadb60aae34a79543e6aec33a7a711a5239f36;hpb=555bd6e95bf2b469306d9cd86e126c0122d0895b;p=elogind.git diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index f5dadb60a..aa92cc96e 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -35,11 +35,6 @@ #include "nss-util.h" #include "util.h" -/* Ensure that glibc's assert is used. We cannot use assert from macro.h, as - * libnss_myhostname will be linked into arbitrary programs which will, in turn - * attempt to write to the journal via log_dispatch() */ -#include - /* We use 127.0.0.2 as IPv4 address. This has the advantage over * 127.0.0.1 that it can be translated back to the local hostname. For * IPv6 we use ::1 which unfortunately will not translate back to the @@ -82,6 +77,18 @@ enum nss_status _nss_myhostname_gethostbyname4_r( canonical = "localhost"; local_address_ipv4 = htonl(INADDR_LOOPBACK); + + } else if (streq(name, "gateway")) { + + n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses); + if (n_addresses <= 0) { + *errnop = ENOENT; + *h_errnop = HOST_NOT_FOUND; + return NSS_STATUS_NOTFOUND; + } + + canonical = "gateway"; + } else { hn = gethostname_malloc(); if (!hn) { @@ -97,7 +104,7 @@ enum nss_status _nss_myhostname_gethostbyname4_r( return NSS_STATUS_NOTFOUND; } - n_addresses = local_addresses(&addresses); + n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses < 0) n_addresses = 0; @@ -170,6 +177,11 @@ enum nss_status _nss_myhostname_gethostbyname4_r( if (ttlp) *ttlp = 0; + /* Explicitly reset all error variables */ + *errnop = 0; + *h_errnop = NETDB_SUCCESS; + h_errno = 0; + return NSS_STATUS_SUCCESS; } @@ -195,7 +207,7 @@ static enum nss_status fill_in_hostent( assert(errnop); assert(h_errnop); - alen = PROTO_ADDRESS_SIZE(af); + alen = FAMILY_ADDRESS_SIZE(af); for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++) if (af == a->family) @@ -294,6 +306,11 @@ static enum nss_status fill_in_hostent( if (canonp) *canonp = r_name; + /* Explicitly reset all error variables */ + *errnop = 0; + *h_errnop = NETDB_SUCCESS; + h_errno = 0; + return NSS_STATUS_SUCCESS; } @@ -309,7 +326,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r( _cleanup_free_ struct local_address *addresses = NULL; const char *canonical, *additional = NULL; _cleanup_free_ char *hn = NULL; - uint32_t local_address_ipv4; + uint32_t local_address_ipv4 = 0; int n_addresses = 0; assert(name); @@ -330,6 +347,18 @@ enum nss_status _nss_myhostname_gethostbyname3_r( if (is_localhost(name)) { canonical = "localhost"; local_address_ipv4 = htonl(INADDR_LOOPBACK); + + } else if (streq(name, "gateway")) { + + n_addresses = local_gateways(NULL, 0, af, &addresses); + if (n_addresses <= 0) { + *errnop = ENOENT; + *h_errnop = HOST_NOT_FOUND; + return NSS_STATUS_NOTFOUND; + } + + canonical = "gateway"; + } else { hn = gethostname_malloc(); if (!hn) { @@ -344,7 +373,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r( return NSS_STATUS_NOTFOUND; } - n_addresses = local_addresses(&addresses); + n_addresses = local_addresses(NULL, 0, af, &addresses); if (n_addresses < 0) n_addresses = 0; @@ -393,7 +422,7 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( return NSS_STATUS_UNAVAIL; } - if (len != PROTO_ADDRESS_SIZE(af)) { + if (len != FAMILY_ADDRESS_SIZE(af)) { *errnop = EINVAL; *h_errnop = NO_RECOVERY; return NSS_STATUS_UNAVAIL; @@ -420,16 +449,42 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( } - n_addresses = local_addresses(&addresses); - if (n_addresses < 0) - n_addresses = 0; + n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses); + if (n_addresses > 0) { + for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) { + if (af != a->family) + continue; + + if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) { + + hn = gethostname_malloc(); + if (!hn) { + *errnop = ENOMEM; + *h_errnop = NO_RECOVERY; + return NSS_STATUS_TRYAGAIN; + } + + canonical = hn; + goto found; + } + } + } + + free(addresses); + addresses = NULL; - for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) { - if (af != a->family) - continue; + n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses); + if (n_addresses > 0) { + for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) { + if (af != a->family) + continue; - if (memcmp(addr, &a->address, PROTO_ADDRESS_SIZE(af)) == 0) - goto found; + if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) { + + canonical = "gateway"; + goto found; + } + } } *errnop = ENOENT; @@ -438,16 +493,6 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( return NSS_STATUS_NOTFOUND; found: - if (!canonical) { - hn = gethostname_malloc(); - if (!hn) { - *errnop = ENOMEM; - *h_errnop = NO_RECOVERY; - return NSS_STATUS_TRYAGAIN; - } - - canonical = hn; - } return fill_in_hostent( canonical, additional,