chiark / gitweb /
logind: hide 'self' links if not available
authorDavid Herrmann <dh.herrmann@gmail.com>
Sun, 18 Jan 2015 11:59:39 +0000 (12:59 +0100)
committerDavid Herrmann <dh.herrmann@gmail.com>
Sun, 18 Jan 2015 11:59:39 +0000 (12:59 +0100)
If the caller does not run in a session/seat or has no tracked user, hide
the /org/freedesktop/login1/.../self links in introspection data.
Otherwise, "busctl tree org.freedesktop.login1" tries to query those nodes
even though it cant.

src/login/logind-seat-dbus.c
src/login/logind-session-dbus.c
src/login/logind-user-dbus.c

index ddf2cd8..50b0b88 100644 (file)
@@ -381,6 +381,7 @@ char *seat_bus_path(Seat *s) {
 
 int seat_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
         _cleanup_strv_free_ char **l = NULL;
+        sd_bus_message *message;
         Manager *m = userdata;
         Seat *seat;
         Iterator i;
@@ -402,9 +403,25 @@ int seat_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
                         return r;
         }
 
-        r = strv_extend(&l, "/org/freedesktop/login1/seat/self");
-        if (r < 0)
-                return r;
+        message = sd_bus_get_current_message(bus);
+        if (message) {
+                _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+                const char *name;
+                Session *session;
+
+                r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
+                if (r >= 0) {
+                        r = sd_bus_creds_get_session(creds, &name);
+                        if (r >= 0) {
+                                session = hashmap_get(m->sessions, name);
+                                if (session && session->seat) {
+                                        r = strv_extend(&l, "/org/freedesktop/login1/seat/self");
+                                        if (r < 0)
+                                                return r;
+                                }
+                        }
+                }
+        }
 
         *nodes = l;
         l = NULL;
index d341131..e3486fe 100644 (file)
@@ -541,6 +541,7 @@ char *session_bus_path(Session *s) {
 
 int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
         _cleanup_strv_free_ char **l = NULL;
+        sd_bus_message *message;
         Manager *m = userdata;
         Session *session;
         Iterator i;
@@ -562,9 +563,24 @@ int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
                         return r;
         }
 
-        r = strv_extend(&l, "/org/freedesktop/login1/session/self");
-        if (r < 0)
-                return r;
+        message = sd_bus_get_current_message(bus);
+        if (message) {
+                _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+                const char *name;
+
+                r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
+                if (r >= 0) {
+                        r = sd_bus_creds_get_session(creds, &name);
+                        if (r >= 0) {
+                                session = hashmap_get(m->sessions, name);
+                                if (session) {
+                                        r = strv_extend(&l, "/org/freedesktop/login1/session/self");
+                                        if (r < 0)
+                                                return r;
+                                }
+                        }
+                }
+        }
 
         *nodes = l;
         l = NULL;
index bff42e8..5cfaac0 100644 (file)
@@ -291,6 +291,7 @@ char *user_bus_path(User *u) {
 
 int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
         _cleanup_strv_free_ char **l = NULL;
+        sd_bus_message *message;
         Manager *m = userdata;
         User *user;
         Iterator i;
@@ -312,9 +313,24 @@ int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
                         return r;
         }
 
-        r = strv_extend(&l, "/org/freedesktop/login1/user/self");
-        if (r < 0)
-                return r;
+        message = sd_bus_get_current_message(bus);
+        if (message) {
+                _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+                uid_t uid;
+
+                r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
+                if (r >= 0) {
+                        r = sd_bus_creds_get_owner_uid(creds, &uid);
+                        if (r >= 0) {
+                                user = hashmap_get(m->users, UID_TO_PTR(uid));
+                                if (user) {
+                                        r = strv_extend(&l, "/org/freedesktop/login1/user/self");
+                                        if (r < 0)
+                                                return r;
+                                }
+                        }
+                }
+        }
 
         *nodes = l;
         l = NULL;