1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
9 #include "alloc-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-label.h"
14 #include "logind-session-device.h"
15 #include "logind-session.h"
17 #include "signal-util.h"
21 static int property_get_user(
24 const char *interface
,
26 sd_bus_message
*reply
,
28 sd_bus_error
*error
) {
30 _cleanup_free_
char *p
= NULL
;
31 Session
*s
= userdata
;
37 p
= user_bus_path(s
->user
);
41 return sd_bus_message_append(reply
, "(uo)", (uint32_t) s
->user
->uid
, p
);
44 static int property_get_name(
47 const char *interface
,
49 sd_bus_message
*reply
,
51 sd_bus_error
*error
) {
53 Session
*s
= userdata
;
59 return sd_bus_message_append(reply
, "s", s
->user
->name
);
62 static int property_get_seat(
65 const char *interface
,
67 sd_bus_message
*reply
,
69 sd_bus_error
*error
) {
71 _cleanup_free_
char *p
= NULL
;
72 Session
*s
= userdata
;
78 p
= s
->seat
? seat_bus_path(s
->seat
) : strdup("/");
82 return sd_bus_message_append(reply
, "(so)", s
->seat
? s
->seat
->id
: "", p
);
85 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, session_type
, SessionType
);
86 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, session_class
, SessionClass
);
87 static BUS_DEFINE_PROPERTY_GET(property_get_active
, "b", Session
, session_is_active
);
88 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Session
, session_get_state
, session_state_to_string
);
90 static int property_get_idle_hint(
93 const char *interface
,
95 sd_bus_message
*reply
,
97 sd_bus_error
*error
) {
99 Session
*s
= userdata
;
105 return sd_bus_message_append(reply
, "b", session_get_idle_hint(s
, NULL
) > 0);
108 static int property_get_idle_since_hint(
111 const char *interface
,
112 const char *property
,
113 sd_bus_message
*reply
,
115 sd_bus_error
*error
) {
117 Session
*s
= userdata
;
118 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
126 r
= session_get_idle_hint(s
, &t
);
130 u
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
132 return sd_bus_message_append(reply
, "t", u
);
135 static int property_get_locked_hint(
138 const char *interface
,
139 const char *property
,
140 sd_bus_message
*reply
,
142 sd_bus_error
*error
) {
144 Session
*s
= userdata
;
150 return sd_bus_message_append(reply
, "b", session_get_locked_hint(s
) > 0);
153 int bus_session_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
154 Session
*s
= userdata
;
160 r
= bus_verify_polkit_async(
163 "org.freedesktop.login1.manage",
167 &s
->manager
->polkit_registry
,
172 return 1; /* Will call us back */
174 r
= session_stop(s
, true);
178 return sd_bus_reply_method_return(message
, NULL
);
181 int bus_session_method_activate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
182 Session
*s
= userdata
;
188 r
= session_activate(s
);
192 return sd_bus_reply_method_return(message
, NULL
);
195 int bus_session_method_lock(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
196 Session
*s
= userdata
;
202 r
= bus_verify_polkit_async(
205 "org.freedesktop.login1.lock-sessions",
209 &s
->manager
->polkit_registry
,
214 return 1; /* Will call us back */
216 r
= session_send_lock(s
, strstr(sd_bus_message_get_member(message
), "Lock"));
220 return sd_bus_reply_method_return(message
, NULL
);
223 static int method_set_idle_hint(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
224 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
225 Session
*s
= userdata
;
232 r
= sd_bus_message_read(message
, "b", &b
);
236 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
240 r
= sd_bus_creds_get_euid(creds
, &uid
);
244 if (uid
!= 0 && uid
!= s
->user
->uid
)
245 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Only owner of session may set idle hint");
247 session_set_idle_hint(s
, b
);
249 return sd_bus_reply_method_return(message
, NULL
);
252 static int method_set_locked_hint(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
253 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
254 Session
*s
= userdata
;
261 r
= sd_bus_message_read(message
, "b", &b
);
265 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
269 r
= sd_bus_creds_get_euid(creds
, &uid
);
273 if (uid
!= 0 && uid
!= s
->user
->uid
)
274 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Only owner of session may set locked hint");
276 session_set_locked_hint(s
, b
);
278 return sd_bus_reply_method_return(message
, NULL
);
281 int bus_session_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
282 Session
*s
= userdata
;
291 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
298 who
= kill_who_from_string(swho
);
300 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
303 if (!SIGNAL_VALID(signo
))
304 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
306 r
= bus_verify_polkit_async(
309 "org.freedesktop.login1.manage",
313 &s
->manager
->polkit_registry
,
318 return 1; /* Will call us back */
320 r
= session_kill(s
, who
, signo
);
324 return sd_bus_reply_method_return(message
, NULL
);
327 static int method_take_control(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
328 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
329 Session
*s
= userdata
;
336 r
= sd_bus_message_read(message
, "b", &force
);
340 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
344 r
= sd_bus_creds_get_euid(creds
, &uid
);
348 if (uid
!= 0 && (force
|| uid
!= s
->user
->uid
))
349 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Only owner of session may take control");
351 r
= session_set_controller(s
, sd_bus_message_get_sender(message
), force
, true);
355 return sd_bus_reply_method_return(message
, NULL
);
358 static int method_release_control(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
359 Session
*s
= userdata
;
364 if (!session_is_controller(s
, sd_bus_message_get_sender(message
)))
365 return sd_bus_error_setf(error
, BUS_ERROR_NOT_IN_CONTROL
, "You are not in control of this session");
367 session_drop_controller(s
);
369 return sd_bus_reply_method_return(message
, NULL
);
372 static int method_take_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
373 Session
*s
= userdata
;
374 uint32_t major
, minor
;
382 r
= sd_bus_message_read(message
, "uu", &major
, &minor
);
386 if (!session_is_controller(s
, sd_bus_message_get_sender(message
)))
387 return sd_bus_error_setf(error
, BUS_ERROR_NOT_IN_CONTROL
, "You are not in control of this session");
389 dev
= makedev(major
, minor
);
390 sd
= hashmap_get(s
->devices
, &dev
);
392 /* We don't allow retrieving a device multiple times.
393 * The related ReleaseDevice call is not ref-counted.
394 * The caller should use dup() if it requires more
395 * than one fd (it would be functionally
397 return sd_bus_error_setf(error
, BUS_ERROR_DEVICE_IS_TAKEN
, "Device already taken");
399 r
= session_device_new(s
, dev
, true, &sd
);
403 r
= session_device_save(sd
);
407 r
= sd_bus_reply_method_return(message
, "hb", sd
->fd
, !sd
->active
);
415 session_device_free(sd
);
419 static int method_release_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
420 Session
*s
= userdata
;
421 uint32_t major
, minor
;
429 r
= sd_bus_message_read(message
, "uu", &major
, &minor
);
433 if (!session_is_controller(s
, sd_bus_message_get_sender(message
)))
434 return sd_bus_error_setf(error
, BUS_ERROR_NOT_IN_CONTROL
, "You are not in control of this session");
436 dev
= makedev(major
, minor
);
437 sd
= hashmap_get(s
->devices
, &dev
);
439 return sd_bus_error_setf(error
, BUS_ERROR_DEVICE_NOT_TAKEN
, "Device not taken");
441 session_device_free(sd
);
444 return sd_bus_reply_method_return(message
, NULL
);
447 static int method_pause_device_complete(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
448 Session
*s
= userdata
;
449 uint32_t major
, minor
;
457 r
= sd_bus_message_read(message
, "uu", &major
, &minor
);
461 if (!session_is_controller(s
, sd_bus_message_get_sender(message
)))
462 return sd_bus_error_setf(error
, BUS_ERROR_NOT_IN_CONTROL
, "You are not in control of this session");
464 dev
= makedev(major
, minor
);
465 sd
= hashmap_get(s
->devices
, &dev
);
467 return sd_bus_error_setf(error
, BUS_ERROR_DEVICE_NOT_TAKEN
, "Device not taken");
469 session_device_complete_pause(sd
);
471 return sd_bus_reply_method_return(message
, NULL
);
474 const sd_bus_vtable session_vtable
[] = {
475 SD_BUS_VTABLE_START(0),
477 SD_BUS_PROPERTY("Id", "s", NULL
, offsetof(Session
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
478 SD_BUS_PROPERTY("User", "(uo)", property_get_user
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
479 SD_BUS_PROPERTY("Name", "s", property_get_name
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
480 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Session
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
481 SD_BUS_PROPERTY("VTNr", "u", NULL
, offsetof(Session
, vtnr
), SD_BUS_VTABLE_PROPERTY_CONST
),
482 SD_BUS_PROPERTY("Seat", "(so)", property_get_seat
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
483 SD_BUS_PROPERTY("TTY", "s", NULL
, offsetof(Session
, tty
), SD_BUS_VTABLE_PROPERTY_CONST
),
484 SD_BUS_PROPERTY("Display", "s", NULL
, offsetof(Session
, display
), SD_BUS_VTABLE_PROPERTY_CONST
),
485 SD_BUS_PROPERTY("Remote", "b", bus_property_get_bool
, offsetof(Session
, remote
), SD_BUS_VTABLE_PROPERTY_CONST
),
486 SD_BUS_PROPERTY("RemoteHost", "s", NULL
, offsetof(Session
, remote_host
), SD_BUS_VTABLE_PROPERTY_CONST
),
487 SD_BUS_PROPERTY("RemoteUser", "s", NULL
, offsetof(Session
, remote_user
), SD_BUS_VTABLE_PROPERTY_CONST
),
488 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Session
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
489 SD_BUS_PROPERTY("Desktop", "s", NULL
, offsetof(Session
, desktop
), SD_BUS_VTABLE_PROPERTY_CONST
),
490 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Session
, scope
), SD_BUS_VTABLE_PROPERTY_CONST
),
491 SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid
, offsetof(Session
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
492 SD_BUS_PROPERTY("Audit", "u", NULL
, offsetof(Session
, audit_id
), SD_BUS_VTABLE_PROPERTY_CONST
),
493 SD_BUS_PROPERTY("Type", "s", property_get_type
, offsetof(Session
, type
), SD_BUS_VTABLE_PROPERTY_CONST
),
494 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Session
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
495 SD_BUS_PROPERTY("Active", "b", property_get_active
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
496 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
497 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
498 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
499 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
500 SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
502 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_session_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
503 SD_BUS_METHOD("Activate", NULL
, NULL
, bus_session_method_activate
, SD_BUS_VTABLE_UNPRIVILEGED
),
504 SD_BUS_METHOD("Lock", NULL
, NULL
, bus_session_method_lock
, SD_BUS_VTABLE_UNPRIVILEGED
),
505 SD_BUS_METHOD("Unlock", NULL
, NULL
, bus_session_method_lock
, SD_BUS_VTABLE_UNPRIVILEGED
),
506 SD_BUS_METHOD("SetIdleHint", "b", NULL
, method_set_idle_hint
, SD_BUS_VTABLE_UNPRIVILEGED
),
507 SD_BUS_METHOD("SetLockedHint", "b", NULL
, method_set_locked_hint
, SD_BUS_VTABLE_UNPRIVILEGED
),
508 SD_BUS_METHOD("Kill", "si", NULL
, bus_session_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
509 SD_BUS_METHOD("TakeControl", "b", NULL
, method_take_control
, SD_BUS_VTABLE_UNPRIVILEGED
),
510 SD_BUS_METHOD("ReleaseControl", NULL
, NULL
, method_release_control
, SD_BUS_VTABLE_UNPRIVILEGED
),
511 SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
512 SD_BUS_METHOD("ReleaseDevice", "uu", NULL
, method_release_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
513 SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL
, method_pause_device_complete
, SD_BUS_VTABLE_UNPRIVILEGED
),
515 SD_BUS_SIGNAL("PauseDevice", "uus", 0),
516 SD_BUS_SIGNAL("ResumeDevice", "uuh", 0),
517 SD_BUS_SIGNAL("Lock", NULL
, 0),
518 SD_BUS_SIGNAL("Unlock", NULL
, 0),
523 int session_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
524 Manager
*m
= userdata
;
534 if (streq(path
, "/org/freedesktop/login1/session/self")) {
535 sd_bus_message
*message
;
537 message
= sd_bus_get_current_message(bus
);
541 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
545 _cleanup_free_
char *e
= NULL
;
548 p
= startswith(path
, "/org/freedesktop/login1/session/");
552 e
= bus_label_unescape(p
);
556 session
= hashmap_get(m
->sessions
, e
);
565 char *session_bus_path(Session
*s
) {
566 _cleanup_free_
char *t
= NULL
;
570 t
= bus_label_escape(s
->id
);
574 return strappend("/org/freedesktop/login1/session/", t
);
577 int session_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
578 _cleanup_strv_free_
char **l
= NULL
;
579 sd_bus_message
*message
;
580 Manager
*m
= userdata
;
589 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
592 p
= session_bus_path(session
);
596 r
= strv_consume(&l
, p
);
601 message
= sd_bus_get_current_message(bus
);
603 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
606 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
608 r
= sd_bus_creds_get_session(creds
, &name
);
610 session
= hashmap_get(m
->sessions
, name
);
612 r
= strv_extend(&l
, "/org/freedesktop/login1/session/self");
620 *nodes
= TAKE_PTR(l
);
625 int session_send_signal(Session
*s
, bool new_session
) {
626 _cleanup_free_
char *p
= NULL
;
630 p
= session_bus_path(s
);
634 return sd_bus_emit_signal(
636 "/org/freedesktop/login1",
637 "org.freedesktop.login1.Manager",
638 new_session
? "SessionNew" : "SessionRemoved",
642 int session_send_changed(Session
*s
, const char *properties
, ...) {
643 _cleanup_free_
char *p
= NULL
;
651 p
= session_bus_path(s
);
655 l
= strv_from_stdarg_alloca(properties
);
657 return sd_bus_emit_properties_changed_strv(s
->manager
->bus
, p
, "org.freedesktop.login1.Session", l
);
660 int session_send_lock(Session
*s
, bool lock
) {
661 _cleanup_free_
char *p
= NULL
;
665 p
= session_bus_path(s
);
669 return sd_bus_emit_signal(
672 "org.freedesktop.login1.Session",
673 lock
? "Lock" : "Unlock",
677 int session_send_lock_all(Manager
*m
, bool lock
) {
684 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
687 k
= session_send_lock(session
, lock
);
695 int session_send_create_reply(Session
*s
, sd_bus_error
*error
) {
696 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
697 _cleanup_close_
int fifo_fd
= -1;
698 _cleanup_free_
char *p
= NULL
;
702 /* This is called after the session scope and the user service
703 * were successfully created, and finishes where
704 * bus_manager_create_session() left off. */
706 if (!s
->create_message
)
709 if (!sd_bus_error_is_set(error
) && (s
->scope_job
|| s
->user
->service_job
))
712 c
= s
->create_message
;
713 s
->create_message
= NULL
;
716 return sd_bus_reply_method_error(c
, error
);
718 fifo_fd
= session_create_fifo(s
);
722 /* Update the session state file before we notify the client
723 * about the result. */
726 p
= session_bus_path(s
);
730 log_debug("Sending reply about created session: "
731 "id=%s object_path=%s uid=%u runtime_path=%s "
732 "session_fd=%d seat=%s vtnr=%u",
735 (uint32_t) s
->user
->uid
,
736 s
->user
->runtime_path
,
738 s
->seat
? s
->seat
->id
: "",
741 return sd_bus_reply_method_return(
745 s
->user
->runtime_path
,
747 (uint32_t) s
->user
->uid
,
748 s
->seat
? s
->seat
->id
: "",