-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <limits.h>
-#include <nss.h>
-#include <sys/types.h>
-#include <netdb.h>
#include <errno.h>
-#include <string.h>
-#include <unistd.h>
#include <net/if.h>
+#include <netdb.h>
+#include <nss.h>
#include <stdlib.h>
-#include <arpa/inet.h>
+#include <string.h>
+#include "alloc-util.h"
+#include "hostname-util.h"
#include "local-addresses.h"
#include "macro.h"
#include "nss-util.h"
+#include "signal-util.h"
+#include "string-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 <assert.h>
-
/* 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
- * hostname but instead something like "localhost6" or so. */
+ * hostname but instead something like "localhost" or so. */
#define LOCALADDRESS_IPV4 (htonl(0x7F000002))
#define LOCALADDRESS_IPV6 &in6addr_loopback
-#define LOOPBACK_INTERFACE "lo"
NSS_GETHOSTBYNAME_PROTOTYPES(myhostname);
NSS_GETHOSTBYADDR_PROTOTYPES(myhostname);
char *r_name;
unsigned n;
+ BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
+
assert(name);
assert(pat);
assert(buffer);
canonical = "localhost";
local_address_ipv4 = htonl(INADDR_LOOPBACK);
+
+ } else if (is_gateway_hostname(name)) {
+
+ 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) {
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;
}
/* If this call fails we fill in 0 as scope. Which is fine */
- lo_ifi = n_addresses <= 0 ? if_nametoindex(LOOPBACK_INTERFACE) : 0;
+ lo_ifi = n_addresses <= 0 ? LOOPBACK_IFINDEX : 0;
l = strlen(canonical);
ms = ALIGN(l+1) + ALIGN(sizeof(struct gaih_addrtuple)) * (n_addresses > 0 ? n_addresses : 2);
if (ttlp)
*ttlp = 0;
+ /* Explicitly reset all error variables */
+ *errnop = 0;
+ *h_errnop = NETDB_SUCCESS;
+ h_errno = 0;
+
return NSS_STATUS_SUCCESS;
}
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)
if (canonp)
*canonp = r_name;
+ /* Explicitly reset all error variables */
+ *errnop = 0;
+ *h_errnop = NETDB_SUCCESS;
+ h_errno = 0;
+
return NSS_STATUS_SUCCESS;
}
_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;
+ BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
+
assert(name);
assert(host);
assert(buffer);
if (is_localhost(name)) {
canonical = "localhost";
local_address_ipv4 = htonl(INADDR_LOOPBACK);
+
+ } else if (is_gateway_hostname(name)) {
+
+ 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) {
return NSS_STATUS_NOTFOUND;
}
- n_addresses = local_addresses(&addresses);
+ n_addresses = local_addresses(NULL, 0, af, &addresses);
if (n_addresses < 0)
n_addresses = 0;
_cleanup_free_ char *hn = NULL;
int n_addresses = 0;
struct local_address *a;
+ bool additional_from_hostname = false;
unsigned n;
+ BLOCK_SIGNALS(NSS_SIGNALS_BLOCK);
+
assert(addr);
assert(host);
assert(buffer);
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;
}
if (af == AF_INET) {
-
if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4)
goto found;
assert(af == AF_INET6);
if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) {
- additional = "localhost";
+ canonical = "localhost";
+ additional_from_hostname = true;
goto found;
}
+ }
+ 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)
+ goto found;
+ }
}
- n_addresses = local_addresses(&addresses);
- if (n_addresses < 0)
- n_addresses = 0;
+ addresses = mfree(addresses);
- 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;
return NSS_STATUS_NOTFOUND;
found:
- if (!canonical) {
+ if (!canonical || (!additional && additional_from_hostname)) {
hn = gethostname_malloc();
if (!hn) {
*errnop = ENOMEM;
return NSS_STATUS_TRYAGAIN;
}
- canonical = hn;
+ if (!canonical)
+ canonical = hn;
+
+ if (!additional && additional_from_hostname)
+ additional = hn;
}
return fill_in_hostent(
errnop, h_errnop,
ttlp,
NULL);
-
}
NSS_GETHOSTBYNAME_FALLBACKS(myhostname);