803fb7
From d3706690bf2953cb8e0362253dc68d77989de3be Mon Sep 17 00:00:00 2001
803fb7
From: Lukas Nykryn <lnykryn@redhat.com>
803fb7
Date: Fri, 11 Sep 2015 13:37:59 +0200
803fb7
Subject: [PATCH] loginctl: print nontrivial properties in logictl show-*
803fb7
803fb7
Cherry-picked from: 2a998c74028a109d6bd8888951abfa8e25a15fb1
803fb7
Resolves: #1260465
803fb7
---
803fb7
 src/login/loginctl.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++-
803fb7
 src/shared/macro.h   |  13 +++++
803fb7
 2 files changed, 162 insertions(+), 3 deletions(-)
803fb7
803fb7
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
803fb7
index b0eede9a3..6c8a59e7c 100644
803fb7
--- a/src/login/loginctl.c
803fb7
+++ b/src/login/loginctl.c
803fb7
@@ -657,19 +657,165 @@ finish:
803fb7
         return r;
803fb7
 }
803fb7
 
803fb7
+static int print_property(const char *name, sd_bus_message *m, const char *contents) {
803fb7
+        int r;
803fb7
+
803fb7
+        assert(name);
803fb7
+        assert(m);
803fb7
+        assert(contents);
803fb7
+
803fb7
+        if (arg_property && !strv_find(arg_property, name))
803fb7
+                /* skip what we didn't read */
803fb7
+                return sd_bus_message_skip(m, contents);
803fb7
+
803fb7
+        switch (contents[0]) {
803fb7
+
803fb7
+        case SD_BUS_TYPE_STRUCT_BEGIN:
803fb7
+
803fb7
+                if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
803fb7
+                        const char *s;
803fb7
+
803fb7
+                        r = sd_bus_message_read(m, "(so)", &s, NULL);
803fb7
+                        if (r < 0)
803fb7
+                                return bus_log_parse_error(r);
803fb7
+
803fb7
+                        if (arg_all || !isempty(s))
803fb7
+                                printf("%s=%s\n", name, s);
803fb7
+
803fb7
+                        return 0;
803fb7
+
803fb7
+                } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
803fb7
+                        uint32_t uid;
803fb7
+
803fb7
+                        r = sd_bus_message_read(m, "(uo)", &uid, NULL);
803fb7
+                        if (r < 0)
803fb7
+                                return bus_log_parse_error(r);
803fb7
+
803fb7
+                        if (UID_IS_INVALID(uid)) {
803fb7
+                                log_error("Invalid user ID: " UID_FMT, uid);
803fb7
+                                return -EINVAL;
803fb7
+                        }
803fb7
+
803fb7
+                        printf("%s=" UID_FMT "\n", name, uid);
803fb7
+
803fb7
+                        return 0;
803fb7
+                }
803fb7
+
803fb7
+                break;
803fb7
+
803fb7
+        case SD_BUS_TYPE_ARRAY:
803fb7
+
803fb7
+                if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
803fb7
+                        const char *s;
803fb7
+                        bool space = false;
803fb7
+
803fb7
+                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
803fb7
+                        if (r < 0)
803fb7
+                                return bus_log_parse_error(r);
803fb7
+
803fb7
+                        printf("%s=", name);
803fb7
+
803fb7
+                        while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
803fb7
+                                printf("%s%s", space ? " " : "", s);
803fb7
+                                space = true;
803fb7
+                        }
803fb7
+
803fb7
+                        printf("\n");
803fb7
+
803fb7
+                        if (r < 0)
803fb7
+                                return bus_log_parse_error(r);
803fb7
+
803fb7
+                        r = sd_bus_message_exit_container(m);
803fb7
+                        if (r < 0)
803fb7
+                                return bus_log_parse_error(r);
803fb7
+
803fb7
+                        return 0;
803fb7
+                }
803fb7
+
803fb7
+                break;
803fb7
+        }
803fb7
+
803fb7
+        r = bus_print_property(name, m, arg_all);
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        if (r == 0) {
803fb7
+                r = sd_bus_message_skip(m, contents);
803fb7
+                if (r < 0)
803fb7
+                        return bus_log_parse_error(r);
803fb7
+
803fb7
+                if (arg_all)
803fb7
+                        printf("%s=[unprintable]\n", name);
803fb7
+        }
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
803fb7
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
803fb7
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
         int r;
803fb7
 
803fb7
+        assert(bus);
803fb7
+        assert(path);
803fb7
+        assert(new_line);
803fb7
+
803fb7
+        r = sd_bus_call_method(
803fb7
+                        bus,
803fb7
+                        "org.freedesktop.login1",
803fb7
+                        path,
803fb7
+                        "org.freedesktop.DBus.Properties",
803fb7
+                        "GetAll",
803fb7
+                        &error,
803fb7
+                        &reply,
803fb7
+                        "s", "");
803fb7
+        if (r < 0)
803fb7
+                return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
803fb7
+
803fb7
+        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
         if (*new_line)
803fb7
                 printf("\n");
803fb7
 
803fb7
         *new_line = true;
803fb7
 
803fb7
-        r = bus_print_all_properties(bus, "org.freedesktop.login1", path, arg_property, arg_all);
803fb7
+        while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
803fb7
+                const char *name, *contents;
803fb7
+
803fb7
+                r = sd_bus_message_read(reply, "s", &name);
803fb7
+                if (r < 0)
803fb7
+                        return bus_log_parse_error(r);
803fb7
+
803fb7
+                r = sd_bus_message_peek_type(reply, NULL, &contents);
803fb7
+                if (r < 0)
803fb7
+                        return bus_log_parse_error(r);
803fb7
+
803fb7
+                r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
803fb7
+                if (r < 0)
803fb7
+                        return bus_log_parse_error(r);
803fb7
+
803fb7
+                r = print_property(name, reply, contents);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+
803fb7
+                r = sd_bus_message_exit_container(reply);
803fb7
+                if (r < 0)
803fb7
+                        return bus_log_parse_error(r);
803fb7
+
803fb7
+                r = sd_bus_message_exit_container(reply);
803fb7
+                if (r < 0)
803fb7
+                        return bus_log_parse_error(r);
803fb7
+        }
803fb7
         if (r < 0)
803fb7
-                log_error_errno(r, "Could not get properties: %m");
803fb7
+                return bus_log_parse_error(r);
803fb7
 
803fb7
-        return r;
803fb7
+        r = sd_bus_message_exit_container(reply);
803fb7
+        if (r < 0)
803fb7
+                return bus_log_parse_error(r);
803fb7
+
803fb7
+        return 0;
803fb7
 }
803fb7
 
803fb7
 static int show_session(int argc, char *argv[], void *userdata) {
803fb7
diff --git a/src/shared/macro.h b/src/shared/macro.h
803fb7
index 7f89951d6..9d857dc8d 100644
803fb7
--- a/src/shared/macro.h
803fb7
+++ b/src/shared/macro.h
803fb7
@@ -26,6 +26,7 @@
803fb7
 #include <sys/types.h>
803fb7
 #include <sys/uio.h>
803fb7
 #include <inttypes.h>
803fb7
+#include <stdbool.h>
803fb7
 
803fb7
 #define _printf_(a,b) __attribute__ ((format (printf, a, b)))
803fb7
 #define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__)))
803fb7
@@ -451,6 +452,18 @@ do {                                                                    \
803fb7
 #define GID_INVALID ((gid_t) -1)
803fb7
 #define MODE_INVALID ((mode_t) -1)
803fb7
 
803fb7
+static inline bool UID_IS_INVALID(uid_t uid) {
803fb7
+        /* We consider both the old 16bit -1 user and the newer 32bit
803fb7
+         * -1 user invalid, since they are or used to be incompatible
803fb7
+         * with syscalls such as setresuid() or chown(). */
803fb7
+
803fb7
+        return uid == (uid_t) ((uint32_t) -1) || uid == (uid_t) ((uint16_t) -1);
803fb7
+}
803fb7
+
803fb7
+static inline bool GID_IS_INVALID(gid_t gid) {
803fb7
+        return gid == (gid_t) ((uint32_t) -1) || gid == (gid_t) ((uint16_t) -1);
803fb7
+}
803fb7
+
803fb7
 #define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func)                 \
803fb7
         static inline void func##p(type *p) {                   \
803fb7
                 if (*p)                                         \