]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/portable/portable.c
man/crypttab: rework formatting in "key acquisition section"
[thirdparty/systemd.git] / src / portable / portable.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/loop.h>
4
5 #include "bus-common-errors.h"
6 #include "bus-error.h"
7 #include "chase-symlinks.h"
8 #include "conf-files.h"
9 #include "copy.h"
10 #include "data-fd-util.h"
11 #include "def.h"
12 #include "dirent-util.h"
13 #include "discover-image.h"
14 #include "dissect-image.h"
15 #include "env-file.h"
16 #include "env-util.h"
17 #include "errno-list.h"
18 #include "escape.h"
19 #include "extension-release.h"
20 #include "fd-util.h"
21 #include "fileio.h"
22 #include "fs-util.h"
23 #include "install.h"
24 #include "io-util.h"
25 #include "locale-util.h"
26 #include "loop-util.h"
27 #include "mkdir.h"
28 #include "nulstr-util.h"
29 #include "os-util.h"
30 #include "path-lookup.h"
31 #include "portable.h"
32 #include "process-util.h"
33 #include "selinux-util.h"
34 #include "set.h"
35 #include "signal-util.h"
36 #include "socket-util.h"
37 #include "sort-util.h"
38 #include "string-table.h"
39 #include "strv.h"
40 #include "tmpfile-util.h"
41 #include "user-util.h"
42
43 /* Markers used in the first line of our 20-portable.conf unit file drop-in to determine, that a) the unit file was
44 * dropped there by the portable service logic and b) for which image it was dropped there. */
45 #define PORTABLE_DROPIN_MARKER_BEGIN "# Drop-in created for image '"
46 #define PORTABLE_DROPIN_MARKER_END "', do not edit."
47
48 static bool prefix_match(const char *unit, const char *prefix) {
49 const char *p;
50
51 p = startswith(unit, prefix);
52 if (!p)
53 return false;
54
55 /* Only respect prefixes followed by dash or dot or when there's a complete match */
56 return IN_SET(*p, '-', '.', '@', 0);
57 }
58
59 static bool unit_match(const char *unit, char **matches) {
60 const char *dot;
61
62 dot = strrchr(unit, '.');
63 if (!dot)
64 return false;
65
66 if (!STR_IN_SET(dot, ".service", ".socket", ".target", ".timer", ".path"))
67 return false;
68
69 /* Empty match expression means: everything */
70 if (strv_isempty(matches))
71 return true;
72
73 /* Otherwise, at least one needs to match */
74 STRV_FOREACH(i, matches)
75 if (prefix_match(unit, *i))
76 return true;
77
78 return false;
79 }
80
81 static PortableMetadata *portable_metadata_new(const char *name, const char *path, const char *selinux_label, int fd) {
82 PortableMetadata *m;
83
84 m = malloc0(offsetof(PortableMetadata, name) + strlen(name) + 1);
85 if (!m)
86 return NULL;
87
88 /* In case of a layered attach, we want to remember which image the unit came from */
89 if (path) {
90 m->image_path = strdup(path);
91 if (!m->image_path)
92 return mfree(m);
93 }
94
95 /* The metadata file might have SELinux labels, we need to carry them and reapply them */
96 if (!isempty(selinux_label)) {
97 m->selinux_label = strdup(selinux_label);
98 if (!m->selinux_label) {
99 free(m->image_path);
100 return mfree(m);
101 }
102 }
103
104 strcpy(m->name, name);
105 m->fd = fd;
106
107 return TAKE_PTR(m);
108 }
109
110 PortableMetadata *portable_metadata_unref(PortableMetadata *i) {
111 if (!i)
112 return NULL;
113
114 safe_close(i->fd);
115 free(i->source);
116 free(i->image_path);
117 free(i->selinux_label);
118
119 return mfree(i);
120 }
121
122 static int compare_metadata(PortableMetadata *const *x, PortableMetadata *const *y) {
123 return strcmp((*x)->name, (*y)->name);
124 }
125
126 int portable_metadata_hashmap_to_sorted_array(Hashmap *unit_files, PortableMetadata ***ret) {
127
128 _cleanup_free_ PortableMetadata **sorted = NULL;
129 PortableMetadata *item;
130 size_t k = 0;
131
132 sorted = new(PortableMetadata*, hashmap_size(unit_files));
133 if (!sorted)
134 return -ENOMEM;
135
136 HASHMAP_FOREACH(item, unit_files)
137 sorted[k++] = item;
138
139 assert(k == hashmap_size(unit_files));
140
141 typesafe_qsort(sorted, k, compare_metadata);
142
143 *ret = TAKE_PTR(sorted);
144 return 0;
145 }
146
147 static int send_one_fd_iov_with_data_fd(
148 int socket_fd,
149 const struct iovec *iov,
150 size_t iovlen,
151 int fd) {
152
153 _cleanup_close_ int data_fd = -1;
154
155 assert(iov || iovlen == 0);
156 assert(socket_fd >= 0);
157 assert(fd >= 0);
158
159 data_fd = copy_data_fd(fd);
160 if (data_fd < 0)
161 return data_fd;
162
163 return send_one_fd_iov(socket_fd, data_fd, iov, iovlen, 0);
164 }
165
166 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(portable_metadata_hash_ops, char, string_hash_func, string_compare_func,
167 PortableMetadata, portable_metadata_unref);
168
169 static int extract_now(
170 const char *where,
171 char **matches,
172 const char *image_name,
173 bool path_is_extension,
174 int socket_fd,
175 PortableMetadata **ret_os_release,
176 Hashmap **ret_unit_files) {
177
178 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
179 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
180 _cleanup_(lookup_paths_free) LookupPaths paths = {};
181 _cleanup_close_ int os_release_fd = -1;
182 _cleanup_free_ char *os_release_path = NULL;
183 const char *os_release_id;
184 int r;
185
186 /* Extracts the metadata from a directory tree 'where'. Extracts two kinds of information: the /etc/os-release
187 * data, and all unit files matching the specified expression. Note that this function is called in two very
188 * different but also similar contexts. When the tool gets invoked on a directory tree, we'll process it
189 * directly, and in-process, and thus can return the requested data directly, via 'ret_os_release' and
190 * 'ret_unit_files'. However, if the tool is invoked on a raw disk image — which needs to be mounted first — we
191 * are invoked in a child process with private mounts and then need to send the collected data to our
192 * parent. To handle both cases in one call this function also gets a 'socket_fd' parameter, which when >= 0 is
193 * used to send the data to the parent. */
194
195 assert(where);
196
197 /* First, find os-release/extension-release and send it upstream (or just save it). */
198 if (path_is_extension) {
199 os_release_id = strjoina("/usr/lib/extension-release.d/extension-release.", image_name);
200 r = open_extension_release(where, image_name, &os_release_path, &os_release_fd);
201 } else {
202 os_release_id = "/etc/os-release";
203 r = open_os_release(where, &os_release_path, &os_release_fd);
204 }
205 if (r < 0)
206 log_debug_errno(r,
207 "Couldn't acquire %s file, ignoring: %m",
208 path_is_extension ? "extension-release " : "os-release");
209 else {
210 if (socket_fd >= 0) {
211 struct iovec iov[] = {
212 IOVEC_MAKE_STRING(os_release_id),
213 IOVEC_MAKE((char *)"\0", sizeof(char)),
214 };
215
216 r = send_one_fd_iov_with_data_fd(socket_fd, iov, ELEMENTSOF(iov), os_release_fd);
217 if (r < 0)
218 return log_debug_errno(r, "Failed to send os-release file: %m");
219 }
220
221 if (ret_os_release) {
222 os_release = portable_metadata_new(os_release_id, NULL, NULL, os_release_fd);
223 if (!os_release)
224 return -ENOMEM;
225
226 os_release_fd = -1;
227 os_release->source = TAKE_PTR(os_release_path);
228 }
229 }
230
231 /* Then, send unit file data to the parent (or/and add it to the hashmap). For that we use our usual unit
232 * discovery logic. Note that we force looking inside of /lib/systemd/system/ for units too, as we mightbe
233 * compiled for a split-usr system but the image might be a legacy-usr one. */
234 r = lookup_paths_init(&paths, LOOKUP_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, where);
235 if (r < 0)
236 return log_debug_errno(r, "Failed to acquire lookup paths: %m");
237
238 unit_files = hashmap_new(&portable_metadata_hash_ops);
239 if (!unit_files)
240 return -ENOMEM;
241
242 STRV_FOREACH(i, paths.search_path) {
243 _cleanup_free_ char *resolved = NULL;
244 _cleanup_closedir_ DIR *d = NULL;
245
246 r = chase_symlinks_and_opendir(*i, where, 0, &resolved, &d);
247 if (r < 0) {
248 log_debug_errno(r, "Failed to open unit path '%s', ignoring: %m", *i);
249 continue;
250 }
251
252 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to read directory: %m")) {
253 _cleanup_(portable_metadata_unrefp) PortableMetadata *m = NULL;
254 _cleanup_(mac_selinux_freep) char *con = NULL;
255 _cleanup_close_ int fd = -1;
256
257 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
258 continue;
259
260 if (!unit_match(de->d_name, matches))
261 continue;
262
263 /* Filter out duplicates */
264 if (hashmap_get(unit_files, de->d_name))
265 continue;
266
267 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
268 continue;
269
270 fd = openat(dirfd(d), de->d_name, O_CLOEXEC|O_RDONLY);
271 if (fd < 0) {
272 log_debug_errno(errno, "Failed to open unit file '%s', ignoring: %m", de->d_name);
273 continue;
274 }
275
276 #if HAVE_SELINUX
277 /* The units will be copied on the host's filesystem, so if they had a SELinux label
278 * we have to preserve it. Copy it out so that it can be applied later. */
279
280 r = fgetfilecon_raw(fd, &con);
281 if (r < 0 && errno != ENODATA)
282 log_debug_errno(errno, "Failed to get SELinux file context from '%s', ignoring: %m", de->d_name);
283 #endif
284
285 if (socket_fd >= 0) {
286 struct iovec iov[] = {
287 IOVEC_MAKE_STRING(de->d_name),
288 IOVEC_MAKE((char *)"\0", sizeof(char)),
289 IOVEC_MAKE_STRING(strempty(con)),
290 };
291
292 r = send_one_fd_iov_with_data_fd(socket_fd, iov, ELEMENTSOF(iov), fd);
293 if (r < 0)
294 return log_debug_errno(r, "Failed to send unit metadata to parent: %m");
295 }
296
297 m = portable_metadata_new(de->d_name, where, con, fd);
298 if (!m)
299 return -ENOMEM;
300 fd = -1;
301
302 m->source = path_join(resolved, de->d_name);
303 if (!m->source)
304 return -ENOMEM;
305
306 r = hashmap_put(unit_files, m->name, m);
307 if (r < 0)
308 return log_debug_errno(r, "Failed to add unit to hashmap: %m");
309 m = NULL;
310 }
311 }
312
313 if (ret_os_release)
314 *ret_os_release = TAKE_PTR(os_release);
315 if (ret_unit_files)
316 *ret_unit_files = TAKE_PTR(unit_files);
317
318 return 0;
319 }
320
321 static int portable_extract_by_path(
322 const char *path,
323 bool path_is_extension,
324 char **matches,
325 PortableMetadata **ret_os_release,
326 Hashmap **ret_unit_files,
327 sd_bus_error *error) {
328
329 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
330 _cleanup_(portable_metadata_unrefp) PortableMetadata* os_release = NULL;
331 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
332 int r;
333
334 assert(path);
335
336 r = loop_device_make_by_path(path, O_RDONLY, LO_FLAGS_PARTSCAN, &d);
337 if (r == -EISDIR) {
338 _cleanup_free_ char *image_name = NULL;
339
340 /* We can't turn this into a loop-back block device, and this returns EISDIR? Then this is a directory
341 * tree and not a raw device. It's easy then. */
342
343 r = path_extract_filename(path, &image_name);
344 if (r < 0)
345 return log_error_errno(r, "Failed to extract image name from path '%s': %m", path);
346
347 r = extract_now(path, matches, image_name, path_is_extension, -1, &os_release, &unit_files);
348 if (r < 0)
349 return r;
350
351 } else if (r < 0)
352 return log_debug_errno(r, "Failed to set up loopback device for %s: %m", path);
353 else {
354 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
355 _cleanup_(rmdir_and_freep) char *tmpdir = NULL;
356 _cleanup_(close_pairp) int seq[2] = { -1, -1 };
357 _cleanup_(sigkill_waitp) pid_t child = 0;
358
359 /* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it
360 * there, and extract the metadata we need. The metadata is sent from the child back to us. */
361
362 r = loop_device_flock(d, LOCK_SH);
363 if (r < 0)
364 return log_debug_errno(r, "Failed to acquire lock on loopback block device: %m");
365
366 BLOCK_SIGNALS(SIGCHLD);
367
368 r = mkdtemp_malloc("/tmp/inspect-XXXXXX", &tmpdir);
369 if (r < 0)
370 return log_debug_errno(r, "Failed to create temporary directory: %m");
371
372 r = dissect_image(
373 d->fd,
374 NULL, NULL,
375 d->diskseq,
376 d->uevent_seqnum_not_before,
377 d->timestamp_not_before,
378 DISSECT_IMAGE_READ_ONLY |
379 DISSECT_IMAGE_GENERIC_ROOT |
380 DISSECT_IMAGE_REQUIRE_ROOT |
381 DISSECT_IMAGE_DISCARD_ON_LOOP |
382 DISSECT_IMAGE_RELAX_VAR_CHECK |
383 DISSECT_IMAGE_USR_NO_ROOT,
384 &m);
385 if (r == -ENOPKG)
386 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't identify a suitable partition table or file system in '%s'.", path);
387 else if (r == -EADDRNOTAVAIL)
388 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No root partition for specified root hash found in '%s'.", path);
389 else if (r == -ENOTUNIQ)
390 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Multiple suitable root partitions found in image '%s'.", path);
391 else if (r == -ENXIO)
392 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No suitable root partition found in image '%s'.", path);
393 else if (r == -EPROTONOSUPPORT)
394 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", path);
395 if (r < 0)
396 return r;
397
398 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, seq) < 0)
399 return log_debug_errno(errno, "Failed to allocated SOCK_SEQPACKET socket: %m");
400
401 r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_LOG, &child);
402 if (r < 0)
403 return r;
404 if (r == 0) {
405 DissectImageFlags flags = DISSECT_IMAGE_READ_ONLY;
406
407 seq[0] = safe_close(seq[0]);
408
409 if (path_is_extension)
410 flags |= DISSECT_IMAGE_VALIDATE_OS_EXT;
411 else
412 flags |= DISSECT_IMAGE_VALIDATE_OS;
413
414 r = dissected_image_mount(m, tmpdir, UID_INVALID, UID_INVALID, flags);
415 if (r < 0) {
416 log_debug_errno(r, "Failed to mount dissected image: %m");
417 goto child_finish;
418 }
419
420 r = extract_now(tmpdir, matches, m->image_name, path_is_extension, seq[1], NULL, NULL);
421
422 child_finish:
423 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
424 }
425
426 seq[1] = safe_close(seq[1]);
427
428 unit_files = hashmap_new(&portable_metadata_hash_ops);
429 if (!unit_files)
430 return -ENOMEM;
431
432 for (;;) {
433 _cleanup_(portable_metadata_unrefp) PortableMetadata *add = NULL;
434 _cleanup_close_ int fd = -1;
435 /* We use NAME_MAX space for the SELinux label here. The kernel currently enforces no limit, but
436 * according to suggestions from the SELinux people this will change and it will probably be
437 * identical to NAME_MAX. For now we use that, but this should be updated one day when the final
438 * limit is known. */
439 char iov_buffer[PATH_MAX + NAME_MAX + 2];
440 struct iovec iov = IOVEC_INIT(iov_buffer, sizeof(iov_buffer));
441
442 ssize_t n = receive_one_fd_iov(seq[0], &iov, 1, 0, &fd);
443 if (n == -EIO)
444 break;
445 if (n < 0)
446 return log_debug_errno(n, "Failed to receive item: %m");
447 iov_buffer[n] = 0;
448
449 /* We can't really distinguish a zero-length datagram without any fds from EOF (both are signalled the
450 * same way by recvmsg()). Hence, accept either as end notification. */
451 if (isempty(iov_buffer) && fd < 0)
452 break;
453
454 if (isempty(iov_buffer) || fd < 0)
455 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
456 "Invalid item sent from child.");
457
458 /* Given recvmsg cannot be used with multiple io vectors if you don't know the size in advance,
459 * use a marker to separate the name and the optional SELinux context. */
460 char *selinux_label = memchr(iov_buffer, 0, n);
461 assert(selinux_label);
462 selinux_label++;
463
464 add = portable_metadata_new(iov_buffer, path, selinux_label, fd);
465 if (!add)
466 return -ENOMEM;
467 fd = -1;
468
469 /* Note that we do not initialize 'add->source' here, as the source path is not usable here as
470 * it refers to a path only valid in the short-living namespaced child process we forked
471 * here. */
472
473 if (PORTABLE_METADATA_IS_UNIT(add)) {
474 r = hashmap_put(unit_files, add->name, add);
475 if (r < 0)
476 return log_debug_errno(r, "Failed to add item to unit file list: %m");
477
478 add = NULL;
479
480 } else if (PORTABLE_METADATA_IS_OS_RELEASE(add) || PORTABLE_METADATA_IS_EXTENSION_RELEASE(add)) {
481
482 assert(!os_release);
483 os_release = TAKE_PTR(add);
484 } else
485 assert_not_reached();
486 }
487
488 r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
489 if (r < 0)
490 return r;
491 child = 0;
492 }
493
494 if (!os_release)
495 return sd_bus_error_setf(error,
496 SD_BUS_ERROR_INVALID_ARGS,
497 "Image '%s' lacks %s data, refusing.",
498 path,
499 path_is_extension ? "extension-release" : "os-release");
500
501 if (ret_unit_files)
502 *ret_unit_files = TAKE_PTR(unit_files);
503
504 if (ret_os_release)
505 *ret_os_release = TAKE_PTR(os_release);
506
507 return 0;
508 }
509
510 static int extract_image_and_extensions(
511 const char *name_or_path,
512 char **matches,
513 char **extension_image_paths,
514 bool validate_sysext,
515 Image **ret_image,
516 OrderedHashmap **ret_extension_images,
517 OrderedHashmap **ret_extension_releases,
518 PortableMetadata **ret_os_release,
519 Hashmap **ret_unit_files,
520 char ***ret_valid_prefixes,
521 sd_bus_error *error) {
522
523 _cleanup_free_ char *id = NULL, *version_id = NULL, *sysext_level = NULL;
524 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
525 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL;
526 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
527 _cleanup_strv_free_ char **valid_prefixes = NULL;
528 _cleanup_(image_unrefp) Image *image = NULL;
529 Image *ext;
530 int r;
531
532 assert(name_or_path);
533
534 r = image_find_harder(IMAGE_PORTABLE, name_or_path, NULL, &image);
535 if (r < 0)
536 return r;
537
538 if (!strv_isempty(extension_image_paths)) {
539 extension_images = ordered_hashmap_new(&image_hash_ops);
540 if (!extension_images)
541 return -ENOMEM;
542
543 if (ret_extension_releases) {
544 extension_releases = ordered_hashmap_new(&portable_metadata_hash_ops);
545 if (!extension_releases)
546 return -ENOMEM;
547 }
548
549 STRV_FOREACH(p, extension_image_paths) {
550 _cleanup_(image_unrefp) Image *new = NULL;
551
552 r = image_find_harder(IMAGE_PORTABLE, *p, NULL, &new);
553 if (r < 0)
554 return r;
555
556 r = ordered_hashmap_put(extension_images, new->name, new);
557 if (r < 0)
558 return r;
559 TAKE_PTR(new);
560 }
561 }
562
563 r = portable_extract_by_path(image->path, /* path_is_extension= */ false, matches, &os_release, &unit_files, error);
564 if (r < 0)
565 return r;
566
567 /* If we are layering extension images on top of a runtime image, check that the os-release and
568 * extension-release metadata match, otherwise reject it immediately as invalid, or it will fail when
569 * the units are started. Also, collect valid portable prefixes if caller requested that. */
570 if (validate_sysext || ret_valid_prefixes) {
571 _cleanup_fclose_ FILE *f = NULL;
572 _cleanup_free_ char *prefixes = NULL;
573
574 r = take_fdopen_unlocked(&os_release->fd, "r", &f);
575 if (r < 0)
576 return r;
577
578 r = parse_env_file(f, os_release->name,
579 "ID", &id,
580 "VERSION_ID", &version_id,
581 "SYSEXT_LEVEL", &sysext_level,
582 "PORTABLE_PREFIXES", &prefixes);
583 if (r < 0)
584 return r;
585 if (isempty(id))
586 return sd_bus_error_set_errnof(error, SYNTHETIC_ERRNO(ESTALE), "Image %s os-release metadata lacks the ID field", name_or_path);
587
588 if (prefixes) {
589 valid_prefixes = strv_split(prefixes, WHITESPACE);
590 if (!valid_prefixes)
591 return -ENOMEM;
592 }
593 }
594
595 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
596 _cleanup_(portable_metadata_unrefp) PortableMetadata *extension_release_meta = NULL;
597 _cleanup_hashmap_free_ Hashmap *extra_unit_files = NULL;
598 _cleanup_strv_free_ char **extension_release = NULL;
599 _cleanup_close_ int extension_release_fd = -1;
600 _cleanup_fclose_ FILE *f = NULL;
601 const char *e;
602
603 r = portable_extract_by_path(ext->path, /* path_is_extension= */ true, matches, &extension_release_meta, &extra_unit_files, error);
604 if (r < 0)
605 return r;
606
607 r = hashmap_move(unit_files, extra_unit_files);
608 if (r < 0)
609 return r;
610
611 if (!validate_sysext && !ret_valid_prefixes && !ret_extension_releases)
612 continue;
613
614 /* We need to keep the fd valid, to return the PortableMetadata to the caller. */
615 extension_release_fd = fd_reopen(extension_release_meta->fd, O_CLOEXEC|O_RDONLY);
616 if (extension_release_fd < 0)
617 return extension_release_fd;
618
619 r = take_fdopen_unlocked(&extension_release_fd, "r", &f);
620 if (r < 0)
621 return r;
622
623 r = load_env_file_pairs(f, extension_release_meta->name, &extension_release);
624 if (r < 0)
625 return r;
626
627 if (validate_sysext) {
628 r = extension_release_validate(ext->path, id, version_id, sysext_level, "portable", extension_release);
629 if (r == 0)
630 return sd_bus_error_set_errnof(error, SYNTHETIC_ERRNO(ESTALE), "Image %s extension-release metadata does not match the root's", ext->path);
631 if (r < 0)
632 return sd_bus_error_set_errnof(error, r, "Failed to compare image %s extension-release metadata with the root's os-release: %m", ext->path);
633 }
634
635 e = strv_env_pairs_get(extension_release, "PORTABLE_PREFIXES");
636 if (e) {
637 _cleanup_strv_free_ char **l = NULL;
638
639 l = strv_split(e, WHITESPACE);
640 if (!l)
641 return -ENOMEM;
642
643 r = strv_extend_strv(&valid_prefixes, l, true);
644 if (r < 0)
645 return r;
646 }
647
648 if (ret_extension_releases) {
649 r = ordered_hashmap_put(extension_releases, ext->name, extension_release_meta);
650 if (r < 0)
651 return r;
652 TAKE_PTR(extension_release_meta);
653 }
654 }
655
656 strv_sort(valid_prefixes);
657
658 if (ret_image)
659 *ret_image = TAKE_PTR(image);
660 if (ret_extension_images)
661 *ret_extension_images = TAKE_PTR(extension_images);
662 if (ret_extension_releases)
663 *ret_extension_releases = TAKE_PTR(extension_releases);
664 if (ret_os_release)
665 *ret_os_release = TAKE_PTR(os_release);
666 if (ret_unit_files)
667 *ret_unit_files = TAKE_PTR(unit_files);
668 if (ret_valid_prefixes)
669 *ret_valid_prefixes = TAKE_PTR(valid_prefixes);
670
671 return 0;
672 }
673
674 int portable_extract(
675 const char *name_or_path,
676 char **matches,
677 char **extension_image_paths,
678 PortableMetadata **ret_os_release,
679 OrderedHashmap **ret_extension_releases,
680 Hashmap **ret_unit_files,
681 char ***ret_valid_prefixes,
682 sd_bus_error *error) {
683
684 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
685 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL;
686 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
687 _cleanup_(strv_freep) char **valid_prefixes = NULL;
688 _cleanup_(image_unrefp) Image *image = NULL;
689 int r;
690
691 assert(name_or_path);
692
693 r = extract_image_and_extensions(
694 name_or_path,
695 matches,
696 extension_image_paths,
697 /* validate_sysext= */ false,
698 &image,
699 &extension_images,
700 &extension_releases,
701 &os_release,
702 &unit_files,
703 ret_valid_prefixes ? &valid_prefixes : NULL,
704 error);
705 if (r < 0)
706 return r;
707
708 if (hashmap_isempty(unit_files)) {
709 _cleanup_free_ char *extensions = strv_join(extension_image_paths, ", ");
710 if (!extensions)
711 return -ENOMEM;
712
713 return sd_bus_error_setf(error,
714 SD_BUS_ERROR_INVALID_ARGS,
715 "Couldn't find any matching unit files in image '%s%s%s', refusing.",
716 image->path,
717 isempty(extensions) ? "" : "' or any of its extensions '",
718 isempty(extensions) ? "" : extensions);
719 }
720
721 if (ret_os_release)
722 *ret_os_release = TAKE_PTR(os_release);
723 if (ret_extension_releases)
724 *ret_extension_releases = TAKE_PTR(extension_releases);
725 if (ret_unit_files)
726 *ret_unit_files = TAKE_PTR(unit_files);
727 if (ret_valid_prefixes)
728 *ret_valid_prefixes = TAKE_PTR(valid_prefixes);
729
730 return 0;
731 }
732
733 static int unit_file_is_active(
734 sd_bus *bus,
735 const char *name,
736 sd_bus_error *error) {
737
738 static const char *const active_states[] = {
739 "activating",
740 "active",
741 "reloading",
742 "deactivating",
743 NULL,
744 };
745 int r;
746
747 if (!bus)
748 return false;
749
750 /* If we are looking at a plain or instance things are easy, we can just query the state */
751 if (unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
752 _cleanup_free_ char *path = NULL, *buf = NULL;
753
754 path = unit_dbus_path_from_name(name);
755 if (!path)
756 return -ENOMEM;
757
758 r = sd_bus_get_property_string(
759 bus,
760 "org.freedesktop.systemd1",
761 path,
762 "org.freedesktop.systemd1.Unit",
763 "ActiveState",
764 error,
765 &buf);
766 if (r < 0)
767 return log_debug_errno(r, "Failed to retrieve unit state: %s", bus_error_message(error, r));
768
769 return strv_contains((char**) active_states, buf);
770 }
771
772 /* Otherwise we need to enumerate. But let's build the most restricted query we can */
773 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
774 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
775 const char *at, *prefix, *joined;
776
777 r = sd_bus_message_new_method_call(
778 bus,
779 &m,
780 "org.freedesktop.systemd1",
781 "/org/freedesktop/systemd1",
782 "org.freedesktop.systemd1.Manager",
783 "ListUnitsByPatterns");
784 if (r < 0)
785 return r;
786
787 r = sd_bus_message_append_strv(m, (char**) active_states);
788 if (r < 0)
789 return r;
790
791 at = strchr(name, '@');
792 assert(at);
793
794 prefix = strndupa_safe(name, at + 1 - name);
795 joined = strjoina(prefix, "*", at + 1);
796
797 r = sd_bus_message_append_strv(m, STRV_MAKE(joined));
798 if (r < 0)
799 return r;
800
801 r = sd_bus_call(bus, m, 0, error, &reply);
802 if (r < 0)
803 return log_debug_errno(r, "Failed to list units: %s", bus_error_message(error, r));
804
805 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
806 if (r < 0)
807 return r;
808
809 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "ssssssouso");
810 if (r < 0)
811 return r;
812
813 return r > 0;
814 }
815
816 return -EINVAL;
817 }
818
819 static int portable_changes_add(
820 PortableChange **changes,
821 size_t *n_changes,
822 int type_or_errno, /* PORTABLE_COPY, PORTABLE_SYMLINK, … if positive, or errno if negative */
823 const char *path,
824 const char *source) {
825
826 _cleanup_free_ char *p = NULL, *s = NULL;
827 PortableChange *c;
828
829 assert(path);
830 assert(!changes == !n_changes);
831
832 if (type_or_errno >= 0)
833 assert(type_or_errno < _PORTABLE_CHANGE_TYPE_MAX);
834 else
835 assert(type_or_errno >= -ERRNO_MAX);
836
837 if (!changes)
838 return 0;
839
840 c = reallocarray(*changes, *n_changes + 1, sizeof(PortableChange));
841 if (!c)
842 return -ENOMEM;
843 *changes = c;
844
845 p = strdup(path);
846 if (!p)
847 return -ENOMEM;
848
849 path_simplify(p);
850
851 if (source) {
852 s = strdup(source);
853 if (!s)
854 return -ENOMEM;
855
856 path_simplify(s);
857 }
858
859 c[(*n_changes)++] = (PortableChange) {
860 .type_or_errno = type_or_errno,
861 .path = TAKE_PTR(p),
862 .source = TAKE_PTR(s),
863 };
864
865 return 0;
866 }
867
868 static int portable_changes_add_with_prefix(
869 PortableChange **changes,
870 size_t *n_changes,
871 int type_or_errno,
872 const char *prefix,
873 const char *path,
874 const char *source) {
875
876 assert(path);
877 assert(!changes == !n_changes);
878
879 if (!changes)
880 return 0;
881
882 if (prefix) {
883 path = prefix_roota(prefix, path);
884
885 if (source)
886 source = prefix_roota(prefix, source);
887 }
888
889 return portable_changes_add(changes, n_changes, type_or_errno, path, source);
890 }
891
892 void portable_changes_free(PortableChange *changes, size_t n_changes) {
893 size_t i;
894
895 assert(changes || n_changes == 0);
896
897 for (i = 0; i < n_changes; i++) {
898 free(changes[i].path);
899 free(changes[i].source);
900 }
901
902 free(changes);
903 }
904
905 static const char *root_setting_from_image(ImageType type) {
906 return IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "RootDirectory=" : "RootImage=";
907 }
908
909 static const char *extension_setting_from_image(ImageType type) {
910 return IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "ExtensionDirectories=" : "ExtensionImages=";
911 }
912
913 static int make_marker_text(const char *image_path, OrderedHashmap *extension_images, char **ret_text) {
914 _cleanup_free_ char *text = NULL, *escaped_image_path = NULL;
915 Image *ext;
916
917 assert(image_path);
918 assert(ret_text);
919
920 escaped_image_path = xescape(image_path, ":");
921 if (!escaped_image_path)
922 return -ENOMEM;
923
924 /* If the image is layered, include all layers in the marker as a colon-separated
925 * list of paths, so that we can do exact matches on removal. */
926 text = strjoin(PORTABLE_DROPIN_MARKER_BEGIN, escaped_image_path);
927 if (!text)
928 return -ENOMEM;
929
930 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
931 _cleanup_free_ char *escaped = NULL;
932
933 escaped = xescape(ext->path, ":");
934 if (!escaped)
935 return -ENOMEM;
936
937 if (!strextend(&text, ":", escaped))
938 return -ENOMEM;
939 }
940
941 if (!strextend(&text, PORTABLE_DROPIN_MARKER_END "\n"))
942 return -ENOMEM;
943
944 *ret_text = TAKE_PTR(text);
945 return 0;
946 }
947
948 static int install_chroot_dropin(
949 const char *image_path,
950 ImageType type,
951 OrderedHashmap *extension_images,
952 const PortableMetadata *m,
953 const char *dropin_dir,
954 char **ret_dropin,
955 PortableChange **changes,
956 size_t *n_changes) {
957
958 _cleanup_free_ char *text = NULL, *dropin = NULL;
959 int r;
960
961 assert(image_path);
962 assert(m);
963 assert(dropin_dir);
964
965 dropin = path_join(dropin_dir, "20-portable.conf");
966 if (!dropin)
967 return -ENOMEM;
968
969 r = make_marker_text(image_path, extension_images, &text);
970 if (r < 0)
971 return log_debug_errno(r, "Failed to generate marker string for portable drop-in: %m");
972
973 if (endswith(m->name, ".service")) {
974 const char *os_release_source, *root_type;
975 _cleanup_free_ char *base_name = NULL;
976 Image *ext;
977
978 root_type = root_setting_from_image(type);
979
980 if (access("/etc/os-release", F_OK) < 0) {
981 if (errno != ENOENT)
982 return log_debug_errno(errno, "Failed to check if /etc/os-release exists: %m");
983
984 os_release_source = "/usr/lib/os-release";
985 } else
986 os_release_source = "/etc/os-release";
987
988 r = path_extract_filename(m->image_path ?: image_path, &base_name);
989 if (r < 0)
990 return log_debug_errno(r, "Failed to extract basename from '%s': %m", m->image_path ?: image_path);
991
992 if (!strextend(&text,
993 "\n"
994 "[Service]\n",
995 root_type, image_path, "\n"
996 "Environment=PORTABLE=", base_name, "\n"
997 "BindReadOnlyPaths=", os_release_source, ":/run/host/os-release\n"
998 "LogExtraFields=PORTABLE=", base_name, "\n"))
999 return -ENOMEM;
1000
1001 if (m->image_path && !path_equal(m->image_path, image_path))
1002 ORDERED_HASHMAP_FOREACH(ext, extension_images)
1003 if (!strextend(&text, extension_setting_from_image(ext->type), ext->path, "\n"))
1004 return -ENOMEM;
1005 }
1006
1007 r = write_string_file(dropin, text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1008 if (r < 0)
1009 return log_debug_errno(r, "Failed to write '%s': %m", dropin);
1010
1011 (void) portable_changes_add(changes, n_changes, PORTABLE_WRITE, dropin, NULL);
1012
1013 if (ret_dropin)
1014 *ret_dropin = TAKE_PTR(dropin);
1015
1016 return 0;
1017 }
1018
1019 static int install_profile_dropin(
1020 const char *image_path,
1021 const PortableMetadata *m,
1022 const char *dropin_dir,
1023 const char *profile,
1024 PortableFlags flags,
1025 char **ret_dropin,
1026 PortableChange **changes,
1027 size_t *n_changes) {
1028
1029 _cleanup_free_ char *dropin = NULL, *from = NULL;
1030 int r;
1031
1032 assert(image_path);
1033 assert(m);
1034 assert(dropin_dir);
1035
1036 if (!profile)
1037 return 0;
1038
1039 r = find_portable_profile(profile, m->name, &from);
1040 if (r < 0) {
1041 if (r != -ENOENT)
1042 return log_debug_errno(errno, "Profile '%s' is not accessible: %m", profile);
1043
1044 log_debug_errno(errno, "Skipping link to profile '%s', as it does not exist: %m", profile);
1045 return 0;
1046 }
1047
1048 dropin = path_join(dropin_dir, "10-profile.conf");
1049 if (!dropin)
1050 return -ENOMEM;
1051
1052 if (flags & PORTABLE_PREFER_COPY) {
1053
1054 r = copy_file_atomic(from, dropin, 0644, 0, 0, COPY_REFLINK);
1055 if (r < 0)
1056 return log_debug_errno(r, "Failed to copy %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin);
1057
1058 (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, dropin, from);
1059
1060 } else {
1061
1062 if (symlink(from, dropin) < 0)
1063 return log_debug_errno(errno, "Failed to link %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin);
1064
1065 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, dropin, from);
1066 }
1067
1068 if (ret_dropin)
1069 *ret_dropin = TAKE_PTR(dropin);
1070
1071 return 0;
1072 }
1073
1074 static const char *attached_path(const LookupPaths *paths, PortableFlags flags) {
1075 const char *where;
1076
1077 assert(paths);
1078
1079 if (flags & PORTABLE_RUNTIME)
1080 where = paths->runtime_attached;
1081 else
1082 where = paths->persistent_attached;
1083
1084 assert(where);
1085 return where;
1086 }
1087
1088 static int attach_unit_file(
1089 const LookupPaths *paths,
1090 const char *image_path,
1091 ImageType type,
1092 OrderedHashmap *extension_images,
1093 const PortableMetadata *m,
1094 const char *profile,
1095 PortableFlags flags,
1096 PortableChange **changes,
1097 size_t *n_changes) {
1098
1099 _cleanup_(unlink_and_freep) char *chroot_dropin = NULL, *profile_dropin = NULL;
1100 _cleanup_(rmdir_and_freep) char *dropin_dir = NULL;
1101 const char *where, *path;
1102 int r;
1103
1104 assert(paths);
1105 assert(image_path);
1106 assert(m);
1107 assert(PORTABLE_METADATA_IS_UNIT(m));
1108
1109 where = attached_path(paths, flags);
1110
1111 (void) mkdir_parents(where, 0755);
1112 if (mkdir(where, 0755) < 0) {
1113 if (errno != EEXIST)
1114 return -errno;
1115 } else
1116 (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, where, NULL);
1117
1118 path = prefix_roota(where, m->name);
1119 dropin_dir = strjoin(path, ".d");
1120 if (!dropin_dir)
1121 return -ENOMEM;
1122
1123 if (mkdir(dropin_dir, 0755) < 0) {
1124 if (errno != EEXIST)
1125 return -errno;
1126 } else
1127 (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, dropin_dir, NULL);
1128
1129 /* We install the drop-ins first, and the actual unit file last to achieve somewhat atomic behaviour if PID 1
1130 * is reloaded while we are creating things here: as long as only the drop-ins exist the unit doesn't exist at
1131 * all for PID 1. */
1132
1133 r = install_chroot_dropin(image_path, type, extension_images, m, dropin_dir, &chroot_dropin, changes, n_changes);
1134 if (r < 0)
1135 return r;
1136
1137 r = install_profile_dropin(image_path, m, dropin_dir, profile, flags, &profile_dropin, changes, n_changes);
1138 if (r < 0)
1139 return r;
1140
1141 if ((flags & PORTABLE_PREFER_SYMLINK) && m->source) {
1142
1143 if (symlink(m->source, path) < 0)
1144 return log_debug_errno(errno, "Failed to symlink unit file '%s': %m", path);
1145
1146 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, path, m->source);
1147
1148 } else {
1149 _cleanup_(unlink_and_freep) char *tmp = NULL;
1150 _cleanup_close_ int fd = -1;
1151
1152 (void) mac_selinux_create_file_prepare_label(path, m->selinux_label);
1153
1154 fd = open_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &tmp);
1155 mac_selinux_create_file_clear(); /* Clear immediately in case of errors */
1156 if (fd < 0)
1157 return log_debug_errno(fd, "Failed to create unit file '%s': %m", path);
1158
1159 r = copy_bytes(m->fd, fd, UINT64_MAX, COPY_REFLINK);
1160 if (r < 0)
1161 return log_debug_errno(r, "Failed to copy unit file '%s': %m", path);
1162
1163 if (fchmod(fd, 0644) < 0)
1164 return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path);
1165
1166 r = link_tmpfile(fd, tmp, path);
1167 if (r < 0)
1168 return log_debug_errno(r, "Failed to install unit file '%s': %m", path);
1169
1170 tmp = mfree(tmp);
1171
1172 (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, path, m->source);
1173 }
1174
1175 /* All is established now, now let's disable any rollbacks */
1176 chroot_dropin = mfree(chroot_dropin);
1177 profile_dropin = mfree(profile_dropin);
1178 dropin_dir = mfree(dropin_dir);
1179
1180 return 0;
1181 }
1182
1183 static int image_symlink(
1184 const char *image_path,
1185 PortableFlags flags,
1186 char **ret) {
1187
1188 const char *fn, *where;
1189 char *joined = NULL;
1190
1191 assert(image_path);
1192 assert(ret);
1193
1194 fn = last_path_component(image_path);
1195
1196 if (flags & PORTABLE_RUNTIME)
1197 where = "/run/portables/";
1198 else
1199 where = "/etc/portables/";
1200
1201 joined = strjoin(where, fn);
1202 if (!joined)
1203 return -ENOMEM;
1204
1205 *ret = joined;
1206 return 0;
1207 }
1208
1209 static int install_image_symlink(
1210 const char *image_path,
1211 PortableFlags flags,
1212 PortableChange **changes,
1213 size_t *n_changes) {
1214
1215 _cleanup_free_ char *sl = NULL;
1216 int r;
1217
1218 assert(image_path);
1219
1220 /* If the image is outside of the image search also link it into it, so that it can be found with short image
1221 * names and is listed among the images. */
1222
1223 if (image_in_search_path(IMAGE_PORTABLE, NULL, image_path))
1224 return 0;
1225
1226 r = image_symlink(image_path, flags, &sl);
1227 if (r < 0)
1228 return log_debug_errno(r, "Failed to generate image symlink path: %m");
1229
1230 (void) mkdir_parents(sl, 0755);
1231
1232 if (symlink(image_path, sl) < 0)
1233 return log_debug_errno(errno, "Failed to link %s %s %s: %m", image_path, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), sl);
1234
1235 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, sl, image_path);
1236 return 0;
1237 }
1238
1239 static int install_image_and_extensions_symlinks(
1240 const Image *image,
1241 OrderedHashmap *extension_images,
1242 PortableFlags flags,
1243 PortableChange **changes,
1244 size_t *n_changes) {
1245
1246 Image *ext;
1247 int r;
1248
1249 assert(image);
1250
1251 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
1252 r = install_image_symlink(ext->path, flags, changes, n_changes);
1253 if (r < 0)
1254 return r;
1255 }
1256
1257 r = install_image_symlink(image->path, flags, changes, n_changes);
1258 if (r < 0)
1259 return r;
1260
1261 return 0;
1262 }
1263
1264 static bool prefix_matches_compatible(char **matches, char **valid_prefixes) {
1265 /* Checks if all 'matches' are included in the list of 'valid_prefixes' */
1266
1267 STRV_FOREACH(m, matches)
1268 if (!strv_contains(valid_prefixes, *m))
1269 return false;
1270
1271 return true;
1272 }
1273
1274 int portable_attach(
1275 sd_bus *bus,
1276 const char *name_or_path,
1277 char **matches,
1278 const char *profile,
1279 char **extension_image_paths,
1280 PortableFlags flags,
1281 PortableChange **changes,
1282 size_t *n_changes,
1283 sd_bus_error *error) {
1284
1285 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL;
1286 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
1287 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1288 _cleanup_strv_free_ char **valid_prefixes = NULL;
1289 _cleanup_(image_unrefp) Image *image = NULL;
1290 PortableMetadata *item;
1291 int r;
1292
1293 r = extract_image_and_extensions(
1294 name_or_path,
1295 matches,
1296 extension_image_paths,
1297 /* validate_sysext= */ true,
1298 &image,
1299 &extension_images,
1300 /* extension_releases= */ NULL,
1301 /* os_release= */ NULL,
1302 &unit_files,
1303 &valid_prefixes,
1304 error);
1305 if (r < 0)
1306 return r;
1307
1308 if (valid_prefixes && !prefix_matches_compatible(matches, valid_prefixes)) {
1309 _cleanup_free_ char *matches_joined = NULL, *extensions_joined = NULL, *valid_prefixes_joined = NULL;
1310
1311 matches_joined = strv_join(matches, "', '");
1312 if (!matches_joined)
1313 return -ENOMEM;
1314
1315 extensions_joined = strv_join(extension_image_paths, ", ");
1316 if (!extensions_joined)
1317 return -ENOMEM;
1318
1319 valid_prefixes_joined = strv_join(valid_prefixes, ", ");
1320 if (!valid_prefixes_joined)
1321 return -ENOMEM;
1322
1323 return sd_bus_error_setf(
1324 error,
1325 SD_BUS_ERROR_INVALID_ARGS,
1326 "Selected matches '%s' are not compatible with portable service image '%s%s%s', refusing. (Acceptable prefix matches are: %s)",
1327 matches_joined,
1328 image->path,
1329 isempty(extensions_joined) ? "" : "' or any of its extensions '",
1330 strempty(extensions_joined),
1331 valid_prefixes_joined);
1332 }
1333
1334 if (hashmap_isempty(unit_files)) {
1335 _cleanup_free_ char *extensions_joined = strv_join(extension_image_paths, ", ");
1336 if (!extensions_joined)
1337 return -ENOMEM;
1338
1339 return sd_bus_error_setf(
1340 error,
1341 SD_BUS_ERROR_INVALID_ARGS,
1342 "Couldn't find any matching unit files in image '%s%s%s', refusing.",
1343 image->path,
1344 isempty(extensions_joined) ? "" : "' or any of its extensions '",
1345 strempty(extensions_joined));
1346 }
1347
1348 r = lookup_paths_init(&paths, LOOKUP_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1349 if (r < 0)
1350 return r;
1351
1352 HASHMAP_FOREACH(item, unit_files) {
1353 r = unit_file_exists(LOOKUP_SCOPE_SYSTEM, &paths, item->name);
1354 if (r < 0)
1355 return sd_bus_error_set_errnof(error, r, "Failed to determine whether unit '%s' exists on the host: %m", item->name);
1356 if (!FLAGS_SET(flags, PORTABLE_REATTACH) && r > 0)
1357 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' exists on the host already, refusing.", item->name);
1358
1359 r = unit_file_is_active(bus, item->name, error);
1360 if (r < 0)
1361 return r;
1362 if (!FLAGS_SET(flags, PORTABLE_REATTACH) && r > 0)
1363 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active already, refusing.", item->name);
1364 }
1365
1366 HASHMAP_FOREACH(item, unit_files) {
1367 r = attach_unit_file(&paths, image->path, image->type, extension_images,
1368 item, profile, flags, changes, n_changes);
1369 if (r < 0)
1370 return r;
1371 }
1372
1373 /* We don't care too much for the image symlink, it's just a convenience thing, it's not necessary for proper
1374 * operation otherwise. */
1375 (void) install_image_and_extensions_symlinks(image, extension_images, flags, changes, n_changes);
1376
1377 return 0;
1378 }
1379
1380 static bool marker_matches_images(const char *marker, const char *name_or_path, char **extension_image_paths) {
1381 _cleanup_strv_free_ char **root_and_extensions = NULL;
1382 const char *a;
1383 int r;
1384
1385 assert(marker);
1386 assert(name_or_path);
1387
1388 /* If extensions were used when attaching, the marker will be a colon-separated
1389 * list of images/paths. We enforce strict 1:1 matching, so that we are sure
1390 * we are detaching exactly what was attached.
1391 * For each image, starting with the root, we look for a token in the marker,
1392 * and return a negative answer on any non-matching combination. */
1393
1394 root_and_extensions = strv_new(name_or_path);
1395 if (!root_and_extensions)
1396 return -ENOMEM;
1397
1398 r = strv_extend_strv(&root_and_extensions, extension_image_paths, false);
1399 if (r < 0)
1400 return r;
1401
1402 STRV_FOREACH(image_name_or_path, root_and_extensions) {
1403 _cleanup_free_ char *image = NULL;
1404
1405 r = extract_first_word(&marker, &image, ":", EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
1406 if (r < 0)
1407 return log_debug_errno(r, "Failed to parse marker: %s", marker);
1408 if (r == 0)
1409 return false;
1410
1411 a = last_path_component(image);
1412
1413 if (image_name_is_valid(*image_name_or_path)) {
1414 const char *e, *underscore;
1415
1416 /* We shall match against an image name. In that case let's compare the last component, and optionally
1417 * allow either a suffix of ".raw" or a series of "/".
1418 * But allow matching on a different version of the same image, when a "_" is used as a separator. */
1419 underscore = strchr(*image_name_or_path, '_');
1420 if (underscore) {
1421 if (strneq(a, *image_name_or_path, underscore - *image_name_or_path))
1422 continue;
1423 return false;
1424 }
1425
1426 e = startswith(a, *image_name_or_path);
1427 if (!e)
1428 return false;
1429
1430 if(!(e[strspn(e, "/")] == 0 || streq(e, ".raw")))
1431 return false;
1432 } else {
1433 const char *b, *underscore;
1434 size_t l;
1435
1436 /* We shall match against a path. Let's ignore any prefix here though, as often there are many ways to
1437 * reach the same file. However, in this mode, let's validate any file suffix.
1438 * But also ensure that we don't fail if both components don't have a '/' at all
1439 * (strcspn returns the full length of the string in that case, which might not
1440 * match as the versions might differ). */
1441
1442 l = strcspn(a, "/");
1443 b = last_path_component(*image_name_or_path);
1444
1445 if ((a[l] != '/') != !strchr(b, '/')) /* One is a directory, the other is not */
1446 return false;
1447
1448 if (a[l] != 0 && strcspn(b, "/") != l)
1449 return false;
1450
1451 underscore = strchr(b, '_');
1452 if (underscore)
1453 l = underscore - b;
1454 else { /* Either component could be versioned */
1455 underscore = strchr(a, '_');
1456 if (underscore)
1457 l = underscore - a;
1458 }
1459
1460 if (!strneq(a, b, l))
1461 return false;
1462 }
1463 }
1464
1465 return true;
1466 }
1467
1468 static int test_chroot_dropin(
1469 DIR *d,
1470 const char *where,
1471 const char *fname,
1472 const char *name_or_path,
1473 char **extension_image_paths,
1474 char **ret_marker) {
1475
1476 _cleanup_free_ char *line = NULL, *marker = NULL;
1477 _cleanup_fclose_ FILE *f = NULL;
1478 _cleanup_close_ int fd = -1;
1479 const char *p, *e, *k;
1480 int r;
1481
1482 assert(d);
1483 assert(where);
1484 assert(fname);
1485
1486 /* We recognize unis created from portable images via the drop-in we created for them */
1487
1488 p = strjoina(fname, ".d/20-portable.conf");
1489 fd = openat(dirfd(d), p, O_RDONLY|O_CLOEXEC);
1490 if (fd < 0) {
1491 if (errno == ENOENT)
1492 return 0;
1493
1494 return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p);
1495 }
1496
1497 r = take_fdopen_unlocked(&fd, "r", &f);
1498 if (r < 0)
1499 return log_debug_errno(r, "Failed to convert file handle: %m");
1500
1501 r = read_line(f, LONG_LINE_MAX, &line);
1502 if (r < 0)
1503 return log_debug_errno(r, "Failed to read from %s/%s: %m", where, p);
1504
1505 e = startswith(line, PORTABLE_DROPIN_MARKER_BEGIN);
1506 if (!e)
1507 return 0;
1508
1509 k = endswith(e, PORTABLE_DROPIN_MARKER_END);
1510 if (!k)
1511 return 0;
1512
1513 marker = strndup(e, k - e);
1514 if (!marker)
1515 return -ENOMEM;
1516
1517 if (!name_or_path)
1518 r = true;
1519 else
1520 r = marker_matches_images(marker, name_or_path, extension_image_paths);
1521
1522 if (ret_marker)
1523 *ret_marker = TAKE_PTR(marker);
1524
1525 return r;
1526 }
1527
1528 int portable_detach(
1529 sd_bus *bus,
1530 const char *name_or_path,
1531 char **extension_image_paths,
1532 PortableFlags flags,
1533 PortableChange **changes,
1534 size_t *n_changes,
1535 sd_bus_error *error) {
1536
1537 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1538 _cleanup_set_free_ Set *unit_files = NULL, *markers = NULL;
1539 _cleanup_free_ char *extensions = NULL;
1540 _cleanup_closedir_ DIR *d = NULL;
1541 const char *where, *item;
1542 int ret = 0;
1543 int r;
1544
1545 assert(name_or_path);
1546
1547 r = lookup_paths_init(&paths, LOOKUP_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1548 if (r < 0)
1549 return r;
1550
1551 where = attached_path(&paths, flags);
1552
1553 d = opendir(where);
1554 if (!d) {
1555 if (errno == ENOENT)
1556 goto not_found;
1557
1558 return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1559 }
1560
1561 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) {
1562 _cleanup_free_ char *marker = NULL, *unit_name = NULL;
1563 const char *dot;
1564
1565 /* When a portable service is enabled with "portablectl --copy=symlink --enable --now attach",
1566 * and is disabled with "portablectl --enable --now detach", which calls DisableUnitFilesWithFlags
1567 * DBus method, the main unit file is removed, but its drop-ins are not. Hence, here we need
1568 * to list both main unit files and drop-in directories (without the main unit files). */
1569
1570 dot = endswith(de->d_name, ".d");
1571 if (dot)
1572 unit_name = strndup(de->d_name, dot - de->d_name);
1573 else
1574 unit_name = strdup(de->d_name);
1575 if (!unit_name)
1576 return -ENOMEM;
1577
1578 if (!unit_name_is_valid(unit_name, UNIT_NAME_ANY))
1579 continue;
1580
1581 /* Filter out duplicates */
1582 if (set_contains(unit_files, unit_name))
1583 continue;
1584
1585 if (dot ? !IN_SET(de->d_type, DT_LNK, DT_DIR) : !IN_SET(de->d_type, DT_LNK, DT_REG))
1586 continue;
1587
1588 r = test_chroot_dropin(d, where, unit_name, name_or_path, extension_image_paths, &marker);
1589 if (r < 0)
1590 return r;
1591 if (r == 0)
1592 continue;
1593
1594 r = unit_file_is_active(bus, unit_name, error);
1595 if (r < 0)
1596 return r;
1597 if (!FLAGS_SET(flags, PORTABLE_REATTACH) && r > 0)
1598 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active, can't detach.", unit_name);
1599
1600 r = set_ensure_consume(&unit_files, &string_hash_ops_free, TAKE_PTR(unit_name));
1601 if (r < 0)
1602 return log_oom_debug();
1603
1604 for (const char *p = marker;;) {
1605 _cleanup_free_ char *image = NULL;
1606
1607 r = extract_first_word(&p, &image, ":", EXTRACT_UNESCAPE_SEPARATORS|EXTRACT_RETAIN_ESCAPE);
1608 if (r < 0)
1609 return log_debug_errno(r, "Failed to parse marker: %s", p);
1610 if (r == 0)
1611 break;
1612
1613 if (path_is_absolute(image) && !image_in_search_path(IMAGE_PORTABLE, NULL, image)) {
1614 r = set_ensure_consume(&markers, &path_hash_ops_free, TAKE_PTR(image));
1615 if (r < 0)
1616 return r;
1617 }
1618 }
1619 }
1620
1621 if (set_isempty(unit_files))
1622 goto not_found;
1623
1624 SET_FOREACH(item, unit_files) {
1625 _cleanup_free_ char *md = NULL;
1626
1627 if (unlinkat(dirfd(d), item, 0) < 0) {
1628 log_debug_errno(errno, "Can't remove unit file %s/%s: %m", where, item);
1629
1630 if (errno != ENOENT && ret >= 0)
1631 ret = -errno;
1632 } else
1633 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, item, NULL);
1634
1635 FOREACH_STRING(suffix, ".d/10-profile.conf", ".d/20-portable.conf") {
1636 _cleanup_free_ char *dropin = NULL;
1637
1638 dropin = strjoin(item, suffix);
1639 if (!dropin)
1640 return -ENOMEM;
1641
1642 if (unlinkat(dirfd(d), dropin, 0) < 0) {
1643 log_debug_errno(errno, "Can't remove drop-in %s/%s: %m", where, dropin);
1644
1645 if (errno != ENOENT && ret >= 0)
1646 ret = -errno;
1647 } else
1648 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, dropin, NULL);
1649 }
1650
1651 md = strjoin(item, ".d");
1652 if (!md)
1653 return -ENOMEM;
1654
1655 if (unlinkat(dirfd(d), md, AT_REMOVEDIR) < 0) {
1656 log_debug_errno(errno, "Can't remove drop-in directory %s/%s: %m", where, md);
1657
1658 if (errno != ENOENT && ret >= 0)
1659 ret = -errno;
1660 } else
1661 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, md, NULL);
1662 }
1663
1664 /* Now, also drop any image symlink, for images outside of the sarch path */
1665 SET_FOREACH(item, markers) {
1666 _cleanup_free_ char *sl = NULL;
1667 struct stat st;
1668
1669 r = image_symlink(item, flags, &sl);
1670 if (r < 0) {
1671 log_debug_errno(r, "Failed to determine image symlink for '%s', ignoring: %m", item);
1672 continue;
1673 }
1674
1675 if (lstat(sl, &st) < 0) {
1676 log_debug_errno(errno, "Failed to stat '%s', ignoring: %m", sl);
1677 continue;
1678 }
1679
1680 if (!S_ISLNK(st.st_mode)) {
1681 log_debug("Image '%s' is not a symlink, ignoring.", sl);
1682 continue;
1683 }
1684
1685 if (unlink(sl) < 0) {
1686 log_debug_errno(errno, "Can't remove image symlink '%s': %m", sl);
1687
1688 if (errno != ENOENT && ret >= 0)
1689 ret = -errno;
1690 } else
1691 portable_changes_add(changes, n_changes, PORTABLE_UNLINK, sl, NULL);
1692 }
1693
1694 /* Try to remove the unit file directory, if we can */
1695 if (rmdir(where) >= 0)
1696 portable_changes_add(changes, n_changes, PORTABLE_UNLINK, where, NULL);
1697
1698 return ret;
1699
1700 not_found:
1701 extensions = strv_join(extension_image_paths, ", ");
1702 if (!extensions)
1703 return -ENOMEM;
1704
1705 r = sd_bus_error_setf(error,
1706 BUS_ERROR_NO_SUCH_UNIT,
1707 "No unit files associated with '%s%s%s' found attached to the system. Image not attached?",
1708 name_or_path,
1709 isempty(extensions) ? "" : "' or any of its extensions '",
1710 isempty(extensions) ? "" : extensions);
1711 return log_debug_errno(r, "%s", error->message);
1712 }
1713
1714 static int portable_get_state_internal(
1715 sd_bus *bus,
1716 const char *name_or_path,
1717 char **extension_image_paths,
1718 PortableFlags flags,
1719 PortableState *ret,
1720 sd_bus_error *error) {
1721
1722 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1723 bool found_enabled = false, found_running = false;
1724 _cleanup_set_free_ Set *unit_files = NULL;
1725 _cleanup_closedir_ DIR *d = NULL;
1726 const char *where;
1727 int r;
1728
1729 assert(name_or_path);
1730 assert(ret);
1731
1732 r = lookup_paths_init(&paths, LOOKUP_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1733 if (r < 0)
1734 return r;
1735
1736 where = attached_path(&paths, flags);
1737
1738 d = opendir(where);
1739 if (!d) {
1740 if (errno == ENOENT) {
1741 /* If the 'attached' directory doesn't exist at all, then we know for sure this image isn't attached. */
1742 *ret = PORTABLE_DETACHED;
1743 return 0;
1744 }
1745
1746 return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1747 }
1748
1749 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) {
1750 UnitFileState state;
1751
1752 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
1753 continue;
1754
1755 /* Filter out duplicates */
1756 if (set_contains(unit_files, de->d_name))
1757 continue;
1758
1759 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
1760 continue;
1761
1762 r = test_chroot_dropin(d, where, de->d_name, name_or_path, extension_image_paths, NULL);
1763 if (r < 0)
1764 return r;
1765 if (r == 0)
1766 continue;
1767
1768 r = unit_file_lookup_state(LOOKUP_SCOPE_SYSTEM, &paths, de->d_name, &state);
1769 if (r < 0)
1770 return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name);
1771 if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_LINKED_RUNTIME))
1772 found_enabled = true;
1773
1774 r = unit_file_is_active(bus, de->d_name, error);
1775 if (r < 0)
1776 return r;
1777 if (r > 0)
1778 found_running = true;
1779
1780 r = set_put_strdup(&unit_files, de->d_name);
1781 if (r < 0)
1782 return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name);
1783 }
1784
1785 *ret = found_running ? (!set_isempty(unit_files) && (flags & PORTABLE_RUNTIME) ? PORTABLE_RUNNING_RUNTIME : PORTABLE_RUNNING) :
1786 found_enabled ? (flags & PORTABLE_RUNTIME ? PORTABLE_ENABLED_RUNTIME : PORTABLE_ENABLED) :
1787 !set_isempty(unit_files) ? (flags & PORTABLE_RUNTIME ? PORTABLE_ATTACHED_RUNTIME : PORTABLE_ATTACHED) : PORTABLE_DETACHED;
1788
1789 return 0;
1790 }
1791
1792 int portable_get_state(
1793 sd_bus *bus,
1794 const char *name_or_path,
1795 char **extension_image_paths,
1796 PortableFlags flags,
1797 PortableState *ret,
1798 sd_bus_error *error) {
1799
1800 PortableState state;
1801 int r;
1802
1803 assert(name_or_path);
1804 assert(ret);
1805
1806 /* We look for matching units twice: once in the regular directories, and once in the runtime directories — but
1807 * the latter only if we didn't find anything in the former. */
1808
1809 r = portable_get_state_internal(bus, name_or_path, extension_image_paths, flags & ~PORTABLE_RUNTIME, &state, error);
1810 if (r < 0)
1811 return r;
1812
1813 if (state == PORTABLE_DETACHED) {
1814 r = portable_get_state_internal(bus, name_or_path, extension_image_paths, flags | PORTABLE_RUNTIME, &state, error);
1815 if (r < 0)
1816 return r;
1817 }
1818
1819 *ret = state;
1820 return 0;
1821 }
1822
1823 int portable_get_profiles(char ***ret) {
1824 assert(ret);
1825
1826 return conf_files_list_nulstr(ret, NULL, NULL, CONF_FILES_DIRECTORY|CONF_FILES_BASENAME|CONF_FILES_FILTER_MASKED, PORTABLE_PROFILE_DIRS);
1827 }
1828
1829 static const char* const portable_change_type_table[_PORTABLE_CHANGE_TYPE_MAX] = {
1830 [PORTABLE_COPY] = "copy",
1831 [PORTABLE_MKDIR] = "mkdir",
1832 [PORTABLE_SYMLINK] = "symlink",
1833 [PORTABLE_UNLINK] = "unlink",
1834 [PORTABLE_WRITE] = "write",
1835 };
1836
1837 DEFINE_STRING_TABLE_LOOKUP(portable_change_type, int);
1838
1839 static const char* const portable_state_table[_PORTABLE_STATE_MAX] = {
1840 [PORTABLE_DETACHED] = "detached",
1841 [PORTABLE_ATTACHED] = "attached",
1842 [PORTABLE_ATTACHED_RUNTIME] = "attached-runtime",
1843 [PORTABLE_ENABLED] = "enabled",
1844 [PORTABLE_ENABLED_RUNTIME] = "enabled-runtime",
1845 [PORTABLE_RUNNING] = "running",
1846 [PORTABLE_RUNNING_RUNTIME] = "running-runtime",
1847 };
1848
1849 DEFINE_STRING_TABLE_LOOKUP(portable_state, PortableState);