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
) {
29 User
*u
= ASSERT_PTR(userdata
);
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
) {
46 User
*u
= ASSERT_PTR(userdata
);
51 return sd_bus_message_append(reply
, "u", (uint32_t) u
->user_record
->gid
);
54 static int property_get_name(
57 const char *interface
,
59 sd_bus_message
*reply
,
61 sd_bus_error
*error
) {
63 User
*u
= ASSERT_PTR(userdata
);
68 return sd_bus_message_append(reply
, "s", u
->user_record
->user_name
);
71 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
73 static int property_get_display(
76 const char *interface
,
78 sd_bus_message
*reply
,
80 sd_bus_error
*error
) {
82 _cleanup_free_
char *p
= NULL
;
83 User
*u
= ASSERT_PTR(userdata
);
88 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
92 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
95 static int property_get_sessions(
98 const char *interface
,
100 sd_bus_message
*reply
,
102 sd_bus_error
*error
) {
104 User
*u
= ASSERT_PTR(userdata
);
110 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
114 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
115 _cleanup_free_
char *p
= NULL
;
117 p
= session_bus_path(session
);
121 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
127 return sd_bus_message_close_container(reply
);
130 static int property_get_idle_hint(
133 const char *interface
,
134 const char *property
,
135 sd_bus_message
*reply
,
137 sd_bus_error
*error
) {
139 User
*u
= ASSERT_PTR(userdata
);
144 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
147 static int property_get_idle_since_hint(
150 const char *interface
,
151 const char *property
,
152 sd_bus_message
*reply
,
154 sd_bus_error
*error
) {
156 User
*u
= ASSERT_PTR(userdata
);
157 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
163 (void) user_get_idle_hint(u
, &t
);
164 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
166 return sd_bus_message_append(reply
, "t", k
);
169 static int property_get_linger(
172 const char *interface
,
173 const char *property
,
174 sd_bus_message
*reply
,
176 sd_bus_error
*error
) {
178 User
*u
= ASSERT_PTR(userdata
);
184 r
= user_check_linger_file(u
);
186 return sd_bus_message_append(reply
, "b", r
> 0);
189 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
190 User
*u
= ASSERT_PTR(userdata
);
195 r
= bus_verify_polkit_async_full(
197 "org.freedesktop.login1.manage",
199 /* interactive= */ false,
201 &u
->manager
->polkit_registry
,
206 return 1; /* Will call us back */
208 r
= user_stop(u
, /* force = */ true);
212 return sd_bus_reply_method_return(message
, NULL
);
215 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
216 User
*u
= ASSERT_PTR(userdata
);
222 r
= bus_verify_polkit_async_full(
224 "org.freedesktop.login1.manage",
226 /* interactive= */ false,
228 &u
->manager
->polkit_registry
,
233 return 1; /* Will call us back */
235 r
= sd_bus_message_read(message
, "i", &signo
);
239 if (!SIGNAL_VALID(signo
))
240 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
242 r
= user_kill(u
, signo
);
246 return sd_bus_reply_method_return(message
, NULL
);
249 static int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
250 Manager
*m
= ASSERT_PTR(userdata
);
260 if (streq(path
, "/org/freedesktop/login1/user/self")) {
261 sd_bus_message
*message
;
263 message
= sd_bus_get_current_message(bus
);
265 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
267 sd_bus_error_free(error
);
275 p
= startswith(path
, "/org/freedesktop/login1/user/_");
279 r
= parse_uid(p
, &uid
);
283 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
292 char *user_bus_path(User
*u
) {
297 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->user_record
->uid
) < 0)
303 static int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
304 _cleanup_strv_free_
char **l
= NULL
;
305 sd_bus_message
*message
;
306 Manager
*m
= userdata
;
314 HASHMAP_FOREACH(user
, m
->users
) {
317 p
= user_bus_path(user
);
321 r
= strv_consume(&l
, p
);
326 message
= sd_bus_get_current_message(bus
);
328 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
330 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
334 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
336 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
338 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
346 *nodes
= TAKE_PTR(l
);
351 static const sd_bus_vtable user_vtable
[] = {
352 SD_BUS_VTABLE_START(0),
354 SD_BUS_PROPERTY("UID", "u", property_get_uid
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
355 SD_BUS_PROPERTY("GID", "u", property_get_gid
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
356 SD_BUS_PROPERTY("Name", "s", property_get_name
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
357 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
358 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
359 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
360 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
361 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
362 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
363 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
364 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
365 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
366 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
367 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
369 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
370 SD_BUS_METHOD_WITH_ARGS("Kill",
371 SD_BUS_ARGS("i", signal_number
),
373 bus_user_method_kill
,
374 SD_BUS_VTABLE_UNPRIVILEGED
),
379 const BusObjectImplementation user_object
= {
380 "/org/freedesktop/login1/user",
381 "org.freedesktop.login1.User",
382 .fallback_vtables
= BUS_FALLBACK_VTABLES({user_vtable
, user_object_find
}),
383 .node_enumerator
= user_node_enumerator
,
386 int user_send_signal(User
*u
, bool new_user
) {
387 _cleanup_free_
char *p
= NULL
;
391 p
= user_bus_path(u
);
395 return sd_bus_emit_signal(
397 "/org/freedesktop/login1",
398 "org.freedesktop.login1.Manager",
399 new_user
? "UserNew" : "UserRemoved",
400 "uo", (uint32_t) u
->user_record
->uid
, p
);
403 int user_send_changed(User
*u
, const char *properties
, ...) {
404 _cleanup_free_
char *p
= NULL
;
412 p
= user_bus_path(u
);
416 l
= strv_from_stdarg_alloca(properties
);
418 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);