1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "bus-get-properties.h"
7 #include "bus-object.h"
8 #include "bus-polkit.h"
9 #include "format-util.h"
12 #include "logind-dbus.h"
13 #include "logind-session.h"
14 #include "logind-session-dbus.h"
15 #include "logind-user.h"
16 #include "logind-user-dbus.h"
17 #include "signal-util.h"
18 #include "string-util.h"
20 #include "user-record.h"
21 #include "user-util.h"
23 static int property_get_uid(
26 const char *interface
,
28 sd_bus_message
*reply
,
30 sd_bus_error
*error
) {
32 User
*u
= ASSERT_PTR(userdata
);
37 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->uid
);
40 static int property_get_gid(
43 const char *interface
,
45 sd_bus_message
*reply
,
47 sd_bus_error
*error
) {
49 User
*u
= ASSERT_PTR(userdata
);
54 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->gid
);
57 static int property_get_name(
60 const char *interface
,
62 sd_bus_message
*reply
,
64 sd_bus_error
*error
) {
66 User
*u
= ASSERT_PTR(userdata
);
71 return sd_bus_message_append(reply
, "s", u
->user_record
->user_name
);
74 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
76 static int property_get_display(
79 const char *interface
,
81 sd_bus_message
*reply
,
83 sd_bus_error
*error
) {
85 _cleanup_free_
char *p
= NULL
;
86 User
*u
= ASSERT_PTR(userdata
);
91 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
95 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
98 static int property_get_sessions(
101 const char *interface
,
102 const char *property
,
103 sd_bus_message
*reply
,
105 sd_bus_error
*error
) {
107 User
*u
= ASSERT_PTR(userdata
);
113 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
117 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
118 _cleanup_free_
char *p
= NULL
;
120 p
= session_bus_path(session
);
124 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
130 return sd_bus_message_close_container(reply
);
133 static int property_get_idle_hint(
136 const char *interface
,
137 const char *property
,
138 sd_bus_message
*reply
,
140 sd_bus_error
*error
) {
142 User
*u
= ASSERT_PTR(userdata
);
147 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
150 static int property_get_idle_since_hint(
153 const char *interface
,
154 const char *property
,
155 sd_bus_message
*reply
,
157 sd_bus_error
*error
) {
159 User
*u
= ASSERT_PTR(userdata
);
160 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
166 (void) user_get_idle_hint(u
, &t
);
167 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
169 return sd_bus_message_append(reply
, "t", k
);
172 static int property_get_linger(
175 const char *interface
,
176 const char *property
,
177 sd_bus_message
*reply
,
179 sd_bus_error
*error
) {
181 User
*u
= ASSERT_PTR(userdata
);
187 r
= user_check_linger_file(u
);
189 return sd_bus_message_append(reply
, "b", r
> 0);
192 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
193 User
*u
= ASSERT_PTR(userdata
);
198 r
= bus_verify_polkit_async_full(
200 "org.freedesktop.login1.manage",
204 &u
->manager
->polkit_registry
,
209 return 1; /* Will call us back */
211 r
= user_stop(u
, /* force = */ true);
215 return sd_bus_reply_method_return(message
, NULL
);
218 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
219 User
*u
= ASSERT_PTR(userdata
);
225 r
= bus_verify_polkit_async_full(
227 "org.freedesktop.login1.manage",
231 &u
->manager
->polkit_registry
,
236 return 1; /* Will call us back */
238 r
= sd_bus_message_read(message
, "i", &signo
);
242 if (!SIGNAL_VALID(signo
))
243 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
245 r
= user_kill(u
, signo
);
249 return sd_bus_reply_method_return(message
, NULL
);
252 static int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
253 Manager
*m
= ASSERT_PTR(userdata
);
263 if (streq(path
, "/org/freedesktop/login1/user/self")) {
264 sd_bus_message
*message
;
266 message
= sd_bus_get_current_message(bus
);
268 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
270 sd_bus_error_free(error
);
278 p
= startswith(path
, "/org/freedesktop/login1/user/_");
282 r
= parse_uid(p
, &uid
);
286 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
295 char* user_bus_path(User
*u
) {
300 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->user_record
->uid
) < 0)
306 static int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
307 _cleanup_strv_free_
char **l
= NULL
;
308 sd_bus_message
*message
;
309 Manager
*m
= userdata
;
317 HASHMAP_FOREACH(user
, m
->users
) {
320 p
= user_bus_path(user
);
324 r
= strv_consume(&l
, p
);
329 message
= sd_bus_get_current_message(bus
);
331 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
333 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
337 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
339 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
341 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
349 *nodes
= TAKE_PTR(l
);
354 static const sd_bus_vtable user_vtable
[] = {
355 SD_BUS_VTABLE_START(0),
357 SD_BUS_PROPERTY("UID", "u", property_get_uid
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
358 SD_BUS_PROPERTY("GID", "u", property_get_gid
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
359 SD_BUS_PROPERTY("Name", "s", property_get_name
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
360 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
361 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
362 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service_manager_unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
363 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
364 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
365 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
366 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
367 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
368 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
369 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
370 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
372 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
373 SD_BUS_METHOD_WITH_ARGS("Kill",
374 SD_BUS_ARGS("i", signal_number
),
376 bus_user_method_kill
,
377 SD_BUS_VTABLE_UNPRIVILEGED
),
382 const BusObjectImplementation user_object
= {
383 "/org/freedesktop/login1/user",
384 "org.freedesktop.login1.User",
385 .fallback_vtables
= BUS_FALLBACK_VTABLES({user_vtable
, user_object_find
}),
386 .node_enumerator
= user_node_enumerator
,
389 int user_send_signal(User
*u
, bool new_user
) {
390 _cleanup_free_
char *p
= NULL
;
394 p
= user_bus_path(u
);
398 return sd_bus_emit_signal(
400 "/org/freedesktop/login1",
401 "org.freedesktop.login1.Manager",
402 new_user
? "UserNew" : "UserRemoved",
403 "uo", (uint32_t) u
->user_record
->uid
, p
);
406 int user_send_changed_strv(User
*u
, char **properties
) {
407 _cleanup_free_
char *p
= NULL
;
414 p
= user_bus_path(u
);
418 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", properties
);