1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "bus-get-properties.h"
7 #include "bus-polkit.h"
9 #include "format-util.h"
10 #include "logind-dbus.h"
11 #include "logind-session-dbus.h"
12 #include "logind-user-dbus.h"
13 #include "logind-user.h"
15 #include "missing_capability.h"
16 #include "signal-util.h"
18 #include "user-util.h"
20 static int property_get_uid(
23 const char *interface
,
25 sd_bus_message
*reply
,
27 sd_bus_error
*error
) {
35 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->uid
);
38 static int property_get_gid(
41 const char *interface
,
43 sd_bus_message
*reply
,
45 sd_bus_error
*error
) {
53 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->gid
);
56 static int property_get_name(
59 const char *interface
,
61 sd_bus_message
*reply
,
63 sd_bus_error
*error
) {
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
;
92 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
96 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
99 static int property_get_sessions(
102 const char *interface
,
103 const char *property
,
104 sd_bus_message
*reply
,
106 sd_bus_error
*error
) {
116 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
120 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
121 _cleanup_free_
char *p
= NULL
;
123 p
= session_bus_path(session
);
127 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
133 return sd_bus_message_close_container(reply
);
136 static int property_get_idle_hint(
139 const char *interface
,
140 const char *property
,
141 sd_bus_message
*reply
,
143 sd_bus_error
*error
) {
151 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
154 static int property_get_idle_since_hint(
157 const char *interface
,
158 const char *property
,
159 sd_bus_message
*reply
,
161 sd_bus_error
*error
) {
164 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
171 (void) user_get_idle_hint(u
, &t
);
172 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
174 return sd_bus_message_append(reply
, "t", k
);
177 static int property_get_linger(
180 const char *interface
,
181 const char *property
,
182 sd_bus_message
*reply
,
184 sd_bus_error
*error
) {
193 r
= user_check_linger_file(u
);
195 return sd_bus_message_append(reply
, "b", r
> 0);
198 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
205 r
= bus_verify_polkit_async(
208 "org.freedesktop.login1.manage",
212 &u
->manager
->polkit_registry
,
217 return 1; /* Will call us back */
219 r
= user_stop(u
, /* force */ true);
223 return sd_bus_reply_method_return(message
, NULL
);
226 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
234 r
= bus_verify_polkit_async(
237 "org.freedesktop.login1.manage",
241 &u
->manager
->polkit_registry
,
246 return 1; /* Will call us back */
248 r
= sd_bus_message_read(message
, "i", &signo
);
252 if (!SIGNAL_VALID(signo
))
253 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
255 r
= user_kill(u
, signo
);
259 return sd_bus_reply_method_return(message
, NULL
);
262 static int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
263 Manager
*m
= userdata
;
274 if (streq(path
, "/org/freedesktop/login1/user/self")) {
275 sd_bus_message
*message
;
277 message
= sd_bus_get_current_message(bus
);
279 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
281 sd_bus_error_free(error
);
289 p
= startswith(path
, "/org/freedesktop/login1/user/_");
293 r
= parse_uid(p
, &uid
);
297 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
306 char *user_bus_path(User
*u
) {
311 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->user_record
->uid
) < 0)
317 static int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
318 _cleanup_strv_free_
char **l
= NULL
;
319 sd_bus_message
*message
;
320 Manager
*m
= userdata
;
328 HASHMAP_FOREACH(user
, m
->users
) {
331 p
= user_bus_path(user
);
335 r
= strv_consume(&l
, p
);
340 message
= sd_bus_get_current_message(bus
);
342 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
344 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
348 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
350 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
352 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
360 *nodes
= TAKE_PTR(l
);
365 static const sd_bus_vtable user_vtable
[] = {
366 SD_BUS_VTABLE_START(0),
368 SD_BUS_PROPERTY("UID", "u", property_get_uid
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
369 SD_BUS_PROPERTY("GID", "u", property_get_gid
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
370 SD_BUS_PROPERTY("Name", "s", property_get_name
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
371 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
372 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
373 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
374 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
375 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
376 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
377 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
378 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
379 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
380 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
381 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
383 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
384 SD_BUS_METHOD_WITH_NAMES("Kill",
386 SD_BUS_PARAM(signal_number
),
388 bus_user_method_kill
,
389 SD_BUS_VTABLE_UNPRIVILEGED
),
394 const BusObjectImplementation user_object
= {
395 "/org/freedesktop/login1/user",
396 "org.freedesktop.login1.User",
397 .fallback_vtables
= BUS_FALLBACK_VTABLES({user_vtable
, user_object_find
}),
398 .node_enumerator
= user_node_enumerator
,
401 int user_send_signal(User
*u
, bool new_user
) {
402 _cleanup_free_
char *p
= NULL
;
406 p
= user_bus_path(u
);
410 return sd_bus_emit_signal(
412 "/org/freedesktop/login1",
413 "org.freedesktop.login1.Manager",
414 new_user
? "UserNew" : "UserRemoved",
415 "uo", (uint32_t) u
->user_record
->uid
, p
);
418 int user_send_changed(User
*u
, const char *properties
, ...) {
419 _cleanup_free_
char *p
= NULL
;
427 p
= user_bus_path(u
);
431 l
= strv_from_stdarg_alloca(properties
);
433 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);