From 65e5293aeedfca68c4fd1f7f1bf22d43ca65e2ad Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Wed, 28 Jan 2015 14:55:31 +0100 Subject: [PATCH] priv: replace the use of `gethostbyname()` by `getaddrinfo()` `gethostbyname()` was uses to get the canonical name. `getaddrinfo()` should work even if there is no IPv4 at all in the system. --- src/daemon/priv.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/daemon/priv.c b/src/daemon/priv.c index f660bafd..0d9ea4a2 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #if defined HOST_OS_FREEBSD || HOST_OS_OSX || HOST_OS_DRAGONFLY @@ -216,11 +215,14 @@ static void asroot_gethostname() { struct utsname un; - struct hostent *hp; + struct addrinfo hints = { + .ai_flags = AI_CANONNAME + }; + struct addrinfo *res; int len; if (uname(&un) < 0) fatal("privsep", "failed to get system information"); - if ((hp = gethostbyname(un.nodename)) == NULL) { + if (getaddrinfo(un.nodename, NULL, &hints, &res) != 0) { log_info("privsep", "unable to get system name"); #ifdef HAVE_RES_INIT res_init(); @@ -229,9 +231,10 @@ asroot_gethostname() must_write(PRIV_PRIVILEGED, &len, sizeof(int)); must_write(PRIV_PRIVILEGED, un.nodename, len + 1); } else { - len = strlen(hp->h_name); + len = strlen(res->ai_canonname); must_write(PRIV_PRIVILEGED, &len, sizeof(int)); - must_write(PRIV_PRIVILEGED, hp->h_name, len + 1); + must_write(PRIV_PRIVILEGED, res->ai_canonname, len + 1); + freeaddrinfo(res); } } -- 2.39.5