]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/login/logind-user-dbus.c
shared: split out polkit stuff from bus-util.c → bus-polkit.c
[thirdparty/systemd.git] / src / login / logind-user-dbus.c
index 921f1e0f91181cc08ab64a0274ee508971563d3d..9bf68a610e409ac2f9cef55e673008c19864f0ac 100644 (file)
@@ -1,35 +1,22 @@
-/***
-  This file is part of systemd.
-
-  Copyright 2011 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <errno.h>
-#include <string.h>
 
 #include "alloc-util.h"
+#include "bus-polkit.h"
 #include "bus-util.h"
 #include "format-util.h"
+#include "logind-dbus.h"
+#include "logind-session-dbus.h"
+#include "logind-user-dbus.h"
 #include "logind-user.h"
 #include "logind.h"
+#include "missing_capability.h"
 #include "signal-util.h"
 #include "strv.h"
 #include "user-util.h"
 
-static int property_get_display(
+static int property_get_uid(
                 sd_bus *bus,
                 const char *path,
                 const char *interface,
@@ -38,21 +25,34 @@ static int property_get_display(
                 void *userdata,
                 sd_bus_error *error) {
 
-        _cleanup_free_ char *p = NULL;
         User *u = userdata;
 
         assert(bus);
         assert(reply);
         assert(u);
 
-        p = u->display ? session_bus_path(u->display) : strdup("/");
-        if (!p)
-                return -ENOMEM;
+        return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->uid);
+}
 
-        return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p);
+static int property_get_gid(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        User *u = userdata;
+
+        assert(bus);
+        assert(reply);
+        assert(u);
+
+        return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->gid);
 }
 
-static int property_get_state(
+static int property_get_name(
                 sd_bus *bus,
                 const char *path,
                 const char *interface,
@@ -67,7 +67,32 @@ static int property_get_state(
         assert(reply);
         assert(u);
 
-        return sd_bus_message_append(reply, "s", user_state_to_string(user_get_state(u)));
+        return sd_bus_message_append(reply, "s", u->user_record->user_name);
+}
+
+static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", User, user_get_state, user_state_to_string);
+
+static int property_get_display(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        _cleanup_free_ char *p = NULL;
+        User *u = userdata;
+
+        assert(bus);
+        assert(reply);
+        assert(u);
+
+        p = u->display ? session_bus_path(u->display) : strdup("/");
+        if (!p)
+                return -ENOMEM;
+
+        return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p);
 }
 
 static int property_get_sessions(
@@ -142,7 +167,7 @@ static int property_get_idle_since_hint(
         assert(reply);
         assert(u);
 
-        user_get_idle_hint(u, &t);
+        (void) user_get_idle_hint(u, &t);
         k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
 
         return sd_bus_message_append(reply, "t", k);
@@ -182,7 +207,7 @@ int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_er
                         "org.freedesktop.login1.manage",
                         NULL,
                         false,
-                        u->uid,
+                        u->user_record->uid,
                         &u->manager->polkit_registry,
                         error);
         if (r < 0)
@@ -211,7 +236,7 @@ int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *
                         "org.freedesktop.login1.manage",
                         NULL,
                         false,
-                        u->uid,
+                        u->user_record->uid,
                         &u->manager->polkit_registry,
                         error);
         if (r < 0)
@@ -236,9 +261,9 @@ int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *
 const sd_bus_vtable user_vtable[] = {
         SD_BUS_VTABLE_START(0),
 
-        SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), SD_BUS_VTABLE_PROPERTY_CONST),
-        SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), SD_BUS_VTABLE_PROPERTY_CONST),
-        SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("UID", "u", property_get_uid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("GID", "u", property_get_gid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
         BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST),
@@ -273,10 +298,12 @@ int user_object_find(sd_bus *bus, const char *path, const char *interface, void
                 sd_bus_message *message;
 
                 message = sd_bus_get_current_message(bus);
-                if (!message)
-                        return 0;
 
                 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
+                if (r == -ENXIO) {
+                        sd_bus_error_free(error);
+                        return 0;
+                }
                 if (r < 0)
                         return r;
         } else {
@@ -287,13 +314,13 @@ int user_object_find(sd_bus *bus, const char *path, const char *interface, void
                         return 0;
 
                 r = parse_uid(p, &uid);
-        }
-        if (r < 0)
-                return 0;
+                if (r < 0)
+                        return 0;
 
-        user = hashmap_get(m->users, UID_TO_PTR(uid));
-        if (!user)
-                return 0;
+                user = hashmap_get(m->users, UID_TO_PTR(uid));
+                if (!user)
+                        return 0;
+        }
 
         *found = user;
         return 1;
@@ -304,7 +331,7 @@ char *user_bus_path(User *u) {
 
         assert(u);
 
-        if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->uid) < 0)
+        if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->user_record->uid) < 0)
                 return NULL;
 
         return s;
@@ -337,10 +364,11 @@ int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
         message = sd_bus_get_current_message(bus);
         if (message) {
                 _cleanup_(sd_bus_creds_unrefp) 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) {
+                        uid_t uid;
+
                         r = sd_bus_creds_get_owner_uid(creds, &uid);
                         if (r >= 0) {
                                 user = hashmap_get(m->users, UID_TO_PTR(uid));
@@ -353,8 +381,7 @@ int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
                 }
         }
 
-        *nodes = l;
-        l = NULL;
+        *nodes = TAKE_PTR(l);
 
         return 1;
 }
@@ -373,7 +400,7 @@ int user_send_signal(User *u, bool new_user) {
                         "/org/freedesktop/login1",
                         "org.freedesktop.login1.Manager",
                         new_user ? "UserNew" : "UserRemoved",
-                        "uo", (uint32_t) u->uid, p);
+                        "uo", (uint32_t) u->user_record->uid, p);
 }
 
 int user_send_changed(User *u, const char *properties, ...) {