2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2005-11-19 19:19:39 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2008-10-27 00:02:23 +01:00
|
|
|
#include <net/if.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <nss.h>
|
2011-05-09 14:57:15 +02:00
|
|
|
#include <stdlib.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <string.h>
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "hostname-util.h"
|
2014-07-10 21:01:25 +02:00
|
|
|
#include "local-addresses.h"
|
2013-04-06 04:24:10 +02:00
|
|
|
#include "macro.h"
|
2014-07-10 23:33:55 +02:00
|
|
|
#include "nss-util.h"
|
2016-01-26 22:34:46 +01:00
|
|
|
#include "signal-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2014-07-10 23:33:55 +02:00
|
|
|
#include "util.h"
|
2008-10-27 00:02:23 +01:00
|
|
|
|
|
|
|
/* 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
|
2015-01-05 16:23:21 +01:00
|
|
|
* hostname but instead something like "localhost" or so. */
|
2008-10-27 00:02:23 +01:00
|
|
|
|
2016-06-15 01:26:01 +02:00
|
|
|
#define LOCALADDRESS_IPV4 (htobe32(0x7F000002))
|
2008-10-27 00:02:23 +01:00
|
|
|
#define LOCALADDRESS_IPV6 &in6addr_loopback
|
|
|
|
|
2014-07-10 23:33:55 +02:00
|
|
|
NSS_GETHOSTBYNAME_PROTOTYPES(myhostname);
|
|
|
|
NSS_GETHOSTBYADDR_PROTOTYPES(myhostname);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
enum nss_status _nss_myhostname_gethostbyname4_r(
|
|
|
|
const char *name,
|
|
|
|
struct gaih_addrtuple **pat,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp) {
|
|
|
|
|
2011-05-09 14:57:15 +02:00
|
|
|
struct gaih_addrtuple *r_tuple, *r_tuple_prev = NULL;
|
2014-07-10 21:01:25 +02:00
|
|
|
_cleanup_free_ struct local_address *addresses = NULL;
|
2014-07-10 19:55:53 +02:00
|
|
|
_cleanup_free_ char *hn = NULL;
|
|
|
|
const char *canonical = NULL;
|
2017-02-08 20:00:07 +01:00
|
|
|
int n_addresses = 0;
|
2013-04-24 22:18:01 +02:00
|
|
|
uint32_t local_address_ipv4;
|
2014-07-10 21:01:25 +02:00
|
|
|
struct local_address *a;
|
2014-07-10 19:55:53 +02:00
|
|
|
size_t l, idx, ms;
|
|
|
|
char *r_name;
|
2014-07-10 21:01:25 +02:00
|
|
|
unsigned n;
|
2008-10-27 00:02:23 +01: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-10 19:55:53 +02:00
|
|
|
assert(name);
|
|
|
|
assert(pat);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
|
|
|
|
|
|
|
if (is_localhost(name)) {
|
2013-04-24 22:18:01 +02:00
|
|
|
/* We respond to 'localhost', so that /etc/hosts
|
|
|
|
* is optional */
|
2008-10-27 00:02:23 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
canonical = "localhost";
|
2016-06-15 01:26:01 +02:00
|
|
|
local_address_ipv4 = htobe32(INADDR_LOOPBACK);
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2015-08-17 23:35:09 +02:00
|
|
|
} else if (is_gateway_hostname(name)) {
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses);
|
2014-12-03 21:42:58 +01:00
|
|
|
if (n_addresses <= 0) {
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
resolved,nss-myhostname: use _gateway for the gateway
This changes the symbolic name for the default gateway from "gateway" to
"_gateway". A new configuration option -Dcompat-gateway-hostname=true|false
is added. If it is set, the old name is also supported, but the new name
is used as the canonical name in either case. This is intended as a temporary
measure to make the transition easier, and the option should be removed
after a few releases, at which point only the new name will be used.
The old "gateway" name mostly works OK, but hasn't gained widespread acceptance
because of the following (potential) conflicts:
- it is completely legal to have a host called "gateway"
- there is no guarantee that "gateway" will not be registered as a TLD, even
though this currently seems unlikely. (Even then, there would be no
conflict except for the case when the top-level domain itself was being resolved.
The "gateway" or "_gateway" labels have only special meaning when the
whole name consists of a single label, so resolution of any subdomain
of the hypothetical gateway. TLD would still work OK. )
Moving to "_gateway" avoids those issues because underscores are not allowed
in host names (RFC 1123, §2.1) and avoids potential conflicts with local or
global names.
v2:
- simplify the logic to hardcode "_gateway" and allow
-Dcompat-gateway-hostname=true as a temporary measure.
2017-07-11 08:15:08 +02:00
|
|
|
canonical = "_gateway";
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
} else {
|
2014-07-10 19:55:53 +02:00
|
|
|
hn = gethostname_malloc();
|
|
|
|
if (!hn) {
|
|
|
|
*errnop = ENOMEM;
|
2013-04-24 22:18:01 +02:00
|
|
|
*h_errnop = NO_RECOVERY;
|
2014-07-10 19:55:53 +02:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
2013-04-24 22:18:01 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 14:48:23 +02:00
|
|
|
/* We respond to our local host name, our hostname suffixed with a single dot. */
|
2014-07-10 19:55:53 +02:00
|
|
|
if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
|
2013-04-24 22:18:01 +02:00
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
|
2014-07-10 21:01:25 +02:00
|
|
|
if (n_addresses < 0)
|
|
|
|
n_addresses = 0;
|
2013-04-24 22:18:01 +02:00
|
|
|
|
|
|
|
canonical = hn;
|
|
|
|
local_address_ipv4 = LOCALADDRESS_IPV4;
|
|
|
|
}
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
l = strlen(canonical);
|
2014-07-10 19:55:53 +02:00
|
|
|
ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
|
2008-10-27 00:02:23 +01:00
|
|
|
if (buflen < ms) {
|
2017-02-16 21:29:09 +01:00
|
|
|
*errnop = ERANGE;
|
|
|
|
*h_errnop = NETDB_INTERNAL;
|
2008-10-27 00:02:23 +01:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First, fill in hostname */
|
|
|
|
r_name = buffer;
|
2013-04-24 22:18:01 +02:00
|
|
|
memcpy(r_name, canonical, l+1);
|
2008-10-27 00:02:23 +01:00
|
|
|
idx = ALIGN(l+1);
|
|
|
|
|
2016-03-03 21:07:02 +01:00
|
|
|
assert(n_addresses >= 0);
|
|
|
|
if (n_addresses == 0) {
|
2011-05-09 14:57:15 +02:00
|
|
|
/* Second, fill in IPv6 tuple */
|
|
|
|
r_tuple = (struct gaih_addrtuple*) (buffer + idx);
|
|
|
|
r_tuple->next = r_tuple_prev;
|
|
|
|
r_tuple->name = r_name;
|
|
|
|
r_tuple->family = AF_INET6;
|
|
|
|
memcpy(r_tuple->addr, LOCALADDRESS_IPV6, 16);
|
2017-02-08 20:00:07 +01:00
|
|
|
r_tuple->scopeid = 0;
|
2011-05-09 14:57:15 +02:00
|
|
|
|
|
|
|
idx += ALIGN(sizeof(struct gaih_addrtuple));
|
|
|
|
r_tuple_prev = r_tuple;
|
|
|
|
|
|
|
|
/* Third, fill in IPv4 tuple */
|
|
|
|
r_tuple = (struct gaih_addrtuple*) (buffer + idx);
|
|
|
|
r_tuple->next = r_tuple_prev;
|
|
|
|
r_tuple->name = r_name;
|
|
|
|
r_tuple->family = AF_INET;
|
2013-04-24 22:18:01 +02:00
|
|
|
*(uint32_t*) r_tuple->addr = local_address_ipv4;
|
2017-02-08 20:00:07 +01:00
|
|
|
r_tuple->scopeid = 0;
|
2011-05-09 14:57:15 +02:00
|
|
|
|
|
|
|
idx += ALIGN(sizeof(struct gaih_addrtuple));
|
|
|
|
r_tuple_prev = r_tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fourth, fill actual addresses in, but in backwards order */
|
2014-07-10 21:01:25 +02:00
|
|
|
for (a = addresses + n_addresses - 1, n = 0; (int) n < n_addresses; n++, a--) {
|
2011-05-09 14:57:15 +02:00
|
|
|
r_tuple = (struct gaih_addrtuple*) (buffer + idx);
|
|
|
|
r_tuple->next = r_tuple_prev;
|
|
|
|
r_tuple->name = r_name;
|
|
|
|
r_tuple->family = a->family;
|
2017-02-08 20:00:07 +01:00
|
|
|
r_tuple->scopeid = a->family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&a->address.in6) ? a->ifindex : 0;
|
2014-07-10 19:55:53 +02:00
|
|
|
memcpy(r_tuple->addr, &a->address, 16);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
|
|
|
idx += ALIGN(sizeof(struct gaih_addrtuple));
|
|
|
|
r_tuple_prev = r_tuple;
|
|
|
|
}
|
2008-10-27 00:02:23 +01:00
|
|
|
|
|
|
|
/* Verify the size matches */
|
|
|
|
assert(idx == ms);
|
|
|
|
|
2013-02-04 15:56:26 +01:00
|
|
|
/* Nscd expects us to store the first record in **pat. */
|
|
|
|
if (*pat)
|
|
|
|
**pat = *r_tuple_prev;
|
|
|
|
else
|
|
|
|
*pat = r_tuple_prev;
|
2008-10-27 00:02:23 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
return NSS_STATUS_SUCCESS;
|
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
|
|
|
static enum nss_status fill_in_hostent(
|
2013-04-24 22:18:01 +02:00
|
|
|
const char *canonical, const char *additional,
|
2008-10-27 00:02:23 +01:00
|
|
|
int af,
|
2014-07-10 21:01:25 +02:00
|
|
|
struct local_address *addresses, unsigned n_addresses,
|
2013-04-24 22:18:01 +02:00
|
|
|
uint32_t local_address_ipv4,
|
2008-10-27 00:02:23 +01:00
|
|
|
struct hostent *result,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp,
|
|
|
|
char **canonp) {
|
|
|
|
|
2014-07-11 15:34:18 +02:00
|
|
|
size_t l_canonical, l_additional, idx, ms, alen;
|
2013-04-24 22:18:01 +02:00
|
|
|
char *r_addr, *r_name, *r_aliases, *r_alias = NULL, *r_addr_list;
|
2014-07-10 21:01:25 +02:00
|
|
|
struct local_address *a;
|
2013-04-24 22:18:01 +02:00
|
|
|
unsigned n, c;
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2014-07-10 19:55:53 +02:00
|
|
|
assert(canonical);
|
|
|
|
assert(result);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
|
|
|
|
2014-07-18 02:36:10 +02:00
|
|
|
alen = FAMILY_ADDRESS_SIZE(af);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
|
|
|
for (a = addresses, n = 0, c = 0; n < n_addresses; a++, n++)
|
|
|
|
if (af == a->family)
|
|
|
|
c++;
|
2008-10-27 00:02:23 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
l_canonical = strlen(canonical);
|
2017-07-20 11:38:15 +02:00
|
|
|
l_additional = strlen_ptr(additional);
|
2013-04-24 22:18:01 +02:00
|
|
|
ms = ALIGN(l_canonical+1)+
|
|
|
|
(additional ? ALIGN(l_additional+1) : 0) +
|
2014-07-10 19:55:53 +02:00
|
|
|
sizeof(char*) +
|
2013-04-24 22:18:01 +02:00
|
|
|
(additional ? sizeof(char*) : 0) +
|
2014-07-11 15:34:18 +02:00
|
|
|
(c > 0 ? c : 1) * ALIGN(alen) +
|
2014-07-10 19:55:53 +02:00
|
|
|
(c > 0 ? c+1 : 2) * sizeof(char*);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
if (buflen < ms) {
|
2017-02-16 21:29:09 +01:00
|
|
|
*errnop = ERANGE;
|
|
|
|
*h_errnop = NETDB_INTERNAL;
|
2008-10-27 00:02:23 +01:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
/* First, fill in hostnames */
|
2008-10-27 00:02:23 +01:00
|
|
|
r_name = buffer;
|
2013-04-24 22:18:01 +02:00
|
|
|
memcpy(r_name, canonical, l_canonical+1);
|
|
|
|
idx = ALIGN(l_canonical+1);
|
2008-10-27 00:02:23 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
if (additional) {
|
|
|
|
r_alias = buffer + idx;
|
|
|
|
memcpy(r_alias, additional, l_additional+1);
|
|
|
|
idx += ALIGN(l_additional+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second, create aliases array */
|
2008-10-27 00:02:23 +01:00
|
|
|
r_aliases = buffer + idx;
|
2013-04-24 22:18:01 +02:00
|
|
|
if (additional) {
|
|
|
|
((char**) r_aliases)[0] = r_alias;
|
|
|
|
((char**) r_aliases)[1] = NULL;
|
|
|
|
idx += 2*sizeof(char*);
|
|
|
|
} else {
|
|
|
|
((char**) r_aliases)[0] = NULL;
|
|
|
|
idx += sizeof(char*);
|
|
|
|
}
|
2008-10-27 00:02:23 +01:00
|
|
|
|
2011-05-09 14:57:15 +02:00
|
|
|
/* Third, add addresses */
|
2008-10-27 00:02:23 +01:00
|
|
|
r_addr = buffer + idx;
|
2011-05-09 14:57:15 +02:00
|
|
|
if (c > 0) {
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
for (a = addresses, n = 0; n < n_addresses; a++, n++) {
|
|
|
|
if (af != a->family)
|
|
|
|
continue;
|
|
|
|
|
2014-07-10 19:55:53 +02:00
|
|
|
memcpy(r_addr + i*ALIGN(alen), &a->address, alen);
|
2011-05-09 14:57:15 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(i == c);
|
|
|
|
idx += c*ALIGN(alen);
|
|
|
|
} else {
|
|
|
|
if (af == AF_INET)
|
2013-04-24 22:18:01 +02:00
|
|
|
*(uint32_t*) r_addr = local_address_ipv4;
|
2011-05-09 14:57:15 +02:00
|
|
|
else
|
|
|
|
memcpy(r_addr, LOCALADDRESS_IPV6, 16);
|
|
|
|
|
|
|
|
idx += ALIGN(alen);
|
|
|
|
}
|
2008-10-27 00:02:23 +01:00
|
|
|
|
|
|
|
/* Fourth, add address pointer array */
|
|
|
|
r_addr_list = buffer + idx;
|
2011-05-09 14:57:15 +02:00
|
|
|
if (c > 0) {
|
2014-07-11 15:34:18 +02:00
|
|
|
unsigned i;
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2014-07-11 15:34:18 +02:00
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
((char**) r_addr_list)[i] = r_addr + i*ALIGN(alen);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2014-07-11 15:34:18 +02:00
|
|
|
((char**) r_addr_list)[i] = NULL;
|
|
|
|
idx += (c+1) * sizeof(char*);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
((char**) r_addr_list)[0] = r_addr;
|
|
|
|
((char**) r_addr_list)[1] = NULL;
|
2014-07-11 15:34:18 +02:00
|
|
|
idx += 2 * sizeof(char*);
|
2011-05-09 14:57:15 +02:00
|
|
|
}
|
2008-10-27 00:02:23 +01:00
|
|
|
|
|
|
|
/* Verify the size matches */
|
|
|
|
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 */
|
2014-08-01 16:01:12 +02:00
|
|
|
*h_errnop = NETDB_SUCCESS;
|
|
|
|
h_errno = 0;
|
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
return NSS_STATUS_SUCCESS;
|
2005-11-19 19:19:39 +01:00
|
|
|
}
|
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
enum nss_status _nss_myhostname_gethostbyname3_r(
|
|
|
|
const char *name,
|
|
|
|
int af,
|
|
|
|
struct hostent *host,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp,
|
|
|
|
char **canonp) {
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2014-07-10 21:01:25 +02:00
|
|
|
_cleanup_free_ struct local_address *addresses = NULL;
|
2013-04-24 22:18:01 +02:00
|
|
|
const char *canonical, *additional = NULL;
|
2014-07-10 19:55:53 +02:00
|
|
|
_cleanup_free_ char *hn = NULL;
|
2014-12-03 21:42:58 +01:00
|
|
|
uint32_t local_address_ipv4 = 0;
|
2014-07-10 21:01:25 +02:00
|
|
|
int n_addresses = 0;
|
2014-07-10 19:55:53 +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-10 19:55:53 +02:00
|
|
|
assert(name);
|
|
|
|
assert(host);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
if (af == AF_UNSPEC)
|
|
|
|
af = AF_INET;
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2017-09-29 09:58:22 +02:00
|
|
|
if (!IN_SET(af, AF_INET, AF_INET6)) {
|
2008-10-27 00:02:23 +01:00
|
|
|
*errnop = EAFNOSUPPORT;
|
|
|
|
*h_errnop = NO_DATA;
|
|
|
|
return NSS_STATUS_UNAVAIL;
|
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2014-07-10 19:55:53 +02:00
|
|
|
if (is_localhost(name)) {
|
2013-04-24 22:18:01 +02:00
|
|
|
canonical = "localhost";
|
2016-06-15 01:26:01 +02:00
|
|
|
local_address_ipv4 = htobe32(INADDR_LOOPBACK);
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2015-08-17 23:35:09 +02:00
|
|
|
} else if (is_gateway_hostname(name)) {
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n_addresses = local_gateways(NULL, 0, af, &addresses);
|
2014-12-03 21:42:58 +01:00
|
|
|
if (n_addresses <= 0) {
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
resolved,nss-myhostname: use _gateway for the gateway
This changes the symbolic name for the default gateway from "gateway" to
"_gateway". A new configuration option -Dcompat-gateway-hostname=true|false
is added. If it is set, the old name is also supported, but the new name
is used as the canonical name in either case. This is intended as a temporary
measure to make the transition easier, and the option should be removed
after a few releases, at which point only the new name will be used.
The old "gateway" name mostly works OK, but hasn't gained widespread acceptance
because of the following (potential) conflicts:
- it is completely legal to have a host called "gateway"
- there is no guarantee that "gateway" will not be registered as a TLD, even
though this currently seems unlikely. (Even then, there would be no
conflict except for the case when the top-level domain itself was being resolved.
The "gateway" or "_gateway" labels have only special meaning when the
whole name consists of a single label, so resolution of any subdomain
of the hypothetical gateway. TLD would still work OK. )
Moving to "_gateway" avoids those issues because underscores are not allowed
in host names (RFC 1123, §2.1) and avoids potential conflicts with local or
global names.
v2:
- simplify the logic to hardcode "_gateway" and allow
-Dcompat-gateway-hostname=true as a temporary measure.
2017-07-11 08:15:08 +02:00
|
|
|
canonical = "_gateway";
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
} else {
|
2014-07-10 19:55:53 +02:00
|
|
|
hn = gethostname_malloc();
|
|
|
|
if (!hn) {
|
|
|
|
*errnop = ENOMEM;
|
2013-04-24 22:18:01 +02:00
|
|
|
*h_errnop = NO_RECOVERY;
|
2014-07-10 19:55:53 +02:00
|
|
|
return NSS_STATUS_TRYAGAIN;
|
2013-04-24 22:18:01 +02:00
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2014-07-10 19:55:53 +02:00
|
|
|
if (!streq(name, hn) && !streq_ptr(startswith(name, hn), ".")) {
|
2013-04-24 22:18:01 +02:00
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n_addresses = local_addresses(NULL, 0, af, &addresses);
|
2014-07-10 21:01:25 +02:00
|
|
|
if (n_addresses < 0)
|
|
|
|
n_addresses = 0;
|
2013-04-24 22:18:01 +02:00
|
|
|
|
|
|
|
canonical = hn;
|
|
|
|
additional = n_addresses <= 0 && af == AF_INET6 ? "localhost" : NULL;
|
|
|
|
local_address_ipv4 = LOCALADDRESS_IPV4;
|
2008-10-27 00:02:23 +01:00
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
return fill_in_hostent(
|
|
|
|
canonical, additional,
|
|
|
|
af,
|
|
|
|
addresses, n_addresses,
|
|
|
|
local_address_ipv4,
|
|
|
|
host,
|
|
|
|
buffer, buflen,
|
|
|
|
errnop, h_errnop,
|
|
|
|
ttlp,
|
|
|
|
canonp);
|
2008-10-27 00:02:23 +01:00
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
enum nss_status _nss_myhostname_gethostbyaddr2_r(
|
|
|
|
const void* addr, socklen_t len,
|
|
|
|
int af,
|
|
|
|
struct hostent *host,
|
|
|
|
char *buffer, size_t buflen,
|
|
|
|
int *errnop, int *h_errnop,
|
|
|
|
int32_t *ttlp) {
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
const char *canonical = NULL, *additional = NULL;
|
2014-07-10 19:55:53 +02:00
|
|
|
uint32_t local_address_ipv4 = LOCALADDRESS_IPV4;
|
2014-07-10 21:01:25 +02:00
|
|
|
_cleanup_free_ struct local_address *addresses = NULL;
|
2014-07-10 19:55:53 +02:00
|
|
|
_cleanup_free_ char *hn = NULL;
|
2014-07-10 21:01:25 +02:00
|
|
|
int n_addresses = 0;
|
|
|
|
struct local_address *a;
|
2015-01-05 16:23:21 +01:00
|
|
|
bool additional_from_hostname = false;
|
2014-07-10 21:01:25 +02:00
|
|
|
unsigned n;
|
2014-07-10 19:55:53 +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-10 19:55:53 +02:00
|
|
|
assert(addr);
|
|
|
|
assert(host);
|
|
|
|
assert(buffer);
|
|
|
|
assert(errnop);
|
|
|
|
assert(h_errnop);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2014-07-16 03:30:40 +02:00
|
|
|
if (!IN_SET(af, AF_INET, AF_INET6)) {
|
|
|
|
*errnop = EAFNOSUPPORT;
|
|
|
|
*h_errnop = NO_DATA;
|
|
|
|
return NSS_STATUS_UNAVAIL;
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:36:10 +02:00
|
|
|
if (len != FAMILY_ADDRESS_SIZE(af)) {
|
2011-05-09 14:57:15 +02:00
|
|
|
*errnop = EINVAL;
|
|
|
|
*h_errnop = NO_RECOVERY;
|
|
|
|
return NSS_STATUS_UNAVAIL;
|
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2008-10-27 00:02:23 +01:00
|
|
|
if (af == AF_INET) {
|
2011-05-09 14:57:15 +02:00
|
|
|
if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4)
|
|
|
|
goto found;
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2016-06-15 01:26:01 +02:00
|
|
|
if ((*(uint32_t*) addr) == htobe32(INADDR_LOOPBACK)) {
|
2013-04-24 22:18:01 +02:00
|
|
|
canonical = "localhost";
|
2016-06-15 01:26:01 +02:00
|
|
|
local_address_ipv4 = htobe32(INADDR_LOOPBACK);
|
2013-04-24 22:18:01 +02:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
2014-07-16 03:30:40 +02:00
|
|
|
} else {
|
|
|
|
assert(af == AF_INET6);
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) {
|
2015-01-05 16:23:21 +01:00
|
|
|
canonical = "localhost";
|
|
|
|
additional_from_hostname = true;
|
2011-05-09 14:57:15 +02:00
|
|
|
goto found;
|
2013-04-24 22:18:01 +02:00
|
|
|
}
|
2008-10-27 00:02:23 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses);
|
2016-03-03 21:07:02 +01:00
|
|
|
for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
|
|
|
|
if (af != a->family)
|
|
|
|
continue;
|
2011-05-09 14:57:15 +02:00
|
|
|
|
2016-03-03 21:07:02 +01:00
|
|
|
if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0)
|
|
|
|
goto found;
|
2014-12-03 21:42:58 +01:00
|
|
|
}
|
|
|
|
|
2015-07-31 19:56:38 +02:00
|
|
|
addresses = mfree(addresses);
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2014-12-04 01:41:12 +01:00
|
|
|
n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses);
|
2016-03-03 21:07:02 +01:00
|
|
|
for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) {
|
|
|
|
if (af != a->family)
|
|
|
|
continue;
|
2014-12-03 21:42:58 +01:00
|
|
|
|
2016-03-03 21:07:02 +01:00
|
|
|
if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) {
|
resolved,nss-myhostname: use _gateway for the gateway
This changes the symbolic name for the default gateway from "gateway" to
"_gateway". A new configuration option -Dcompat-gateway-hostname=true|false
is added. If it is set, the old name is also supported, but the new name
is used as the canonical name in either case. This is intended as a temporary
measure to make the transition easier, and the option should be removed
after a few releases, at which point only the new name will be used.
The old "gateway" name mostly works OK, but hasn't gained widespread acceptance
because of the following (potential) conflicts:
- it is completely legal to have a host called "gateway"
- there is no guarantee that "gateway" will not be registered as a TLD, even
though this currently seems unlikely. (Even then, there would be no
conflict except for the case when the top-level domain itself was being resolved.
The "gateway" or "_gateway" labels have only special meaning when the
whole name consists of a single label, so resolution of any subdomain
of the hypothetical gateway. TLD would still work OK. )
Moving to "_gateway" avoids those issues because underscores are not allowed
in host names (RFC 1123, §2.1) and avoids potential conflicts with local or
global names.
v2:
- simplify the logic to hardcode "_gateway" and allow
-Dcompat-gateway-hostname=true as a temporary measure.
2017-07-11 08:15:08 +02:00
|
|
|
canonical = "_gateway";
|
2016-03-03 21:07:02 +01:00
|
|
|
goto found;
|
2014-12-03 21:42:58 +01:00
|
|
|
}
|
2011-05-09 14:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*h_errnop = HOST_NOT_FOUND;
|
|
|
|
return NSS_STATUS_NOTFOUND;
|
|
|
|
|
|
|
|
found:
|
2016-03-15 05:50:25 +01:00
|
|
|
if (!canonical || additional_from_hostname) {
|
2015-01-05 16:23:21 +01:00
|
|
|
hn = gethostname_malloc();
|
|
|
|
if (!hn) {
|
|
|
|
*errnop = ENOMEM;
|
|
|
|
*h_errnop = NO_RECOVERY;
|
|
|
|
return NSS_STATUS_TRYAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canonical)
|
|
|
|
canonical = hn;
|
2016-03-15 05:50:25 +01:00
|
|
|
else
|
2015-01-05 16:23:21 +01:00
|
|
|
additional = hn;
|
|
|
|
}
|
2008-10-27 00:02:23 +01:00
|
|
|
|
2013-04-24 22:18:01 +02:00
|
|
|
return fill_in_hostent(
|
|
|
|
canonical, additional,
|
|
|
|
af,
|
|
|
|
addresses, n_addresses,
|
|
|
|
local_address_ipv4,
|
|
|
|
host,
|
|
|
|
buffer, buflen,
|
|
|
|
errnop, h_errnop,
|
|
|
|
ttlp,
|
|
|
|
NULL);
|
2008-10-27 00:02:23 +01:00
|
|
|
}
|
2005-11-19 19:19:39 +01:00
|
|
|
|
2014-07-10 23:33:55 +02:00
|
|
|
NSS_GETHOSTBYNAME_FALLBACKS(myhostname);
|
|
|
|
NSS_GETHOSTBYADDR_FALLBACKS(myhostname);
|