]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/pull-tar.c
ae573f1b5403e8e340523a77458d86dce012797d
[thirdparty/systemd.git] / src / import / pull-tar.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <curl/curl.h>
4 #include <sys/prctl.h>
5
6 #include "sd-daemon.h"
7
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "copy.h"
11 #include "curl-util.h"
12 #include "fd-util.h"
13 #include "fs-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-util.h"
17 #include "install-file.h"
18 #include "macro.h"
19 #include "mkdir-label.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "pull-common.h"
23 #include "pull-job.h"
24 #include "pull-tar.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "strv.h"
28 #include "tmpfile-util.h"
29 #include "user-util.h"
30 #include "utf8.h"
31 #include "web-util.h"
32
33 typedef enum TarProgress {
34 TAR_DOWNLOADING,
35 TAR_VERIFYING,
36 TAR_FINALIZING,
37 TAR_COPYING,
38 } TarProgress;
39
40 struct TarPull {
41 sd_event *event;
42 CurlGlue *glue;
43
44 ImportFlags flags;
45 ImportVerify verify;
46 char *image_root;
47
48 PullJob *tar_job;
49 PullJob *checksum_job;
50 PullJob *signature_job;
51 PullJob *settings_job;
52
53 TarPullFinished on_finished;
54 void *userdata;
55
56 char *local;
57
58 pid_t tar_pid;
59
60 char *final_path;
61 char *temp_path;
62
63 char *settings_path;
64 char *settings_temp_path;
65
66 char *checksum;
67 };
68
69 TarPull* tar_pull_unref(TarPull *i) {
70 if (!i)
71 return NULL;
72
73 if (i->tar_pid > 1)
74 sigkill_wait(i->tar_pid);
75
76 pull_job_unref(i->tar_job);
77 pull_job_unref(i->checksum_job);
78 pull_job_unref(i->signature_job);
79 pull_job_unref(i->settings_job);
80
81 curl_glue_unref(i->glue);
82 sd_event_unref(i->event);
83
84 rm_rf_subvolume_and_free(i->temp_path);
85 unlink_and_free(i->settings_temp_path);
86
87 free(i->final_path);
88 free(i->settings_path);
89 free(i->image_root);
90 free(i->local);
91 free(i->checksum);
92
93 return mfree(i);
94 }
95
96 int tar_pull_new(
97 TarPull **ret,
98 sd_event *event,
99 const char *image_root,
100 TarPullFinished on_finished,
101 void *userdata) {
102
103 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
104 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
105 _cleanup_(tar_pull_unrefp) TarPull *i = NULL;
106 _cleanup_free_ char *root = NULL;
107 int r;
108
109 assert(image_root);
110 assert(ret);
111
112 root = strdup(image_root);
113 if (!root)
114 return -ENOMEM;
115
116 if (event)
117 e = sd_event_ref(event);
118 else {
119 r = sd_event_default(&e);
120 if (r < 0)
121 return r;
122 }
123
124 r = curl_glue_new(&g, e);
125 if (r < 0)
126 return r;
127
128 i = new(TarPull, 1);
129 if (!i)
130 return -ENOMEM;
131
132 *i = (TarPull) {
133 .on_finished = on_finished,
134 .userdata = userdata,
135 .image_root = TAKE_PTR(root),
136 .event = TAKE_PTR(e),
137 .glue = TAKE_PTR(g),
138 };
139
140 i->glue->on_finished = pull_job_curl_on_finished;
141 i->glue->userdata = i;
142
143 *ret = TAKE_PTR(i);
144
145 return 0;
146 }
147
148 static void tar_pull_report_progress(TarPull *i, TarProgress p) {
149 unsigned percent;
150
151 assert(i);
152
153 switch (p) {
154
155 case TAR_DOWNLOADING: {
156 unsigned remain = 85;
157
158 percent = 0;
159
160 if (i->checksum_job) {
161 percent += i->checksum_job->progress_percent * 5 / 100;
162 remain -= 5;
163 }
164
165 if (i->signature_job) {
166 percent += i->signature_job->progress_percent * 5 / 100;
167 remain -= 5;
168 }
169
170 if (i->settings_job) {
171 percent += i->settings_job->progress_percent * 5 / 100;
172 remain -= 5;
173 }
174
175 if (i->tar_job)
176 percent += i->tar_job->progress_percent * remain / 100;
177 break;
178 }
179
180 case TAR_VERIFYING:
181 percent = 85;
182 break;
183
184 case TAR_FINALIZING:
185 percent = 90;
186 break;
187
188 case TAR_COPYING:
189 percent = 95;
190 break;
191
192 default:
193 assert_not_reached();
194 }
195
196 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
197 log_debug("Combined progress %u%%", percent);
198 }
199
200 static int tar_pull_determine_path(
201 TarPull *i,
202 const char *suffix,
203 char **field /* input + output (!) */) {
204 int r;
205
206 assert(i);
207 assert(field);
208
209 if (*field)
210 return 0;
211
212 assert(i->tar_job);
213
214 r = pull_make_path(i->tar_job->url, i->tar_job->etag, i->image_root, ".tar-", suffix, field);
215 if (r < 0)
216 return log_oom();
217
218 return 1;
219 }
220
221 static int tar_pull_make_local_copy(TarPull *i) {
222 _cleanup_(rm_rf_subvolume_and_freep) char *t = NULL;
223 _cleanup_free_ char *p = NULL;
224 int r;
225
226 assert(i);
227 assert(i->tar_job);
228
229 if (!i->local)
230 return 0;
231
232 assert(i->final_path);
233
234 p = path_join(i->image_root, i->local);
235 if (!p)
236 return log_oom();
237
238 r = tempfn_random(p, NULL, &t);
239 if (r < 0)
240 return log_error_errno(r, "Failed to generate temporary filename for %s: %m", p);
241
242 if (i->flags & IMPORT_BTRFS_SUBVOL)
243 r = btrfs_subvol_snapshot_at(
244 AT_FDCWD, i->final_path,
245 AT_FDCWD, t,
246 (i->flags & IMPORT_BTRFS_QUOTA ? BTRFS_SNAPSHOT_QUOTA : 0)|
247 BTRFS_SNAPSHOT_FALLBACK_COPY|
248 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY|
249 BTRFS_SNAPSHOT_RECURSIVE);
250 else
251 r = copy_tree(i->final_path, t, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_HARDLINKS, NULL, NULL);
252 if (r < 0)
253 return log_error_errno(r, "Failed to create local image: %m");
254
255 r = install_file(AT_FDCWD, t,
256 AT_FDCWD, p,
257 (i->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) |
258 (i->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY : 0) |
259 (i->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0));
260 if (r < 0)
261 return log_error_errno(r, "Failed to install local image '%s': %m", p);
262
263 t = mfree(t);
264
265 log_info("Created new local image '%s'.", i->local);
266
267 if (FLAGS_SET(i->flags, IMPORT_PULL_SETTINGS)) {
268 const char *local_settings;
269 assert(i->settings_job);
270
271 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
272 if (r < 0)
273 return r;
274
275 local_settings = strjoina(i->image_root, "/", i->local, ".nspawn");
276
277 r = copy_file_atomic(
278 i->settings_path,
279 local_settings,
280 0664,
281 COPY_REFLINK |
282 (FLAGS_SET(i->flags, IMPORT_FORCE) ? COPY_REPLACE : 0) |
283 (FLAGS_SET(i->flags, IMPORT_SYNC) ? COPY_FSYNC_FULL : 0));
284 if (r == -EEXIST)
285 log_warning_errno(r, "Settings file %s already exists, not replacing.", local_settings);
286 else if (r == -ENOENT)
287 log_debug_errno(r, "Skipping creation of settings file, since none was found.");
288 else if (r < 0)
289 log_warning_errno(r, "Failed to copy settings files %s, ignoring: %m", local_settings);
290 else
291 log_info("Created new settings file %s.", local_settings);
292 }
293
294 return 0;
295 }
296
297 static bool tar_pull_is_done(TarPull *i) {
298 assert(i);
299 assert(i->tar_job);
300
301 if (!PULL_JOB_IS_COMPLETE(i->tar_job))
302 return false;
303 if (i->checksum_job && !PULL_JOB_IS_COMPLETE(i->checksum_job))
304 return false;
305 if (i->signature_job && !PULL_JOB_IS_COMPLETE(i->signature_job))
306 return false;
307 if (i->settings_job && !PULL_JOB_IS_COMPLETE(i->settings_job))
308 return false;
309
310 return true;
311 }
312
313 static void tar_pull_job_on_finished(PullJob *j) {
314 TarPull *i;
315 int r;
316
317 assert(j);
318 assert(j->userdata);
319
320 i = j->userdata;
321
322 if (j->error != 0) {
323 if (j == i->tar_job) {
324 if (j->error == ENOMEDIUM) /* HTTP 404 */
325 r = log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
326 else
327 r = log_error_errno(j->error, "Failed to retrieve image file.");
328 goto finish;
329 } else if (j == i->checksum_job) {
330 r = log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
331 goto finish;
332 } else if (j == i->signature_job)
333 log_debug_errno(j->error, "Signature job for %s failed, proceeding for now.", j->url);
334 else if (j == i->settings_job)
335 log_info_errno(j->error, "Settings file could not be retrieved, proceeding without.");
336 else
337 assert("unexpected job");
338 }
339
340 /* This is invoked if either the download completed successfully, or the download was skipped because
341 * we already have the etag. */
342
343 if (!tar_pull_is_done(i))
344 return;
345
346 if (i->signature_job && i->signature_job->error != 0) {
347 VerificationStyle style;
348
349 assert(i->checksum_job);
350
351 r = verification_style_from_url(i->checksum_job->url, &style);
352 if (r < 0) {
353 log_error_errno(r, "Failed to determine verification style from checksum URL: %m");
354 goto finish;
355 }
356
357 if (style == VERIFICATION_PER_DIRECTORY) { /* A failed signature file download only matters
358 * in per-directory verification mode, since only
359 * then the signature is detached, and thus a file
360 * of its own. */
361 r = log_error_errno(i->signature_job->error,
362 "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
363 goto finish;
364 }
365 }
366
367 pull_job_close_disk_fd(i->tar_job);
368 pull_job_close_disk_fd(i->settings_job);
369
370 if (i->tar_pid > 0) {
371 r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG);
372 if (r < 0)
373 goto finish;
374 if (r != EXIT_SUCCESS) {
375 r = -EIO;
376 goto finish;
377 }
378 }
379
380 if (!i->tar_job->etag_exists) {
381 /* This is a new download, verify it, and move it into place */
382
383 tar_pull_report_progress(i, TAR_VERIFYING);
384
385 r = pull_verify(i->verify,
386 i->checksum,
387 i->tar_job,
388 i->checksum_job,
389 i->signature_job,
390 i->settings_job,
391 /* roothash_job = */ NULL,
392 /* roothash_signature_job = */ NULL,
393 /* verity_job = */ NULL);
394 if (r < 0)
395 goto finish;
396 }
397
398 if (i->flags & IMPORT_DIRECT) {
399 assert(!i->settings_job);
400 assert(i->local);
401 assert(!i->temp_path);
402
403 tar_pull_report_progress(i, TAR_FINALIZING);
404
405 r = import_mangle_os_tree(i->local);
406 if (r < 0)
407 goto finish;
408
409 r = install_file(
410 AT_FDCWD, i->local,
411 AT_FDCWD, NULL,
412 (i->flags & IMPORT_READ_ONLY) ? INSTALL_READ_ONLY : 0 |
413 (i->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0));
414 if (r < 0) {
415 log_error_errno(r, "Failed to finalize '%s': %m", i->local);
416 goto finish;
417 }
418 } else {
419 r = tar_pull_determine_path(i, NULL, &i->final_path);
420 if (r < 0)
421 goto finish;
422
423 if (!i->tar_job->etag_exists) {
424 /* This is a new download, verify it, and move it into place */
425
426 assert(i->temp_path);
427 assert(i->final_path);
428
429 tar_pull_report_progress(i, TAR_FINALIZING);
430
431 r = import_mangle_os_tree(i->temp_path);
432 if (r < 0)
433 goto finish;
434
435 r = install_file(
436 AT_FDCWD, i->temp_path,
437 AT_FDCWD, i->final_path,
438 INSTALL_READ_ONLY|
439 (i->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0));
440 if (r < 0) {
441 log_error_errno(r, "Failed to rename to final image name to %s: %m", i->final_path);
442 goto finish;
443 }
444
445 i->temp_path = mfree(i->temp_path);
446
447 if (i->settings_job &&
448 i->settings_job->error == 0) {
449
450 /* Also move the settings file into place, if it exists. Note that we do so only if we also
451 * moved the tar file in place, to keep things strictly in sync. */
452 assert(i->settings_temp_path);
453
454 /* Regenerate final name for this auxiliary file, we might know the etag of the file now, and
455 * we should incorporate it in the file name if we can */
456 i->settings_path = mfree(i->settings_path);
457
458 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
459 if (r < 0)
460 goto finish;
461
462 r = install_file(
463 AT_FDCWD, i->settings_temp_path,
464 AT_FDCWD, i->settings_path,
465 INSTALL_READ_ONLY|
466 (i->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0));
467 if (r < 0) {
468 log_error_errno(r, "Failed to rename settings file to %s: %m", i->settings_path);
469 goto finish;
470 }
471
472 i->settings_temp_path = mfree(i->settings_temp_path);
473 }
474 }
475
476 tar_pull_report_progress(i, TAR_COPYING);
477
478 r = tar_pull_make_local_copy(i);
479 if (r < 0)
480 goto finish;
481 }
482
483 r = 0;
484
485 finish:
486 if (i->on_finished)
487 i->on_finished(i, r, i->userdata);
488 else
489 sd_event_exit(i->event, r);
490 }
491
492 static int tar_pull_job_on_open_disk_tar(PullJob *j) {
493 const char *where;
494 TarPull *i;
495 int r;
496
497 assert(j);
498 assert(j->userdata);
499
500 i = j->userdata;
501 assert(i->tar_job == j);
502 assert(i->tar_pid <= 0);
503
504 if (i->flags & IMPORT_DIRECT)
505 where = i->local;
506 else {
507 if (!i->temp_path) {
508 r = tempfn_random_child(i->image_root, "tar", &i->temp_path);
509 if (r < 0)
510 return log_oom();
511 }
512
513 where = i->temp_path;
514 }
515
516 (void) mkdir_parents_label(where, 0700);
517
518 if (FLAGS_SET(i->flags, IMPORT_DIRECT|IMPORT_FORCE))
519 (void) rm_rf(where, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
520
521 if (i->flags & IMPORT_BTRFS_SUBVOL)
522 r = btrfs_subvol_make_fallback(AT_FDCWD, where, 0755);
523 else
524 r = RET_NERRNO(mkdir(where, 0755));
525 if (r == -EEXIST && (i->flags & IMPORT_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise,
526 * because in that case our temporary path collided */
527 r = 0;
528 if (r < 0)
529 return log_error_errno(r, "Failed to create directory/subvolume %s: %m", where);
530 if (r > 0 && (i->flags & IMPORT_BTRFS_QUOTA)) { /* actually btrfs subvol */
531 if (!(i->flags & IMPORT_DIRECT))
532 (void) import_assign_pool_quota_and_warn(i->image_root);
533 (void) import_assign_pool_quota_and_warn(where);
534 }
535
536 j->disk_fd = import_fork_tar_x(where, &i->tar_pid);
537 if (j->disk_fd < 0)
538 return j->disk_fd;
539
540 return 0;
541 }
542
543 static int tar_pull_job_on_open_disk_settings(PullJob *j) {
544 TarPull *i;
545 int r;
546
547 assert(j);
548 assert(j->userdata);
549
550 i = j->userdata;
551 assert(i->settings_job == j);
552
553 if (!i->settings_temp_path) {
554 r = tempfn_random_child(i->image_root, "settings", &i->settings_temp_path);
555 if (r < 0)
556 return log_oom();
557 }
558
559 (void) mkdir_parents_label(i->settings_temp_path, 0700);
560
561 j->disk_fd = open(i->settings_temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
562 if (j->disk_fd < 0)
563 return log_error_errno(errno, "Failed to create %s: %m", i->settings_temp_path);
564
565 return 0;
566 }
567
568 static void tar_pull_job_on_progress(PullJob *j) {
569 TarPull *i;
570
571 assert(j);
572 assert(j->userdata);
573
574 i = j->userdata;
575
576 tar_pull_report_progress(i, TAR_DOWNLOADING);
577 }
578
579 int tar_pull_start(
580 TarPull *i,
581 const char *url,
582 const char *local,
583 ImportFlags flags,
584 ImportVerify verify,
585 const char *checksum) {
586
587 int r;
588
589 assert(i);
590 assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX);
591 assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0);
592 assert((verify < 0) || !checksum);
593 assert(!(flags & ~IMPORT_PULL_FLAGS_MASK_TAR));
594 assert(!(flags & IMPORT_PULL_SETTINGS) || !(flags & IMPORT_DIRECT));
595 assert(!(flags & IMPORT_PULL_SETTINGS) || !checksum);
596
597 if (!http_url_is_valid(url) && !file_url_is_valid(url))
598 return -EINVAL;
599
600 if (local && !pull_validate_local(local, flags))
601 return -EINVAL;
602
603 if (i->tar_job)
604 return -EBUSY;
605
606 r = free_and_strdup(&i->local, local);
607 if (r < 0)
608 return r;
609
610 r = free_and_strdup(&i->checksum, checksum);
611 if (r < 0)
612 return r;
613
614 i->flags = flags;
615 i->verify = verify;
616
617 /* Set up download job for TAR file */
618 r = pull_job_new(&i->tar_job, url, i->glue, i);
619 if (r < 0)
620 return r;
621
622 i->tar_job->on_finished = tar_pull_job_on_finished;
623 i->tar_job->on_open_disk = tar_pull_job_on_open_disk_tar;
624 i->tar_job->calc_checksum = checksum || IN_SET(verify, IMPORT_VERIFY_CHECKSUM, IMPORT_VERIFY_SIGNATURE);
625
626 if (!FLAGS_SET(flags, IMPORT_DIRECT)) {
627 r = pull_find_old_etags(url, i->image_root, DT_DIR, ".tar-", NULL, &i->tar_job->old_etags);
628 if (r < 0)
629 return r;
630 }
631
632 /* Set up download of checksum/signature files */
633 r = pull_make_verification_jobs(
634 &i->checksum_job,
635 &i->signature_job,
636 verify,
637 checksum,
638 url,
639 i->glue,
640 tar_pull_job_on_finished,
641 i);
642 if (r < 0)
643 return r;
644
645 /* Set up download job for the settings file (.nspawn) */
646 if (FLAGS_SET(flags, IMPORT_PULL_SETTINGS)) {
647 r = pull_make_auxiliary_job(
648 &i->settings_job,
649 url,
650 tar_strip_suffixes,
651 ".nspawn",
652 verify,
653 i->glue,
654 tar_pull_job_on_open_disk_settings,
655 tar_pull_job_on_finished,
656 i);
657 if (r < 0)
658 return r;
659 }
660
661 PullJob *j;
662 FOREACH_ARGUMENT(j,
663 i->tar_job,
664 i->checksum_job,
665 i->signature_job,
666 i->settings_job) {
667
668 if (!j)
669 continue;
670
671 j->on_progress = tar_pull_job_on_progress;
672 j->sync = FLAGS_SET(flags, IMPORT_SYNC);
673
674 r = pull_job_begin(j);
675 if (r < 0)
676 return r;
677 }
678
679 return 0;
680 }