Debian Codegraph @

src/portable/portabled-image-bus.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "sd-bus.h"
7
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "bus-get-properties.h"
11 #include "bus-object.h"
12 #include "bus-polkit.h"
13 #include "bus-util.h"
14 #include "discover-image.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "hashmap.h"
18 #include "image-policy.h"
19 #include "io-util.h"
20 #include "os-util.h"
21 #include "path-util.h"
22 #include "portable.h"
23 #include "portabled.h"
24 #include "portabled-bus.h"
25 #include "portabled-image.h"
26 #include "portabled-image-bus.h"
27 #include "portabled-operation.h"
28 #include "process-util.h"
29 #include "strv.h"
30
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, image_type, ImageType);
32
33 int bus_image_common_get_os_release(
34 Manager *m,
35 sd_bus_message *message,
36 const char *name_or_path,
37 Image *image,
38 sd_bus_error *error) {
39
40 int r;
41
42 assert(name_or_path || image);
43 assert(message);
44
45 if (!m) {
46 assert(image);
47 m = image->userdata;
48 }
49
50 r = bus_image_acquire(m,
51 message,
52 name_or_path,
53 image,
54 BUS_IMAGE_AUTHENTICATE_BY_PATH,
55 "org.freedesktop.portable1.inspect-images",
56 &image,
57 error);
58 if (r < 0)
59 return r;
60 if (r == 0) /* Will call us back */
61 return 1;
62
63 if (!image->metadata_valid) {
64 r = image_read_metadata(image, /* root= */ NULL, &image_policy_service, m->runtime_scope);
65 if (r < 0)
66 return sd_bus_error_set_errnof(error, r, "Failed to read image metadata: %m");
67 }
68
69 return bus_reply_pair_array(message, image->os_release);
70 }
71
72 static int bus_image_method_get_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
73 return bus_image_common_get_os_release(NULL, message, NULL, userdata, error);
74 }
75
76 static int append_fd(sd_bus_message *m, PortableMetadata *d) {
77 _cleanup_fclose_ FILE *f = NULL;
78 _cleanup_free_ char *buf = NULL;
79 size_t n = 0;
80 int r;
81
82 assert(m);
83
84 if (d) {
85 assert(d->fd >= 0);
86
87 r = fdopen_independent(d->fd, "r", &f);
88 if (r < 0)
89 return r;
90
91 r = read_full_stream(f, &buf, &n);
92 if (r < 0)
93 return r;
94 }
95
96 return sd_bus_message_append_array(m, 'y', buf, n);
97 }
98
99 int bus_image_common_get_metadata(
100 Manager *m,
101 sd_bus_message *message,
102 const char *name_or_path,
103 Image *image,
104 sd_bus_error *error) {
105
106 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_releases = NULL;
107 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
108 _cleanup_strv_free_ char **matches = NULL, **extension_images = NULL;
109 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
110 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
111 _cleanup_free_ PortableMetadata **sorted = NULL;
112 PortableFlags flags = 0;
113 int r;
114
115 assert(name_or_path || image);
116 assert(message);
117
118 if (!m)
119 m = ASSERT_PTR(ASSERT_PTR(image)->userdata);
120
121 bool have_exti = sd_bus_message_is_method_call(message, NULL, "GetImageMetadataWithExtensions") ||
122 sd_bus_message_is_method_call(message, NULL, "GetMetadataWithExtensions");
123
124 if (have_exti) {
125 r = sd_bus_message_read_strv(message, &extension_images);
126 if (r < 0)
127 return r;
128 }
129
130 r = sd_bus_message_read_strv(message, &matches);
131 if (r < 0)
132 return r;
133
134 if (have_exti) {
135 uint64_t input_flags = 0;
136
137 r = sd_bus_message_read(message, "t", &input_flags);
138 if (r < 0)
139 return r;
140
141 if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0)
142 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
143 "Invalid 'flags' parameter '%" PRIu64 "'",
144 input_flags);
145 flags |= input_flags;
146 }
147
148 r = bus_image_acquire(m,
149 message,
150 name_or_path,
151 image,
152 BUS_IMAGE_AUTHENTICATE_BY_PATH,
153 "org.freedesktop.portable1.inspect-images",
154 &image,
155 error);
156 if (r < 0)
157 return r;
158 if (r == 0) /* Will call us back */
159 return 1;
160
161 r = portable_extract(
162 m->runtime_scope,
163 image->path,
164 matches,
165 extension_images,
166 /* image_policy= */ NULL,
167 flags,
168 &os_release,
169 &extension_releases,
170 &unit_files,
171 NULL,
172 error);
173 if (r < 0)
174 return r;
175
176 r = portable_metadata_hashmap_to_sorted_array(unit_files, &sorted);
177 if (r < 0)
178 return r;
179
180 r = sd_bus_message_new_method_return(message, &reply);
181 if (r < 0)
182 return r;
183
184 r = sd_bus_message_append(reply, "s", image->path);
185 if (r < 0)
186 return r;
187
188 r = append_fd(reply, os_release);
189 if (r < 0)
190 return r;
191
192 /* If it was requested, also send back the extension path and the content
193 * of each extension-release file. Behind a flag, as it's an incompatible
194 * change. */
195 if (have_exti) {
196 PortableMetadata *extension_release;
197
198 r = sd_bus_message_open_container(reply, 'a', "{say}");
199 if (r < 0)
200 return r;
201
202 ORDERED_HASHMAP_FOREACH(extension_release, extension_releases) {
203
204 r = sd_bus_message_open_container(reply, 'e', "say");
205 if (r < 0)
206 return r;
207
208 r = sd_bus_message_append(reply, "s", extension_release->image_path);
209 if (r < 0)
210 return r;
211
212 r = append_fd(reply, extension_release);
213 if (r < 0)
214 return r;
215
216 r = sd_bus_message_close_container(reply);
217 if (r < 0)
218 return r;
219 }
220
221 r = sd_bus_message_close_container(reply);
222 if (r < 0)
223 return r;
224 }
225
226 r = sd_bus_message_open_container(reply, 'a', "{say}");
227 if (r < 0)
228 return r;
229
230 for (size_t i = 0; i < hashmap_size(unit_files); i++) {
231
232 r = sd_bus_message_open_container(reply, 'e', "say");
233 if (r < 0)
234 return r;
235
236 r = sd_bus_message_append(reply, "s", sorted[i]->name);
237 if (r < 0)
238 return r;
239
240 r = append_fd(reply, sorted[i]);
241 if (r < 0)
242 return r;
243
244 r = sd_bus_message_close_container(reply);
245 if (r < 0)
246 return r;
247 }
248
249 r = sd_bus_message_close_container(reply);
250 if (r < 0)
251 return r;
252
253 return sd_bus_message_send(reply);
254 }
255
256 static int bus_image_method_get_metadata(sd_bus_message *message, void *userdata, sd_bus_error *error) {
257 return bus_image_common_get_metadata(NULL, message, NULL, userdata, error);
258 }
259
260 static int bus_image_method_get_state(
261 sd_bus_message *message,
262 void *userdata,
263 sd_bus_error *error) {
264
265 _cleanup_strv_free_ char **extension_images = NULL;
266 Image *image = ASSERT_PTR(userdata);
267 Manager *m = ASSERT_PTR(image->userdata);
268 PortableState state;
269 int r;
270
271 assert(message);
272
273 if (sd_bus_message_is_method_call(message, NULL, "GetStateWithExtensions")) {
274 uint64_t input_flags = 0;
275
276 r = sd_bus_message_read_strv(message, &extension_images);
277 if (r < 0)
278 return r;
279
280 r = sd_bus_message_read(message, "t", &input_flags);
281 if (r < 0)
282 return r;
283
284 /* No flags are supported by this method for now. */
285 if (input_flags != 0)
286 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
287 "Invalid 'flags' parameter '%" PRIu64 "'",
288 input_flags);
289 }
290
291 r = portable_get_state(
292 m->runtime_scope,
293 sd_bus_message_get_bus(message),
294 image->path,
295 extension_images,
296 0,
297 &state,
298 error);
299 if (r < 0)
300 return r;
301
302 return sd_bus_reply_method_return(message, "s", portable_state_to_string(state));
303 }
304
305 int bus_image_common_attach(
306 Manager *m,
307 sd_bus_message *message,
308 const char *name_or_path,
309 Image *image,
310 sd_bus_error *error) {
311
312 _cleanup_strv_free_ char **matches = NULL, **extension_images = NULL;
313 PortableChange *changes = NULL;
314 PortableFlags flags = 0;
315 const char *profile, *copy_mode;
316 size_t n_changes = 0;
317 int r;
318
319 assert(message);
320 assert(name_or_path || image);
321
322 CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
323
324 if (!m) {
325 assert(image);
326 m = image->userdata;
327 }
328
329 if (sd_bus_message_is_method_call(message, NULL, "AttachImageWithExtensions") ||
330 sd_bus_message_is_method_call(message, NULL, "AttachWithExtensions")) {
331 r = sd_bus_message_read_strv(message, &extension_images);
332 if (r < 0)
333 return r;
334 }
335
336 r = sd_bus_message_read_strv(message, &matches);
337 if (r < 0)
338 return r;
339
340 r = sd_bus_message_read(message, "s", &profile);
341 if (r < 0)
342 return r;
343
344 if (sd_bus_message_is_method_call(message, NULL, "AttachImageWithExtensions") ||
345 sd_bus_message_is_method_call(message, NULL, "AttachWithExtensions")) {
346 uint64_t input_flags = 0;
347
348 r = sd_bus_message_read(message, "st", &copy_mode, &input_flags);
349 if (r < 0)
350 return r;
351 if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0)
352 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
353 "Invalid 'flags' parameter '%" PRIu64 "'",
354 input_flags);
355 flags |= input_flags;
356 } else {
357 int runtime;
358
359 r = sd_bus_message_read(message, "bs", &runtime, &copy_mode);
360 if (r < 0)
361 return r;
362
363 if (runtime)
364 flags |= PORTABLE_RUNTIME;
365 }
366
367 if (streq(copy_mode, "symlink"))
368 flags |= PORTABLE_PREFER_SYMLINK;
369 else if (streq(copy_mode, "copy"))
370 flags |= PORTABLE_PREFER_COPY;
371 else if (streq(copy_mode, "mixed"))
372 flags |= PORTABLE_MIXED_COPY_LINK;
373 else if (!isempty(copy_mode))
374 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, "Unknown copy mode '%s'", copy_mode);
375
376 r = bus_image_acquire(m,
377 message,
378 name_or_path,
379 image,
380 BUS_IMAGE_AUTHENTICATE_ALL,
381 "org.freedesktop.portable1.attach-images",
382 &image,
383 error);
384 if (r < 0)
385 return r;
386 if (r == 0) /* Will call us back */
387 return 1;
388
389 r = portable_attach(
390 m->runtime_scope,
391 sd_bus_message_get_bus(message),
392 image->path,
393 matches,
394 profile,
395 extension_images,
396 /* image_policy= */ NULL,
397 flags,
398 &changes,
399 &n_changes,
400 error);
401 if (r < 0)
402 return r;
403
404 return reply_portable_changes(message, changes, n_changes);
405 }
406
407 static int bus_image_method_attach(sd_bus_message *message, void *userdata, sd_bus_error *error) {
408 return bus_image_common_attach(NULL, message, NULL, userdata, error);
409 }
410
411 static int bus_image_method_detach(
412 sd_bus_message *message,
413 void *userdata,
414 sd_bus_error *error) {
415
416 _cleanup_strv_free_ char **extension_images = NULL;
417 PortableChange *changes = NULL;
418 Image *image = ASSERT_PTR(userdata);
419 Manager *m = ASSERT_PTR(image->userdata);
420 PortableFlags flags = 0;
421 size_t n_changes = 0;
422 int r;
423
424 assert(message);
425
426 CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
427
428 if (sd_bus_message_is_method_call(message, NULL, "DetachWithExtensions")) {
429 r = sd_bus_message_read_strv(message, &extension_images);
430 if (r < 0)
431 return r;
432 }
433
434 if (sd_bus_message_is_method_call(message, NULL, "DetachWithExtensions")) {
435 uint64_t input_flags = 0;
436
437 r = sd_bus_message_read(message, "t", &input_flags);
438 if (r < 0)
439 return r;
440
441 if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0)
442 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
443 "Invalid 'flags' parameter '%" PRIu64 "'",
444 input_flags);
445 flags |= input_flags;
446 } else {
447 int runtime;
448
449 r = sd_bus_message_read(message, "b", &runtime);
450 if (r < 0)
451 return r;
452
453 if (runtime)
454 flags |= PORTABLE_RUNTIME;
455 }
456
457 if (m->runtime_scope != RUNTIME_SCOPE_USER) {
458 r = bus_verify_polkit_async(
459 message,
460 "org.freedesktop.portable1.attach-images",
461 /* details= */ NULL,
462 &m->polkit_registry,
463 error);
464 if (r < 0)
465 return r;
466 if (r == 0)
467 return 1; /* Will call us back */
468 }
469
470 r = portable_detach(
471 m->runtime_scope,
472 sd_bus_message_get_bus(message),
473 image->path,
474 extension_images,
475 flags,
476 &changes,
477 &n_changes,
478 error);
479 if (r < 0)
480 return r;
481
482 return reply_portable_changes(message, changes, n_changes);
483 }
484
485 int bus_image_common_remove(
486 Manager *m,
487 sd_bus_message *message,
488 const char *name_or_path,
489 Image *image,
490 sd_bus_error *error) {
491
492 _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
493 _cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL;
494 PortableState state;
495 int r;
496
497 assert(message);
498 assert(name_or_path || image);
499
500 if (!m) {
501 assert(image);
502 m = image->userdata;
503 }
504
505 if (m->n_operations >= OPERATIONS_MAX)
506 return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations.");
507
508 r = bus_image_acquire(m,
509 message,
510 name_or_path,
511 image,
512 BUS_IMAGE_AUTHENTICATE_ALL,
513 "org.freedesktop.portable1.manage-images",
514 &image,
515 error);
516 if (r < 0)
517 return r;
518 if (r == 0)
519 return 1; /* Will call us back */
520
521 r = portable_get_state(
522 m->runtime_scope,
523 sd_bus_message_get_bus(message),
524 image->path,
525 NULL,
526 0,
527 &state,
528 error);
529 if (r < 0)
530 return r;
531
532 if (state != PORTABLE_DETACHED)
533 return sd_bus_error_set_errnof(error, EBUSY, "Image '%s' is not detached, refusing.", image->path);
534
535 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
536 return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
537
538 r = pidref_safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child);
539 if (r < 0)
540 return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
541 if (r == 0) {
542 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
543
544 r = image_remove(image, m->runtime_scope);
545 if (r < 0) {
546 (void) write(errno_pipe_fd[1], &r, sizeof(r));
547 _exit(EXIT_FAILURE);
548 }
549
550 _exit(EXIT_SUCCESS);
551 }
552
553 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
554
555 r = operation_new(m, &child, message, errno_pipe_fd[0], NULL);
556 if (r < 0)
557 return r;
558
559 /* We don't need to disarm child cleanup here because operation_new() takes over ownership internally. */
560 errno_pipe_fd[0] = -EBADF;
561
562 return 1;
563 }
564
565 static int bus_image_method_remove(sd_bus_message *message, void *userdata, sd_bus_error *error) {
566 return bus_image_common_remove(NULL, message, NULL, userdata, error);
567 }
568
569 /* Given two PortableChange arrays, return a new array that has all elements of the first that are
570 * not also present in the second, comparing the basename of the path values. */
571 static int normalize_portable_changes(
572 const PortableChange *changes_attached,
573 size_t n_changes_attached,
574 const PortableChange *changes_detached,
575 size_t n_changes_detached,
576 PortableChange **ret_changes,
577 size_t *ret_n_changes) {
578
579 PortableChange *changes = NULL;
580 size_t n_changes = 0;
581
582 assert(ret_n_changes);
583 assert(ret_changes);
584
585 if (n_changes_detached == 0)
586 return 0; /* Nothing to do */
587
588 changes = new0(PortableChange, n_changes_attached + n_changes_detached);
589 if (!changes)
590 return -ENOMEM;
591
592 CLEANUP_ARRAY(changes, n_changes, portable_changes_free);
593
594 for (size_t i = 0; i < n_changes_detached; ++i) {
595 bool found = false;
596
597 for (size_t j = 0; j < n_changes_attached; ++j)
598 if (path_equal_filename(changes_detached[i].path, changes_attached[j].path)) {
599 found = true;
600 break;
601 }
602
603 if (!found) {
604 _cleanup_free_ char *path = NULL, *source = NULL;
605
606 path = strdup(changes_detached[i].path);
607 if (!path)
608 return -ENOMEM;
609
610 if (changes_detached[i].source) {
611 source = strdup(changes_detached[i].source);
612 if (!source)
613 return -ENOMEM;
614 }
615
616 changes[n_changes++] = (PortableChange) {
617 .type_or_errno = changes_detached[i].type_or_errno,
618 .path = TAKE_PTR(path),
619 .source = TAKE_PTR(source),
620 };
621 }
622 }
623
624 *ret_n_changes = n_changes;
625 *ret_changes = TAKE_PTR(changes);
626
627 return 0;
628 }
629
630 int bus_image_common_reattach(
631 Manager *m,
632 sd_bus_message *message,
633 const char *name_or_path,
634 Image *image,
635 sd_bus_error *error) {
636
637 PortableChange *changes_detached = NULL, *changes_attached = NULL, *changes_gone = NULL;
638 size_t n_changes_detached = 0, n_changes_attached = 0, n_changes_gone = 0;
639 _cleanup_strv_free_ char **matches = NULL, **extension_images = NULL;
640 PortableFlags flags = PORTABLE_REATTACH;
641 const char *profile, *copy_mode;
642 int r;
643
644 assert(message);
645 assert(name_or_path || image);
646
647 CLEANUP_ARRAY(changes_detached, n_changes_detached, portable_changes_free);
648 CLEANUP_ARRAY(changes_attached, n_changes_attached, portable_changes_free);
649 CLEANUP_ARRAY(changes_gone, n_changes_gone, portable_changes_free);
650
651 if (!m) {
652 assert(image);
653 m = image->userdata;
654 }
655
656 if (sd_bus_message_is_method_call(message, NULL, "ReattachImageWithExtensions") ||
657 sd_bus_message_is_method_call(message, NULL, "ReattachWithExtensions")) {
658 r = sd_bus_message_read_strv(message, &extension_images);
659 if (r < 0)
660 return r;
661 }
662
663 r = sd_bus_message_read_strv(message, &matches);
664 if (r < 0)
665 return r;
666
667 r = sd_bus_message_read(message, "s", &profile);
668 if (r < 0)
669 return r;
670
671 if (sd_bus_message_is_method_call(message, NULL, "ReattachImageWithExtensions") ||
672 sd_bus_message_is_method_call(message, NULL, "ReattachWithExtensions")) {
673 uint64_t input_flags = 0;
674
675 r = sd_bus_message_read(message, "st", &copy_mode, &input_flags);
676 if (r < 0)
677 return r;
678
679 if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0)
680 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
681 "Invalid 'flags' parameter '%" PRIu64 "'",
682 input_flags);
683 flags |= input_flags;
684 } else {
685 int runtime;
686
687 r = sd_bus_message_read(message, "bs", &runtime, &copy_mode);
688 if (r < 0)
689 return r;
690
691 if (runtime)
692 flags |= PORTABLE_RUNTIME;
693 }
694
695 if (streq(copy_mode, "symlink"))
696 flags |= PORTABLE_PREFER_SYMLINK;
697 else if (streq(copy_mode, "copy"))
698 flags |= PORTABLE_PREFER_COPY;
699 else if (streq(copy_mode, "mixed"))
700 flags |= PORTABLE_MIXED_COPY_LINK;
701 else if (!isempty(copy_mode))
702 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, "Unknown copy mode '%s'", copy_mode);
703
704 r = bus_image_acquire(m,
705 message,
706 name_or_path,
707 image,
708 BUS_IMAGE_AUTHENTICATE_ALL,
709 "org.freedesktop.portable1.attach-images",
710 &image,
711 error);
712 if (r < 0)
713 return r;
714 if (r == 0) /* Will call us back */
715 return 1;
716
717 r = portable_detach(
718 m->runtime_scope,
719 sd_bus_message_get_bus(message),
720 image->path,
721 extension_images,
722 flags,
723 &changes_detached,
724 &n_changes_detached,
725 error);
726 if (r < 0)
727 return r;
728
729 r = portable_attach(
730 m->runtime_scope,
731 sd_bus_message_get_bus(message),
732 image->path,
733 matches,
734 profile,
735 extension_images,
736 /* image_policy= */ NULL,
737 flags,
738 &changes_attached,
739 &n_changes_attached,
740 error);
741 if (r < 0)
742 return r;
743
744 /* We want to return the list of units really removed by the detach,
745 * and not added again by the attach */
746 r = normalize_portable_changes(changes_attached, n_changes_attached,
747 changes_detached, n_changes_detached,
748 &changes_gone, &n_changes_gone);
749 if (r < 0)
750 return r;
751
752 /* First, return the units that are gone (so that the caller can stop them)
753 * Then, return the units that are changed/added (so that the caller can
754 * start/restart/enable them) */
755 return reply_portable_changes_pair(message,
756 changes_gone, n_changes_gone,
757 changes_attached, n_changes_attached);
758 }
759
760 static int bus_image_method_reattach(sd_bus_message *message, void *userdata, sd_bus_error *error) {
761 return bus_image_common_reattach(NULL, message, NULL, userdata, error);
762 }
763
764 int bus_image_common_mark_read_only(
765 Manager *m,
766 sd_bus_message *message,
767 const char *name_or_path,
768 Image *image,
769 sd_bus_error *error) {
770
771 int r, read_only;
772
773 assert(message);
774 assert(name_or_path || image);
775
776 if (!m) {
777 assert(image);
778 m = image->userdata;
779 }
780
781 r = sd_bus_message_read(message, "b", &read_only);
782 if (r < 0)
783 return r;
784
785 r = bus_image_acquire(m,
786 message,
787 name_or_path,
788 image,
789 BUS_IMAGE_AUTHENTICATE_ALL,
790 "org.freedesktop.portable1.manage-images",
791 &image,
792 error);
793 if (r < 0)
794 return r;
795 if (r == 0)
796 return 1; /* Will call us back */
797
798 r = image_read_only(image, read_only, m->runtime_scope);
799 if (r < 0)
800 return r;
801
802 return sd_bus_reply_method_return(message, NULL);
803 }
804
805 static int bus_image_method_mark_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) {
806 return bus_image_common_mark_read_only(NULL, message, NULL, userdata, error);
807 }
808
809 int bus_image_common_set_limit(
810 Manager *m,
811 sd_bus_message *message,
812 const char *name_or_path,
813 Image *image,
814 sd_bus_error *error) {
815
816 uint64_t limit;
817 int r;
818
819 assert(message);
820 assert(name_or_path || image);
821
822 if (!m) {
823 assert(image);
824 m = image->userdata;
825 }
826
827 r = sd_bus_message_read(message, "t", &limit);
828 if (r < 0)
829 return r;
830 if (!FILE_SIZE_VALID_OR_INFINITY(limit))
831 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
832
833 r = bus_image_acquire(m,
834 message,
835 name_or_path,
836 image,
837 BUS_IMAGE_AUTHENTICATE_ALL,
838 "org.freedesktop.portable1.manage-images",
839 &image,
840 error);
841 if (r < 0)
842 return r;
843 if (r == 0)
844 return 1; /* Will call us back */
845
846 r = image_set_limit(image, limit);
847 if (r < 0)
848 return r;
849
850 return sd_bus_reply_method_return(message, NULL);
851 }
852
853 static int bus_image_method_set_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
854 return bus_image_common_set_limit(NULL, message, NULL, userdata, error);
855 }
856
857 const sd_bus_vtable image_vtable[] = {
858 SD_BUS_VTABLE_START(0),
859 SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Image, name), 0),
860 SD_BUS_PROPERTY("Path", "s", NULL, offsetof(Image, path), 0),
861 SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Image, type), 0),
862 SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_image_is_read_only, 0, 0),
863 SD_BUS_PROPERTY("CreationTimestamp", "t", NULL, offsetof(Image, crtime), 0),
864 SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL, offsetof(Image, mtime), 0),
865 SD_BUS_PROPERTY("Usage", "t", NULL, offsetof(Image, usage), 0),
866 SD_BUS_PROPERTY("Limit", "t", NULL, offsetof(Image, limit), 0),
867 SD_BUS_PROPERTY("UsageExclusive", "t", NULL, offsetof(Image, usage_exclusive), 0),
868 SD_BUS_PROPERTY("LimitExclusive", "t", NULL, offsetof(Image, limit_exclusive), 0),
869 SD_BUS_METHOD_WITH_ARGS("GetOSRelease",
870 SD_BUS_NO_ARGS,
871 SD_BUS_RESULT("a{ss}", os_release),
872 bus_image_method_get_os_release,
873 SD_BUS_VTABLE_UNPRIVILEGED),
874 SD_BUS_METHOD_WITH_ARGS("GetMetadata",
875 SD_BUS_ARGS("as", matches),
876 SD_BUS_RESULT("s", image,
877 "ay", os_release,
878 "a{say}", units),
879 bus_image_method_get_metadata,
880 SD_BUS_VTABLE_UNPRIVILEGED),
881 SD_BUS_METHOD_WITH_ARGS("GetMetadataWithExtensions",
882 SD_BUS_ARGS("as", extensions,
883 "as", matches,
884 "t", flags),
885 SD_BUS_RESULT("s", image,
886 "ay", os_release,
887 "a{say}", extensions,
888 "a{say}", units),
889 bus_image_method_get_metadata,
890 SD_BUS_VTABLE_UNPRIVILEGED),
891 SD_BUS_METHOD_WITH_ARGS("GetState",
892 SD_BUS_NO_ARGS,
893 SD_BUS_RESULT("s", state),
894 bus_image_method_get_state,
895 SD_BUS_VTABLE_UNPRIVILEGED),
896 SD_BUS_METHOD_WITH_ARGS("GetStateWithExtensions",
897 SD_BUS_ARGS("as", extensions,
898 "t", flags),
899 SD_BUS_RESULT("s", state),
900 bus_image_method_get_state,
901 SD_BUS_VTABLE_UNPRIVILEGED),
902 SD_BUS_METHOD_WITH_ARGS("Attach",
903 SD_BUS_ARGS("as", matches,
904 "s", profile,
905 "b", runtime,
906 "s", copy_mode),
907 SD_BUS_RESULT("a(sss)", changes),
908 bus_image_method_attach,
909 SD_BUS_VTABLE_UNPRIVILEGED),
910 SD_BUS_METHOD_WITH_ARGS("AttachWithExtensions",
911 SD_BUS_ARGS("as", extensions,
912 "as", matches,
913 "s", profile,
914 "s", copy_mode,
915 "t", flags),
916 SD_BUS_RESULT("a(sss)", changes),
917 bus_image_method_attach,
918 SD_BUS_VTABLE_UNPRIVILEGED),
919 SD_BUS_METHOD_WITH_ARGS("Detach",
920 SD_BUS_ARGS("b", runtime),
921 SD_BUS_RESULT("a(sss)", changes),
922 bus_image_method_detach,
923 SD_BUS_VTABLE_UNPRIVILEGED),
924 SD_BUS_METHOD_WITH_ARGS("DetachWithExtensions",
925 SD_BUS_ARGS("as", extensions,
926 "t", flags),
927 SD_BUS_RESULT("a(sss)", changes),
928 bus_image_method_detach,
929 SD_BUS_VTABLE_UNPRIVILEGED),
930 SD_BUS_METHOD_WITH_ARGS("Reattach",
931 SD_BUS_ARGS("as", matches,
932 "s", profile,
933 "b", runtime,
934 "s", copy_mode),
935 SD_BUS_RESULT("a(sss)", changes_removed,
936 "a(sss)", changes_updated),
937 bus_image_method_reattach,
938 SD_BUS_VTABLE_UNPRIVILEGED),
939 SD_BUS_METHOD_WITH_ARGS("ReattachWithExtensions",
940 SD_BUS_ARGS("as", extensions,
941 "as", matches,
942 "s", profile,
943 "s", copy_mode,
944 "t", flags),
945 SD_BUS_RESULT("a(sss)", changes_removed,
946 "a(sss)", changes_updated),
947 bus_image_method_reattach,
948 SD_BUS_VTABLE_UNPRIVILEGED),
949 SD_BUS_METHOD_WITH_ARGS("Remove",
950 SD_BUS_NO_ARGS,
951 SD_BUS_NO_RESULT,
952 bus_image_method_remove,
953 SD_BUS_VTABLE_UNPRIVILEGED),
954 SD_BUS_METHOD_WITH_ARGS("MarkReadOnly",
955 SD_BUS_ARGS("b", read_only),
956 SD_BUS_NO_RESULT,
957 bus_image_method_mark_read_only,
958 SD_BUS_VTABLE_UNPRIVILEGED),
959 SD_BUS_METHOD_WITH_ARGS("SetLimit",
960 SD_BUS_ARGS("t", limit),
961 SD_BUS_NO_RESULT,
962 bus_image_method_set_limit,
963 SD_BUS_VTABLE_UNPRIVILEGED),
964 /* Deprecated silly typo */
965 SD_BUS_METHOD_WITH_ARGS("ReattacheWithExtensions",
966 SD_BUS_ARGS("as", extensions,
967 "as", matches,
968 "s", profile,
969 "s", copy_mode,
970 "t", flags),
971 SD_BUS_RESULT("a(sss)", changes_removed,
972 "a(sss)", changes_updated),
973 bus_image_method_reattach,
974 SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_HIDDEN),
975 SD_BUS_VTABLE_END
976 };
977
978 int bus_image_path(Image *image, char **ret) {
979 assert(image);
980 assert(ret);
981
982 if (!image->discoverable)
983 return -EINVAL;
984
985 return sd_bus_path_encode("/org/freedesktop/portable1/image", image->name, ret);
986 }
987
988 int bus_image_acquire(
989 Manager *m,
990 sd_bus_message *message,
991 const char *name_or_path,
992 Image *image,
993 ImageAcquireMode mode,
994 const char *polkit_action,
995 Image **ret,
996 sd_bus_error *error) {
997
998 _cleanup_(image_unrefp) Image *loaded = NULL;
999 Image *cached;
1000 int r;
1001
1002 assert(m);
1003 assert(message);
1004 assert(name_or_path || image);
1005 assert(mode >= 0);
1006 assert(mode < _BUS_IMAGE_ACQUIRE_MODE_MAX);
1007 assert(polkit_action || mode == BUS_IMAGE_REFUSE_BY_PATH);
1008 assert(ret);
1009
1010 /* Acquires an 'Image' object if not acquired yet, and enforces necessary authentication while doing so. */
1011
1012 if (mode == BUS_IMAGE_AUTHENTICATE_ALL && m->runtime_scope != RUNTIME_SCOPE_USER) {
1013 r = bus_verify_polkit_async(
1014 message,
1015 polkit_action,
1016 /* details= */ NULL,
1017 &m->polkit_registry,
1018 error);
1019 if (r < 0)
1020 return r;
1021 if (r == 0) { /* Will call us back */
1022 *ret = NULL;
1023 return 0;
1024 }
1025 }
1026
1027 /* Already passed in? */
1028 if (image) {
1029 *ret = image;
1030 return 1;
1031 }
1032
1033 /* Let's see if this image is already cached? */
1034 cached = manager_image_cache_get(m, name_or_path);
1035 if (cached) {
1036 *ret = cached;
1037 return 1;
1038 }
1039
1040 if (image_name_is_valid(name_or_path)) {
1041
1042 /* If it's a short name, let's search for it */
1043 r = image_find(m->runtime_scope, IMAGE_PORTABLE, name_or_path, NULL, &loaded);
1044 if (r == -ENOENT)
1045 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PORTABLE_IMAGE,
1046 "No image '%s' found.", name_or_path);
1047
1048 /* other errors are handled below… */
1049 } else {
1050 /* Don't accept path if this is always forbidden */
1051 if (mode == BUS_IMAGE_REFUSE_BY_PATH)
1052 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1053 "Expected image name, not path in place of '%s'.", name_or_path);
1054
1055 if (!path_is_absolute(name_or_path))
1056 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1057 "Image name '%s' is not valid or not a valid path.", name_or_path);
1058
1059 if (!path_is_normalized(name_or_path))
1060 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1061 "Image path '%s' is not normalized.", name_or_path);
1062
1063 if (mode == BUS_IMAGE_AUTHENTICATE_BY_PATH && m->runtime_scope != RUNTIME_SCOPE_USER) {
1064 r = bus_verify_polkit_async(
1065 message,
1066 polkit_action,
1067 /* details= */ NULL,
1068 &m->polkit_registry,
1069 error);
1070 if (r < 0)
1071 return r;
1072 if (r == 0) { /* Will call us back */
1073 *ret = NULL;
1074 return 0;
1075 }
1076 }
1077
1078 r = image_from_path(name_or_path, &loaded);
1079 }
1080 if (r == -EMEDIUMTYPE) {
1081 sd_bus_error_setf(error, BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE,
1082 "Type of image '%s' not recognized; supported image types are directories/btrfs subvolumes, block devices, and raw disk image files with suffix '.raw'.",
1083 name_or_path);
1084 return r;
1085 }
1086 if (r < 0)
1087 return r;
1088
1089 /* Add what we just loaded to the cache. This has as side-effect that the object stays in memory until the
1090 * cache is purged again, i.e. at least for the current event loop iteration, which is all we need, and which
1091 * means we don't actually need to ref the return object. */
1092 r = manager_image_cache_add(m, loaded);
1093 if (r < 0)
1094 return r;
1095
1096 *ret = loaded;
1097 return 1;
1098 }
1099
1100 int bus_image_object_find(
1101 sd_bus *bus,
1102 const char *path,
1103 const char *interface,
1104 void *userdata,
1105 void **found,
1106 sd_bus_error *error) {
1107
1108 _cleanup_free_ char *e = NULL;
1109 Manager *m = userdata;
1110 Image *image = NULL;
1111 int r;
1112
1113 assert(bus);
1114 assert(path);
1115 assert(interface);
1116 assert(found);
1117
1118 r = sd_bus_path_decode(path, "/org/freedesktop/portable1/image", &e);
1119 if (r < 0)
1120 return 0;
1121 if (r == 0)
1122 goto not_found;
1123 if (isempty(e))
1124 /* The path is "/org/freedesktop/portable1/image" itself */
1125 goto not_found;
1126
1127 r = bus_image_acquire(m, sd_bus_get_current_message(bus), e, NULL, BUS_IMAGE_REFUSE_BY_PATH, NULL, &image, error);
1128 if (r == -ENOENT)
1129 goto not_found;
1130 if (r < 0)
1131 return r;
1132
1133 *found = image;
1134 return 1;
1135
1136 not_found:
1137 *found = NULL;
1138 return 0;
1139 }
1140
1141 int bus_image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
1142 _cleanup_hashmap_free_ Hashmap *images = NULL;
1143 _cleanup_strv_free_ char **l = NULL;
1144 Manager *m = userdata;
1145 size_t n = 0;
1146 Image *image;
1147 int r;
1148
1149 assert(bus);
1150 assert(path);
1151 assert(nodes);
1152
1153 r = manager_image_cache_discover(m, &images, error);
1154 if (r < 0)
1155 return r;
1156
1157 HASHMAP_FOREACH(image, images) {
1158 char *p;
1159
1160 r = bus_image_path(image, &p);
1161 if (r < 0)
1162 return r;
1163
1164 if (!GREEDY_REALLOC(l, n+2)) {
1165 free(p);
1166 return -ENOMEM;
1167 }
1168
1169 l[n++] = p;
1170 l[n] = NULL;
1171 }
1172
1173 *nodes = TAKE_PTR(l);
1174
1175 return 1;
1176 }
1177
1178 const BusObjectImplementation image_object = {
1179 "/org/freedesktop/portable1/image",
1180 "org.freedesktop.portable1.Image",
1181 .fallback_vtables = BUS_FALLBACK_VTABLES({image_vtable, bus_image_object_find}),
1182 .node_enumerator = bus_image_node_enumerator,
1183 };