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