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