From 2458541961425473a17468682014d3898886879a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 6 Mar 2018 21:31:16 +0900 Subject: [PATCH] nss-systemd: cleanup bypassing dbus logic --- src/nss-systemd/nss-systemd.c | 111 ++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/src/nss-systemd/nss-systemd.c b/src/nss-systemd/nss-systemd.c index c502b5f..d64fa25 100644 --- a/src/nss-systemd/nss-systemd.c +++ b/src/nss-systemd/nss-systemd.c @@ -115,6 +115,9 @@ enum nss_status _nss_systemd_getpwnam_r( char *buffer, size_t buflen, int *errnop) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; uint32_t translated; size_t l; int bypass, r; @@ -150,16 +153,18 @@ enum nss_status _nss_systemd_getpwnam_r( bypass = getenv_bool_secure("SYSTEMD_NSS_BYPASS_BUS"); if (bypass <= 0) { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; - _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - r = sd_bus_open_system(&bus); - if (r < 0) { + if (r < 0) bypass = 1; - goto direct_lookup; - } + } + if (bypass > 0) { + r = direct_lookup_name(name, (uid_t*) &translated); + if (r == -ENOENT) + goto not_found; + if (r < 0) + goto fail; + } else { r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", @@ -181,16 +186,6 @@ enum nss_status _nss_systemd_getpwnam_r( goto fail; } -direct_lookup: - if (bypass > 0) { - /* Access the dynamic UID allocation directly if we are called from dbus-daemon, see above. */ - r = direct_lookup_name(name, (uid_t*) &translated); - if (r == -ENOENT) - goto not_found; - if (r < 0) - goto fail; - } - l = strlen(name); if (buflen < l+1) { *errnop = ERANGE; @@ -262,11 +257,20 @@ enum nss_status _nss_systemd_getpwuid_r( bypass = getenv_bool_secure("SYSTEMD_NSS_BYPASS_BUS"); if (bypass <= 0) { r = sd_bus_open_system(&bus); - if (r < 0) { + if (r < 0) bypass = 1; - goto direct_lookup; - } + } + + if (bypass > 0) { + r = direct_lookup_uid(uid, &direct); + if (r == -ENOENT) + goto not_found; + if (r < 0) + goto fail; + + translated = direct; + } else { r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", @@ -288,18 +292,6 @@ enum nss_status _nss_systemd_getpwuid_r( goto fail; } -direct_lookup: - if (bypass > 0) { - r = direct_lookup_uid(uid, &direct); - if (r == -ENOENT) - goto not_found; - if (r < 0) - goto fail; - - translated = direct; - - } - l = strlen(translated) + 1; if (buflen < l) { *errnop = ERANGE; @@ -336,6 +328,9 @@ enum nss_status _nss_systemd_getgrnam_r( char *buffer, size_t buflen, int *errnop) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; uint32_t translated; size_t l; int bypass, r; @@ -368,16 +363,18 @@ enum nss_status _nss_systemd_getgrnam_r( bypass = getenv_bool_secure("SYSTEMD_NSS_BYPASS_BUS"); if (bypass <= 0) { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_(sd_bus_message_unrefp) sd_bus_message* reply = NULL; - _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - r = sd_bus_open_system(&bus); - if (r < 0) { + if (r < 0) bypass = 1; - goto direct_lookup; - } + } + if (bypass > 0) { + r = direct_lookup_name(name, (uid_t*) &translated); + if (r == -ENOENT) + goto not_found; + if (r < 0) + goto fail; + } else { r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", @@ -399,16 +396,6 @@ enum nss_status _nss_systemd_getgrnam_r( goto fail; } -direct_lookup: - if (bypass > 0) { - /* Access the dynamic GID allocation directly if we are called from dbus-daemon, see above. */ - r = direct_lookup_name(name, (uid_t*) &translated); - if (r == -ENOENT) - goto not_found; - if (r < 0) - goto fail; - } - l = sizeof(char*) + strlen(name) + 1; if (buflen < l) { *errnop = ERANGE; @@ -478,11 +465,20 @@ enum nss_status _nss_systemd_getgrgid_r( bypass = getenv_bool_secure("SYSTEMD_NSS_BYPASS_BUS"); if (bypass <= 0) { r = sd_bus_open_system(&bus); - if (r < 0) { + if (r < 0) bypass = 1; - goto direct_lookup; - } + } + + if (bypass > 0) { + r = direct_lookup_uid(gid, &direct); + if (r == -ENOENT) + goto not_found; + if (r < 0) + goto fail; + + translated = direct; + } else { r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", @@ -504,17 +500,6 @@ enum nss_status _nss_systemd_getgrgid_r( goto fail; } -direct_lookup: - if (bypass > 0) { - r = direct_lookup_uid(gid, &direct); - if (r == -ENOENT) - goto not_found; - if (r < 0) - goto fail; - - translated = direct; - } - l = sizeof(char*) + strlen(translated) + 1; if (buflen < l) { *errnop = ERANGE; -- 2.7.4