]>
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 | ||
22c902fa | 46 | return sd_bus_message_append(reply, "(uo)", (uint32_t) s->user->user_record->uid, p); |
cc377381 | 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 | |
22c902fa | 64 | return sd_bus_message_append(reply, "s", s->user->user_record->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 | 170 | false, |
22c902fa | 171 | s->user->user_record->uid, |
c529695e LP |
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 | 212 | false, |
22c902fa | 213 | s->user->user_record->uid, |
c529695e LP |
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 | 248 | |
22c902fa | 249 | if (uid != 0 && uid != s->user->user_record->uid) |
2b233285 | 250 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set idle hint"); |
cc377381 | 251 | |
be2bb14f LP |
252 | r = session_set_idle_hint(s, b); |
253 | if (r == -ENOTTY) | |
254 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Idle hint control is not supported on non-graphical sessions."); | |
255 | if (r < 0) | |
256 | return r; | |
3f49d45a | 257 | |
df2d202e | 258 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
259 | } |
260 | ||
42d35e13 VT |
261 | static int method_set_locked_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
262 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
263 | Session *s = userdata; | |
264 | uid_t uid; | |
265 | int r, b; | |
266 | ||
267 | assert(message); | |
268 | assert(s); | |
269 | ||
270 | r = sd_bus_message_read(message, "b", &b); | |
271 | if (r < 0) | |
272 | return r; | |
273 | ||
274 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
275 | if (r < 0) | |
276 | return r; | |
277 | ||
278 | r = sd_bus_creds_get_euid(creds, &uid); | |
279 | if (r < 0) | |
280 | return r; | |
281 | ||
22c902fa | 282 | if (uid != 0 && uid != s->user->user_record->uid) |
42d35e13 VT |
283 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set locked hint"); |
284 | ||
285 | session_set_locked_hint(s, b); | |
286 | ||
287 | return sd_bus_reply_method_return(message, NULL); | |
288 | } | |
289 | ||
19070062 | 290 | int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
291 | Session *s = userdata; |
292 | const char *swho; | |
293 | int32_t signo; | |
294 | KillWho who; | |
bef422ae LP |
295 | int r; |
296 | ||
3f49d45a | 297 | assert(message); |
cc377381 | 298 | assert(s); |
3f49d45a | 299 | |
cc377381 LP |
300 | r = sd_bus_message_read(message, "si", &swho, &signo); |
301 | if (r < 0) | |
ebcf1f97 | 302 | return r; |
cc377381 LP |
303 | |
304 | if (isempty(swho)) | |
305 | who = KILL_ALL; | |
306 | else { | |
307 | who = kill_who_from_string(swho); | |
308 | if (who < 0) | |
ebcf1f97 | 309 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho); |
cc377381 | 310 | } |
bef422ae | 311 | |
6eb7c172 | 312 | if (!SIGNAL_VALID(signo)) |
ebcf1f97 | 313 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
bef422ae | 314 | |
c529695e LP |
315 | r = bus_verify_polkit_async( |
316 | message, | |
317 | CAP_KILL, | |
318 | "org.freedesktop.login1.manage", | |
403ed0e5 | 319 | NULL, |
c529695e | 320 | false, |
22c902fa | 321 | s->user->user_record->uid, |
c529695e LP |
322 | &s->manager->polkit_registry, |
323 | error); | |
324 | if (r < 0) | |
325 | return r; | |
326 | if (r == 0) | |
327 | return 1; /* Will call us back */ | |
328 | ||
cc377381 LP |
329 | r = session_kill(s, who, signo); |
330 | if (r < 0) | |
ebcf1f97 | 331 | return r; |
bef422ae | 332 | |
df2d202e | 333 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 334 | } |
bef422ae | 335 | |
19070062 | 336 | static int method_take_control(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 337 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
cc377381 LP |
338 | Session *s = userdata; |
339 | int r, force; | |
340 | uid_t uid; | |
bef422ae | 341 | |
cc377381 LP |
342 | assert(message); |
343 | assert(s); | |
bef422ae | 344 | |
cc377381 LP |
345 | r = sd_bus_message_read(message, "b", &force); |
346 | if (r < 0) | |
ebcf1f97 | 347 | return r; |
bef422ae | 348 | |
05bae4a6 | 349 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
5b12334d LP |
350 | if (r < 0) |
351 | return r; | |
352 | ||
05bae4a6 | 353 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 354 | if (r < 0) |
ebcf1f97 | 355 | return r; |
bef422ae | 356 | |
22c902fa | 357 | if (uid != 0 && (force || uid != s->user->user_record->uid)) |
ebcf1f97 | 358 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may take control"); |
bef422ae | 359 | |
dc6284e9 | 360 | r = session_set_controller(s, sd_bus_message_get_sender(message), force, true); |
cc377381 | 361 | if (r < 0) |
ebcf1f97 | 362 | return r; |
bef422ae | 363 | |
df2d202e | 364 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 365 | } |
bef422ae | 366 | |
19070062 | 367 | static int method_release_control(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 368 | Session *s = userdata; |
bef422ae | 369 | |
cc377381 LP |
370 | assert(message); |
371 | assert(s); | |
5bc849fd | 372 | |
cc377381 | 373 | if (!session_is_controller(s, sd_bus_message_get_sender(message))) |
ebcf1f97 | 374 | return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session"); |
5bc849fd | 375 | |
cc377381 | 376 | session_drop_controller(s); |
bef422ae | 377 | |
df2d202e | 378 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 379 | } |
bef422ae | 380 | |
19070062 | 381 | static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
382 | Session *s = userdata; |
383 | uint32_t major, minor; | |
384 | SessionDevice *sd; | |
385 | dev_t dev; | |
386 | int r; | |
de07ab16 | 387 | |
cc377381 LP |
388 | assert(message); |
389 | assert(s); | |
de07ab16 | 390 | |
cc377381 LP |
391 | r = sd_bus_message_read(message, "uu", &major, &minor); |
392 | if (r < 0) | |
ebcf1f97 | 393 | return r; |
cc377381 | 394 | |
fa583ab1 LP |
395 | if (!DEVICE_MAJOR_VALID(major) || !DEVICE_MINOR_VALID(minor)) |
396 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device major/minor is not valid."); | |
397 | ||
cc377381 | 398 | if (!session_is_controller(s, sd_bus_message_get_sender(message))) |
ebcf1f97 | 399 | return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session"); |
cc377381 LP |
400 | |
401 | dev = makedev(major, minor); | |
402 | sd = hashmap_get(s->devices, &dev); | |
403 | if (sd) | |
404 | /* We don't allow retrieving a device multiple times. | |
405 | * The related ReleaseDevice call is not ref-counted. | |
406 | * The caller should use dup() if it requires more | |
407 | * than one fd (it would be functionally | |
408 | * equivalent). */ | |
ebcf1f97 | 409 | return sd_bus_error_setf(error, BUS_ERROR_DEVICE_IS_TAKEN, "Device already taken"); |
cc377381 | 410 | |
aed24c4c | 411 | r = session_device_new(s, dev, true, &sd); |
cc377381 | 412 | if (r < 0) |
ebcf1f97 | 413 | return r; |
de07ab16 | 414 | |
aed24c4c FB |
415 | r = session_device_save(sd); |
416 | if (r < 0) | |
417 | goto error; | |
418 | ||
df2d202e | 419 | r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active); |
cc377381 | 420 | if (r < 0) |
aed24c4c FB |
421 | goto error; |
422 | ||
423 | session_save(s); | |
2e681921 | 424 | return 1; |
118ecf32 | 425 | |
aed24c4c FB |
426 | error: |
427 | session_device_free(sd); | |
cc377381 LP |
428 | return r; |
429 | } | |
118ecf32 | 430 | |
19070062 | 431 | static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
432 | Session *s = userdata; |
433 | uint32_t major, minor; | |
434 | SessionDevice *sd; | |
435 | dev_t dev; | |
436 | int r; | |
118ecf32 | 437 | |
cc377381 LP |
438 | assert(message); |
439 | assert(s); | |
118ecf32 | 440 | |
cc377381 LP |
441 | r = sd_bus_message_read(message, "uu", &major, &minor); |
442 | if (r < 0) | |
ebcf1f97 | 443 | return r; |
118ecf32 | 444 | |
fa583ab1 LP |
445 | if (!DEVICE_MAJOR_VALID(major) || !DEVICE_MINOR_VALID(minor)) |
446 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device major/minor is not valid."); | |
447 | ||
cc377381 | 448 | if (!session_is_controller(s, sd_bus_message_get_sender(message))) |
ebcf1f97 | 449 | return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session"); |
118ecf32 | 450 | |
cc377381 LP |
451 | dev = makedev(major, minor); |
452 | sd = hashmap_get(s->devices, &dev); | |
453 | if (!sd) | |
ebcf1f97 | 454 | return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken"); |
118ecf32 | 455 | |
cc377381 | 456 | session_device_free(sd); |
aed24c4c FB |
457 | session_save(s); |
458 | ||
df2d202e | 459 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 460 | } |
118ecf32 | 461 | |
19070062 | 462 | static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
463 | Session *s = userdata; |
464 | uint32_t major, minor; | |
465 | SessionDevice *sd; | |
466 | dev_t dev; | |
467 | int r; | |
118ecf32 | 468 | |
cc377381 LP |
469 | assert(message); |
470 | assert(s); | |
bef422ae | 471 | |
cc377381 LP |
472 | r = sd_bus_message_read(message, "uu", &major, &minor); |
473 | if (r < 0) | |
ebcf1f97 | 474 | return r; |
cc377381 | 475 | |
fa583ab1 LP |
476 | if (!DEVICE_MAJOR_VALID(major) || !DEVICE_MINOR_VALID(minor)) |
477 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device major/minor is not valid."); | |
478 | ||
cc377381 | 479 | if (!session_is_controller(s, sd_bus_message_get_sender(message))) |
ebcf1f97 | 480 | return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session"); |
bef422ae | 481 | |
cc377381 LP |
482 | dev = makedev(major, minor); |
483 | sd = hashmap_get(s->devices, &dev); | |
484 | if (!sd) | |
ebcf1f97 | 485 | return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken"); |
bef422ae | 486 | |
cc377381 | 487 | session_device_complete_pause(sd); |
bef422ae | 488 | |
df2d202e | 489 | return sd_bus_reply_method_return(message, NULL); |
3f49d45a LP |
490 | } |
491 | ||
2a66c2a1 LP |
492 | static int method_set_brightness(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
493 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
494 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; | |
495 | const char *subsystem, *name, *seat; | |
496 | Session *s = userdata; | |
497 | uint32_t brightness; | |
498 | uid_t uid; | |
499 | int r; | |
500 | ||
501 | assert(message); | |
502 | assert(s); | |
503 | ||
504 | r = sd_bus_message_read(message, "ssu", &subsystem, &name, &brightness); | |
505 | if (r < 0) | |
506 | return r; | |
507 | ||
508 | if (!STR_IN_SET(subsystem, "backlight", "leds")) | |
509 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Subsystem type %s not supported, must be one of 'backlight' or 'leds'.", subsystem); | |
510 | if (!filename_is_valid(name)) | |
511 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not a valid device name %s, refusing.", name); | |
512 | ||
513 | if (!s->seat) | |
514 | return sd_bus_error_setf(error, BUS_ERROR_NOT_YOUR_DEVICE, "Your session has no seat, refusing."); | |
515 | if (s->seat->active != s) | |
516 | return sd_bus_error_setf(error, BUS_ERROR_NOT_YOUR_DEVICE, "Session is not in foreground, refusing."); | |
517 | ||
518 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
519 | if (r < 0) | |
520 | return r; | |
521 | ||
522 | r = sd_bus_creds_get_euid(creds, &uid); | |
523 | if (r < 0) | |
524 | return r; | |
525 | ||
22c902fa | 526 | if (uid != 0 && uid != s->user->user_record->uid) |
2a66c2a1 LP |
527 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may change brightness."); |
528 | ||
529 | r = sd_device_new_from_subsystem_sysname(&d, subsystem, name); | |
530 | if (r < 0) | |
531 | return sd_bus_error_set_errnof(error, r, "Failed to open device %s:%s: %m", subsystem, name); | |
532 | ||
533 | if (sd_device_get_property_value(d, "ID_SEAT", &seat) >= 0 && !streq_ptr(seat, s->seat->id)) | |
534 | 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); | |
535 | ||
536 | r = manager_write_brightness(s->manager, d, brightness, message); | |
537 | if (r < 0) | |
538 | return r; | |
539 | ||
540 | return 1; | |
541 | } | |
542 | ||
cc377381 LP |
543 | const sd_bus_vtable session_vtable[] = { |
544 | SD_BUS_VTABLE_START(0), | |
545 | ||
556089dc LP |
546 | SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Session, id), SD_BUS_VTABLE_PROPERTY_CONST), |
547 | SD_BUS_PROPERTY("User", "(uo)", property_get_user, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
548 | SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
549 | BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Session, timestamp), SD_BUS_VTABLE_PROPERTY_CONST), | |
550 | SD_BUS_PROPERTY("VTNr", "u", NULL, offsetof(Session, vtnr), SD_BUS_VTABLE_PROPERTY_CONST), | |
551 | SD_BUS_PROPERTY("Seat", "(so)", property_get_seat, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
552 | SD_BUS_PROPERTY("TTY", "s", NULL, offsetof(Session, tty), SD_BUS_VTABLE_PROPERTY_CONST), | |
553 | SD_BUS_PROPERTY("Display", "s", NULL, offsetof(Session, display), SD_BUS_VTABLE_PROPERTY_CONST), | |
554 | SD_BUS_PROPERTY("Remote", "b", bus_property_get_bool, offsetof(Session, remote), SD_BUS_VTABLE_PROPERTY_CONST), | |
555 | SD_BUS_PROPERTY("RemoteHost", "s", NULL, offsetof(Session, remote_host), SD_BUS_VTABLE_PROPERTY_CONST), | |
556 | SD_BUS_PROPERTY("RemoteUser", "s", NULL, offsetof(Session, remote_user), SD_BUS_VTABLE_PROPERTY_CONST), | |
557 | SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Session, service), SD_BUS_VTABLE_PROPERTY_CONST), | |
a4cd87e9 | 558 | SD_BUS_PROPERTY("Desktop", "s", NULL, offsetof(Session, desktop), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
559 | SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Session, scope), SD_BUS_VTABLE_PROPERTY_CONST), |
560 | SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid, offsetof(Session, leader), SD_BUS_VTABLE_PROPERTY_CONST), | |
561 | SD_BUS_PROPERTY("Audit", "u", NULL, offsetof(Session, audit_id), SD_BUS_VTABLE_PROPERTY_CONST), | |
562 | SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Session, type), SD_BUS_VTABLE_PROPERTY_CONST), | |
563 | SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Session, class), SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 | 564 | SD_BUS_PROPERTY("Active", "b", property_get_active, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
5afb1f27 | 565 | SD_BUS_PROPERTY("State", "s", property_get_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
cc377381 LP |
566 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
567 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
568 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
42d35e13 | 569 | SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
cc377381 | 570 | |
c529695e LP |
571 | SD_BUS_METHOD("Terminate", NULL, NULL, bus_session_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED), |
572 | SD_BUS_METHOD("Activate", NULL, NULL, bus_session_method_activate, SD_BUS_VTABLE_UNPRIVILEGED), | |
573 | SD_BUS_METHOD("Lock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED), | |
574 | SD_BUS_METHOD("Unlock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED), | |
adacb957 | 575 | SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED), |
42d35e13 | 576 | SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED), |
c529695e | 577 | SD_BUS_METHOD("Kill", "si", NULL, bus_session_method_kill, SD_BUS_VTABLE_UNPRIVILEGED), |
adacb957 LP |
578 | SD_BUS_METHOD("TakeControl", "b", NULL, method_take_control, SD_BUS_VTABLE_UNPRIVILEGED), |
579 | SD_BUS_METHOD("ReleaseControl", NULL, NULL, method_release_control, SD_BUS_VTABLE_UNPRIVILEGED), | |
580 | SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED), | |
581 | SD_BUS_METHOD("ReleaseDevice", "uu", NULL, method_release_device, SD_BUS_VTABLE_UNPRIVILEGED), | |
582 | SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL, method_pause_device_complete, SD_BUS_VTABLE_UNPRIVILEGED), | |
2a66c2a1 | 583 | SD_BUS_METHOD("SetBrightness", "ssu", NULL, method_set_brightness, SD_BUS_VTABLE_UNPRIVILEGED), |
cc377381 LP |
584 | |
585 | SD_BUS_SIGNAL("PauseDevice", "uus", 0), | |
586 | SD_BUS_SIGNAL("ResumeDevice", "uuh", 0), | |
587 | SD_BUS_SIGNAL("Lock", NULL, 0), | |
588 | SD_BUS_SIGNAL("Unlock", NULL, 0), | |
589 | ||
590 | SD_BUS_VTABLE_END | |
591 | }; | |
3f49d45a | 592 | |
f00c3121 | 593 | int session_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
3b92c086 LP |
594 | _cleanup_free_ char *e = NULL; |
595 | sd_bus_message *message; | |
3f49d45a | 596 | Manager *m = userdata; |
cc377381 | 597 | Session *session; |
3b92c086 | 598 | const char *p; |
927b1649 | 599 | int r; |
3f49d45a | 600 | |
cc377381 LP |
601 | assert(bus); |
602 | assert(path); | |
603 | assert(interface); | |
604 | assert(found); | |
605 | assert(m); | |
3f49d45a | 606 | |
3b92c086 LP |
607 | p = startswith(path, "/org/freedesktop/login1/session/"); |
608 | if (!p) | |
609 | return 0; | |
3f49d45a | 610 | |
3b92c086 LP |
611 | e = bus_label_unescape(p); |
612 | if (!e) | |
613 | return -ENOMEM; | |
927b1649 | 614 | |
3b92c086 | 615 | message = sd_bus_get_current_message(bus); |
927b1649 | 616 | |
3b92c086 LP |
617 | r = manager_get_session_from_creds(m, message, e, error, &session); |
618 | if (r == -ENXIO) { | |
619 | sd_bus_error_free(error); | |
620 | return 0; | |
927b1649 | 621 | } |
3b92c086 LP |
622 | if (r < 0) |
623 | return r; | |
3f49d45a | 624 | |
cc377381 LP |
625 | *found = session; |
626 | return 1; | |
3f49d45a LP |
627 | } |
628 | ||
3f49d45a | 629 | char *session_bus_path(Session *s) { |
9444b1f2 | 630 | _cleanup_free_ char *t = NULL; |
3f49d45a LP |
631 | |
632 | assert(s); | |
633 | ||
a6278b88 | 634 | t = bus_label_escape(s->id); |
3f49d45a LP |
635 | if (!t) |
636 | return NULL; | |
637 | ||
b910cc72 | 638 | return strjoin("/org/freedesktop/login1/session/", t); |
3f49d45a | 639 | } |
da119395 | 640 | |
f00c3121 | 641 | int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
cc377381 | 642 | _cleanup_strv_free_ char **l = NULL; |
ca56b0a6 | 643 | sd_bus_message *message; |
cc377381 LP |
644 | Manager *m = userdata; |
645 | Session *session; | |
646 | Iterator i; | |
647 | int r; | |
648 | ||
649 | assert(bus); | |
650 | assert(path); | |
651 | assert(nodes); | |
652 | ||
653 | HASHMAP_FOREACH(session, m->sessions, i) { | |
654 | char *p; | |
655 | ||
656 | p = session_bus_path(session); | |
657 | if (!p) | |
658 | return -ENOMEM; | |
659 | ||
6e18964d ZJS |
660 | r = strv_consume(&l, p); |
661 | if (r < 0) | |
cc377381 | 662 | return r; |
cc377381 LP |
663 | } |
664 | ||
ca56b0a6 DH |
665 | message = sd_bus_get_current_message(bus); |
666 | if (message) { | |
4afd3348 | 667 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
ca56b0a6 | 668 | |
3b92c086 | 669 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); |
ca56b0a6 | 670 | if (r >= 0) { |
3b92c086 LP |
671 | bool may_auto = false; |
672 | const char *name; | |
673 | ||
ca56b0a6 DH |
674 | r = sd_bus_creds_get_session(creds, &name); |
675 | if (r >= 0) { | |
676 | session = hashmap_get(m->sessions, name); | |
677 | if (session) { | |
678 | r = strv_extend(&l, "/org/freedesktop/login1/session/self"); | |
679 | if (r < 0) | |
680 | return r; | |
3b92c086 LP |
681 | |
682 | may_auto = true; | |
683 | } | |
684 | } | |
685 | ||
686 | if (!may_auto) { | |
687 | uid_t uid; | |
688 | ||
689 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
690 | if (r >= 0) { | |
691 | User *user; | |
692 | ||
693 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
694 | may_auto = user && user->display; | |
ca56b0a6 DH |
695 | } |
696 | } | |
3b92c086 LP |
697 | |
698 | if (may_auto) { | |
699 | r = strv_extend(&l, "/org/freedesktop/login1/session/auto"); | |
700 | if (r < 0) | |
701 | return r; | |
702 | } | |
ca56b0a6 DH |
703 | } |
704 | } | |
b298e984 | 705 | |
1cc6c93a | 706 | *nodes = TAKE_PTR(l); |
cc377381 LP |
707 | return 1; |
708 | } | |
709 | ||
da119395 | 710 | int session_send_signal(Session *s, bool new_session) { |
ce0fc5f5 | 711 | _cleanup_free_ char *p = NULL; |
da119395 LP |
712 | |
713 | assert(s); | |
714 | ||
da119395 LP |
715 | p = session_bus_path(s); |
716 | if (!p) | |
4654e558 | 717 | return -ENOMEM; |
da119395 | 718 | |
cc377381 LP |
719 | return sd_bus_emit_signal( |
720 | s->manager->bus, | |
721 | "/org/freedesktop/login1", | |
722 | "org.freedesktop.login1.Manager", | |
723 | new_session ? "SessionNew" : "SessionRemoved", | |
724 | "so", s->id, p); | |
da119395 | 725 | } |
9418f147 | 726 | |
cc377381 | 727 | int session_send_changed(Session *s, const char *properties, ...) { |
ce0fc5f5 | 728 | _cleanup_free_ char *p = NULL; |
cc377381 | 729 | char **l; |
9418f147 LP |
730 | |
731 | assert(s); | |
732 | ||
ed18b08b LP |
733 | if (!s->started) |
734 | return 0; | |
735 | ||
9418f147 LP |
736 | p = session_bus_path(s); |
737 | if (!p) | |
738 | return -ENOMEM; | |
739 | ||
cc377381 | 740 | l = strv_from_stdarg_alloca(properties); |
9418f147 | 741 | |
cc377381 | 742 | return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Session", l); |
9418f147 | 743 | } |
88e3dc90 LP |
744 | |
745 | int session_send_lock(Session *s, bool lock) { | |
ce0fc5f5 | 746 | _cleanup_free_ char *p = NULL; |
88e3dc90 LP |
747 | |
748 | assert(s); | |
749 | ||
750 | p = session_bus_path(s); | |
751 | if (!p) | |
752 | return -ENOMEM; | |
753 | ||
cc377381 LP |
754 | return sd_bus_emit_signal( |
755 | s->manager->bus, | |
756 | p, | |
757 | "org.freedesktop.login1.Session", | |
758 | lock ? "Lock" : "Unlock", | |
759 | NULL); | |
88e3dc90 | 760 | } |
7ba64386 LP |
761 | |
762 | int session_send_lock_all(Manager *m, bool lock) { | |
763 | Session *session; | |
764 | Iterator i; | |
765 | int r = 0; | |
766 | ||
767 | assert(m); | |
768 | ||
769 | HASHMAP_FOREACH(session, m->sessions, i) { | |
770 | int k; | |
771 | ||
772 | k = session_send_lock(session, lock); | |
773 | if (k < 0) | |
774 | r = k; | |
775 | } | |
776 | ||
777 | return r; | |
778 | } | |
fb6becb4 | 779 | |
b1951bc8 LP |
780 | static bool session_ready(Session *s) { |
781 | assert(s); | |
782 | ||
783 | /* Returns true when the session is ready, i.e. all jobs we enqueued for it are done (regardless if successful or not) */ | |
784 | ||
785 | return !s->scope_job && | |
786 | !s->user->service_job; | |
787 | } | |
788 | ||
cc377381 | 789 | int session_send_create_reply(Session *s, sd_bus_error *error) { |
4afd3348 | 790 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL; |
cc377381 LP |
791 | _cleanup_close_ int fifo_fd = -1; |
792 | _cleanup_free_ char *p = NULL; | |
fb6becb4 LP |
793 | |
794 | assert(s); | |
795 | ||
b1951bc8 | 796 | /* This is called after the session scope and the user service were successfully created, and finishes where |
dd9b67aa | 797 | * bus_manager_create_session() left off. */ |
cba38758 | 798 | |
cc377381 LP |
799 | if (!s->create_message) |
800 | return 0; | |
fb6becb4 | 801 | |
b1951bc8 | 802 | if (!sd_bus_error_is_set(error) && !session_ready(s)) |
dd9b67aa LP |
803 | return 0; |
804 | ||
1b88ed3b | 805 | c = TAKE_PTR(s->create_message); |
cc377381 | 806 | if (error) |
df2d202e | 807 | return sd_bus_reply_method_error(c, error); |
fb6becb4 | 808 | |
cc377381 LP |
809 | fifo_fd = session_create_fifo(s); |
810 | if (fifo_fd < 0) | |
811 | return fifo_fd; | |
fb6becb4 | 812 | |
b1951bc8 | 813 | /* Update the session state file before we notify the client about the result. */ |
38fdcbed TA |
814 | session_save(s); |
815 | ||
cc377381 LP |
816 | p = session_bus_path(s); |
817 | if (!p) | |
818 | return -ENOMEM; | |
fb6becb4 | 819 | |
5a330cda | 820 | log_debug("Sending reply about created session: " |
236af516 DH |
821 | "id=%s object_path=%s uid=%u runtime_path=%s " |
822 | "session_fd=%d seat=%s vtnr=%u", | |
5a330cda ZJS |
823 | s->id, |
824 | p, | |
22c902fa | 825 | (uint32_t) s->user->user_record->uid, |
5a330cda ZJS |
826 | s->user->runtime_path, |
827 | fifo_fd, | |
828 | s->seat ? s->seat->id : "", | |
829 | (uint32_t) s->vtnr); | |
830 | ||
cc377381 | 831 | return sd_bus_reply_method_return( |
baae0358 | 832 | c, "soshusub", |
cc377381 LP |
833 | s->id, |
834 | p, | |
835 | s->user->runtime_path, | |
836 | fifo_fd, | |
22c902fa | 837 | (uint32_t) s->user->user_record->uid, |
cc377381 LP |
838 | s->seat ? s->seat->id : "", |
839 | (uint32_t) s->vtnr, | |
840 | false); | |
fb6becb4 | 841 | } |