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