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