| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-daemon.h" |
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "copy.h" |
| 10 |
#include "curl-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fs-util.h" |
| 13 |
#include "import-common.h" |
| 14 |
#include "import-util.h" |
| 15 |
#include "install-file.h" |
| 16 |
#include "iovec-util.h" |
| 17 |
#include "log.h" |
| 18 |
#include "mkdir.h" |
| 19 |
#include "pull-common.h" |
| 20 |
#include "pull-job.h" |
| 21 |
#include "pull-raw.h" |
| 22 |
#include "qcow2-util.h" |
| 23 |
#include "string-util.h" |
| 24 |
#include "tmpfile-util.h" |
| 25 |
#include "web-util.h" |
| 26 |
|
| 27 |
typedef enum RawProgress { |
| 28 |
RAW_DOWNLOADING, |
| 29 |
RAW_VERIFYING, |
| 30 |
RAW_UNPACKING, |
| 31 |
RAW_FINALIZING, |
| 32 |
RAW_COPYING, |
| 33 |
} RawProgress; |
| 34 |
|
| 35 |
typedef struct RawPull { |
| 36 |
sd_event *event; |
| 37 |
CurlGlue *glue; |
| 38 |
|
| 39 |
ImportFlags flags; |
| 40 |
ImportVerify verify; |
| 41 |
char *image_root; |
| 42 |
|
| 43 |
uint64_t offset; |
| 44 |
|
| 45 |
PullJob *raw_job; |
| 46 |
PullJob *checksum_job; |
| 47 |
PullJob *signature_job; |
| 48 |
PullJob *settings_job; |
| 49 |
PullJob *roothash_job; |
| 50 |
PullJob *roothash_signature_job; |
| 51 |
PullJob *verity_job; |
| 52 |
|
| 53 |
RawPullFinished on_finished; |
| 54 |
void *userdata; |
| 55 |
|
| 56 |
char *local; /* In PULL_DIRECT mode the path we are supposed to place things in, otherwise the |
| 57 |
* image name of the final copy we make */ |
| 58 |
|
| 59 |
char *final_path; |
| 60 |
char *temp_path; |
| 61 |
|
| 62 |
char *settings_path; |
| 63 |
char *settings_temp_path; |
| 64 |
|
| 65 |
char *roothash_path; |
| 66 |
char *roothash_temp_path; |
| 67 |
|
| 68 |
char *roothash_signature_path; |
| 69 |
char *roothash_signature_temp_path; |
| 70 |
|
| 71 |
char *verity_path; |
| 72 |
char *verity_temp_path; |
| 73 |
} RawPull; |
| 74 |
|
| 75 |
RawPull* raw_pull_unref(RawPull *p) { |
| 76 |
if (!p) |
| 77 |
return NULL; |
| 78 |
|
| 79 |
pull_job_unref(p->raw_job); |
| 80 |
pull_job_unref(p->checksum_job); |
| 81 |
pull_job_unref(p->signature_job); |
| 82 |
pull_job_unref(p->settings_job); |
| 83 |
pull_job_unref(p->roothash_job); |
| 84 |
pull_job_unref(p->roothash_signature_job); |
| 85 |
pull_job_unref(p->verity_job); |
| 86 |
|
| 87 |
curl_glue_unref(p->glue); |
| 88 |
sd_event_unref(p->event); |
| 89 |
|
| 90 |
unlink_and_free(p->temp_path); |
| 91 |
unlink_and_free(p->settings_temp_path); |
| 92 |
unlink_and_free(p->roothash_temp_path); |
| 93 |
unlink_and_free(p->roothash_signature_temp_path); |
| 94 |
unlink_and_free(p->verity_temp_path); |
| 95 |
|
| 96 |
free(p->final_path); |
| 97 |
free(p->settings_path); |
| 98 |
free(p->roothash_path); |
| 99 |
free(p->roothash_signature_path); |
| 100 |
free(p->verity_path); |
| 101 |
free(p->image_root); |
| 102 |
free(p->local); |
| 103 |
|
| 104 |
return mfree(p); |
| 105 |
} |
| 106 |
|
| 107 |
int raw_pull_new( |
| 108 |
RawPull **ret, |
| 109 |
sd_event *event, |
| 110 |
const char *image_root, |
| 111 |
RawPullFinished on_finished, |
| 112 |
void *userdata) { |
| 113 |
|
| 114 |
_cleanup_(curl_glue_unrefp) CurlGlue *g = NULL; |
| 115 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 116 |
_cleanup_(raw_pull_unrefp) RawPull *p = NULL; |
| 117 |
_cleanup_free_ char *root = NULL; |
| 118 |
int r; |
| 119 |
|
| 120 |
assert(ret); |
| 121 |
assert(image_root); |
| 122 |
|
| 123 |
root = strdup(image_root); |
| 124 |
if (!root) |
| 125 |
return -ENOMEM; |
| 126 |
|
| 127 |
if (event) |
| 128 |
e = sd_event_ref(event); |
| 129 |
else { |
| 130 |
r = sd_event_default(&e); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
} |
| 134 |
|
| 135 |
r = curl_glue_new(&g, e); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
p = new(RawPull, 1); |
| 140 |
if (!p) |
| 141 |
return -ENOMEM; |
| 142 |
|
| 143 |
*p = (RawPull) { |
| 144 |
.on_finished = on_finished, |
| 145 |
.userdata = userdata, |
| 146 |
.image_root = TAKE_PTR(root), |
| 147 |
.event = TAKE_PTR(e), |
| 148 |
.glue = TAKE_PTR(g), |
| 149 |
.offset = UINT64_MAX, |
| 150 |
}; |
| 151 |
|
| 152 |
*ret = TAKE_PTR(p); |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
static void raw_pull_report_progress(RawPull *p, RawProgress progress) { |
| 158 |
unsigned percent; |
| 159 |
|
| 160 |
assert(p); |
| 161 |
|
| 162 |
switch (progress) { |
| 163 |
|
| 164 |
case RAW_DOWNLOADING: { |
| 165 |
unsigned remain = 80; |
| 166 |
|
| 167 |
percent = 0; |
| 168 |
|
| 169 |
if (p->checksum_job) { |
| 170 |
percent += p->checksum_job->progress_percent * 5 / 100; |
| 171 |
remain -= 5; |
| 172 |
} |
| 173 |
|
| 174 |
if (p->signature_job) { |
| 175 |
percent += p->signature_job->progress_percent * 5 / 100; |
| 176 |
remain -= 5; |
| 177 |
} |
| 178 |
|
| 179 |
if (p->settings_job) { |
| 180 |
percent += p->settings_job->progress_percent * 5 / 100; |
| 181 |
remain -= 5; |
| 182 |
} |
| 183 |
|
| 184 |
if (p->roothash_job) { |
| 185 |
percent += p->roothash_job->progress_percent * 5 / 100; |
| 186 |
remain -= 5; |
| 187 |
} |
| 188 |
|
| 189 |
if (p->roothash_signature_job) { |
| 190 |
percent += p->roothash_signature_job->progress_percent * 5 / 100; |
| 191 |
remain -= 5; |
| 192 |
} |
| 193 |
|
| 194 |
if (p->verity_job) { |
| 195 |
percent += p->verity_job->progress_percent * 10 / 100; |
| 196 |
remain -= 10; |
| 197 |
} |
| 198 |
|
| 199 |
if (p->raw_job) |
| 200 |
percent += p->raw_job->progress_percent * remain / 100; |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
case RAW_VERIFYING: |
| 205 |
percent = 80; |
| 206 |
break; |
| 207 |
|
| 208 |
case RAW_UNPACKING: |
| 209 |
percent = 85; |
| 210 |
break; |
| 211 |
|
| 212 |
case RAW_FINALIZING: |
| 213 |
percent = 90; |
| 214 |
break; |
| 215 |
|
| 216 |
case RAW_COPYING: |
| 217 |
percent = 95; |
| 218 |
break; |
| 219 |
|
| 220 |
default: |
| 221 |
assert_not_reached(); |
| 222 |
} |
| 223 |
|
| 224 |
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent); |
| 225 |
log_debug("Combined progress %u%%", percent); |
| 226 |
} |
| 227 |
|
| 228 |
static int raw_pull_maybe_convert_qcow2(RawPull *p) { |
| 229 |
_cleanup_(unlink_and_freep) char *t = NULL; |
| 230 |
_cleanup_close_ int converted_fd = -EBADF; |
| 231 |
_cleanup_free_ char *f = NULL; |
| 232 |
int r; |
| 233 |
|
| 234 |
assert(p); |
| 235 |
assert(p->raw_job); |
| 236 |
assert(!FLAGS_SET(p->flags, IMPORT_DIRECT)); |
| 237 |
|
| 238 |
if (!FLAGS_SET(p->flags, IMPORT_CONVERT_QCOW2)) |
| 239 |
return 0; |
| 240 |
|
| 241 |
assert(p->final_path); |
| 242 |
assert(p->raw_job->close_disk_fd); |
| 243 |
|
| 244 |
r = qcow2_detect(p->raw_job->disk_fd); |
| 245 |
if (r < 0) |
| 246 |
return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m"); |
| 247 |
if (r == 0) |
| 248 |
return 0; |
| 249 |
|
| 250 |
/* This is a QCOW2 image, let's convert it */ |
| 251 |
r = tempfn_random(p->final_path, NULL, &f); |
| 252 |
if (r < 0) |
| 253 |
return log_oom(); |
| 254 |
|
| 255 |
converted_fd = open(f, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); |
| 256 |
if (converted_fd < 0) |
| 257 |
return log_error_errno(errno, "Failed to create %s: %m", f); |
| 258 |
|
| 259 |
t = TAKE_PTR(f); |
| 260 |
|
| 261 |
(void) import_set_nocow_and_log(converted_fd, t); |
| 262 |
|
| 263 |
log_info("Unpacking QCOW2 file."); |
| 264 |
|
| 265 |
r = qcow2_convert(p->raw_job->disk_fd, converted_fd); |
| 266 |
if (r < 0) |
| 267 |
return log_error_errno(r, "Failed to convert qcow2 image: %m"); |
| 268 |
|
| 269 |
unlink_and_free(p->temp_path); |
| 270 |
p->temp_path = TAKE_PTR(t); |
| 271 |
close_and_replace(p->raw_job->disk_fd, converted_fd); |
| 272 |
|
| 273 |
return 1; |
| 274 |
} |
| 275 |
|
| 276 |
static int raw_pull_determine_path( |
| 277 |
RawPull *p, |
| 278 |
const char *suffix, |
| 279 |
char **field /* input + output (!) */) { |
| 280 |
int r; |
| 281 |
|
| 282 |
assert(p); |
| 283 |
assert(field); |
| 284 |
|
| 285 |
if (*field) |
| 286 |
return 0; |
| 287 |
|
| 288 |
assert(p->raw_job); |
| 289 |
|
| 290 |
r = pull_make_path(p->raw_job->url, p->raw_job->etag, p->image_root, ".raw-", suffix, field); |
| 291 |
if (r < 0) |
| 292 |
return log_oom(); |
| 293 |
|
| 294 |
return 1; |
| 295 |
} |
| 296 |
|
| 297 |
static int raw_pull_copy_auxiliary_file( |
| 298 |
RawPull *p, |
| 299 |
const char *suffix, |
| 300 |
char **path /* input + output (!) */) { |
| 301 |
|
| 302 |
_cleanup_free_ char *local = NULL; |
| 303 |
int r; |
| 304 |
|
| 305 |
assert(p); |
| 306 |
assert(suffix); |
| 307 |
assert(path); |
| 308 |
|
| 309 |
r = raw_pull_determine_path(p, suffix, path); |
| 310 |
if (r < 0) |
| 311 |
return r; |
| 312 |
|
| 313 |
local = strjoin(p->image_root, "/", p->local, suffix); |
| 314 |
if (!local) |
| 315 |
return log_oom(); |
| 316 |
|
| 317 |
if (FLAGS_SET(p->flags, IMPORT_PULL_KEEP_DOWNLOAD)) |
| 318 |
r = copy_file_atomic( |
| 319 |
*path, |
| 320 |
local, |
| 321 |
0644, |
| 322 |
COPY_REFLINK | |
| 323 |
(FLAGS_SET(p->flags, IMPORT_FORCE) ? COPY_REPLACE : 0) | |
| 324 |
(FLAGS_SET(p->flags, IMPORT_SYNC) ? COPY_FSYNC_FULL : 0)); |
| 325 |
else |
| 326 |
r = install_file(AT_FDCWD, *path, |
| 327 |
AT_FDCWD, local, |
| 328 |
(p->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) | |
| 329 |
(p->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0)); |
| 330 |
if (r == -EEXIST) |
| 331 |
log_warning_errno(r, "File %s already exists, not replacing.", local); |
| 332 |
else if (r == -ENOENT) |
| 333 |
log_debug_errno(r, "Skipping creation of auxiliary file, since none was found."); |
| 334 |
else if (r < 0) |
| 335 |
log_warning_errno(r, "Failed to install file %s, ignoring: %m", local); |
| 336 |
else |
| 337 |
log_info("Created new file %s.", local); |
| 338 |
|
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
static int raw_pull_make_local_copy(RawPull *p) { |
| 343 |
_cleanup_(unlink_and_freep) char *tp = NULL; |
| 344 |
_cleanup_free_ char *path = NULL; |
| 345 |
int r; |
| 346 |
|
| 347 |
assert(p); |
| 348 |
assert(p->raw_job); |
| 349 |
assert(!FLAGS_SET(p->flags, IMPORT_DIRECT)); |
| 350 |
|
| 351 |
if (!p->local) |
| 352 |
return 0; |
| 353 |
|
| 354 |
if (p->raw_job->etag_exists) { |
| 355 |
/* We have downloaded this one previously, reopen it */ |
| 356 |
|
| 357 |
assert(p->raw_job->disk_fd < 0); |
| 358 |
|
| 359 |
p->raw_job->disk_fd = open(p->final_path, O_RDONLY|O_NOCTTY|O_CLOEXEC); |
| 360 |
if (p->raw_job->disk_fd < 0) |
| 361 |
return log_error_errno(errno, "Failed to open vendor image: %m"); |
| 362 |
} else { |
| 363 |
/* We freshly downloaded the image, use it */ |
| 364 |
|
| 365 |
assert(p->raw_job->disk_fd >= 0); |
| 366 |
assert(p->offset == UINT64_MAX); |
| 367 |
|
| 368 |
if (lseek(p->raw_job->disk_fd, 0, SEEK_SET) < 0) |
| 369 |
return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m"); |
| 370 |
} |
| 371 |
|
| 372 |
path = strjoin(p->image_root, "/", p->local, ".raw"); |
| 373 |
if (!path) |
| 374 |
return log_oom(); |
| 375 |
|
| 376 |
const char *source; |
| 377 |
if (FLAGS_SET(p->flags, IMPORT_PULL_KEEP_DOWNLOAD)) { |
| 378 |
_cleanup_close_ int dfd = -EBADF; |
| 379 |
_cleanup_free_ char *f = NULL; |
| 380 |
|
| 381 |
r = tempfn_random(path, NULL, &f); |
| 382 |
if (r < 0) |
| 383 |
return log_oom(); |
| 384 |
|
| 385 |
dfd = open(f, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); |
| 386 |
if (dfd < 0) |
| 387 |
return log_error_errno(errno, "Failed to create writable copy of image: %m"); |
| 388 |
|
| 389 |
tp = TAKE_PTR(f); |
| 390 |
|
| 391 |
/* Turn off COW writing. This should greatly improve performance on COW file systems like btrfs, |
| 392 |
* since it reduces fragmentation caused by not allowing in-place writes. */ |
| 393 |
(void) import_set_nocow_and_log(dfd, tp); |
| 394 |
|
| 395 |
r = copy_bytes(p->raw_job->disk_fd, dfd, UINT64_MAX, COPY_REFLINK); |
| 396 |
if (r < 0) |
| 397 |
return log_error_errno(r, "Failed to make writable copy of image: %m"); |
| 398 |
|
| 399 |
(void) copy_times(p->raw_job->disk_fd, dfd, COPY_CRTIME); |
| 400 |
(void) copy_xattr(p->raw_job->disk_fd, NULL, dfd, NULL, 0); |
| 401 |
|
| 402 |
dfd = safe_close(dfd); |
| 403 |
|
| 404 |
source = tp; |
| 405 |
} else |
| 406 |
source = p->final_path; |
| 407 |
|
| 408 |
r = install_file(AT_FDCWD, source, |
| 409 |
AT_FDCWD, path, |
| 410 |
(p->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) | |
| 411 |
(p->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY : 0) | |
| 412 |
(p->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0)); |
| 413 |
if (r < 0) |
| 414 |
return log_error_errno(r, "Failed to move local image into place '%s': %m", path); |
| 415 |
|
| 416 |
tp = mfree(tp); |
| 417 |
|
| 418 |
log_info("Created new local image '%s'.", p->local); |
| 419 |
|
| 420 |
if (FLAGS_SET(p->flags, IMPORT_PULL_SETTINGS)) { |
| 421 |
r = raw_pull_copy_auxiliary_file(p, ".nspawn", &p->settings_path); |
| 422 |
if (r < 0) |
| 423 |
return r; |
| 424 |
} |
| 425 |
|
| 426 |
if (FLAGS_SET(p->flags, IMPORT_PULL_ROOTHASH)) { |
| 427 |
r = raw_pull_copy_auxiliary_file(p, ".roothash", &p->roothash_path); |
| 428 |
if (r < 0) |
| 429 |
return r; |
| 430 |
} |
| 431 |
|
| 432 |
if (FLAGS_SET(p->flags, IMPORT_PULL_ROOTHASH_SIGNATURE)) { |
| 433 |
r = raw_pull_copy_auxiliary_file(p, ".roothash.p7s", &p->roothash_signature_path); |
| 434 |
if (r < 0) |
| 435 |
return r; |
| 436 |
} |
| 437 |
|
| 438 |
if (FLAGS_SET(p->flags, IMPORT_PULL_VERITY)) { |
| 439 |
r = raw_pull_copy_auxiliary_file(p, ".verity", &p->verity_path); |
| 440 |
if (r < 0) |
| 441 |
return r; |
| 442 |
} |
| 443 |
|
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
static bool raw_pull_is_done(RawPull *p) { |
| 448 |
assert(p); |
| 449 |
assert(p->raw_job); |
| 450 |
|
| 451 |
if (!PULL_JOB_IS_COMPLETE(p->raw_job)) |
| 452 |
return false; |
| 453 |
if (p->checksum_job && !PULL_JOB_IS_COMPLETE(p->checksum_job)) |
| 454 |
return false; |
| 455 |
if (p->signature_job && !PULL_JOB_IS_COMPLETE(p->signature_job)) |
| 456 |
return false; |
| 457 |
if (p->settings_job && !PULL_JOB_IS_COMPLETE(p->settings_job)) |
| 458 |
return false; |
| 459 |
if (p->roothash_job && !PULL_JOB_IS_COMPLETE(p->roothash_job)) |
| 460 |
return false; |
| 461 |
if (p->roothash_signature_job && !PULL_JOB_IS_COMPLETE(p->roothash_signature_job)) |
| 462 |
return false; |
| 463 |
if (p->verity_job && !PULL_JOB_IS_COMPLETE(p->verity_job)) |
| 464 |
return false; |
| 465 |
|
| 466 |
return true; |
| 467 |
} |
| 468 |
|
| 469 |
static int raw_pull_rename_auxiliary_file( |
| 470 |
RawPull *p, |
| 471 |
const char *suffix, |
| 472 |
char **temp_path, |
| 473 |
char **path) { |
| 474 |
|
| 475 |
int r; |
| 476 |
|
| 477 |
assert(p); |
| 478 |
assert(path); |
| 479 |
assert(temp_path); |
| 480 |
assert(*temp_path); |
| 481 |
assert(suffix); |
| 482 |
|
| 483 |
/* Regenerate final name for this auxiliary file, we might know the etag of the file now, and we should |
| 484 |
* incorporate it in the file name if we can */ |
| 485 |
*path = mfree(*path); |
| 486 |
r = raw_pull_determine_path(p, suffix, path); |
| 487 |
if (r < 0) |
| 488 |
return r; |
| 489 |
|
| 490 |
r = install_file( |
| 491 |
AT_FDCWD, *temp_path, |
| 492 |
AT_FDCWD, *path, |
| 493 |
INSTALL_READ_ONLY| |
| 494 |
(p->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0)); |
| 495 |
if (r < 0) |
| 496 |
return log_error_errno(r, "Failed to move '%s' into place: %m", *path); |
| 497 |
|
| 498 |
*temp_path = mfree(*temp_path); |
| 499 |
return 1; |
| 500 |
} |
| 501 |
|
| 502 |
static void raw_pull_job_on_finished(PullJob *j) { |
| 503 |
int r; |
| 504 |
|
| 505 |
assert(j); |
| 506 |
RawPull *p = ASSERT_PTR(j->userdata); |
| 507 |
|
| 508 |
if (j->error != 0) { |
| 509 |
/* Only the main job and the checksum job are fatal if they fail. The other fails are just |
| 510 |
* "decoration", that we'll download if we can. The signature job isn't fatal here because we |
| 511 |
* might not actually need it in case Suse style signatures are used, that are inline in the |
| 512 |
* checksum file. */ |
| 513 |
|
| 514 |
if (j == p->raw_job) { |
| 515 |
if (j->error == -ENOMEDIUM) /* HTTP 404 */ |
| 516 |
r = log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)"); |
| 517 |
else |
| 518 |
r = log_error_errno(j->error, "Failed to retrieve image file."); |
| 519 |
goto finish; |
| 520 |
} else if (j == p->checksum_job) { |
| 521 |
r = log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)"); |
| 522 |
goto finish; |
| 523 |
} else if (j == p->signature_job) |
| 524 |
log_debug_errno(j->error, "Signature job for %s failed, proceeding for now.", j->url); |
| 525 |
else if (j == p->settings_job) |
| 526 |
log_info_errno(j->error, "Settings file could not be retrieved, proceeding without."); |
| 527 |
else if (j == p->roothash_job) |
| 528 |
log_info_errno(j->error, "Root hash file could not be retrieved, proceeding without."); |
| 529 |
else if (j == p->roothash_signature_job) |
| 530 |
log_info_errno(j->error, "Root hash signature file could not be retrieved, proceeding without."); |
| 531 |
else if (j == p->verity_job) |
| 532 |
log_info_errno(j->error, "Verity integrity file could not be retrieved, proceeding without."); |
| 533 |
else |
| 534 |
assert_not_reached(); |
| 535 |
} |
| 536 |
|
| 537 |
/* This is invoked if either the download completed successfully, or the download was skipped because |
| 538 |
* we already have the etag. In this case ->etag_exists is true. |
| 539 |
* |
| 540 |
* We only do something when we got all files */ |
| 541 |
|
| 542 |
if (!raw_pull_is_done(p)) |
| 543 |
return; |
| 544 |
|
| 545 |
if (p->signature_job && p->signature_job->error != 0) { |
| 546 |
assert(p->checksum_job || p->raw_job); |
| 547 |
|
| 548 |
r = log_error_errno(p->signature_job->error, |
| 549 |
"Failed to retrieve signature file, cannot verify. (Try --verify=no?)"); |
| 550 |
goto finish; |
| 551 |
} |
| 552 |
|
| 553 |
PullJob *jj; |
| 554 |
/* Let's close these auxiliary files now, we don't need access to them anymore. */ |
| 555 |
FOREACH_ARGUMENT(jj, p->settings_job, p->roothash_job, p->roothash_signature_job, p->verity_job) |
| 556 |
pull_job_close_disk_fd(jj); |
| 557 |
|
| 558 |
if (!p->raw_job->etag_exists) { |
| 559 |
raw_pull_report_progress(p, RAW_VERIFYING); |
| 560 |
|
| 561 |
r = pull_verify(p->verify, |
| 562 |
p->raw_job, |
| 563 |
p->checksum_job, |
| 564 |
p->signature_job, |
| 565 |
p->settings_job, |
| 566 |
p->roothash_job, |
| 567 |
p->roothash_signature_job, |
| 568 |
p->verity_job); |
| 569 |
if (r < 0) |
| 570 |
goto finish; |
| 571 |
} |
| 572 |
|
| 573 |
if (p->flags & IMPORT_DIRECT) { |
| 574 |
assert(!p->settings_job); |
| 575 |
assert(!p->roothash_job); |
| 576 |
assert(!p->roothash_signature_job); |
| 577 |
assert(!p->verity_job); |
| 578 |
|
| 579 |
raw_pull_report_progress(p, RAW_FINALIZING); |
| 580 |
|
| 581 |
if (p->local) { |
| 582 |
r = install_file(AT_FDCWD, p->local, |
| 583 |
AT_FDCWD, NULL, |
| 584 |
((p->flags & IMPORT_READ_ONLY) && p->offset == UINT64_MAX ? INSTALL_READ_ONLY : 0) | |
| 585 |
(p->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0)); |
| 586 |
if (r < 0) { |
| 587 |
log_error_errno(r, "Failed to finalize raw file to '%s': %m", p->local); |
| 588 |
goto finish; |
| 589 |
} |
| 590 |
} |
| 591 |
} else { |
| 592 |
r = raw_pull_determine_path(p, ".raw", &p->final_path); |
| 593 |
if (r < 0) |
| 594 |
goto finish; |
| 595 |
|
| 596 |
if (!p->raw_job->etag_exists) { |
| 597 |
/* This is a new download, verify it, and move it into place */ |
| 598 |
|
| 599 |
assert(p->temp_path); |
| 600 |
assert(p->final_path); |
| 601 |
|
| 602 |
raw_pull_report_progress(p, RAW_UNPACKING); |
| 603 |
|
| 604 |
r = raw_pull_maybe_convert_qcow2(p); |
| 605 |
if (r < 0) |
| 606 |
goto finish; |
| 607 |
|
| 608 |
raw_pull_report_progress(p, RAW_FINALIZING); |
| 609 |
|
| 610 |
r = install_file(AT_FDCWD, p->temp_path, |
| 611 |
AT_FDCWD, p->final_path, |
| 612 |
(p->flags & IMPORT_PULL_KEEP_DOWNLOAD ? INSTALL_READ_ONLY : 0) | |
| 613 |
(p->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0)); |
| 614 |
if (r < 0) { |
| 615 |
log_error_errno(r, "Failed to move raw file to '%s': %m", p->final_path); |
| 616 |
goto finish; |
| 617 |
} |
| 618 |
|
| 619 |
p->temp_path = mfree(p->temp_path); |
| 620 |
|
| 621 |
if (p->settings_job && |
| 622 |
p->settings_job->error == 0) { |
| 623 |
r = raw_pull_rename_auxiliary_file(p, ".nspawn", &p->settings_temp_path, &p->settings_path); |
| 624 |
if (r < 0) |
| 625 |
goto finish; |
| 626 |
} |
| 627 |
|
| 628 |
if (p->roothash_job && |
| 629 |
p->roothash_job->error == 0) { |
| 630 |
r = raw_pull_rename_auxiliary_file(p, ".roothash", &p->roothash_temp_path, &p->roothash_path); |
| 631 |
if (r < 0) |
| 632 |
goto finish; |
| 633 |
} |
| 634 |
|
| 635 |
if (p->roothash_signature_job && |
| 636 |
p->roothash_signature_job->error == 0) { |
| 637 |
r = raw_pull_rename_auxiliary_file(p, ".roothash.p7s", &p->roothash_signature_temp_path, &p->roothash_signature_path); |
| 638 |
if (r < 0) |
| 639 |
goto finish; |
| 640 |
} |
| 641 |
|
| 642 |
if (p->verity_job && |
| 643 |
p->verity_job->error == 0) { |
| 644 |
r = raw_pull_rename_auxiliary_file(p, ".verity", &p->verity_temp_path, &p->verity_path); |
| 645 |
if (r < 0) |
| 646 |
goto finish; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
raw_pull_report_progress(p, RAW_COPYING); |
| 651 |
|
| 652 |
r = raw_pull_make_local_copy(p); |
| 653 |
if (r < 0) |
| 654 |
goto finish; |
| 655 |
} |
| 656 |
|
| 657 |
r = 0; |
| 658 |
|
| 659 |
finish: |
| 660 |
if (p->on_finished) |
| 661 |
p->on_finished(p, r, p->userdata); |
| 662 |
else |
| 663 |
sd_event_exit(p->event, r); |
| 664 |
} |
| 665 |
|
| 666 |
static int raw_pull_job_on_open_disk_generic( |
| 667 |
RawPull *p, |
| 668 |
PullJob *j, |
| 669 |
const char *extra, |
| 670 |
char **temp_path /* input + output */) { |
| 671 |
|
| 672 |
int r; |
| 673 |
|
| 674 |
assert(p); |
| 675 |
assert(j); |
| 676 |
assert(extra); |
| 677 |
assert(temp_path); |
| 678 |
|
| 679 |
assert(!FLAGS_SET(p->flags, IMPORT_DIRECT)); |
| 680 |
|
| 681 |
if (!*temp_path) { |
| 682 |
r = tempfn_random_child(p->image_root, extra, temp_path); |
| 683 |
if (r < 0) |
| 684 |
return log_oom(); |
| 685 |
} |
| 686 |
|
| 687 |
(void) mkdir_parents_label(*temp_path, 0700); |
| 688 |
|
| 689 |
j->disk_fd = open(*temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); |
| 690 |
if (j->disk_fd < 0) |
| 691 |
return log_error_errno(errno, "Failed to create %s: %m", *temp_path); |
| 692 |
|
| 693 |
return 0; |
| 694 |
} |
| 695 |
|
| 696 |
static int raw_pull_job_on_open_disk_raw(PullJob *j) { |
| 697 |
RawPull *p; |
| 698 |
int r; |
| 699 |
|
| 700 |
assert(j); |
| 701 |
assert(j->userdata); |
| 702 |
|
| 703 |
p = j->userdata; |
| 704 |
assert(p->raw_job == j); |
| 705 |
assert(j->disk_fd < 0); |
| 706 |
|
| 707 |
if (p->flags & IMPORT_DIRECT) { |
| 708 |
|
| 709 |
if (!p->local) { /* If no local name specified, the pull job will write its data to stdout */ |
| 710 |
j->disk_fd = STDOUT_FILENO; |
| 711 |
j->close_disk_fd = false; |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
(void) mkdir_parents_label(p->local, 0700); |
| 716 |
|
| 717 |
j->disk_fd = open(p->local, O_RDWR|O_NOCTTY|O_CLOEXEC|(p->offset == UINT64_MAX ? O_TRUNC|O_CREAT : 0), 0664); |
| 718 |
if (j->disk_fd < 0) |
| 719 |
return log_error_errno(errno, "Failed to open destination '%s': %m", p->local); |
| 720 |
|
| 721 |
if (p->offset == UINT64_MAX) |
| 722 |
(void) import_set_nocow_and_log(j->disk_fd, p->local); |
| 723 |
|
| 724 |
} else { |
| 725 |
r = raw_pull_job_on_open_disk_generic(p, j, "raw", &p->temp_path); |
| 726 |
if (r < 0) |
| 727 |
return r; |
| 728 |
|
| 729 |
assert(p->offset == UINT64_MAX); |
| 730 |
(void) import_set_nocow_and_log(j->disk_fd, p->temp_path); |
| 731 |
} |
| 732 |
|
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
static int raw_pull_job_on_open_disk_settings(PullJob *j) { |
| 737 |
RawPull *p; |
| 738 |
|
| 739 |
assert(j); |
| 740 |
assert(j->userdata); |
| 741 |
|
| 742 |
p = j->userdata; |
| 743 |
assert(p->settings_job == j); |
| 744 |
|
| 745 |
return raw_pull_job_on_open_disk_generic(p, j, "settings", &p->settings_temp_path); |
| 746 |
} |
| 747 |
|
| 748 |
static int raw_pull_job_on_open_disk_roothash(PullJob *j) { |
| 749 |
RawPull *p; |
| 750 |
|
| 751 |
assert(j); |
| 752 |
assert(j->userdata); |
| 753 |
|
| 754 |
p = j->userdata; |
| 755 |
assert(p->roothash_job == j); |
| 756 |
|
| 757 |
return raw_pull_job_on_open_disk_generic(p, j, "roothash", &p->roothash_temp_path); |
| 758 |
} |
| 759 |
|
| 760 |
static int raw_pull_job_on_open_disk_roothash_signature(PullJob *j) { |
| 761 |
RawPull *p; |
| 762 |
|
| 763 |
assert(j); |
| 764 |
assert(j->userdata); |
| 765 |
|
| 766 |
p = j->userdata; |
| 767 |
assert(p->roothash_signature_job == j); |
| 768 |
|
| 769 |
return raw_pull_job_on_open_disk_generic(p, j, "roothash.p7s", &p->roothash_signature_temp_path); |
| 770 |
} |
| 771 |
|
| 772 |
static int raw_pull_job_on_open_disk_verity(PullJob *j) { |
| 773 |
RawPull *p; |
| 774 |
|
| 775 |
assert(j); |
| 776 |
assert(j->userdata); |
| 777 |
|
| 778 |
p = j->userdata; |
| 779 |
assert(p->verity_job == j); |
| 780 |
|
| 781 |
return raw_pull_job_on_open_disk_generic(p, j, "verity", &p->verity_temp_path); |
| 782 |
} |
| 783 |
|
| 784 |
static void raw_pull_job_on_progress(PullJob *j) { |
| 785 |
RawPull *p; |
| 786 |
|
| 787 |
assert(j); |
| 788 |
assert(j->userdata); |
| 789 |
|
| 790 |
p = j->userdata; |
| 791 |
|
| 792 |
raw_pull_report_progress(p, RAW_DOWNLOADING); |
| 793 |
} |
| 794 |
|
| 795 |
int raw_pull_start( |
| 796 |
RawPull *p, |
| 797 |
const char *url, |
| 798 |
const char *local, |
| 799 |
uint64_t offset, |
| 800 |
uint64_t size_max, |
| 801 |
ImportFlags flags, |
| 802 |
ImportVerify verify, |
| 803 |
const struct iovec *checksum) { |
| 804 |
|
| 805 |
int r; |
| 806 |
|
| 807 |
assert(p); |
| 808 |
assert(url); |
| 809 |
assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX); |
| 810 |
assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0); |
| 811 |
assert((verify < 0) || !iovec_is_set(checksum)); |
| 812 |
assert(!(flags & ~IMPORT_PULL_FLAGS_MASK_RAW)); |
| 813 |
assert(offset == UINT64_MAX || FLAGS_SET(flags, IMPORT_DIRECT)); |
| 814 |
assert(!(flags & (IMPORT_PULL_SETTINGS|IMPORT_PULL_ROOTHASH|IMPORT_PULL_ROOTHASH_SIGNATURE|IMPORT_PULL_VERITY)) || !(flags & IMPORT_DIRECT)); |
| 815 |
assert(!(flags & (IMPORT_PULL_SETTINGS|IMPORT_PULL_ROOTHASH|IMPORT_PULL_ROOTHASH_SIGNATURE|IMPORT_PULL_VERITY)) || !iovec_is_set(checksum)); |
| 816 |
|
| 817 |
if (!http_url_is_valid(url) && !file_url_is_valid(url)) |
| 818 |
return -EINVAL; |
| 819 |
|
| 820 |
if (local && !pull_validate_local(local, flags)) |
| 821 |
return -EINVAL; |
| 822 |
|
| 823 |
if (p->raw_job) |
| 824 |
return -EBUSY; |
| 825 |
|
| 826 |
r = free_and_strdup(&p->local, local); |
| 827 |
if (r < 0) |
| 828 |
return r; |
| 829 |
|
| 830 |
p->flags = flags; |
| 831 |
p->verify = verify; |
| 832 |
|
| 833 |
/* Queue job for the image itself */ |
| 834 |
r = pull_job_new(&p->raw_job, url, p->glue, p); |
| 835 |
if (r < 0) |
| 836 |
return r; |
| 837 |
|
| 838 |
p->raw_job->on_finished = raw_pull_job_on_finished; |
| 839 |
p->raw_job->on_open_disk = raw_pull_job_on_open_disk_raw; |
| 840 |
|
| 841 |
if (iovec_is_set(checksum)) { |
| 842 |
if (!iovec_memdup(checksum, &p->raw_job->expected_checksum)) |
| 843 |
return -ENOMEM; |
| 844 |
|
| 845 |
p->raw_job->calc_checksum = true; |
| 846 |
} else if (verify != IMPORT_VERIFY_NO) { |
| 847 |
/* Calculate checksum of the main download unless the users asks for a SHA256SUM file or its |
| 848 |
* signature, which we let gpg verify instead. */ |
| 849 |
|
| 850 |
r = pull_url_needs_checksum(url); |
| 851 |
if (r < 0) |
| 852 |
return r; |
| 853 |
|
| 854 |
p->raw_job->calc_checksum = r; |
| 855 |
p->raw_job->force_memory = !r; /* make sure this is both written to disk if that's |
| 856 |
* requested and into memory, since we need to verify it */ |
| 857 |
} |
| 858 |
|
| 859 |
if (size_max != UINT64_MAX) |
| 860 |
p->raw_job->uncompressed_max = size_max; |
| 861 |
if (offset != UINT64_MAX) |
| 862 |
p->raw_job->offset = p->offset = offset; |
| 863 |
|
| 864 |
if (!FLAGS_SET(flags, IMPORT_DIRECT)) { |
| 865 |
r = pull_find_old_etags(url, p->image_root, DT_REG, ".raw-", ".raw", &p->raw_job->old_etags); |
| 866 |
if (r < 0) |
| 867 |
return r; |
| 868 |
} |
| 869 |
|
| 870 |
r = pull_make_verification_jobs( |
| 871 |
&p->checksum_job, |
| 872 |
&p->signature_job, |
| 873 |
verify, |
| 874 |
url, |
| 875 |
p->glue, |
| 876 |
raw_pull_job_on_finished, |
| 877 |
p); |
| 878 |
if (r < 0) |
| 879 |
return r; |
| 880 |
|
| 881 |
if (FLAGS_SET(flags, IMPORT_PULL_SETTINGS)) { |
| 882 |
r = pull_make_auxiliary_job( |
| 883 |
&p->settings_job, |
| 884 |
url, |
| 885 |
raw_strip_suffixes, |
| 886 |
".nspawn", |
| 887 |
verify, |
| 888 |
p->glue, |
| 889 |
raw_pull_job_on_open_disk_settings, |
| 890 |
raw_pull_job_on_finished, |
| 891 |
p); |
| 892 |
if (r < 0) |
| 893 |
return r; |
| 894 |
} |
| 895 |
|
| 896 |
if (FLAGS_SET(flags, IMPORT_PULL_ROOTHASH)) { |
| 897 |
r = pull_make_auxiliary_job( |
| 898 |
&p->roothash_job, |
| 899 |
url, |
| 900 |
raw_strip_suffixes, |
| 901 |
".roothash", |
| 902 |
verify, |
| 903 |
p->glue, |
| 904 |
raw_pull_job_on_open_disk_roothash, |
| 905 |
raw_pull_job_on_finished, |
| 906 |
p); |
| 907 |
if (r < 0) |
| 908 |
return r; |
| 909 |
} |
| 910 |
|
| 911 |
if (FLAGS_SET(flags, IMPORT_PULL_ROOTHASH_SIGNATURE)) { |
| 912 |
r = pull_make_auxiliary_job( |
| 913 |
&p->roothash_signature_job, |
| 914 |
url, |
| 915 |
raw_strip_suffixes, |
| 916 |
".roothash.p7s", |
| 917 |
verify, |
| 918 |
p->glue, |
| 919 |
raw_pull_job_on_open_disk_roothash_signature, |
| 920 |
raw_pull_job_on_finished, |
| 921 |
p); |
| 922 |
if (r < 0) |
| 923 |
return r; |
| 924 |
} |
| 925 |
|
| 926 |
if (FLAGS_SET(flags, IMPORT_PULL_VERITY)) { |
| 927 |
r = pull_make_auxiliary_job( |
| 928 |
&p->verity_job, |
| 929 |
url, |
| 930 |
raw_strip_suffixes, |
| 931 |
".verity", |
| 932 |
verify, |
| 933 |
p->glue, |
| 934 |
raw_pull_job_on_open_disk_verity, |
| 935 |
raw_pull_job_on_finished, |
| 936 |
p); |
| 937 |
if (r < 0) |
| 938 |
return r; |
| 939 |
} |
| 940 |
|
| 941 |
PullJob *j; |
| 942 |
FOREACH_ARGUMENT(j, |
| 943 |
p->raw_job, |
| 944 |
p->checksum_job, |
| 945 |
p->signature_job, |
| 946 |
p->settings_job, |
| 947 |
p->roothash_job, |
| 948 |
p->roothash_signature_job, |
| 949 |
p->verity_job) { |
| 950 |
|
| 951 |
if (!j) |
| 952 |
continue; |
| 953 |
|
| 954 |
j->on_progress = raw_pull_job_on_progress; |
| 955 |
j->sync = FLAGS_SET(flags, IMPORT_SYNC); |
| 956 |
|
| 957 |
r = pull_job_begin(j); |
| 958 |
if (r < 0) |
| 959 |
return r; |
| 960 |
} |
| 961 |
|
| 962 |
return 0; |
| 963 |
} |