]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-session-dbus.c
shared: split out polkit stuff from bus-util.c → bus-polkit.c
[thirdparty/systemd.git] / src / login / logind-session-dbus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4
5 #include "alloc-util.h"
6 #include "bus-common-errors.h"
7 #include "bus-label.h"
8 #include "bus-polkit.h"
9 #include "bus-util.h"
10 #include "fd-util.h"
11 #include "logind-brightness.h"
12 #include "logind-dbus.h"
13 #include "logind-seat-dbus.h"
14 #include "logind-session-dbus.h"
15 #include "logind-session-device.h"
16 #include "logind-session.h"
17 #include "logind-user-dbus.h"
18 #include "logind.h"
19 #include "missing_capability.h"
20 #include "path-util.h"
21 #include "signal-util.h"
22 #include "stat-util.h"
23 #include "strv.h"
24 #include "user-util.h"
25 #include "util.h"
26
27 static int property_get_user(
28 sd_bus *bus,
29 const char *path,
30 const char *interface,
31 const char *property,
32 sd_bus_message *reply,
33 void *userdata,
34 sd_bus_error *error) {
35
36 _cleanup_free_ char *p = NULL;
37 Session *s = userdata;
38
39 assert(bus);
40 assert(reply);
41 assert(s);
42
43 p = user_bus_path(s->user);
44 if (!p)
45 return -ENOMEM;
46
47 return sd_bus_message_append(reply, "(uo)", (uint32_t) s->user->user_record->uid, p);
48 }
49
50 static int property_get_name(
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 Session *s = userdata;
60
61 assert(bus);
62 assert(reply);
63 assert(s);
64
65 return sd_bus_message_append(reply, "s", s->user->user_record->user_name);
66 }
67
68 static int property_get_seat(
69 sd_bus *bus,
70 const char *path,
71 const char *interface,
72 const char *property,
73 sd_bus_message *reply,
74 void *userdata,
75 sd_bus_error *error) {
76
77 _cleanup_free_ char *p = NULL;
78 Session *s = userdata;
79
80 assert(bus);
81 assert(reply);
82 assert(s);
83
84 p = s->seat ? seat_bus_path(s->seat) : strdup("/");
85 if (!p)
86 return -ENOMEM;
87
88 return sd_bus_message_append(reply, "(so)", s->seat ? s->seat->id : "", p);
89 }
90
91 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, session_type, SessionType);
92 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, session_class, SessionClass);
93 static BUS_DEFINE_PROPERTY_GET(property_get_active, "b", Session, session_is_active);
94 static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", Session, session_get_state, session_state_to_string);
95
96 static int property_get_idle_hint(
97 sd_bus *bus,
98 const char *path,
99 const char *interface,
100 const char *property,
101 sd_bus_message *reply,
102 void *userdata,
103 sd_bus_error *error) {
104
105 Session *s = userdata;
106
107 assert(bus);
108 assert(reply);
109 assert(s);
110
111 return sd_bus_message_append(reply, "b", session_get_idle_hint(s, NULL) > 0);
112 }
113
114 static int property_get_idle_since_hint(
115 sd_bus *bus,
116 const char *path,
117 const char *interface,
118 const char *property,
119 sd_bus_message *reply,
120 void *userdata,
121 sd_bus_error *error) {
122
123 Session *s = userdata;
124 dual_timestamp t = DUAL_TIMESTAMP_NULL;
125 uint64_t u;
126 int r;
127
128 assert(bus);
129 assert(reply);
130 assert(s);
131
132 r = session_get_idle_hint(s, &t);
133 if (r < 0)
134 return r;
135
136 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
137
138 return sd_bus_message_append(reply, "t", u);
139 }
140
141 static int property_get_locked_hint(
142 sd_bus *bus,
143 const char *path,
144 const char *interface,
145 const char *property,
146 sd_bus_message *reply,
147 void *userdata,
148 sd_bus_error *error) {
149
150 Session *s = userdata;
151
152 assert(bus);
153 assert(reply);
154 assert(s);
155
156 return sd_bus_message_append(reply, "b", session_get_locked_hint(s) > 0);
157 }
158
159 int bus_session_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
160 Session *s = userdata;
161 int r;
162
163 assert(message);
164 assert(s);
165
166 r = bus_verify_polkit_async(
167 message,
168 CAP_KILL,
169 "org.freedesktop.login1.manage",
170 NULL,
171 false,
172 s->user->user_record->uid,
173 &s->manager->polkit_registry,
174 error);
175 if (r < 0)
176 return r;
177 if (r == 0)
178 return 1; /* Will call us back */
179
180 r = session_stop(s, true);
181 if (r < 0)
182 return r;
183
184 return sd_bus_reply_method_return(message, NULL);
185 }
186
187 int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
188 Session *s = userdata;
189 int r;
190
191 assert(message);
192 assert(s);
193
194 r = session_activate(s);
195 if (r < 0)
196 return r;
197
198 return sd_bus_reply_method_return(message, NULL);
199 }
200
201 int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error) {
202 Session *s = userdata;
203 int r;
204
205 assert(message);
206 assert(s);
207
208 r = bus_verify_polkit_async(
209 message,
210 CAP_SYS_ADMIN,
211 "org.freedesktop.login1.lock-sessions",
212 NULL,
213 false,
214 s->user->user_record->uid,
215 &s->manager->polkit_registry,
216 error);
217 if (r < 0)
218 return r;
219 if (r == 0)
220 return 1; /* Will call us back */
221
222 r = session_send_lock(s, strstr(sd_bus_message_get_member(message), "Lock"));
223 if (r < 0)
224 return r;
225
226 return sd_bus_reply_method_return(message, NULL);
227 }
228
229 static int method_set_idle_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
230 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
231 Session *s = userdata;
232 uid_t uid;
233 int r, b;
234
235 assert(message);
236 assert(s);
237
238 r = sd_bus_message_read(message, "b", &b);
239 if (r < 0)
240 return r;
241
242 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
243 if (r < 0)
244 return r;
245
246 r = sd_bus_creds_get_euid(creds, &uid);
247 if (r < 0)
248 return r;
249
250 if (uid != 0 && uid != s->user->user_record->uid)
251 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set idle hint");
252
253 r = session_set_idle_hint(s, b);
254 if (r == -ENOTTY)
255 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Idle hint control is not supported on non-graphical sessions.");
256 if (r < 0)
257 return r;
258
259 return sd_bus_reply_method_return(message, NULL);
260 }
261
262 static int method_set_locked_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
263 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
264 Session *s = userdata;
265 uid_t uid;
266 int r, b;
267
268 assert(message);
269 assert(s);
270
271 r = sd_bus_message_read(message, "b", &b);
272 if (r < 0)
273 return r;
274
275 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
276 if (r < 0)
277 return r;
278
279 r = sd_bus_creds_get_euid(creds, &uid);
280 if (r < 0)
281 return r;
282
283 if (uid != 0 && uid != s->user->user_record->uid)
284 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set locked hint");
285
286 session_set_locked_hint(s, b);
287
288 return sd_bus_reply_method_return(message, NULL);
289 }
290
291 int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
292 Session *s = userdata;
293 const char *swho;
294 int32_t signo;
295 KillWho who;
296 int r;
297
298 assert(message);
299 assert(s);
300
301 r = sd_bus_message_read(message, "si", &swho, &signo);
302 if (r < 0)
303 return r;
304
305 if (isempty(swho))
306 who = KILL_ALL;
307 else {
308 who = kill_who_from_string(swho);
309 if (who < 0)
310 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
311 }
312
313 if (!SIGNAL_VALID(signo))
314 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
315
316 r = bus_verify_polkit_async(
317 message,
318 CAP_KILL,
319 "org.freedesktop.login1.manage",
320 NULL,
321 false,
322 s->user->user_record->uid,
323 &s->manager->polkit_registry,
324 error);
325 if (r < 0)
326 return r;
327 if (r == 0)
328 return 1; /* Will call us back */
329
330 r = session_kill(s, who, signo);
331 if (r < 0)
332 return r;
333
334 return sd_bus_reply_method_return(message, NULL);
335 }
336
337 static int method_take_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
338 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
339 Session *s = userdata;
340 int r, force;
341 uid_t uid;
342
343 assert(message);
344 assert(s);
345
346 r = sd_bus_message_read(message, "b", &force);
347 if (r < 0)
348 return r;
349
350 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
351 if (r < 0)
352 return r;
353
354 r = sd_bus_creds_get_euid(creds, &uid);
355 if (r < 0)
356 return r;
357
358 if (uid != 0 && (force || uid != s->user->user_record->uid))
359 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may take control");
360
361 r = session_set_controller(s, sd_bus_message_get_sender(message), force, true);
362 if (r < 0)
363 return r;
364
365 return sd_bus_reply_method_return(message, NULL);
366 }
367
368 static int method_release_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
369 Session *s = userdata;
370
371 assert(message);
372 assert(s);
373
374 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
375 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
376
377 session_drop_controller(s);
378
379 return sd_bus_reply_method_return(message, NULL);
380 }
381
382 static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
383 Session *s = userdata;
384 uint32_t major, minor;
385 SessionDevice *sd;
386 dev_t dev;
387 int r;
388
389 assert(message);
390 assert(s);
391
392 r = sd_bus_message_read(message, "uu", &major, &minor);
393 if (r < 0)
394 return r;
395
396 if (!DEVICE_MAJOR_VALID(major) || !DEVICE_MINOR_VALID(minor))
397 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device major/minor is not valid.");
398
399 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
400 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
401
402 dev = makedev(major, minor);
403 sd = hashmap_get(s->devices, &dev);
404 if (sd)
405 /* We don't allow retrieving a device multiple times.
406 * The related ReleaseDevice call is not ref-counted.
407 * The caller should use dup() if it requires more
408 * than one fd (it would be functionally
409 * equivalent). */
410 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_IS_TAKEN, "Device already taken");
411
412 r = session_device_new(s, dev, true, &sd);
413 if (r < 0)
414 return r;
415
416 r = session_device_save(sd);
417 if (r < 0)
418 goto error;
419
420 r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active);
421 if (r < 0)
422 goto error;
423
424 session_save(s);
425 return 1;
426
427 error:
428 session_device_free(sd);
429 return r;
430 }
431
432 static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
433 Session *s = userdata;
434 uint32_t major, minor;
435 SessionDevice *sd;
436 dev_t dev;
437 int r;
438
439 assert(message);
440 assert(s);
441
442 r = sd_bus_message_read(message, "uu", &major, &minor);
443 if (r < 0)
444 return r;
445
446 if (!DEVICE_MAJOR_VALID(major) || !DEVICE_MINOR_VALID(minor))
447 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device major/minor is not valid.");
448
449 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
450 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
451
452 dev = makedev(major, minor);
453 sd = hashmap_get(s->devices, &dev);
454 if (!sd)
455 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
456
457 session_device_free(sd);
458 session_save(s);
459
460 return sd_bus_reply_method_return(message, NULL);
461 }
462
463 static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) {
464 Session *s = userdata;
465 uint32_t major, minor;
466 SessionDevice *sd;
467 dev_t dev;
468 int r;
469
470 assert(message);
471 assert(s);
472
473 r = sd_bus_message_read(message, "uu", &major, &minor);
474 if (r < 0)
475 return r;
476
477 if (!DEVICE_MAJOR_VALID(major) || !DEVICE_MINOR_VALID(minor))
478 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device major/minor is not valid.");
479
480 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
481 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
482
483 dev = makedev(major, minor);
484 sd = hashmap_get(s->devices, &dev);
485 if (!sd)
486 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
487
488 session_device_complete_pause(sd);
489
490 return sd_bus_reply_method_return(message, NULL);
491 }
492
493 static int method_set_brightness(sd_bus_message *message, void *userdata, sd_bus_error *error) {
494 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
495 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
496 const char *subsystem, *name, *seat;
497 Session *s = userdata;
498 uint32_t brightness;
499 uid_t uid;
500 int r;
501
502 assert(message);
503 assert(s);
504
505 r = sd_bus_message_read(message, "ssu", &subsystem, &name, &brightness);
506 if (r < 0)
507 return r;
508
509 if (!STR_IN_SET(subsystem, "backlight", "leds"))
510 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Subsystem type %s not supported, must be one of 'backlight' or 'leds'.", subsystem);
511 if (!filename_is_valid(name))
512 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not a valid device name %s, refusing.", name);
513
514 if (!s->seat)
515 return sd_bus_error_setf(error, BUS_ERROR_NOT_YOUR_DEVICE, "Your session has no seat, refusing.");
516 if (s->seat->active != s)
517 return sd_bus_error_setf(error, BUS_ERROR_NOT_YOUR_DEVICE, "Session is not in foreground, refusing.");
518
519 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
520 if (r < 0)
521 return r;
522
523 r = sd_bus_creds_get_euid(creds, &uid);
524 if (r < 0)
525 return r;
526
527 if (uid != 0 && uid != s->user->user_record->uid)
528 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may change brightness.");
529
530 r = sd_device_new_from_subsystem_sysname(&d, subsystem, name);
531 if (r < 0)
532 return sd_bus_error_set_errnof(error, r, "Failed to open device %s:%s: %m", subsystem, name);
533
534 if (sd_device_get_property_value(d, "ID_SEAT", &seat) >= 0 && !streq_ptr(seat, s->seat->id))
535 return sd_bus_error_setf(error, BUS_ERROR_NOT_YOUR_DEVICE, "Device %s:%s does not belong to your seat %s, refusing.", subsystem, name, s->seat->id);
536
537 r = manager_write_brightness(s->manager, d, brightness, message);
538 if (r < 0)
539 return r;
540
541 return 1;
542 }
543
544 const sd_bus_vtable session_vtable[] = {
545 SD_BUS_VTABLE_START(0),
546
547 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Session, id), SD_BUS_VTABLE_PROPERTY_CONST),
548 SD_BUS_PROPERTY("User", "(uo)", property_get_user, 0, SD_BUS_VTABLE_PROPERTY_CONST),
549 SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
550 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Session, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
551 SD_BUS_PROPERTY("VTNr", "u", NULL, offsetof(Session, vtnr), SD_BUS_VTABLE_PROPERTY_CONST),
552 SD_BUS_PROPERTY("Seat", "(so)", property_get_seat, 0, SD_BUS_VTABLE_PROPERTY_CONST),
553 SD_BUS_PROPERTY("TTY", "s", NULL, offsetof(Session, tty), SD_BUS_VTABLE_PROPERTY_CONST),
554 SD_BUS_PROPERTY("Display", "s", NULL, offsetof(Session, display), SD_BUS_VTABLE_PROPERTY_CONST),
555 SD_BUS_PROPERTY("Remote", "b", bus_property_get_bool, offsetof(Session, remote), SD_BUS_VTABLE_PROPERTY_CONST),
556 SD_BUS_PROPERTY("RemoteHost", "s", NULL, offsetof(Session, remote_host), SD_BUS_VTABLE_PROPERTY_CONST),
557 SD_BUS_PROPERTY("RemoteUser", "s", NULL, offsetof(Session, remote_user), SD_BUS_VTABLE_PROPERTY_CONST),
558 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Session, service), SD_BUS_VTABLE_PROPERTY_CONST),
559 SD_BUS_PROPERTY("Desktop", "s", NULL, offsetof(Session, desktop), SD_BUS_VTABLE_PROPERTY_CONST),
560 SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Session, scope), SD_BUS_VTABLE_PROPERTY_CONST),
561 SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid, offsetof(Session, leader), SD_BUS_VTABLE_PROPERTY_CONST),
562 SD_BUS_PROPERTY("Audit", "u", NULL, offsetof(Session, audit_id), SD_BUS_VTABLE_PROPERTY_CONST),
563 SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Session, type), SD_BUS_VTABLE_PROPERTY_CONST),
564 SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Session, class), SD_BUS_VTABLE_PROPERTY_CONST),
565 SD_BUS_PROPERTY("Active", "b", property_get_active, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
566 SD_BUS_PROPERTY("State", "s", property_get_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
567 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
568 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
569 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
570 SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
571
572 SD_BUS_METHOD("Terminate", NULL, NULL, bus_session_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
573 SD_BUS_METHOD("Activate", NULL, NULL, bus_session_method_activate, SD_BUS_VTABLE_UNPRIVILEGED),
574 SD_BUS_METHOD("Lock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
575 SD_BUS_METHOD("Unlock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
576 SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED),
577 SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED),
578 SD_BUS_METHOD("Kill", "si", NULL, bus_session_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
579 SD_BUS_METHOD("TakeControl", "b", NULL, method_take_control, SD_BUS_VTABLE_UNPRIVILEGED),
580 SD_BUS_METHOD("ReleaseControl", NULL, NULL, method_release_control, SD_BUS_VTABLE_UNPRIVILEGED),
581 SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED),
582 SD_BUS_METHOD("ReleaseDevice", "uu", NULL, method_release_device, SD_BUS_VTABLE_UNPRIVILEGED),
583 SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL, method_pause_device_complete, SD_BUS_VTABLE_UNPRIVILEGED),
584 SD_BUS_METHOD("SetBrightness", "ssu", NULL, method_set_brightness, SD_BUS_VTABLE_UNPRIVILEGED),
585
586 SD_BUS_SIGNAL("PauseDevice", "uus", 0),
587 SD_BUS_SIGNAL("ResumeDevice", "uuh", 0),
588 SD_BUS_SIGNAL("Lock", NULL, 0),
589 SD_BUS_SIGNAL("Unlock", NULL, 0),
590
591 SD_BUS_VTABLE_END
592 };
593
594 int session_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
595 _cleanup_free_ char *e = NULL;
596 sd_bus_message *message;
597 Manager *m = userdata;
598 Session *session;
599 const char *p;
600 int r;
601
602 assert(bus);
603 assert(path);
604 assert(interface);
605 assert(found);
606 assert(m);
607
608 p = startswith(path, "/org/freedesktop/login1/session/");
609 if (!p)
610 return 0;
611
612 e = bus_label_unescape(p);
613 if (!e)
614 return -ENOMEM;
615
616 message = sd_bus_get_current_message(bus);
617
618 r = manager_get_session_from_creds(m, message, e, error, &session);
619 if (r == -ENXIO) {
620 sd_bus_error_free(error);
621 return 0;
622 }
623 if (r < 0)
624 return r;
625
626 *found = session;
627 return 1;
628 }
629
630 char *session_bus_path(Session *s) {
631 _cleanup_free_ char *t = NULL;
632
633 assert(s);
634
635 t = bus_label_escape(s->id);
636 if (!t)
637 return NULL;
638
639 return strjoin("/org/freedesktop/login1/session/", t);
640 }
641
642 int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
643 _cleanup_strv_free_ char **l = NULL;
644 sd_bus_message *message;
645 Manager *m = userdata;
646 Session *session;
647 Iterator i;
648 int r;
649
650 assert(bus);
651 assert(path);
652 assert(nodes);
653
654 HASHMAP_FOREACH(session, m->sessions, i) {
655 char *p;
656
657 p = session_bus_path(session);
658 if (!p)
659 return -ENOMEM;
660
661 r = strv_consume(&l, p);
662 if (r < 0)
663 return r;
664 }
665
666 message = sd_bus_get_current_message(bus);
667 if (message) {
668 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
669
670 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
671 if (r >= 0) {
672 bool may_auto = false;
673 const char *name;
674
675 r = sd_bus_creds_get_session(creds, &name);
676 if (r >= 0) {
677 session = hashmap_get(m->sessions, name);
678 if (session) {
679 r = strv_extend(&l, "/org/freedesktop/login1/session/self");
680 if (r < 0)
681 return r;
682
683 may_auto = true;
684 }
685 }
686
687 if (!may_auto) {
688 uid_t uid;
689
690 r = sd_bus_creds_get_owner_uid(creds, &uid);
691 if (r >= 0) {
692 User *user;
693
694 user = hashmap_get(m->users, UID_TO_PTR(uid));
695 may_auto = user && user->display;
696 }
697 }
698
699 if (may_auto) {
700 r = strv_extend(&l, "/org/freedesktop/login1/session/auto");
701 if (r < 0)
702 return r;
703 }
704 }
705 }
706
707 *nodes = TAKE_PTR(l);
708 return 1;
709 }
710
711 int session_send_signal(Session *s, bool new_session) {
712 _cleanup_free_ char *p = NULL;
713
714 assert(s);
715
716 p = session_bus_path(s);
717 if (!p)
718 return -ENOMEM;
719
720 return sd_bus_emit_signal(
721 s->manager->bus,
722 "/org/freedesktop/login1",
723 "org.freedesktop.login1.Manager",
724 new_session ? "SessionNew" : "SessionRemoved",
725 "so", s->id, p);
726 }
727
728 int session_send_changed(Session *s, const char *properties, ...) {
729 _cleanup_free_ char *p = NULL;
730 char **l;
731
732 assert(s);
733
734 if (!s->started)
735 return 0;
736
737 p = session_bus_path(s);
738 if (!p)
739 return -ENOMEM;
740
741 l = strv_from_stdarg_alloca(properties);
742
743 return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Session", l);
744 }
745
746 int session_send_lock(Session *s, bool lock) {
747 _cleanup_free_ char *p = NULL;
748
749 assert(s);
750
751 p = session_bus_path(s);
752 if (!p)
753 return -ENOMEM;
754
755 return sd_bus_emit_signal(
756 s->manager->bus,
757 p,
758 "org.freedesktop.login1.Session",
759 lock ? "Lock" : "Unlock",
760 NULL);
761 }
762
763 int session_send_lock_all(Manager *m, bool lock) {
764 Session *session;
765 Iterator i;
766 int r = 0;
767
768 assert(m);
769
770 HASHMAP_FOREACH(session, m->sessions, i) {
771 int k;
772
773 k = session_send_lock(session, lock);
774 if (k < 0)
775 r = k;
776 }
777
778 return r;
779 }
780
781 static bool session_ready(Session *s) {
782 assert(s);
783
784 /* Returns true when the session is ready, i.e. all jobs we enqueued for it are done (regardless if successful or not) */
785
786 return !s->scope_job &&
787 !s->user->service_job;
788 }
789
790 int session_send_create_reply(Session *s, sd_bus_error *error) {
791 _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
792 _cleanup_close_ int fifo_fd = -1;
793 _cleanup_free_ char *p = NULL;
794
795 assert(s);
796
797 /* This is called after the session scope and the user service were successfully created, and finishes where
798 * bus_manager_create_session() left off. */
799
800 if (!s->create_message)
801 return 0;
802
803 if (!sd_bus_error_is_set(error) && !session_ready(s))
804 return 0;
805
806 c = TAKE_PTR(s->create_message);
807 if (error)
808 return sd_bus_reply_method_error(c, error);
809
810 fifo_fd = session_create_fifo(s);
811 if (fifo_fd < 0)
812 return fifo_fd;
813
814 /* Update the session state file before we notify the client about the result. */
815 session_save(s);
816
817 p = session_bus_path(s);
818 if (!p)
819 return -ENOMEM;
820
821 log_debug("Sending reply about created session: "
822 "id=%s object_path=%s uid=%u runtime_path=%s "
823 "session_fd=%d seat=%s vtnr=%u",
824 s->id,
825 p,
826 (uint32_t) s->user->user_record->uid,
827 s->user->runtime_path,
828 fifo_fd,
829 s->seat ? s->seat->id : "",
830 (uint32_t) s->vtnr);
831
832 return sd_bus_reply_method_return(
833 c, "soshusub",
834 s->id,
835 p,
836 s->user->runtime_path,
837 fifo_fd,
838 (uint32_t) s->user->user_record->uid,
839 s->seat ? s->seat->id : "",
840 (uint32_t) s->vtnr,
841 false);
842 }