| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-common-errors.h" |
| 7 |
#include "bus-get-properties.h" |
| 8 |
#include "bus-label.h" |
| 9 |
#include "bus-object.h" |
| 10 |
#include "bus-polkit.h" |
| 11 |
#include "hashmap.h" |
| 12 |
#include "logind-session.h" |
| 13 |
#include "logind.h" |
| 14 |
#include "logind-dbus.h" |
| 15 |
#include "logind-polkit.h" |
| 16 |
#include "logind-seat.h" |
| 17 |
#include "logind-seat-dbus.h" |
| 18 |
#include "logind-session-dbus.h" |
| 19 |
#include "logind-user.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "user-util.h" |
| 23 |
|
| 24 |
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_const_true, "b", true); |
| 25 |
static BUS_DEFINE_PROPERTY_GET(property_get_can_tty, "b", Seat, seat_can_tty); |
| 26 |
static BUS_DEFINE_PROPERTY_GET(property_get_can_graphical, "b", Seat, seat_can_graphical); |
| 27 |
|
| 28 |
static int property_get_active_session( |
| 29 |
sd_bus *bus, |
| 30 |
const char *path, |
| 31 |
const char *interface, |
| 32 |
const char *property, |
| 33 |
sd_bus_message *reply, |
| 34 |
void *userdata, |
| 35 |
sd_bus_error *error) { |
| 36 |
|
| 37 |
_cleanup_free_ char *p = NULL; |
| 38 |
Seat *s = ASSERT_PTR(userdata); |
| 39 |
|
| 40 |
assert(bus); |
| 41 |
assert(reply); |
| 42 |
|
| 43 |
p = s->active ? session_bus_path(s->active) : strdup("/"); |
| 44 |
if (!p) |
| 45 |
return -ENOMEM; |
| 46 |
|
| 47 |
return sd_bus_message_append(reply, "(so)", s->active ? s->active->id : "", p); |
| 48 |
} |
| 49 |
|
| 50 |
static int property_get_sessions( |
| 51 |
sd_bus *bus, |
| 52 |
const char *path, |
| 53 |
const char *interface, |
| 54 |
const char *property, |
| 55 |
sd_bus_message *reply, |
| 56 |
void *userdata, |
| 57 |
sd_bus_error *error) { |
| 58 |
|
| 59 |
Seat *s = ASSERT_PTR(userdata); |
| 60 |
int r; |
| 61 |
|
| 62 |
assert(bus); |
| 63 |
assert(reply); |
| 64 |
|
| 65 |
r = sd_bus_message_open_container(reply, 'a', "(so)"); |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
LIST_FOREACH(sessions_by_seat, session, s->sessions) { |
| 70 |
_cleanup_free_ char *p = NULL; |
| 71 |
|
| 72 |
p = session_bus_path(session); |
| 73 |
if (!p) |
| 74 |
return -ENOMEM; |
| 75 |
|
| 76 |
r = sd_bus_message_append(reply, "(so)", session->id, p); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
r = sd_bus_message_close_container(reply); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
static int property_get_idle_hint( |
| 90 |
sd_bus *bus, |
| 91 |
const char *path, |
| 92 |
const char *interface, |
| 93 |
const char *property, |
| 94 |
sd_bus_message *reply, |
| 95 |
void *userdata, |
| 96 |
sd_bus_error *error) { |
| 97 |
|
| 98 |
Seat *s = ASSERT_PTR(userdata); |
| 99 |
|
| 100 |
assert(bus); |
| 101 |
assert(reply); |
| 102 |
|
| 103 |
return sd_bus_message_append(reply, "b", seat_get_idle_hint(s, /* ret_timestamp= */ NULL)); |
| 104 |
} |
| 105 |
|
| 106 |
static int property_get_idle_since_hint( |
| 107 |
sd_bus *bus, |
| 108 |
const char *path, |
| 109 |
const char *interface, |
| 110 |
const char *property, |
| 111 |
sd_bus_message *reply, |
| 112 |
void *userdata, |
| 113 |
sd_bus_error *error) { |
| 114 |
|
| 115 |
Seat *s = ASSERT_PTR(userdata); |
| 116 |
dual_timestamp t; |
| 117 |
uint64_t u; |
| 118 |
|
| 119 |
assert(bus); |
| 120 |
assert(reply); |
| 121 |
|
| 122 |
seat_get_idle_hint(s, &t); |
| 123 |
|
| 124 |
u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic; |
| 125 |
|
| 126 |
return sd_bus_message_append(reply, "t", u); |
| 127 |
} |
| 128 |
|
| 129 |
int bus_seat_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 130 |
Seat *s = ASSERT_PTR(userdata); |
| 131 |
int r; |
| 132 |
|
| 133 |
assert(message); |
| 134 |
|
| 135 |
r = bus_verify_polkit_async( |
| 136 |
message, |
| 137 |
"org.freedesktop.login1.manage", |
| 138 |
/* details= */ NULL, |
| 139 |
&s->manager->polkit_registry, |
| 140 |
error); |
| 141 |
if (r < 0) |
| 142 |
return r; |
| 143 |
if (r == 0) |
| 144 |
return 1; /* Will call us back */ |
| 145 |
|
| 146 |
r = seat_stop_sessions(s, /* force= */ true); |
| 147 |
if (r < 0) |
| 148 |
return r; |
| 149 |
|
| 150 |
return sd_bus_reply_method_return(message, NULL); |
| 151 |
} |
| 152 |
|
| 153 |
static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 154 |
Seat *s = ASSERT_PTR(userdata); |
| 155 |
const char *name; |
| 156 |
Session *session; |
| 157 |
int r; |
| 158 |
|
| 159 |
assert(message); |
| 160 |
|
| 161 |
r = sd_bus_message_read(message, "s", &name); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
|
| 165 |
session = hashmap_get(s->manager->sessions, name); |
| 166 |
if (!session) |
| 167 |
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
| 168 |
|
| 169 |
if (session->seat != s) |
| 170 |
return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", name, s->id); |
| 171 |
|
| 172 |
r = check_polkit_chvt(message, s->manager, error); |
| 173 |
if (r < 0) |
| 174 |
return r; |
| 175 |
if (r == 0) |
| 176 |
return 1; /* Will call us back */ |
| 177 |
|
| 178 |
r = session_activate(session); |
| 179 |
if (r < 0) |
| 180 |
return r; |
| 181 |
|
| 182 |
return sd_bus_reply_method_return(message, NULL); |
| 183 |
} |
| 184 |
|
| 185 |
static int method_switch_to(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 186 |
Seat *s = ASSERT_PTR(userdata); |
| 187 |
unsigned to; |
| 188 |
int r; |
| 189 |
|
| 190 |
assert(message); |
| 191 |
|
| 192 |
r = sd_bus_message_read(message, "u", &to); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
if (to <= 0) |
| 197 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid virtual terminal"); |
| 198 |
|
| 199 |
r = check_polkit_chvt(message, s->manager, error); |
| 200 |
if (r < 0) |
| 201 |
return r; |
| 202 |
if (r == 0) |
| 203 |
return 1; /* Will call us back */ |
| 204 |
|
| 205 |
r = seat_switch_to(s, to); |
| 206 |
if (r < 0) |
| 207 |
return r; |
| 208 |
|
| 209 |
return sd_bus_reply_method_return(message, NULL); |
| 210 |
} |
| 211 |
|
| 212 |
static int method_switch_to_next(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 213 |
Seat *s = ASSERT_PTR(userdata); |
| 214 |
int r; |
| 215 |
|
| 216 |
assert(message); |
| 217 |
|
| 218 |
r = check_polkit_chvt(message, s->manager, error); |
| 219 |
if (r < 0) |
| 220 |
return r; |
| 221 |
if (r == 0) |
| 222 |
return 1; /* Will call us back */ |
| 223 |
|
| 224 |
r = seat_switch_to_next(s); |
| 225 |
if (r < 0) |
| 226 |
return r; |
| 227 |
|
| 228 |
return sd_bus_reply_method_return(message, NULL); |
| 229 |
} |
| 230 |
|
| 231 |
static int method_switch_to_previous(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 232 |
Seat *s = ASSERT_PTR(userdata); |
| 233 |
int r; |
| 234 |
|
| 235 |
assert(message); |
| 236 |
|
| 237 |
r = check_polkit_chvt(message, s->manager, error); |
| 238 |
if (r < 0) |
| 239 |
return r; |
| 240 |
if (r == 0) |
| 241 |
return 1; /* Will call us back */ |
| 242 |
|
| 243 |
r = seat_switch_to_previous(s); |
| 244 |
if (r < 0) |
| 245 |
return r; |
| 246 |
|
| 247 |
return sd_bus_reply_method_return(message, NULL); |
| 248 |
} |
| 249 |
|
| 250 |
static int seat_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
| 251 |
_cleanup_free_ char *e = NULL; |
| 252 |
sd_bus_message *message; |
| 253 |
Manager *m = ASSERT_PTR(userdata); |
| 254 |
const char *p; |
| 255 |
Seat *seat; |
| 256 |
int r; |
| 257 |
|
| 258 |
assert(bus); |
| 259 |
assert(path); |
| 260 |
assert(interface); |
| 261 |
assert(found); |
| 262 |
|
| 263 |
p = startswith(path, "/org/freedesktop/login1/seat/"); |
| 264 |
if (!p) |
| 265 |
return 0; |
| 266 |
|
| 267 |
e = bus_label_unescape(p); |
| 268 |
if (!e) |
| 269 |
return -ENOMEM; |
| 270 |
|
| 271 |
message = sd_bus_get_current_message(bus); |
| 272 |
|
| 273 |
r = manager_get_seat_from_creds(m, message, e, error, &seat); |
| 274 |
if (r == -ENXIO) { |
| 275 |
sd_bus_error_free(error); |
| 276 |
return 0; |
| 277 |
} |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
*found = seat; |
| 282 |
return 1; |
| 283 |
} |
| 284 |
|
| 285 |
char* seat_bus_path(Seat *s) { |
| 286 |
_cleanup_free_ char *t = NULL; |
| 287 |
|
| 288 |
assert(s); |
| 289 |
|
| 290 |
t = bus_label_escape(s->id); |
| 291 |
if (!t) |
| 292 |
return NULL; |
| 293 |
|
| 294 |
return strjoin("/org/freedesktop/login1/seat/", t); |
| 295 |
} |
| 296 |
|
| 297 |
static int seat_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
| 298 |
_cleanup_strv_free_ char **l = NULL; |
| 299 |
sd_bus_message *message; |
| 300 |
Manager *m = userdata; |
| 301 |
Seat *seat; |
| 302 |
int r; |
| 303 |
|
| 304 |
assert(bus); |
| 305 |
assert(path); |
| 306 |
assert(nodes); |
| 307 |
|
| 308 |
HASHMAP_FOREACH(seat, m->seats) { |
| 309 |
char *p; |
| 310 |
|
| 311 |
p = seat_bus_path(seat); |
| 312 |
if (!p) |
| 313 |
return -ENOMEM; |
| 314 |
|
| 315 |
r = strv_consume(&l, p); |
| 316 |
if (r < 0) |
| 317 |
return r; |
| 318 |
} |
| 319 |
|
| 320 |
message = sd_bus_get_current_message(bus); |
| 321 |
if (message) { |
| 322 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 323 |
|
| 324 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); |
| 325 |
if (r >= 0) { |
| 326 |
bool may_auto = false; |
| 327 |
const char *name; |
| 328 |
|
| 329 |
r = sd_bus_creds_get_session(creds, &name); |
| 330 |
if (r >= 0) { |
| 331 |
Session *session; |
| 332 |
|
| 333 |
session = hashmap_get(m->sessions, name); |
| 334 |
if (session && session->seat) { |
| 335 |
r = strv_extend(&l, "/org/freedesktop/login1/seat/self"); |
| 336 |
if (r < 0) |
| 337 |
return r; |
| 338 |
|
| 339 |
may_auto = true; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if (!may_auto) { |
| 344 |
uid_t uid; |
| 345 |
|
| 346 |
r = sd_bus_creds_get_owner_uid(creds, &uid); |
| 347 |
if (r >= 0) { |
| 348 |
User *user; |
| 349 |
|
| 350 |
user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 351 |
may_auto = user && user->display && user->display->seat; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
if (may_auto) { |
| 356 |
r = strv_extend(&l, "/org/freedesktop/login1/seat/auto"); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
*nodes = TAKE_PTR(l); |
| 364 |
return 1; |
| 365 |
} |
| 366 |
|
| 367 |
int seat_send_signal(Seat *s, bool new_seat) { |
| 368 |
_cleanup_free_ char *p = NULL; |
| 369 |
|
| 370 |
assert(s); |
| 371 |
|
| 372 |
p = seat_bus_path(s); |
| 373 |
if (!p) |
| 374 |
return -ENOMEM; |
| 375 |
|
| 376 |
return sd_bus_emit_signal( |
| 377 |
s->manager->bus, |
| 378 |
"/org/freedesktop/login1", |
| 379 |
"org.freedesktop.login1.Manager", |
| 380 |
new_seat ? "SeatNew" : "SeatRemoved", |
| 381 |
"so", s->id, p); |
| 382 |
} |
| 383 |
|
| 384 |
int seat_send_changed_strv(Seat *s, char **properties) { |
| 385 |
_cleanup_free_ char *p = NULL; |
| 386 |
|
| 387 |
assert(s); |
| 388 |
|
| 389 |
if (!s->started) |
| 390 |
return 0; |
| 391 |
|
| 392 |
p = seat_bus_path(s); |
| 393 |
if (!p) |
| 394 |
return -ENOMEM; |
| 395 |
|
| 396 |
return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Seat", properties); |
| 397 |
} |
| 398 |
|
| 399 |
static const sd_bus_vtable seat_vtable[] = { |
| 400 |
SD_BUS_VTABLE_START(0), |
| 401 |
|
| 402 |
SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Seat, id), SD_BUS_VTABLE_PROPERTY_CONST), |
| 403 |
SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 404 |
SD_BUS_PROPERTY("CanMultiSession", "b", property_get_const_true, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
| 405 |
SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 406 |
SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 407 |
SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0), |
| 408 |
SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 409 |
SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 410 |
SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 411 |
|
| 412 |
SD_BUS_METHOD("Terminate", NULL, NULL, bus_seat_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED), |
| 413 |
|
| 414 |
SD_BUS_METHOD_WITH_ARGS("ActivateSession", |
| 415 |
SD_BUS_ARGS("s", session_id), |
| 416 |
SD_BUS_NO_RESULT, |
| 417 |
method_activate_session, |
| 418 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 419 |
SD_BUS_METHOD_WITH_ARGS("SwitchTo", |
| 420 |
SD_BUS_ARGS("u", vtnr), |
| 421 |
SD_BUS_NO_RESULT, |
| 422 |
method_switch_to, |
| 423 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 424 |
|
| 425 |
SD_BUS_METHOD("SwitchToNext", NULL, NULL, method_switch_to_next, SD_BUS_VTABLE_UNPRIVILEGED), |
| 426 |
SD_BUS_METHOD("SwitchToPrevious", NULL, NULL, method_switch_to_previous, SD_BUS_VTABLE_UNPRIVILEGED), |
| 427 |
|
| 428 |
SD_BUS_VTABLE_END |
| 429 |
}; |
| 430 |
|
| 431 |
const BusObjectImplementation seat_object = { |
| 432 |
"/org/freedesktop/login1/seat", |
| 433 |
"org.freedesktop.login1.Seat", |
| 434 |
.fallback_vtables = BUS_FALLBACK_VTABLES({seat_vtable, seat_object_find}), |
| 435 |
.node_enumerator = seat_node_enumerator, |
| 436 |
}; |