]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/pull-tar.c
import: ignore non-successful HTTP codes for collecing image metadata
[thirdparty/systemd.git] / src / import / pull-tar.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
56ebfaf1 2
56ebfaf1 3#include <curl/curl.h>
cf0fbc49 4#include <sys/prctl.h>
56ebfaf1 5
7079cfef 6#include "sd-daemon.h"
07630cea 7
b5efdb8a 8#include "alloc-util.h"
56ebfaf1 9#include "btrfs-util.h"
07630cea
LP
10#include "copy.h"
11#include "curl-util.h"
3ffd4af2 12#include "fd-util.h"
f4f15635 13#include "fs-util.h"
07630cea
LP
14#include "hostname-util.h"
15#include "import-common.h"
16#include "import-util.h"
56ebfaf1
LP
17#include "macro.h"
18#include "mkdir.h"
26166c88 19#include "path-util.h"
25300b5a 20#include "process-util.h"
dc2c282b 21#include "pull-common.h"
07630cea 22#include "pull-job.h"
3ffd4af2 23#include "pull-tar.h"
07630cea
LP
24#include "rm-rf.h"
25#include "string-util.h"
26#include "strv.h"
e4de7287 27#include "tmpfile-util.h"
07630cea
LP
28#include "utf8.h"
29#include "util.h"
49cf4170 30#include "web-util.h"
56ebfaf1 31
7079cfef
LP
32typedef enum TarProgress {
33 TAR_DOWNLOADING,
34 TAR_VERIFYING,
35 TAR_FINALIZING,
36 TAR_COPYING,
37} TarProgress;
38
dc2c282b 39struct TarPull {
56ebfaf1
LP
40 sd_event *event;
41 CurlGlue *glue;
42
43 char *image_root;
44
dc2c282b 45 PullJob *tar_job;
9854730b 46 PullJob *settings_job;
dc2c282b
LP
47 PullJob *checksum_job;
48 PullJob *signature_job;
56ebfaf1 49
dc2c282b 50 TarPullFinished on_finished;
56ebfaf1
LP
51 void *userdata;
52
56ebfaf1
LP
53 char *local;
54 bool force_local;
9854730b 55 bool settings;
56ebfaf1
LP
56
57 pid_t tar_pid;
58
56ebfaf1 59 char *final_path;
9854730b
LP
60 char *temp_path;
61
62 char *settings_path;
63 char *settings_temp_path;
0100b6e1
LP
64
65 ImportVerify verify;
56ebfaf1
LP
66};
67
dc2c282b 68TarPull* tar_pull_unref(TarPull *i) {
56ebfaf1
LP
69 if (!i)
70 return NULL;
71
0100b6e1 72 if (i->tar_pid > 1) {
8b71fce8 73 (void) kill_and_sigcont(i->tar_pid, SIGKILL);
0100b6e1 74 (void) wait_for_terminate(i->tar_pid, NULL);
56ebfaf1
LP
75 }
76
dc2c282b 77 pull_job_unref(i->tar_job);
9854730b 78 pull_job_unref(i->settings_job);
dc2c282b
LP
79 pull_job_unref(i->checksum_job);
80 pull_job_unref(i->signature_job);
56ebfaf1
LP
81
82 curl_glue_unref(i->glue);
83 sd_event_unref(i->event);
84
85 if (i->temp_path) {
d9e2daaf 86 (void) rm_rf(i->temp_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
0d6e763b 87 free(i->temp_path);
56ebfaf1
LP
88 }
89
9854730b
LP
90 if (i->settings_temp_path) {
91 (void) unlink(i->settings_temp_path);
92 free(i->settings_temp_path);
93 }
94
56ebfaf1 95 free(i->final_path);
9854730b 96 free(i->settings_path);
56ebfaf1
LP
97 free(i->image_root);
98 free(i->local);
e0061812 99
6b430fdb 100 return mfree(i);
56ebfaf1
LP
101}
102
dc2c282b
LP
103int tar_pull_new(
104 TarPull **ret,
8b71fce8
LP
105 sd_event *event,
106 const char *image_root,
dc2c282b 107 TarPullFinished on_finished,
8b71fce8
LP
108 void *userdata) {
109
0d94088e
YW
110 _cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
111 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
dc2c282b 112 _cleanup_(tar_pull_unrefp) TarPull *i = NULL;
0d94088e 113 _cleanup_free_ char *root = NULL;
56ebfaf1
LP
114 int r;
115
116 assert(ret);
56ebfaf1 117
0d94088e
YW
118 root = strdup(image_root ?: "/var/lib/machines");
119 if (!root)
56ebfaf1
LP
120 return -ENOMEM;
121
9854730b 122 if (event)
0d94088e 123 e = sd_event_ref(event);
9854730b 124 else {
0d94088e 125 r = sd_event_default(&e);
9854730b
LP
126 if (r < 0)
127 return r;
128 }
56ebfaf1 129
0d94088e 130 r = curl_glue_new(&g, e);
56ebfaf1
LP
131 if (r < 0)
132 return r;
133
0d94088e
YW
134 i = new(TarPull, 1);
135 if (!i)
136 return -ENOMEM;
137
138 *i = (TarPull) {
139 .on_finished = on_finished,
140 .userdata = userdata,
141 .image_root = TAKE_PTR(root),
0d94088e
YW
142 .event = TAKE_PTR(e),
143 .glue = TAKE_PTR(g),
144 };
145
dc2c282b 146 i->glue->on_finished = pull_job_curl_on_finished;
56ebfaf1
LP
147 i->glue->userdata = i;
148
1cc6c93a 149 *ret = TAKE_PTR(i);
56ebfaf1
LP
150
151 return 0;
152}
153
dc2c282b 154static void tar_pull_report_progress(TarPull *i, TarProgress p) {
7079cfef
LP
155 unsigned percent;
156
157 assert(i);
158
159 switch (p) {
160
161 case TAR_DOWNLOADING: {
162 unsigned remain = 85;
163
164 percent = 0;
165
9854730b
LP
166 if (i->settings_job) {
167 percent += i->settings_job->progress_percent * 5 / 100;
168 remain -= 5;
169 }
170
7079cfef
LP
171 if (i->checksum_job) {
172 percent += i->checksum_job->progress_percent * 5 / 100;
173 remain -= 5;
174 }
175
176 if (i->signature_job) {
177 percent += i->signature_job->progress_percent * 5 / 100;
178 remain -= 5;
179 }
180
181 if (i->tar_job)
182 percent += i->tar_job->progress_percent * remain / 100;
183 break;
184 }
185
186 case TAR_VERIFYING:
187 percent = 85;
188 break;
189
190 case TAR_FINALIZING:
191 percent = 90;
192 break;
193
194 case TAR_COPYING:
195 percent = 95;
196 break;
197
198 default:
199 assert_not_reached("Unknown progress state");
200 }
201
202 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
203 log_debug("Combined progress %u%%", percent);
204}
205
91359193
LP
206static int tar_pull_determine_path(TarPull *i, const char *suffix, char **field) {
207 int r;
208
209 assert(i);
210 assert(field);
211
212 if (*field)
213 return 0;
214
215 assert(i->tar_job);
216
217 r = pull_make_path(i->tar_job->url, i->tar_job->etag, i->image_root, ".tar-", suffix, field);
218 if (r < 0)
219 return log_oom();
220
221 return 1;
222}
223
dc2c282b 224static int tar_pull_make_local_copy(TarPull *i) {
0d6e763b
LP
225 int r;
226
227 assert(i);
228 assert(i->tar_job);
229
230 if (!i->local)
231 return 0;
232
dc2c282b 233 r = pull_make_local_copy(i->final_path, i->image_root, i->local, i->force_local);
0100b6e1
LP
234 if (r < 0)
235 return r;
236
9854730b
LP
237 if (i->settings) {
238 const char *local_settings;
239 assert(i->settings_job);
240
91359193
LP
241 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
242 if (r < 0)
243 return r;
9854730b
LP
244
245 local_settings = strjoina(i->image_root, "/", i->local, ".nspawn");
246
8a016c74 247 r = copy_file_atomic(i->settings_path, local_settings, 0664, 0, 0, COPY_REFLINK | (i->force_local ? COPY_REPLACE : 0));
9854730b
LP
248 if (r == -EEXIST)
249 log_warning_errno(r, "Settings file %s already exists, not replacing.", local_settings);
33859a6b
LP
250 else if (r == -ENOENT)
251 log_debug_errno(r, "Skipping creation of settings file, since none was found.");
252 else if (r < 0)
79b6198b
LP
253 log_warning_errno(r, "Failed to copy settings files %s, ignoring: %m", local_settings);
254 else
33859a6b 255 log_info("Created new settings file %s.", local_settings);
9854730b
LP
256 }
257
0d6e763b
LP
258 return 0;
259}
260
dc2c282b 261static bool tar_pull_is_done(TarPull *i) {
8b71fce8
LP
262 assert(i);
263 assert(i->tar_job);
264
9854730b 265 if (!PULL_JOB_IS_COMPLETE(i->tar_job))
8b71fce8 266 return false;
9854730b 267 if (i->settings_job && !PULL_JOB_IS_COMPLETE(i->settings_job))
8b71fce8 268 return false;
9854730b
LP
269 if (i->checksum_job && !PULL_JOB_IS_COMPLETE(i->checksum_job))
270 return false;
271 if (i->signature_job && !PULL_JOB_IS_COMPLETE(i->signature_job))
8b71fce8
LP
272 return false;
273
274 return true;
275}
276
dc2c282b
LP
277static void tar_pull_job_on_finished(PullJob *j) {
278 TarPull *i;
56ebfaf1
LP
279 int r;
280
281 assert(j);
282 assert(j->userdata);
283
284 i = j->userdata;
9854730b
LP
285
286 if (j == i->settings_job) {
287 if (j->error != 0)
288 log_info_errno(j->error, "Settings file could not be retrieved, proceeding without.");
697be0be 289 } else if (j->error != 0 && j != i->signature_job) {
0100b6e1
LP
290 if (j == i->checksum_job)
291 log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
0100b6e1
LP
292 else
293 log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
294
56ebfaf1
LP
295 r = j->error;
296 goto finish;
297 }
298
c33e405f
LP
299 /* This is invoked if either the download completed successfully, or the download was skipped because
300 * we already have the etag. */
56ebfaf1 301
dc2c282b 302 if (!tar_pull_is_done(i))
0100b6e1
LP
303 return;
304
f14717a7
LP
305 if (i->signature_job && i->signature_job->error != 0) {
306 VerificationStyle style;
697be0be 307
f14717a7
LP
308 r = verification_style_from_url(i->checksum_job->url, &style);
309 if (r < 0) {
310 log_error_errno(r, "Failed to determine verification style from checksum URL: %m");
311 goto finish;
312 }
313
314 if (style == VERIFICATION_PER_DIRECTORY) { /* A failed signature file download only matters
315 * in per-directory verification mode, since only
316 * then the signature is detached, and thus a file
317 * of its own. */
318 log_error_errno(j->error, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
319 r = i->signature_job->error;
320 goto finish;
321 }
697be0be
TB
322 }
323
9854730b
LP
324 i->tar_job->disk_fd = safe_close(i->tar_job->disk_fd);
325 if (i->settings_job)
326 i->settings_job->disk_fd = safe_close(i->settings_job->disk_fd);
56ebfaf1 327
91359193
LP
328 r = tar_pull_determine_path(i, NULL, &i->final_path);
329 if (r < 0)
330 goto finish;
331
56ebfaf1 332 if (i->tar_pid > 0) {
7d4904fe 333 r = wait_for_terminate_and_check("tar", i->tar_pid, WAIT_LOG);
56ebfaf1
LP
334 i->tar_pid = 0;
335 if (r < 0)
336 goto finish;
b4a34311 337 if (r != EXIT_SUCCESS) {
9854730b
LP
338 r = -EIO;
339 goto finish;
340 }
56ebfaf1
LP
341 }
342
0100b6e1
LP
343 if (!i->tar_job->etag_exists) {
344 /* This is a new download, verify it, and move it into place */
345
dc2c282b 346 tar_pull_report_progress(i, TAR_VERIFYING);
7079cfef 347
91359193 348 r = pull_verify(i->tar_job, NULL, i->settings_job, i->checksum_job, i->signature_job);
0100b6e1
LP
349 if (r < 0)
350 goto finish;
351
dc2c282b 352 tar_pull_report_progress(i, TAR_FINALIZING);
7079cfef 353
c33e405f
LP
354 r = import_mangle_os_tree(i->temp_path);
355 if (r < 0)
356 goto finish;
357
b6e676ce 358 r = import_make_read_only(i->temp_path);
56ebfaf1
LP
359 if (r < 0)
360 goto finish;
361
f85ef957
AC
362 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
363 if (r < 0) {
dc38f65a 364 log_error_errno(r, "Failed to rename to final image name to %s: %m", i->final_path);
56ebfaf1
LP
365 goto finish;
366 }
56ebfaf1 367
9854730b
LP
368 i->temp_path = mfree(i->temp_path);
369
370 if (i->settings_job &&
91359193 371 i->settings_job->error == 0) {
9854730b 372
e0061812 373 /* Also move the settings file into place, if it exists. Note that we do so only if we also
91359193 374 * moved the tar file in place, to keep things strictly in sync. */
e0061812 375 assert(i->settings_temp_path);
91359193 376
e0061812
LP
377 /* Regenerate final name for this auxiliary file, we might know the etag of the file now, and
378 * we should incorporate it in the file name if we can */
91359193 379 i->settings_path = mfree(i->settings_path);
e0061812 380
91359193
LP
381 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
382 if (r < 0)
383 goto finish;
9854730b
LP
384
385 r = import_make_read_only(i->settings_temp_path);
386 if (r < 0)
387 goto finish;
388
389 r = rename_noreplace(AT_FDCWD, i->settings_temp_path, AT_FDCWD, i->settings_path);
390 if (r < 0) {
dc38f65a 391 log_error_errno(r, "Failed to rename settings file to %s: %m", i->settings_path);
9854730b
LP
392 goto finish;
393 }
394
395 i->settings_temp_path = mfree(i->settings_temp_path);
396 }
56ebfaf1
LP
397 }
398
dc2c282b 399 tar_pull_report_progress(i, TAR_COPYING);
7079cfef 400
dc2c282b 401 r = tar_pull_make_local_copy(i);
0d6e763b
LP
402 if (r < 0)
403 goto finish;
404
56ebfaf1
LP
405 r = 0;
406
407finish:
56ebfaf1
LP
408 if (i->on_finished)
409 i->on_finished(i, r, i->userdata);
410 else
411 sd_event_exit(i->event, r);
412}
413
9854730b 414static int tar_pull_job_on_open_disk_tar(PullJob *j) {
dc2c282b 415 TarPull *i;
56ebfaf1
LP
416 int r;
417
418 assert(j);
419 assert(j->userdata);
420
421 i = j->userdata;
8b71fce8 422 assert(i->tar_job == j);
8b71fce8 423 assert(i->tar_pid <= 0);
56ebfaf1 424
91359193
LP
425 if (!i->temp_path) {
426 r = tempfn_random_child(i->image_root, "tar", &i->temp_path);
427 if (r < 0)
428 return log_oom();
429 }
56ebfaf1
LP
430
431 mkdir_parents_label(i->temp_path, 0700);
432
82c4440d
LP
433 r = btrfs_subvol_make_fallback(i->temp_path, 0755);
434 if (r < 0)
435 return log_error_errno(r, "Failed to create directory/subvolume %s: %m", i->temp_path);
436 if (r > 0) /* actually btrfs subvol */
8c9cfc28 437 (void) import_assign_pool_quota_and_warn(i->temp_path);
56ebfaf1 438
587fec42 439 j->disk_fd = import_fork_tar_x(i->temp_path, &i->tar_pid);
2c140ded
LP
440 if (j->disk_fd < 0)
441 return j->disk_fd;
56ebfaf1
LP
442
443 return 0;
444}
445
9854730b
LP
446static int tar_pull_job_on_open_disk_settings(PullJob *j) {
447 TarPull *i;
448 int r;
449
450 assert(j);
451 assert(j->userdata);
452
453 i = j->userdata;
454 assert(i->settings_job == j);
9854730b 455
91359193
LP
456 if (!i->settings_temp_path) {
457 r = tempfn_random_child(i->image_root, "settings", &i->settings_temp_path);
458 if (r < 0)
459 return log_oom();
460 }
9854730b
LP
461
462 mkdir_parents_label(i->settings_temp_path, 0700);
463
464 j->disk_fd = open(i->settings_temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
465 if (j->disk_fd < 0)
466 return log_error_errno(errno, "Failed to create %s: %m", i->settings_temp_path);
467
468 return 0;
469}
470
dc2c282b
LP
471static void tar_pull_job_on_progress(PullJob *j) {
472 TarPull *i;
7079cfef
LP
473
474 assert(j);
475 assert(j->userdata);
476
477 i = j->userdata;
478
dc2c282b 479 tar_pull_report_progress(i, TAR_DOWNLOADING);
7079cfef
LP
480}
481
9854730b
LP
482int tar_pull_start(
483 TarPull *i,
484 const char *url,
485 const char *local,
486 bool force_local,
487 ImportVerify verify,
488 bool settings) {
489
56ebfaf1
LP
490 int r;
491
492 assert(i);
9854730b
LP
493 assert(verify < _IMPORT_VERIFY_MAX);
494 assert(verify >= 0);
56ebfaf1 495
56ebfaf1
LP
496 if (!http_url_is_valid(url))
497 return -EINVAL;
498
52ef5dd7 499 if (local && !hostname_is_valid(local, 0))
56ebfaf1
LP
500 return -EINVAL;
501
8b71fce8
LP
502 if (i->tar_job)
503 return -EBUSY;
504
56ebfaf1
LP
505 r = free_and_strdup(&i->local, local);
506 if (r < 0)
507 return r;
9854730b 508
56ebfaf1 509 i->force_local = force_local;
0100b6e1 510 i->verify = verify;
9854730b 511 i->settings = settings;
56ebfaf1 512
9854730b 513 /* Set up download job for TAR file */
dc2c282b 514 r = pull_job_new(&i->tar_job, url, i->glue, i);
56ebfaf1
LP
515 if (r < 0)
516 return r;
517
dc2c282b 518 i->tar_job->on_finished = tar_pull_job_on_finished;
9854730b 519 i->tar_job->on_open_disk = tar_pull_job_on_open_disk_tar;
dc2c282b 520 i->tar_job->on_progress = tar_pull_job_on_progress;
0100b6e1 521 i->tar_job->calc_checksum = verify != IMPORT_VERIFY_NO;
56ebfaf1 522
dc2c282b 523 r = pull_find_old_etags(url, i->image_root, DT_DIR, ".tar-", NULL, &i->tar_job->old_etags);
56ebfaf1
LP
524 if (r < 0)
525 return r;
526
9854730b
LP
527 /* Set up download job for the settings file (.nspawn) */
528 if (settings) {
91359193 529 r = pull_make_auxiliary_job(&i->settings_job, url, tar_strip_suffixes, ".nspawn", i->glue, tar_pull_job_on_finished, i);
9854730b
LP
530 if (r < 0)
531 return r;
532
533 i->settings_job->on_open_disk = tar_pull_job_on_open_disk_settings;
534 i->settings_job->on_progress = tar_pull_job_on_progress;
535 i->settings_job->calc_checksum = verify != IMPORT_VERIFY_NO;
9854730b
LP
536 }
537
538 /* Set up download of checksum/signature files */
dc2c282b 539 r = pull_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, tar_pull_job_on_finished, i);
0100b6e1
LP
540 if (r < 0)
541 return r;
542
dc2c282b 543 r = pull_job_begin(i->tar_job);
0100b6e1
LP
544 if (r < 0)
545 return r;
546
9854730b
LP
547 if (i->settings_job) {
548 r = pull_job_begin(i->settings_job);
549 if (r < 0)
550 return r;
551 }
552
0100b6e1 553 if (i->checksum_job) {
dc2c282b 554 i->checksum_job->on_progress = tar_pull_job_on_progress;
f14717a7 555 i->checksum_job->on_not_found = pull_job_restart_with_sha256sum;
7079cfef 556
dc2c282b 557 r = pull_job_begin(i->checksum_job);
0100b6e1
LP
558 if (r < 0)
559 return r;
560 }
561
562 if (i->signature_job) {
dc2c282b 563 i->signature_job->on_progress = tar_pull_job_on_progress;
7079cfef 564
dc2c282b 565 r = pull_job_begin(i->signature_job);
0100b6e1
LP
566 if (r < 0)
567 return r;
568 }
569
570 return 0;
56ebfaf1 571}