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