1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bus-polkit.h"
8 #include "format-util.h"
9 #include "logind-dbus.h"
10 #include "logind-session-dbus.h"
11 #include "logind-user-dbus.h"
12 #include "logind-user.h"
14 #include "missing_capability.h"
15 #include "signal-util.h"
17 #include "user-util.h"
19 static int property_get_uid(
22 const char *interface
,
24 sd_bus_message
*reply
,
26 sd_bus_error
*error
) {
34 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->uid
);
37 static int property_get_gid(
40 const char *interface
,
42 sd_bus_message
*reply
,
44 sd_bus_error
*error
) {
52 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->gid
);
55 static int property_get_name(
58 const char *interface
,
60 sd_bus_message
*reply
,
62 sd_bus_error
*error
) {
70 return sd_bus_message_append(reply
, "s", u
->user_record
->user_name
);
73 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
75 static int property_get_display(
78 const char *interface
,
80 sd_bus_message
*reply
,
82 sd_bus_error
*error
) {
84 _cleanup_free_
char *p
= NULL
;
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
) {
115 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
119 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
120 _cleanup_free_
char *p
= NULL
;
122 p
= session_bus_path(session
);
126 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
132 return sd_bus_message_close_container(reply
);
135 static int property_get_idle_hint(
138 const char *interface
,
139 const char *property
,
140 sd_bus_message
*reply
,
142 sd_bus_error
*error
) {
150 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
153 static int property_get_idle_since_hint(
156 const char *interface
,
157 const char *property
,
158 sd_bus_message
*reply
,
160 sd_bus_error
*error
) {
163 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
170 (void) user_get_idle_hint(u
, &t
);
171 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
173 return sd_bus_message_append(reply
, "t", k
);
176 static int property_get_linger(
179 const char *interface
,
180 const char *property
,
181 sd_bus_message
*reply
,
183 sd_bus_error
*error
) {
192 r
= user_check_linger_file(u
);
194 return sd_bus_message_append(reply
, "b", r
> 0);
197 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
204 r
= bus_verify_polkit_async(
207 "org.freedesktop.login1.manage",
211 &u
->manager
->polkit_registry
,
216 return 1; /* Will call us back */
218 r
= user_stop(u
, true);
222 return sd_bus_reply_method_return(message
, NULL
);
225 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
233 r
= bus_verify_polkit_async(
236 "org.freedesktop.login1.manage",
240 &u
->manager
->polkit_registry
,
245 return 1; /* Will call us back */
247 r
= sd_bus_message_read(message
, "i", &signo
);
251 if (!SIGNAL_VALID(signo
))
252 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
254 r
= user_kill(u
, signo
);
258 return sd_bus_reply_method_return(message
, NULL
);
261 static int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
262 Manager
*m
= userdata
;
273 if (streq(path
, "/org/freedesktop/login1/user/self")) {
274 sd_bus_message
*message
;
276 message
= sd_bus_get_current_message(bus
);
278 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
280 sd_bus_error_free(error
);
288 p
= startswith(path
, "/org/freedesktop/login1/user/_");
292 r
= parse_uid(p
, &uid
);
296 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
305 char *user_bus_path(User
*u
) {
310 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->user_record
->uid
) < 0)
316 static int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
317 _cleanup_strv_free_
char **l
= NULL
;
318 sd_bus_message
*message
;
319 Manager
*m
= userdata
;
328 HASHMAP_FOREACH(user
, m
->users
, i
) {
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
);