1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.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 BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", User
, user_get_state
, user_state_to_string
);
21 static int property_get_display(
24 const char *interface
,
26 sd_bus_message
*reply
,
28 sd_bus_error
*error
) {
30 _cleanup_free_
char *p
= NULL
;
37 p
= u
->display
? session_bus_path(u
->display
) : strdup("/");
41 return sd_bus_message_append(reply
, "(so)", u
->display
? u
->display
->id
: "", p
);
44 static int property_get_sessions(
47 const char *interface
,
49 sd_bus_message
*reply
,
51 sd_bus_error
*error
) {
61 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
65 LIST_FOREACH(sessions_by_user
, session
, u
->sessions
) {
66 _cleanup_free_
char *p
= NULL
;
68 p
= session_bus_path(session
);
72 r
= sd_bus_message_append(reply
, "(so)", session
->id
, p
);
78 return sd_bus_message_close_container(reply
);
81 static int property_get_idle_hint(
84 const char *interface
,
86 sd_bus_message
*reply
,
88 sd_bus_error
*error
) {
96 return sd_bus_message_append(reply
, "b", user_get_idle_hint(u
, NULL
) > 0);
99 static int property_get_idle_since_hint(
102 const char *interface
,
103 const char *property
,
104 sd_bus_message
*reply
,
106 sd_bus_error
*error
) {
109 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
116 (void) user_get_idle_hint(u
, &t
);
117 k
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
119 return sd_bus_message_append(reply
, "t", k
);
122 static int property_get_linger(
125 const char *interface
,
126 const char *property
,
127 sd_bus_message
*reply
,
129 sd_bus_error
*error
) {
138 r
= user_check_linger_file(u
);
140 return sd_bus_message_append(reply
, "b", r
> 0);
143 int bus_user_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
150 r
= bus_verify_polkit_async(
153 "org.freedesktop.login1.manage",
157 &u
->manager
->polkit_registry
,
162 return 1; /* Will call us back */
164 r
= user_stop(u
, true);
168 return sd_bus_reply_method_return(message
, NULL
);
171 int bus_user_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
179 r
= bus_verify_polkit_async(
182 "org.freedesktop.login1.manage",
186 &u
->manager
->polkit_registry
,
191 return 1; /* Will call us back */
193 r
= sd_bus_message_read(message
, "i", &signo
);
197 if (!SIGNAL_VALID(signo
))
198 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
200 r
= user_kill(u
, signo
);
204 return sd_bus_reply_method_return(message
, NULL
);
207 const sd_bus_vtable user_vtable
[] = {
208 SD_BUS_VTABLE_START(0),
210 SD_BUS_PROPERTY("UID", "u", bus_property_get_uid
, offsetof(User
, uid
), SD_BUS_VTABLE_PROPERTY_CONST
),
211 SD_BUS_PROPERTY("GID", "u", bus_property_get_gid
, offsetof(User
, gid
), SD_BUS_VTABLE_PROPERTY_CONST
),
212 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(User
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
213 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
214 SD_BUS_PROPERTY("RuntimePath", "s", NULL
, offsetof(User
, runtime_path
), SD_BUS_VTABLE_PROPERTY_CONST
),
215 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(User
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
216 SD_BUS_PROPERTY("Slice", "s", NULL
, offsetof(User
, slice
), SD_BUS_VTABLE_PROPERTY_CONST
),
217 SD_BUS_PROPERTY("Display", "(so)", property_get_display
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
218 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
219 SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions
, 0, 0),
220 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
221 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
222 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
223 SD_BUS_PROPERTY("Linger", "b", property_get_linger
, 0, 0),
225 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_user_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
226 SD_BUS_METHOD("Kill", "i", NULL
, bus_user_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
231 int user_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
232 Manager
*m
= userdata
;
243 if (streq(path
, "/org/freedesktop/login1/user/self")) {
244 sd_bus_message
*message
;
246 message
= sd_bus_get_current_message(bus
);
250 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
252 sd_bus_error_free(error
);
260 p
= startswith(path
, "/org/freedesktop/login1/user/_");
264 r
= parse_uid(p
, &uid
);
268 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
277 char *user_bus_path(User
*u
) {
282 if (asprintf(&s
, "/org/freedesktop/login1/user/_"UID_FMT
, u
->uid
) < 0)
288 int user_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
289 _cleanup_strv_free_
char **l
= NULL
;
290 sd_bus_message
*message
;
291 Manager
*m
= userdata
;
300 HASHMAP_FOREACH(user
, m
->users
, i
) {
303 p
= user_bus_path(user
);
307 r
= strv_consume(&l
, p
);
312 message
= sd_bus_get_current_message(bus
);
314 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
316 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
320 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
322 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
324 r
= strv_extend(&l
, "/org/freedesktop/login1/user/self");
332 *nodes
= TAKE_PTR(l
);
337 int user_send_signal(User
*u
, bool new_user
) {
338 _cleanup_free_
char *p
= NULL
;
342 p
= user_bus_path(u
);
346 return sd_bus_emit_signal(
348 "/org/freedesktop/login1",
349 "org.freedesktop.login1.Manager",
350 new_user
? "UserNew" : "UserRemoved",
351 "uo", (uint32_t) u
->uid
, p
);
354 int user_send_changed(User
*u
, const char *properties
, ...) {
355 _cleanup_free_
char *p
= NULL
;
363 p
= user_bus_path(u
);
367 l
= strv_from_stdarg_alloca(properties
);
369 return sd_bus_emit_properties_changed_strv(u
->manager
->bus
, p
, "org.freedesktop.login1.User", l
);