| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-event.h" |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "bus-error.h" |
| 8 |
#include "bus-polkit.h" |
| 9 |
#include "cgroup-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "format-util.h" |
| 12 |
#include "hashmap.h" |
| 13 |
#include "json-util.h" |
| 14 |
#include "login-util.h" |
| 15 |
#include "logind.h" |
| 16 |
#include "logind-dbus.h" |
| 17 |
#include "logind-inhibit.h" |
| 18 |
#include "logind-seat.h" |
| 19 |
#include "logind-session.h" |
| 20 |
#include "logind-shutdown.h" |
| 21 |
#include "logind-user.h" |
| 22 |
#include "logind-varlink.h" |
| 23 |
#include "strv.h" |
| 24 |
#include "terminal-util.h" |
| 25 |
#include "user-record.h" |
| 26 |
#include "user-util.h" |
| 27 |
#include "varlink-io.systemd.Login.h" |
| 28 |
#include "varlink-io.systemd.Shutdown.h" |
| 29 |
#include "varlink-io.systemd.service.h" |
| 30 |
#include "varlink-util.h" |
| 31 |
|
| 32 |
static int manager_varlink_get_session_by_peer( |
| 33 |
Manager *m, |
| 34 |
sd_varlink *link, |
| 35 |
bool consult_display, |
| 36 |
Session **ret) { |
| 37 |
|
| 38 |
int r; |
| 39 |
|
| 40 |
assert(m); |
| 41 |
assert(link); |
| 42 |
assert(ret); |
| 43 |
|
| 44 |
/* Determines the session of the peer. If the peer is not part of a session, but consult_display is |
| 45 |
* true, then will return the display session of the peer's owning user. Returns 0 with *ret set to |
| 46 |
* NULL if no session could be determined; the caller decides which error to report to the client. */ |
| 47 |
|
| 48 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 49 |
r = varlink_get_peer_pidref(link, &pidref); |
| 50 |
if (r < 0) |
| 51 |
return log_error_errno(r, "Failed to acquire peer PID: %m"); |
| 52 |
|
| 53 |
Session *session = NULL; |
| 54 |
_cleanup_free_ char *name = NULL; |
| 55 |
r = cg_pidref_get_session(&pidref, &name); |
| 56 |
if (r < 0) { |
| 57 |
if (!consult_display) |
| 58 |
log_debug_errno(r, "Failed to acquire session of peer, giving up: %m"); |
| 59 |
else { |
| 60 |
log_debug_errno(r, "Failed to acquire session of peer, trying to find owner UID: %m"); |
| 61 |
|
| 62 |
uid_t uid; |
| 63 |
r = cg_pidref_get_owner_uid(&pidref, &uid); |
| 64 |
if (r < 0) |
| 65 |
log_debug_errno(r, "Failed to acquire owning UID of peer, giving up: %m"); |
| 66 |
else { |
| 67 |
User *user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 68 |
if (user) |
| 69 |
session = user->display; |
| 70 |
} |
| 71 |
} |
| 72 |
} else |
| 73 |
session = hashmap_get(m->sessions, name); |
| 74 |
|
| 75 |
*ret = session; |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
static int lookup_session_by_name(Manager *m, sd_varlink *link, const char *name, Session **ret) { |
| 80 |
int r; |
| 81 |
|
| 82 |
assert(m); |
| 83 |
assert(link); |
| 84 |
assert(name); |
| 85 |
assert(ret); |
| 86 |
|
| 87 |
if (session_is_self(name) || session_is_auto(name)) { |
| 88 |
Session *peer; |
| 89 |
r = manager_varlink_get_session_by_peer(m, link, /* consult_display= */ session_is_auto(name), &peer); |
| 90 |
if (r < 0) |
| 91 |
return r; |
| 92 |
if (!peer) |
| 93 |
return -ESRCH; |
| 94 |
|
| 95 |
*ret = peer; |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
if (!session_id_valid(name)) |
| 100 |
return -EINVAL; |
| 101 |
|
| 102 |
Session *session = hashmap_get(m->sessions, name); |
| 103 |
if (!session) |
| 104 |
return -ESRCH; |
| 105 |
|
| 106 |
*ret = session; |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
static int lookup_session_by_pidref(Manager *m, const PidRef *pidref, Session **ret) { |
| 111 |
int r; |
| 112 |
|
| 113 |
assert(m); |
| 114 |
assert(pidref); |
| 115 |
assert(ret); |
| 116 |
|
| 117 |
Session *session; |
| 118 |
r = manager_get_session_by_pidref(m, pidref, &session); |
| 119 |
if (r < 0) |
| 120 |
return log_debug_errno(r, "Failed to look up session by PID " PID_FMT ": %m", pidref->pid); |
| 121 |
if (!session) |
| 122 |
return -ESRCH; |
| 123 |
|
| 124 |
*ret = session; |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static int manager_varlink_get_session_by_name_or_pidref( |
| 129 |
Manager *m, |
| 130 |
sd_varlink *link, |
| 131 |
const char *name, |
| 132 |
const PidRef *pidref, |
| 133 |
Session **ret) { |
| 134 |
|
| 135 |
Session *by_name = NULL, *by_pid = NULL; |
| 136 |
int r; |
| 137 |
|
| 138 |
assert(m); |
| 139 |
assert(link); |
| 140 |
assert(ret); |
| 141 |
|
| 142 |
/* Resolves a session by name and/or PID. Supports the special names "self" and "auto" for the name |
| 143 |
* argument. If both name and pidref are unset, resolves to the caller's session. If both name and |
| 144 |
* pidref are set they must refer to the same session, otherwise -ESRCH is returned. Returns -ESRCH |
| 145 |
* on "not found". Caller is expected to turn that into a varlink error, typically via |
| 146 |
* sd_varlink_set_sentinel(). Returns negative errno on other failures. */ |
| 147 |
|
| 148 |
if (name) { |
| 149 |
r = lookup_session_by_name(m, link, name, &by_name); |
| 150 |
if (r == -EINVAL) |
| 151 |
return sd_varlink_error_invalid_parameter_name(link, "Id"); |
| 152 |
if (r < 0) |
| 153 |
return r; |
| 154 |
} |
| 155 |
|
| 156 |
if (pidref && pidref_is_set(pidref)) { |
| 157 |
r = lookup_session_by_pidref(m, pidref, &by_pid); |
| 158 |
if (r == -EINVAL) |
| 159 |
return sd_varlink_error_invalid_parameter_name(link, "PID"); |
| 160 |
if (r < 0) |
| 161 |
return r; |
| 162 |
} |
| 163 |
|
| 164 |
if (by_name && by_pid && by_name != by_pid) |
| 165 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), |
| 166 |
"Search by session id '%s' and PID " PID_FMT " resulted in two different sessions", |
| 167 |
name, pidref->pid); |
| 168 |
|
| 169 |
if (by_name || by_pid) { |
| 170 |
*ret = by_name ?: by_pid; |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
/* Neither filter set — fall back to caller's session. */ |
| 175 |
Session *by_peer; |
| 176 |
r = manager_varlink_get_session_by_peer(m, link, /* consult_display= */ true, &by_peer); |
| 177 |
if (r < 0) |
| 178 |
return r; |
| 179 |
if (!by_peer) |
| 180 |
return -ESRCH; |
| 181 |
|
| 182 |
*ret = by_peer; |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
int session_send_create_reply_varlink(Session *s, const sd_bus_error *error) { |
| 187 |
assert(s); |
| 188 |
|
| 189 |
/* This is called after the session scope and the user service were successfully created, and |
| 190 |
* finishes where manager_create_session() left off. */ |
| 191 |
|
| 192 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = TAKE_PTR(s->create_link); |
| 193 |
if (!vl) |
| 194 |
return 0; |
| 195 |
|
| 196 |
if (sd_bus_error_is_set(error)) |
| 197 |
return sd_varlink_error(vl, "io.systemd.Login.UnitAllocationFailed", /* parameters= */ NULL); |
| 198 |
|
| 199 |
log_debug("Sending Varlink reply about created session: " |
| 200 |
"id=%s uid=" UID_FMT " runtime_path=%s seat=%s vtnr=%u", |
| 201 |
s->id, |
| 202 |
s->user->user_record->uid, |
| 203 |
s->user->runtime_path, |
| 204 |
s->seat ? s->seat->id : "", |
| 205 |
s->vtnr); |
| 206 |
|
| 207 |
return sd_varlink_replybo( |
| 208 |
vl, |
| 209 |
SD_JSON_BUILD_PAIR_STRING("Id", s->id), |
| 210 |
SD_JSON_BUILD_PAIR_STRING("RuntimePath", s->user->runtime_path), |
| 211 |
SD_JSON_BUILD_PAIR_UNSIGNED("UID", s->user->user_record->uid), |
| 212 |
SD_JSON_BUILD_PAIR_CONDITION(!!s->seat, "Seat", SD_JSON_BUILD_STRING(s->seat ? s->seat->id : NULL)), |
| 213 |
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("VTNr", s->vtnr), |
| 214 |
JSON_BUILD_PAIR_ENUM("Class", session_class_to_string(s->class)), |
| 215 |
JSON_BUILD_PAIR_ENUM("Type", session_type_to_string(s->type))); |
| 216 |
} |
| 217 |
|
| 218 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_session_class, SessionClass, session_class_from_string); |
| 219 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_session_type, SessionType, session_type_from_string); |
| 220 |
|
| 221 |
typedef struct CreateSessionParameters { |
| 222 |
uid_t uid; |
| 223 |
PidRef pid; |
| 224 |
const char *service; |
| 225 |
SessionType type; |
| 226 |
SessionClass class; |
| 227 |
const char *desktop; |
| 228 |
const char *seat; |
| 229 |
unsigned vtnr; |
| 230 |
const char *tty; |
| 231 |
const char *display; |
| 232 |
int remote; |
| 233 |
const char *remote_user; |
| 234 |
const char *remote_host; |
| 235 |
char **extra_device_access; |
| 236 |
} CreateSessionParameters; |
| 237 |
|
| 238 |
static void create_session_parameters_done(CreateSessionParameters *p) { |
| 239 |
pidref_done(&p->pid); |
| 240 |
strv_free(p->extra_device_access); |
| 241 |
} |
| 242 |
|
| 243 |
static int vl_method_create_session(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 244 |
Manager *m = ASSERT_PTR(userdata); |
| 245 |
int r; |
| 246 |
|
| 247 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 248 |
{ "UID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, offsetof(CreateSessionParameters, uid), SD_JSON_MANDATORY }, |
| 249 |
{ "PID", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref, offsetof(CreateSessionParameters, pid), SD_JSON_STRICT }, |
| 250 |
{ "Service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, service), 0 }, |
| 251 |
{ "Type", SD_JSON_VARIANT_STRING, json_dispatch_session_type, offsetof(CreateSessionParameters, type), SD_JSON_MANDATORY }, |
| 252 |
{ "Class", SD_JSON_VARIANT_STRING, json_dispatch_session_class, offsetof(CreateSessionParameters, class), SD_JSON_MANDATORY }, |
| 253 |
{ "Desktop", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, desktop), SD_JSON_STRICT }, |
| 254 |
{ "Seat", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, seat), 0 }, |
| 255 |
{ "VTNr", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint, offsetof(CreateSessionParameters, vtnr), 0 }, |
| 256 |
{ "TTY", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, tty), 0 }, |
| 257 |
{ "Display", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, display), 0 }, |
| 258 |
{ "Remote", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(CreateSessionParameters, remote), 0 }, |
| 259 |
{ "RemoteUser", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, remote_user), 0 }, |
| 260 |
{ "RemoteHost", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CreateSessionParameters, remote_host), 0 }, |
| 261 |
{ "ExtraDeviceAccess", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(CreateSessionParameters, extra_device_access), 0 }, |
| 262 |
{} |
| 263 |
}; |
| 264 |
|
| 265 |
_cleanup_(create_session_parameters_done) CreateSessionParameters p = { |
| 266 |
.uid = UID_INVALID, |
| 267 |
.pid = PIDREF_NULL, |
| 268 |
.class = _SESSION_CLASS_INVALID, |
| 269 |
.type = _SESSION_TYPE_INVALID, |
| 270 |
.remote = -1, |
| 271 |
}; |
| 272 |
|
| 273 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 274 |
if (r != 0) |
| 275 |
return r; |
| 276 |
|
| 277 |
if (p.class == SESSION_NONE) |
| 278 |
return sd_varlink_error_invalid_parameter_name(link, "Class"); |
| 279 |
|
| 280 |
Seat *seat = NULL; |
| 281 |
if (p.seat) { |
| 282 |
seat = hashmap_get(m->seats, p.seat); |
| 283 |
if (!seat) |
| 284 |
return sd_varlink_error(link, "io.systemd.Login.NoSuchSeat", /* parameters= */ NULL); |
| 285 |
} |
| 286 |
|
| 287 |
if (p.tty) { |
| 288 |
if (tty_is_vc(p.tty)) { |
| 289 |
if (!seat) |
| 290 |
seat = m->seat0; |
| 291 |
else if (seat != m->seat0) |
| 292 |
return sd_varlink_error_invalid_parameter_name(link, "Seat"); |
| 293 |
|
| 294 |
int v = vtnr_from_tty(p.tty); |
| 295 |
if (v <= 0) |
| 296 |
return sd_varlink_error_invalid_parameter_name(link, "TTY"); |
| 297 |
|
| 298 |
if (p.vtnr == 0) |
| 299 |
p.vtnr = v; |
| 300 |
else if (p.vtnr != (unsigned) v) |
| 301 |
return sd_varlink_error_invalid_parameter_name(link, "VTNr"); |
| 302 |
|
| 303 |
} else if (tty_is_console(p.tty)) { |
| 304 |
if (!seat) |
| 305 |
seat = m->seat0; |
| 306 |
else if (seat != m->seat0) |
| 307 |
return sd_varlink_error_invalid_parameter_name(link, "Seat"); |
| 308 |
|
| 309 |
if (p.vtnr != 0) |
| 310 |
return sd_varlink_error_invalid_parameter_name(link, "VTNr"); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if (seat) { |
| 315 |
if (seat_has_vts(seat)) { |
| 316 |
if (!vtnr_is_valid(p.vtnr)) |
| 317 |
return sd_varlink_error_invalid_parameter_name(link, "VTNr"); |
| 318 |
} else { |
| 319 |
if (p.vtnr != 0) |
| 320 |
return sd_varlink_error_invalid_parameter_name(link, "VTNr"); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (p.remote < 0) |
| 325 |
p.remote = p.remote_user || p.remote_host; |
| 326 |
|
| 327 |
/* Before we continue processing this, let's ensure the peer is privileged */ |
| 328 |
r = varlink_check_privileged_peer(link); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
|
| 332 |
if (!pidref_is_set(&p.pid)) { |
| 333 |
r = varlink_get_peer_pidref(link, &p.pid); |
| 334 |
if (r < 0) |
| 335 |
return log_debug_errno(r, "Failed to get peer pidref: %m"); |
| 336 |
} |
| 337 |
|
| 338 |
if (p.pid.fd < 0) |
| 339 |
return sd_varlink_error(link, "io.systemd.Login.NoSessionPIDFD", /* parameters= */ NULL); |
| 340 |
|
| 341 |
Session *session; |
| 342 |
r = manager_create_session( |
| 343 |
m, |
| 344 |
p.uid, |
| 345 |
&p.pid, |
| 346 |
p.service, |
| 347 |
p.type, |
| 348 |
p.class, |
| 349 |
p.desktop, |
| 350 |
seat, |
| 351 |
p.vtnr, |
| 352 |
p.tty, |
| 353 |
p.display, |
| 354 |
p.remote, |
| 355 |
p.remote_user, |
| 356 |
p.remote_host, |
| 357 |
p.extra_device_access, |
| 358 |
&session); |
| 359 |
if (r == -EBUSY) |
| 360 |
return sd_varlink_error(link, "io.systemd.Login.AlreadySessionMember", /* parameters= */ NULL); |
| 361 |
if (r == -EADDRNOTAVAIL) |
| 362 |
return sd_varlink_error(link, "io.systemd.Login.VirtualTerminalAlreadyTaken", /* parameters= */ NULL); |
| 363 |
if (r == -EUSERS) |
| 364 |
return sd_varlink_error(link, "io.systemd.Login.TooManySessions", /* parameters= */ NULL); |
| 365 |
if (r < 0) |
| 366 |
return r; |
| 367 |
|
| 368 |
r = session_start(session, /* properties= */ NULL, /* error= */ NULL); |
| 369 |
if (r < 0) |
| 370 |
goto fail; |
| 371 |
|
| 372 |
session->create_link = sd_varlink_ref(link); |
| 373 |
|
| 374 |
/* Let's check if this is complete now */ |
| 375 |
r = session_send_create_reply(session, /* error= */ NULL); |
| 376 |
if (r < 0) |
| 377 |
goto fail; |
| 378 |
|
| 379 |
return 1; |
| 380 |
|
| 381 |
fail: |
| 382 |
if (session) |
| 383 |
session_add_to_gc_queue(session); |
| 384 |
|
| 385 |
return r; |
| 386 |
} |
| 387 |
|
| 388 |
static int emit_session_reply(sd_varlink *link, Session *session) { |
| 389 |
assert(link); |
| 390 |
assert(session); |
| 391 |
|
| 392 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 393 |
int r = session_build_json(session, &v); |
| 394 |
if (r < 0) |
| 395 |
return r; |
| 396 |
|
| 397 |
return sd_varlink_reply(link, v); |
| 398 |
} |
| 399 |
|
| 400 |
typedef struct ListSessionsParameters { |
| 401 |
const char *id; |
| 402 |
PidRef pidref; |
| 403 |
} ListSessionsParameters; |
| 404 |
|
| 405 |
static void list_sessions_parameters_done(ListSessionsParameters *p) { |
| 406 |
assert(p); |
| 407 |
pidref_done(&p->pidref); |
| 408 |
} |
| 409 |
|
| 410 |
static int vl_method_list_sessions(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 411 |
Manager *m = ASSERT_PTR(userdata); |
| 412 |
int r; |
| 413 |
|
| 414 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 415 |
{ "Id", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(ListSessionsParameters, id), 0 }, |
| 416 |
{ "PID", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref, offsetof(ListSessionsParameters, pidref), 0 }, |
| 417 |
{} |
| 418 |
}; |
| 419 |
|
| 420 |
_cleanup_(list_sessions_parameters_done) ListSessionsParameters p = { |
| 421 |
.pidref = PIDREF_NULL, |
| 422 |
}; |
| 423 |
|
| 424 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 425 |
if (r != 0) |
| 426 |
return r; |
| 427 |
|
| 428 |
/* Unique-key path: Id and/or PID provided. Single reply or NoSuchSession. */ |
| 429 |
if (p.id || pidref_is_set(&p.pidref)) { |
| 430 |
r = sd_varlink_set_sentinel(link, "io.systemd.Login.NoSuchSession"); |
| 431 |
if (r < 0) |
| 432 |
return r; |
| 433 |
|
| 434 |
Session *session; |
| 435 |
r = manager_varlink_get_session_by_name_or_pidref(m, link, p.id, &p.pidref, &session); |
| 436 |
if (r == -ESRCH) |
| 437 |
return 0; /* triggers NoSuchSession sentinel */ |
| 438 |
if (r < 0) |
| 439 |
return r; |
| 440 |
|
| 441 |
return emit_session_reply(link, session); |
| 442 |
} |
| 443 |
|
| 444 |
/* Streaming path: no filter. Full list, requires 'more' flag. */ |
| 445 |
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)) |
| 446 |
return sd_varlink_error(link, SD_VARLINK_ERROR_EXPECTED_MORE, /* parameters= */ NULL); |
| 447 |
|
| 448 |
/* Empty hashmap is a valid empty stream, not "not found" — see vl_method_list_inhibitors. */ |
| 449 |
r = sd_varlink_set_sentinel(link, /* error_id= */ NULL); |
| 450 |
if (r < 0) |
| 451 |
return r; |
| 452 |
|
| 453 |
Session *session; |
| 454 |
HASHMAP_FOREACH(session, m->sessions) { |
| 455 |
r = emit_session_reply(link, session); |
| 456 |
if (r < 0) |
| 457 |
return r; |
| 458 |
} |
| 459 |
|
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
static int manager_varlink_get_user_by_uid_or_pidref( |
| 464 |
Manager *m, |
| 465 |
uid_t uid, |
| 466 |
const PidRef *pidref, |
| 467 |
User **ret) { |
| 468 |
|
| 469 |
int r; |
| 470 |
|
| 471 |
assert(m); |
| 472 |
assert(ret); |
| 473 |
|
| 474 |
/* Resolves a user by UID and/or PID. At least one filter must be set. If both are set they must |
| 475 |
* reference the same user, otherwise -ESRCH is returned. Returns -ESRCH on "not found". Returns |
| 476 |
* negative errno on other failures. */ |
| 477 |
|
| 478 |
User *by_uid = NULL; |
| 479 |
if (uid_is_valid(uid)) { |
| 480 |
by_uid = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 481 |
if (!by_uid) |
| 482 |
return -ESRCH; |
| 483 |
} |
| 484 |
|
| 485 |
User *by_pid = NULL; |
| 486 |
if (pidref && pidref_is_set(pidref)) { |
| 487 |
uid_t pid_uid; |
| 488 |
r = cg_pidref_get_owner_uid(pidref, &pid_uid); |
| 489 |
if (r < 0) { |
| 490 |
if (!IN_SET(r, -ENXIO, -ENOENT)) |
| 491 |
return log_debug_errno(r, "Failed to acquire owning UID of PID " PID_FMT ": %m", pidref->pid); |
| 492 |
/* -ENXIO: PID not in a user-N.slice cgroup (e.g. PID 1). |
| 493 |
* -ENOENT: cgroup gone (process terminated). |
| 494 |
* Translate to -ESRCH so the caller maps to the NoSuchUser sentinel. */ |
| 495 |
return -ESRCH; |
| 496 |
} |
| 497 |
|
| 498 |
by_pid = hashmap_get(m->users, UID_TO_PTR(pid_uid)); |
| 499 |
if (!by_pid) |
| 500 |
return -ESRCH; |
| 501 |
} |
| 502 |
|
| 503 |
if (by_uid && by_pid && by_uid != by_pid) |
| 504 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), |
| 505 |
"Search by UID " UID_FMT " and PID " PID_FMT " resulted in two different users", |
| 506 |
uid, pidref->pid); |
| 507 |
|
| 508 |
assert(by_uid || by_pid); |
| 509 |
|
| 510 |
*ret = by_uid ?: by_pid; |
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
static int emit_user_reply(sd_varlink *link, User *user) { |
| 515 |
assert(link); |
| 516 |
assert(user); |
| 517 |
|
| 518 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 519 |
int r = user_build_json(user, &v); |
| 520 |
if (r < 0) |
| 521 |
return r; |
| 522 |
|
| 523 |
return sd_varlink_reply(link, v); |
| 524 |
} |
| 525 |
|
| 526 |
typedef struct ListUsersParameters { |
| 527 |
uid_t uid; |
| 528 |
PidRef pidref; |
| 529 |
} ListUsersParameters; |
| 530 |
|
| 531 |
static void list_users_parameters_done(ListUsersParameters *p) { |
| 532 |
assert(p); |
| 533 |
pidref_done(&p->pidref); |
| 534 |
} |
| 535 |
|
| 536 |
static int vl_method_list_users(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 537 |
Manager *m = ASSERT_PTR(userdata); |
| 538 |
int r; |
| 539 |
|
| 540 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 541 |
{ "UID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, offsetof(ListUsersParameters, uid), 0 }, |
| 542 |
{ "PID", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_pidref, offsetof(ListUsersParameters, pidref), 0 }, |
| 543 |
{} |
| 544 |
}; |
| 545 |
|
| 546 |
_cleanup_(list_users_parameters_done) ListUsersParameters p = { |
| 547 |
.uid = UID_INVALID, |
| 548 |
.pidref = PIDREF_NULL, |
| 549 |
}; |
| 550 |
|
| 551 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 552 |
if (r != 0) |
| 553 |
return r; |
| 554 |
|
| 555 |
/* Unique-key path: UID and/or PID provided. Single reply or NoSuchUser. */ |
| 556 |
if (uid_is_valid(p.uid) || pidref_is_set(&p.pidref)) { |
| 557 |
r = sd_varlink_set_sentinel(link, "io.systemd.Login.NoSuchUser"); |
| 558 |
if (r < 0) |
| 559 |
return r; |
| 560 |
|
| 561 |
User *user; |
| 562 |
r = manager_varlink_get_user_by_uid_or_pidref(m, p.uid, &p.pidref, &user); |
| 563 |
if (r == -ESRCH) |
| 564 |
return 0; /* triggers NoSuchUser sentinel */ |
| 565 |
if (r < 0) |
| 566 |
return r; |
| 567 |
|
| 568 |
return emit_user_reply(link, user); |
| 569 |
} |
| 570 |
|
| 571 |
/* Streaming path: no filter. Full list, requires 'more' flag. */ |
| 572 |
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)) |
| 573 |
return sd_varlink_error(link, SD_VARLINK_ERROR_EXPECTED_MORE, /* parameters= */ NULL); |
| 574 |
|
| 575 |
/* Empty hashmap is a valid empty stream, not "not found" — see vl_method_list_inhibitors. */ |
| 576 |
r = sd_varlink_set_sentinel(link, /* error_id= */ NULL); |
| 577 |
if (r < 0) |
| 578 |
return r; |
| 579 |
|
| 580 |
User *user; |
| 581 |
HASHMAP_FOREACH(user, m->users) { |
| 582 |
r = emit_user_reply(link, user); |
| 583 |
if (r < 0) |
| 584 |
return r; |
| 585 |
} |
| 586 |
|
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
static int manager_varlink_get_seat_by_name( |
| 591 |
Manager *m, |
| 592 |
sd_varlink *link, |
| 593 |
const char *name, |
| 594 |
Seat **ret) { |
| 595 |
|
| 596 |
int r; |
| 597 |
|
| 598 |
assert(m); |
| 599 |
assert(link); |
| 600 |
assert(name); |
| 601 |
assert(ret); |
| 602 |
|
| 603 |
/* Resolves a seat name to a seat object. Supports the special names "self" and "auto" — these |
| 604 |
* resolve to the seat of the caller's session. Returns -EINVAL on invalid seat name. Returns |
| 605 |
* -ESRCH on "not found". Caller is expected to turn that into a varlink error. */ |
| 606 |
|
| 607 |
if (seat_is_self(name) || seat_is_auto(name)) { |
| 608 |
Session *session; |
| 609 |
r = manager_varlink_get_session_by_peer(m, link, /* consult_display= */ seat_is_auto(name), &session); |
| 610 |
if (r < 0) |
| 611 |
return r; |
| 612 |
if (!session || !session->seat) |
| 613 |
return -ESRCH; |
| 614 |
|
| 615 |
*ret = session->seat; |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
if (!seat_name_is_valid(name)) |
| 620 |
return -EINVAL; |
| 621 |
|
| 622 |
Seat *seat = hashmap_get(m->seats, name); |
| 623 |
if (!seat) |
| 624 |
return -ESRCH; |
| 625 |
|
| 626 |
*ret = seat; |
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
static int emit_seat_reply(sd_varlink *link, Seat *seat) { |
| 631 |
assert(link); |
| 632 |
assert(seat); |
| 633 |
|
| 634 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 635 |
int r = seat_build_json(seat, &v); |
| 636 |
if (r < 0) |
| 637 |
return r; |
| 638 |
|
| 639 |
return sd_varlink_reply(link, v); |
| 640 |
} |
| 641 |
|
| 642 |
static int vl_method_list_seats(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 643 |
Manager *m = ASSERT_PTR(userdata); |
| 644 |
int r; |
| 645 |
|
| 646 |
const char *id = NULL; |
| 647 |
|
| 648 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 649 |
{ "Id", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, 0, 0 }, |
| 650 |
{} |
| 651 |
}; |
| 652 |
|
| 653 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &id); |
| 654 |
if (r != 0) |
| 655 |
return r; |
| 656 |
|
| 657 |
/* Unique-key path: Id provided. Single reply or NoSuchSeat. */ |
| 658 |
if (id) { |
| 659 |
r = sd_varlink_set_sentinel(link, "io.systemd.Login.NoSuchSeat"); |
| 660 |
if (r < 0) |
| 661 |
return r; |
| 662 |
|
| 663 |
Seat *seat; |
| 664 |
r = manager_varlink_get_seat_by_name(m, link, id, &seat); |
| 665 |
if (r == -EINVAL) |
| 666 |
return sd_varlink_error_invalid_parameter_name(link, "Id"); |
| 667 |
if (r == -ESRCH) |
| 668 |
return 0; /* triggers NoSuchSeat sentinel */ |
| 669 |
if (r < 0) |
| 670 |
return r; |
| 671 |
|
| 672 |
return emit_seat_reply(link, seat); |
| 673 |
} |
| 674 |
|
| 675 |
/* Streaming path: no filter. Full list, requires 'more' flag. */ |
| 676 |
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)) |
| 677 |
return sd_varlink_error(link, SD_VARLINK_ERROR_EXPECTED_MORE, /* parameters= */ NULL); |
| 678 |
|
| 679 |
/* Empty hashmap is a valid empty stream, not "not found" — see vl_method_list_inhibitors. */ |
| 680 |
r = sd_varlink_set_sentinel(link, /* error_id= */ NULL); |
| 681 |
if (r < 0) |
| 682 |
return r; |
| 683 |
|
| 684 |
Seat *seat; |
| 685 |
HASHMAP_FOREACH(seat, m->seats) { |
| 686 |
r = emit_seat_reply(link, seat); |
| 687 |
if (r < 0) |
| 688 |
return r; |
| 689 |
} |
| 690 |
|
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
static int vl_method_list_inhibitors(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 695 |
Manager *m = ASSERT_PTR(userdata); |
| 696 |
int r; |
| 697 |
|
| 698 |
/* IDL uses SD_VARLINK_REQUIRES_MORE, so the framework rejects non-more calls before this handler. */ |
| 699 |
|
| 700 |
r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, /* userdata= */ NULL); |
| 701 |
if (r != 0) |
| 702 |
return r; |
| 703 |
|
| 704 |
/* Required for multi-reply streaming: sd_varlink_reply() only stashes the previous reply (for |
| 705 |
* the 'continues' flag machinery) when v->sentinel is set. Pass NULL to send an empty reply |
| 706 |
* (not an error) when the inhibitor hashmap is empty — this method has no point-lookup, so an |
| 707 |
* empty list is a valid result, not a "not found" failure. */ |
| 708 |
r = sd_varlink_set_sentinel(link, /* error_id= */ NULL); |
| 709 |
if (r < 0) |
| 710 |
return r; |
| 711 |
|
| 712 |
Inhibitor *inhibitor; |
| 713 |
HASHMAP_FOREACH(inhibitor, m->inhibitors) { |
| 714 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 715 |
|
| 716 |
r = inhibitor_build_json(inhibitor, &v); |
| 717 |
if (r < 0) |
| 718 |
return r; |
| 719 |
|
| 720 |
r = sd_varlink_reply(link, v); |
| 721 |
if (r < 0) |
| 722 |
return r; |
| 723 |
} |
| 724 |
|
| 725 |
return 0; |
| 726 |
} |
| 727 |
|
| 728 |
static int vl_method_release_session(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 729 |
Manager *m = ASSERT_PTR(userdata); |
| 730 |
int r; |
| 731 |
|
| 732 |
struct { |
| 733 |
const char *id; |
| 734 |
} p = {}; |
| 735 |
|
| 736 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 737 |
{ "Id", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, id), 0 }, |
| 738 |
{} |
| 739 |
}; |
| 740 |
|
| 741 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 742 |
if (r != 0) |
| 743 |
return r; |
| 744 |
|
| 745 |
Session *session; |
| 746 |
r = manager_varlink_get_session_by_name_or_pidref(m, link, p.id, /* pidref= */ NULL, &session); |
| 747 |
if (r == -ESRCH) |
| 748 |
return sd_varlink_error(link, "io.systemd.Login.NoSuchSession", /* parameters= */ NULL); |
| 749 |
if (r < 0) |
| 750 |
return r; |
| 751 |
|
| 752 |
Session *peer_session; |
| 753 |
r = manager_varlink_get_session_by_peer(m, link, /* consult_display= */ false, &peer_session); |
| 754 |
if (r < 0) |
| 755 |
return r; |
| 756 |
if (!peer_session) |
| 757 |
return sd_varlink_error(link, "io.systemd.Login.NoSuchSession", /* parameters= */ NULL); |
| 758 |
|
| 759 |
if (session != peer_session) |
| 760 |
return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, /* parameters= */ NULL); |
| 761 |
|
| 762 |
r = session_release(session); |
| 763 |
if (r < 0) |
| 764 |
return r; |
| 765 |
|
| 766 |
return sd_varlink_reply(link, NULL); |
| 767 |
} |
| 768 |
|
| 769 |
static int setup_wall_message_timer(Manager *m, sd_varlink *link) { |
| 770 |
uid_t uid = UID_INVALID; |
| 771 |
int r; |
| 772 |
|
| 773 |
(void) sd_varlink_get_peer_uid(link, &uid); |
| 774 |
m->scheduled_shutdown_uid = uid; |
| 775 |
|
| 776 |
_cleanup_free_ char *tty = NULL; |
| 777 |
pid_t pid = 0; |
| 778 |
r = sd_varlink_get_peer_pid(link, &pid); |
| 779 |
if (r >= 0) |
| 780 |
(void) get_ctty(pid, /* ret_devnr= */ NULL, &tty); |
| 781 |
|
| 782 |
r = free_and_strdup_warn(&m->scheduled_shutdown_tty, tty); |
| 783 |
if (r < 0) |
| 784 |
return log_oom(); |
| 785 |
|
| 786 |
return manager_setup_wall_message_timer(m); |
| 787 |
} |
| 788 |
|
| 789 |
static int manager_do_shutdown_action(sd_varlink *link, sd_json_variant *parameters, HandleAction action) { |
| 790 |
Manager *m = ASSERT_PTR(sd_varlink_get_userdata(link)); |
| 791 |
int skip_inhibitors = -1; |
| 792 |
int r; |
| 793 |
|
| 794 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 795 |
{ "skipInhibitors", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, 0, 0 }, |
| 796 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 797 |
{} |
| 798 |
}; |
| 799 |
|
| 800 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &skip_inhibitors); |
| 801 |
if (r != 0) |
| 802 |
return r; |
| 803 |
|
| 804 |
uint64_t flags = skip_inhibitors > 0 ? SD_LOGIND_SKIP_INHIBITORS : 0; |
| 805 |
|
| 806 |
const HandleActionData *a = handle_action_lookup(action); |
| 807 |
assert(a); |
| 808 |
|
| 809 |
r = manager_verify_shutdown_creds(m, /* message= */ NULL, link, a, flags, /* error= */ NULL); |
| 810 |
if (r != 0) |
| 811 |
return r; |
| 812 |
|
| 813 |
{ |
| 814 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 815 |
|
| 816 |
(void) varlink_get_peer_pidref(link, &pidref); |
| 817 |
log_shutdown_caller(&pidref, handle_action_to_string(action)); |
| 818 |
} |
| 819 |
|
| 820 |
if (m->delayed_action) |
| 821 |
return sd_varlink_error(link, "io.systemd.Shutdown.AlreadyInProgress", /* parameters= */ NULL); |
| 822 |
|
| 823 |
/* Reset in case we're short-circuiting a scheduled shutdown */ |
| 824 |
m->unlink_nologin = false; |
| 825 |
manager_reset_scheduled_shutdown(m); |
| 826 |
|
| 827 |
m->scheduled_shutdown_timeout = 0; |
| 828 |
m->scheduled_shutdown_action = action; |
| 829 |
|
| 830 |
(void) setup_wall_message_timer(m, link); |
| 831 |
|
| 832 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 833 |
r = bus_manager_shutdown_or_sleep_now_or_later(m, a, &error); |
| 834 |
if (r < 0) { |
| 835 |
log_warning_errno(r, "Failed to execute %s: %s", |
| 836 |
handle_action_to_string(action), |
| 837 |
bus_error_message(&error, r)); |
| 838 |
return sd_varlink_error_errno(link, r); |
| 839 |
} |
| 840 |
|
| 841 |
return sd_varlink_reply(link, NULL); |
| 842 |
} |
| 843 |
|
| 844 |
static int vl_method_power_off(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 845 |
return manager_do_shutdown_action(link, parameters, HANDLE_POWEROFF); |
| 846 |
} |
| 847 |
|
| 848 |
static int vl_method_reboot(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 849 |
return manager_do_shutdown_action(link, parameters, HANDLE_REBOOT); |
| 850 |
} |
| 851 |
|
| 852 |
static int vl_method_halt(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 853 |
return manager_do_shutdown_action(link, parameters, HANDLE_HALT); |
| 854 |
} |
| 855 |
|
| 856 |
static int vl_method_kexec(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 857 |
return manager_do_shutdown_action(link, parameters, HANDLE_KEXEC); |
| 858 |
} |
| 859 |
|
| 860 |
static int vl_method_soft_reboot(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 861 |
return manager_do_shutdown_action(link, parameters, HANDLE_SOFT_REBOOT); |
| 862 |
} |
| 863 |
|
| 864 |
int manager_varlink_init(Manager *m, int fd) { |
| 865 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *s = NULL; |
| 866 |
_unused_ _cleanup_close_ int fd_close = fd; |
| 867 |
int r; |
| 868 |
|
| 869 |
assert(m); |
| 870 |
|
| 871 |
if (m->varlink_server) |
| 872 |
return 0; |
| 873 |
|
| 874 |
r = varlink_server_new( |
| 875 |
&s, |
| 876 |
SD_VARLINK_SERVER_ACCOUNT_UID| |
| 877 |
SD_VARLINK_SERVER_INHERIT_USERDATA| |
| 878 |
SD_VARLINK_SERVER_ALLOW_FD_PASSING_OUTPUT, |
| 879 |
m); |
| 880 |
if (r < 0) |
| 881 |
return log_error_errno(r, "Failed to allocate varlink server object: %m"); |
| 882 |
|
| 883 |
r = sd_varlink_server_add_interface_many( |
| 884 |
s, |
| 885 |
&vl_interface_io_systemd_Login, |
| 886 |
&vl_interface_io_systemd_Shutdown, |
| 887 |
&vl_interface_io_systemd_service); |
| 888 |
if (r < 0) |
| 889 |
return log_error_errno(r, "Failed to add varlink interfaces: %m"); |
| 890 |
|
| 891 |
r = sd_varlink_server_bind_method_many( |
| 892 |
s, |
| 893 |
"io.systemd.Login.CreateSession", vl_method_create_session, |
| 894 |
"io.systemd.Login.ReleaseSession", vl_method_release_session, |
| 895 |
"io.systemd.Login.ListSessions", vl_method_list_sessions, |
| 896 |
"io.systemd.Login.ListUsers", vl_method_list_users, |
| 897 |
"io.systemd.Login.ListSeats", vl_method_list_seats, |
| 898 |
"io.systemd.Login.ListInhibitors", vl_method_list_inhibitors, |
| 899 |
"io.systemd.Shutdown.PowerOff", vl_method_power_off, |
| 900 |
"io.systemd.Shutdown.Reboot", vl_method_reboot, |
| 901 |
"io.systemd.Shutdown.Halt", vl_method_halt, |
| 902 |
"io.systemd.Shutdown.KExec", vl_method_kexec, |
| 903 |
"io.systemd.Shutdown.SoftReboot", vl_method_soft_reboot, |
| 904 |
"io.systemd.service.Ping", varlink_method_ping, |
| 905 |
"io.systemd.service.SetLogLevel", varlink_method_set_log_level, |
| 906 |
"io.systemd.service.GetEnvironment", varlink_method_get_environment); |
| 907 |
if (r < 0) |
| 908 |
return log_error_errno(r, "Failed to register varlink methods: %m"); |
| 909 |
|
| 910 |
if (fd < 0) |
| 911 |
r = sd_varlink_server_listen_address(s, "/run/systemd/io.systemd.Login", /* mode= */ 0666); |
| 912 |
else |
| 913 |
r = sd_varlink_server_listen_fd(s, fd); |
| 914 |
if (r < 0) |
| 915 |
return log_error_errno(r, "Failed to bind to varlink socket '/run/systemd/io.systemd.Login': %m"); |
| 916 |
|
| 917 |
TAKE_FD(fd_close); |
| 918 |
|
| 919 |
r = sd_varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 920 |
if (r < 0) |
| 921 |
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); |
| 922 |
|
| 923 |
m->varlink_server = TAKE_PTR(s); |
| 924 |
return 0; |
| 925 |
} |
| 926 |
|
| 927 |
void manager_varlink_done(Manager *m) { |
| 928 |
assert(m); |
| 929 |
|
| 930 |
m->varlink_server = sd_varlink_server_unref(m->varlink_server); |
| 931 |
} |