From 90b98e8795a8523496f796c8c46c1eb6172528b1 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 6 Jul 2017 15:57:18 +0100 Subject: [PATCH] test-utils-glib: Factor out functions for switching uid Signed-off-by: Simon McVittie https://bugs.freedesktop.org/show_bug.cgi?id=101354 --- test/test-utils-glib.c | 66 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/test/test-utils-glib.c b/test/test-utils-glib.c index bb481952..b5352056 100644 --- a/test/test-utils-glib.c +++ b/test/test-utils-glib.c @@ -336,24 +336,20 @@ test_connect_to_bus (TestMainContext *ctx, return conn; } -DBusConnection * -test_connect_to_bus_as_user (TestMainContext *ctx, - const char *address, - TestUser user) +static gboolean +become_other_user (TestUser user) { /* For now we only do tests like this on Linux, because I don't know how * safe this use of setresuid() is on other platforms */ #if defined(HAVE_GETRESUID) && defined(HAVE_SETRESUID) && defined(__linux__) uid_t ruid, euid, suid; const struct passwd *pwd; - DBusConnection *conn; const char *username; + g_return_val_if_fail (user != TEST_USER_ME, FALSE); + switch (user) { - case TEST_USER_ME: - return test_connect_to_bus (ctx, address); - case TEST_USER_ROOT: username = "root"; break; @@ -366,8 +362,9 @@ test_connect_to_bus_as_user (TestMainContext *ctx, username = DBUS_TEST_USER; break; + case TEST_USER_ME: default: - g_return_val_if_reached (NULL); + g_return_val_if_reached (FALSE); } if (getresuid (&ruid, &euid, &suid) != 0) @@ -378,7 +375,7 @@ test_connect_to_bus_as_user (TestMainContext *ctx, g_test_message ("not uid 0 (ruid=%ld euid=%ld suid=%ld)", (unsigned long) ruid, (unsigned long) euid, (unsigned long) suid); g_test_skip ("not uid 0"); - return NULL; + return FALSE; } pwd = getpwnam (username); @@ -387,7 +384,7 @@ test_connect_to_bus_as_user (TestMainContext *ctx, { g_test_message ("getpwnam(\"%s\"): %s", username, g_strerror (errno)); g_test_skip ("not uid 0"); - return NULL; + return FALSE; } /* Impersonate the desired user while we connect to the bus. @@ -396,35 +393,58 @@ test_connect_to_bus_as_user (TestMainContext *ctx, g_error ("setresuid(%ld, (same), 0): %s", (unsigned long) pwd->pw_uid, g_strerror (errno)); - conn = test_connect_to_bus (ctx, address); - - /* go back to our saved uid */ - if (setresuid (0, 0, 0) != 0) - g_error ("setresuid(0, 0, 0): %s", g_strerror (errno)); - - return conn; + return TRUE; #else + g_return_val_if_fail (user != TEST_USER_ME, FALSE); switch (user) { - case TEST_USER_ME: - return test_connect_to_bus (ctx, address); - case TEST_USER_ROOT: case TEST_USER_MESSAGEBUS: case TEST_USER_OTHER: g_test_skip ("setresuid() not available, or unsure about " "credentials-passing semantics on this platform"); - return NULL; + return FALSE; + case TEST_USER_ME: default: - g_return_val_if_reached (NULL); + g_return_val_if_reached (FALSE); } #endif } +/* Undo the effect of a successful call to become_other_user() */ +static void +back_to_root (void) +{ +#if defined(HAVE_GETRESUID) && defined(HAVE_SETRESUID) && defined(__linux__) + if (setresuid (0, 0, 0) != 0) + g_error ("setresuid(0, 0, 0): %s", g_strerror (errno)); +#else + g_error ("become_other_user() cannot succeed on this platform"); +#endif +} + +DBusConnection * +test_connect_to_bus_as_user (TestMainContext *ctx, + const char *address, + TestUser user) +{ + DBusConnection *conn; + + if (user != TEST_USER_ME && !become_other_user (user)) + return NULL; + + conn = test_connect_to_bus (ctx, address); + + if (user != TEST_USER_ME) + back_to_root (); + + return conn; +} + static void pid_died (GPid pid, gint status, -- 2.13.3