Debian Codegraph @

src/portable/portabled-bus.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-error.h"
7 #include "bus-object.h"
8 #include "bus-polkit.h"
9 #include "discover-image.h"
10 #include "hashmap.h"
11 #include "io-util.h"
12 #include "log.h"
13 #include "portable.h"
14 #include "portabled.h"
15 #include "portabled-bus.h"
16 #include "portabled-image.h"
17 #include "portabled-image-bus.h"
18 #include "strv.h"
19
20 static int property_get_pool_path(
21 sd_bus *bus,
22 const char *path,
23 const char *interface,
24 const char *property,
25 sd_bus_message *reply,
26 void *userdata,
27 sd_bus_error *error) {
28
29 _cleanup_free_ char *dir = NULL;
30 Manager *m = ASSERT_PTR(userdata);
31
32 assert(bus);
33 assert(reply);
34
35 (void) image_get_pool_path(m->runtime_scope, IMAGE_PORTABLE, &dir);
36
37 return sd_bus_message_append(reply, "s", strempty(dir));
38 }
39
40 static int property_get_pool_usage(
41 sd_bus *bus,
42 const char *path,
43 const char *interface,
44 const char *property,
45 sd_bus_message *reply,
46 void *userdata,
47 sd_bus_error *error) {
48
49 uint64_t usage = UINT64_MAX;
50 Manager *m = ASSERT_PTR(userdata);
51
52 assert(bus);
53 assert(reply);
54
55 (void) image_get_pool_usage(m->runtime_scope, IMAGE_PORTABLE, &usage);
56
57 return sd_bus_message_append(reply, "t", usage);
58 }
59
60 static int property_get_pool_limit(
61 sd_bus *bus,
62 const char *path,
63 const char *interface,
64 const char *property,
65 sd_bus_message *reply,
66 void *userdata,
67 sd_bus_error *error) {
68
69 Manager *m = ASSERT_PTR(userdata);
70 uint64_t size = UINT64_MAX;
71
72 assert(bus);
73 assert(reply);
74
75 (void) image_get_pool_limit(m->runtime_scope, IMAGE_PORTABLE, &size);
76
77 return sd_bus_message_append(reply, "t", size);
78 }
79
80 static int property_get_profiles(
81 sd_bus *bus,
82 const char *path,
83 const char *interface,
84 const char *property,
85 sd_bus_message *reply,
86 void *userdata,
87 sd_bus_error *error) {
88
89 _cleanup_strv_free_ char **l = NULL;
90 Manager *m = ASSERT_PTR(userdata);
91 int r;
92
93 assert(bus);
94 assert(reply);
95
96 r = portable_get_profiles(m->runtime_scope, &l);
97 if (r < 0)
98 return r;
99
100 return sd_bus_message_append_strv(reply, l);
101 }
102
103 static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
104 _cleanup_free_ char *p = NULL;
105 Manager *m = ASSERT_PTR(userdata);
106 const char *name;
107 Image *image;
108 int r;
109
110 assert(message);
111
112 r = sd_bus_message_read(message, "s", &name);
113 if (r < 0)
114 return r;
115
116 r = bus_image_acquire(m, message, name, NULL, BUS_IMAGE_REFUSE_BY_PATH, NULL, &image, error);
117 if (r < 0)
118 return r;
119
120 r = bus_image_path(image, &p);
121 if (r < 0)
122 return r;
123
124 return sd_bus_reply_method_return(message, "o", p);
125 }
126
127 static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
128 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
129 _cleanup_hashmap_free_ Hashmap *images = NULL;
130 Manager *m = ASSERT_PTR(userdata);
131 Image *image;
132 int r;
133
134 assert(message);
135
136 r = manager_image_cache_discover(m, &images, error);
137 if (r < 0)
138 return r;
139
140 r = sd_bus_message_new_method_return(message, &reply);
141 if (r < 0)
142 return r;
143
144 r = sd_bus_message_open_container(reply, 'a', "(ssbtttso)");
145 if (r < 0)
146 return r;
147
148 HASHMAP_FOREACH(image, images) {
149 _cleanup_(sd_bus_error_free) sd_bus_error error_state = SD_BUS_ERROR_NULL;
150 PortableState state = _PORTABLE_STATE_INVALID;
151 _cleanup_free_ char *p = NULL;
152
153 r = bus_image_path(image, &p);
154 if (r < 0)
155 return r;
156
157 r = portable_get_state(
158 m->runtime_scope,
159 sd_bus_message_get_bus(message),
160 image->path,
161 NULL,
162 0,
163 &state,
164 &error_state);
165 if (r < 0)
166 log_debug_errno(r, "Failed to get state of image '%s', ignoring: %s",
167 image->path, bus_error_message(&error_state, r));
168
169 r = sd_bus_message_append(reply, "(ssbtttso)",
170 image->name,
171 image_type_to_string(image->type),
172 image_is_read_only(image),
173 image->crtime,
174 image->mtime,
175 image->usage,
176 portable_state_to_string(state),
177 p);
178 if (r < 0)
179 return r;
180 }
181
182 r = sd_bus_message_close_container(reply);
183 if (r < 0)
184 return r;
185
186 return sd_bus_message_send(reply);
187 }
188
189 static int redirect_method_to_image(
190 Manager *m,
191 sd_bus_message *message,
192 sd_bus_error *error,
193 int (*method)(Manager *m, sd_bus_message *message, const char *name_or_path, Image *image, sd_bus_error* error)) {
194
195 const char *name_or_path;
196 int r;
197
198 assert(m);
199 assert(message);
200 assert(method);
201
202 r = sd_bus_message_read(message, "s", &name_or_path);
203 if (r < 0)
204 return r;
205
206 return method(m, message, name_or_path, NULL, error);
207 }
208
209 static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
210 return redirect_method_to_image(userdata, message, error, bus_image_common_get_os_release);
211 }
212
213 static int method_get_image_metadata(sd_bus_message *message, void *userdata, sd_bus_error *error) {
214 return redirect_method_to_image(userdata, message, error, bus_image_common_get_metadata);
215 }
216
217 static int method_get_image_state(sd_bus_message *message, void *userdata, sd_bus_error *error) {
218 _cleanup_strv_free_ char **extension_images = NULL;
219 Manager *m = ASSERT_PTR(userdata);
220 const char *name_or_path;
221 PortableState state;
222 int r;
223
224 assert(message);
225
226 r = sd_bus_message_read(message, "s", &name_or_path);
227 if (r < 0)
228 return r;
229
230 if (sd_bus_message_is_method_call(message, NULL, "GetImageStateWithExtensions")) {
231 uint64_t input_flags = 0;
232
233 r = sd_bus_message_read_strv(message, &extension_images);
234 if (r < 0)
235 return r;
236
237 r = sd_bus_message_read(message, "t", &input_flags);
238 if (r < 0)
239 return r;
240
241 /* No flags are supported by this method for now. */
242 if (input_flags != 0)
243 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
244 "Invalid 'flags' parameter '%" PRIu64 "'",
245 input_flags);
246 }
247
248 r = portable_get_state(
249 m->runtime_scope,
250 sd_bus_message_get_bus(message),
251 name_or_path,
252 extension_images,
253 0,
254 &state,
255 error);
256 if (r < 0)
257 return r;
258
259 return sd_bus_reply_method_return(message, "s", portable_state_to_string(state));
260 }
261
262 static int method_attach_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
263 return redirect_method_to_image(userdata, message, error, bus_image_common_attach);
264 }
265
266 static int method_detach_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
267 _cleanup_strv_free_ char **extension_images = NULL;
268 PortableChange *changes = NULL;
269 PortableFlags flags = 0;
270 Manager *m = ASSERT_PTR(userdata);
271 size_t n_changes = 0;
272 const char *name_or_path;
273 int r;
274
275 assert(message);
276
277 CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
278
279 /* Note that we do not redirect detaching to the image object here, because we want to allow that users can
280 * detach already deleted images too, in case the user already deleted an image before properly detaching
281 * it. */
282
283 r = sd_bus_message_read(message, "s", &name_or_path);
284 if (r < 0)
285 return r;
286
287 if (sd_bus_message_is_method_call(message, NULL, "DetachImageWithExtensions")) {
288 uint64_t input_flags = 0;
289
290 r = sd_bus_message_read_strv(message, &extension_images);
291 if (r < 0)
292 return r;
293
294 r = sd_bus_message_read(message, "t", &input_flags);
295 if (r < 0)
296 return r;
297
298 if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0)
299 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
300 "Invalid 'flags' parameter '%" PRIu64 "'",
301 input_flags);
302 flags |= input_flags;
303 } else {
304 int runtime;
305
306 r = sd_bus_message_read(message, "b", &runtime);
307 if (r < 0)
308 return r;
309
310 if (runtime)
311 flags |= PORTABLE_RUNTIME;
312 }
313
314 if (m->runtime_scope != RUNTIME_SCOPE_USER) {
315 r = bus_verify_polkit_async(
316 message,
317 "org.freedesktop.portable1.attach-images",
318 /* details= */ NULL,
319 &m->polkit_registry,
320 error);
321 if (r < 0)
322 return r;
323 if (r == 0)
324 return 1; /* Will call us back */
325 }
326
327 r = portable_detach(
328 m->runtime_scope,
329 sd_bus_message_get_bus(message),
330 name_or_path,
331 extension_images,
332 flags,
333 &changes,
334 &n_changes,
335 error);
336 if (r < 0)
337 return r;
338
339 return reply_portable_changes(message, changes, n_changes);
340 }
341
342 static int method_reattach_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
343 return redirect_method_to_image(userdata, message, error, bus_image_common_reattach);
344 }
345
346 static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
347 return redirect_method_to_image(userdata, message, error, bus_image_common_remove);
348 }
349
350 static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) {
351 return redirect_method_to_image(userdata, message, error, bus_image_common_mark_read_only);
352 }
353
354 static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
355 return redirect_method_to_image(userdata, message, error, bus_image_common_set_limit);
356 }
357
358 static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
359 Manager *m = userdata;
360 uint64_t limit;
361 int r;
362
363 assert(message);
364
365 r = sd_bus_message_read(message, "t", &limit);
366 if (r < 0)
367 return r;
368 if (!FILE_SIZE_VALID_OR_INFINITY(limit))
369 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
370
371 if (m->runtime_scope != RUNTIME_SCOPE_USER) {
372 r = bus_verify_polkit_async(
373 message,
374 "org.freedesktop.portable1.manage-images",
375 /* details= */ NULL,
376 &m->polkit_registry,
377 error);
378 if (r < 0)
379 return r;
380 if (r == 0)
381 return 1; /* Will call us back */
382 }
383
384 r = image_set_pool_limit(m->runtime_scope, IMAGE_MACHINE, limit);
385 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
386 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
387 if (r < 0)
388 return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
389
390 return sd_bus_reply_method_return(message, NULL);
391 }
392
393 const sd_bus_vtable manager_vtable[] = {
394 SD_BUS_VTABLE_START(0),
395 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
396 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
397 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
398 SD_BUS_PROPERTY("Profiles", "as", property_get_profiles, 0, 0),
399 SD_BUS_METHOD_WITH_ARGS("GetImage",
400 SD_BUS_ARGS("s", image),
401 SD_BUS_RESULT("o", object),
402 method_get_image,
403 SD_BUS_VTABLE_UNPRIVILEGED),
404 SD_BUS_METHOD_WITH_ARGS("ListImages",
405 SD_BUS_NO_ARGS,
406 SD_BUS_RESULT("a(ssbtttso)", images),
407 method_list_images,
408 SD_BUS_VTABLE_UNPRIVILEGED),
409 SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease",
410 SD_BUS_ARGS("s", image),
411 SD_BUS_RESULT("a{ss}", os_release),
412 method_get_image_os_release,
413 SD_BUS_VTABLE_UNPRIVILEGED),
414 SD_BUS_METHOD_WITH_ARGS("GetImageMetadata",
415 SD_BUS_ARGS("s", image,
416 "as", matches),
417 SD_BUS_RESULT("s", image,
418 "ay", os_release,
419 "a{say}", units),
420 method_get_image_metadata,
421 SD_BUS_VTABLE_UNPRIVILEGED),
422 SD_BUS_METHOD_WITH_ARGS("GetImageMetadataWithExtensions",
423 SD_BUS_ARGS("s", image,
424 "as", extensions,
425 "as", matches,
426 "t", flags),
427 SD_BUS_RESULT("s", image,
428 "ay", os_release,
429 "a{say}", extensions,
430 "a{say}", units),
431 method_get_image_metadata,
432 SD_BUS_VTABLE_UNPRIVILEGED),
433 SD_BUS_METHOD_WITH_ARGS("GetImageState",
434 SD_BUS_ARGS("s", image),
435 SD_BUS_RESULT("s", state),
436 method_get_image_state,
437 SD_BUS_VTABLE_UNPRIVILEGED),
438 SD_BUS_METHOD_WITH_ARGS("GetImageStateWithExtensions",
439 SD_BUS_ARGS("s", image,
440 "as", extensions,
441 "t", flags),
442 SD_BUS_RESULT("s", state),
443 method_get_image_state,
444 SD_BUS_VTABLE_UNPRIVILEGED),
445 SD_BUS_METHOD_WITH_ARGS("AttachImage",
446 SD_BUS_ARGS("s", image,
447 "as", matches,
448 "s", profile,
449 "b", runtime,
450 "s", copy_mode),
451 SD_BUS_RESULT("a(sss)", changes),
452 method_attach_image,
453 SD_BUS_VTABLE_UNPRIVILEGED),
454 SD_BUS_METHOD_WITH_ARGS("AttachImageWithExtensions",
455 SD_BUS_ARGS("s", image,
456 "as", extensions,
457 "as", matches,
458 "s", profile,
459 "s", copy_mode,
460 "t", flags),
461 SD_BUS_RESULT("a(sss)", changes),
462 method_attach_image,
463 SD_BUS_VTABLE_UNPRIVILEGED),
464 SD_BUS_METHOD_WITH_ARGS("DetachImage",
465 SD_BUS_ARGS("s", image,
466 "b", runtime),
467 SD_BUS_RESULT("a(sss)", changes),
468 method_detach_image,
469 SD_BUS_VTABLE_UNPRIVILEGED),
470 SD_BUS_METHOD_WITH_ARGS("DetachImageWithExtensions",
471 SD_BUS_ARGS("s", image,
472 "as", extensions,
473 "t", flags),
474 SD_BUS_RESULT("a(sss)", changes),
475 method_detach_image,
476 SD_BUS_VTABLE_UNPRIVILEGED),
477 SD_BUS_METHOD_WITH_ARGS("ReattachImage",
478 SD_BUS_ARGS("s", image,
479 "as", matches,
480 "s", profile,
481 "b", runtime,
482 "s", copy_mode),
483 SD_BUS_RESULT("a(sss)", changes_removed,
484 "a(sss)", changes_updated),
485 method_reattach_image,
486 SD_BUS_VTABLE_UNPRIVILEGED),
487 SD_BUS_METHOD_WITH_ARGS("ReattachImageWithExtensions",
488 SD_BUS_ARGS("s", image,
489 "as", extensions,
490 "as", matches,
491 "s", profile,
492 "s", copy_mode,
493 "t", flags),
494 SD_BUS_RESULT("a(sss)", changes_removed,
495 "a(sss)", changes_updated),
496 method_reattach_image,
497 SD_BUS_VTABLE_UNPRIVILEGED),
498 SD_BUS_METHOD_WITH_ARGS("RemoveImage",
499 SD_BUS_ARGS("s", image),
500 SD_BUS_NO_RESULT,
501 method_remove_image,
502 SD_BUS_VTABLE_UNPRIVILEGED),
503 SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly",
504 SD_BUS_ARGS("s", image,
505 "b", read_only),
506 SD_BUS_NO_RESULT,
507 method_mark_image_read_only,
508 SD_BUS_VTABLE_UNPRIVILEGED),
509 SD_BUS_METHOD_WITH_ARGS("SetImageLimit",
510 SD_BUS_ARGS("s", image,
511 "t", limit),
512 SD_BUS_NO_RESULT,
513 method_set_image_limit,
514 SD_BUS_VTABLE_UNPRIVILEGED),
515 SD_BUS_METHOD_WITH_ARGS("SetPoolLimit",
516 SD_BUS_ARGS("t", limit),
517 SD_BUS_NO_RESULT,
518 method_set_pool_limit,
519 SD_BUS_VTABLE_UNPRIVILEGED),
520 SD_BUS_VTABLE_END
521 };
522
523 const BusObjectImplementation manager_object = {
524 "/org/freedesktop/portable1",
525 "org.freedesktop.portable1.Manager",
526 .vtables = BUS_VTABLES(manager_vtable),
527 .children = BUS_IMPLEMENTATIONS(&image_object),
528 };
529
530 static int reply_portable_compose_message(sd_bus_message *reply, const PortableChange *changes, size_t n_changes) {
531 size_t i;
532 int r;
533
534 assert(reply);
535 assert(changes || n_changes == 0);
536
537 r = sd_bus_message_open_container(reply, 'a', "(sss)");
538 if (r < 0)
539 return r;
540
541 for (i = 0; i < n_changes; i++) {
542 if (changes[i].type_or_errno < 0)
543 continue;
544
545 r = sd_bus_message_append(reply, "(sss)",
546 portable_change_type_to_string(changes[i].type_or_errno),
547 changes[i].path,
548 changes[i].source);
549 if (r < 0)
550 return r;
551 }
552
553 r = sd_bus_message_close_container(reply);
554 if (r < 0)
555 return r;
556
557 return 0;
558 }
559
560 int reply_portable_changes(sd_bus_message *m, const PortableChange *changes, size_t n_changes) {
561 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
562 int r;
563
564 assert(m);
565
566 r = sd_bus_message_new_method_return(m, &reply);
567 if (r < 0)
568 return r;
569
570 r = reply_portable_compose_message(reply, changes, n_changes);
571 if (r < 0)
572 return r;
573
574 return sd_bus_message_send(reply);
575 }
576
577 int reply_portable_changes_pair(
578 sd_bus_message *m,
579 const PortableChange *changes_first,
580 size_t n_changes_first,
581 const PortableChange *changes_second,
582 size_t n_changes_second) {
583
584 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
585 int r;
586
587 assert(m);
588
589 r = sd_bus_message_new_method_return(m, &reply);
590 if (r < 0)
591 return r;
592
593 r = reply_portable_compose_message(reply, changes_first, n_changes_first);
594 if (r < 0)
595 return r;
596
597 r = reply_portable_compose_message(reply, changes_second, n_changes_second);
598 if (r < 0)
599 return r;
600
601 return sd_bus_message_send(reply);
602 }