| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-id128.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "dirent-util.h" |
| 9 |
#include "escape.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "hexdecoct.h" |
| 12 |
#include "io-util.h" |
| 13 |
#include "iovec-util.h" |
| 14 |
#include "log.h" |
| 15 |
#include "memory-util.h" |
| 16 |
#include "os-util.h" |
| 17 |
#include "path-util.h" |
| 18 |
#include "pidref.h" |
| 19 |
#include "process-util.h" |
| 20 |
#include "pull-common.h" |
| 21 |
#include "pull-job.h" |
| 22 |
#include "rm-rf.h" |
| 23 |
#include "siphash24.h" |
| 24 |
#include "string-util.h" |
| 25 |
#include "strv.h" |
| 26 |
#include "tmpfile-util.h" |
| 27 |
#include "web-util.h" |
| 28 |
|
| 29 |
#define FILENAME_ESCAPE "/.#\"\'" |
| 30 |
#define HASH_URL_THRESHOLD_LENGTH (_POSIX_PATH_MAX - 16) |
| 31 |
|
| 32 |
int pull_find_old_etags( |
| 33 |
const char *url, |
| 34 |
const char *image_root, |
| 35 |
int dt, |
| 36 |
const char *prefix, |
| 37 |
const char *suffix, |
| 38 |
char ***etags) { |
| 39 |
|
| 40 |
int r; |
| 41 |
|
| 42 |
assert(url); |
| 43 |
assert(image_root); |
| 44 |
assert(etags); |
| 45 |
|
| 46 |
_cleanup_free_ char *escaped_url = xescape(url, FILENAME_ESCAPE); |
| 47 |
if (!escaped_url) |
| 48 |
return -ENOMEM; |
| 49 |
|
| 50 |
_cleanup_closedir_ DIR *d = opendir(image_root); |
| 51 |
if (!d) { |
| 52 |
if (errno == ENOENT) { |
| 53 |
*etags = NULL; |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
return -errno; |
| 58 |
} |
| 59 |
|
| 60 |
_cleanup_strv_free_ char **ans = NULL; |
| 61 |
|
| 62 |
FOREACH_DIRENT_ALL(de, d, return -errno) { |
| 63 |
_cleanup_free_ char *u = NULL; |
| 64 |
const char *a, *b; |
| 65 |
|
| 66 |
if (de->d_type != DT_UNKNOWN && |
| 67 |
de->d_type != dt) |
| 68 |
continue; |
| 69 |
|
| 70 |
if (prefix) { |
| 71 |
a = startswith(de->d_name, prefix); |
| 72 |
if (!a) |
| 73 |
continue; |
| 74 |
} else |
| 75 |
a = de->d_name; |
| 76 |
|
| 77 |
a = startswith(a, escaped_url); |
| 78 |
if (!a) |
| 79 |
continue; |
| 80 |
|
| 81 |
a = startswith(a, "."); |
| 82 |
if (!a) |
| 83 |
continue; |
| 84 |
|
| 85 |
if (suffix) { |
| 86 |
b = endswith(de->d_name, suffix); |
| 87 |
if (!b) |
| 88 |
continue; |
| 89 |
} else |
| 90 |
b = strchr(de->d_name, 0); |
| 91 |
|
| 92 |
if (a >= b) |
| 93 |
continue; |
| 94 |
|
| 95 |
ssize_t l = cunescape_length(a, b - a, 0, &u); |
| 96 |
if (l < 0) { |
| 97 |
assert(l >= INT8_MIN); |
| 98 |
return l; |
| 99 |
} |
| 100 |
|
| 101 |
if (!http_etag_is_valid(u)) |
| 102 |
continue; |
| 103 |
|
| 104 |
r = strv_consume(&ans, TAKE_PTR(u)); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
} |
| 108 |
|
| 109 |
*etags = TAKE_PTR(ans); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
static int hash_url(const char *url, char **ret) { |
| 115 |
uint64_t h; |
| 116 |
static const sd_id128_t k = SD_ID128_ARRAY(df,89,16,87,01,cc,42,30,98,ab,4a,19,a6,a5,63,4f); |
| 117 |
|
| 118 |
assert(url); |
| 119 |
|
| 120 |
h = siphash24(url, strlen(url), k.bytes); |
| 121 |
if (asprintf(ret, "%"PRIx64, h) < 0) |
| 122 |
return -ENOMEM; |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
int pull_make_path(const char *url, const char *etag, const char *image_root, const char *prefix, const char *suffix, char **ret) { |
| 128 |
_cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL; |
| 129 |
char *path; |
| 130 |
|
| 131 |
assert(url); |
| 132 |
assert(image_root); |
| 133 |
assert(ret); |
| 134 |
|
| 135 |
escaped_url = xescape(url, FILENAME_ESCAPE); |
| 136 |
if (!escaped_url) |
| 137 |
return -ENOMEM; |
| 138 |
|
| 139 |
if (etag) { |
| 140 |
escaped_etag = xescape(etag, FILENAME_ESCAPE); |
| 141 |
if (!escaped_etag) |
| 142 |
return -ENOMEM; |
| 143 |
} |
| 144 |
|
| 145 |
path = strjoin(image_root, "/", strempty(prefix), escaped_url, escaped_etag ? "." : "", |
| 146 |
strempty(escaped_etag), strempty(suffix)); |
| 147 |
if (!path) |
| 148 |
return -ENOMEM; |
| 149 |
|
| 150 |
/* URLs might make the path longer than the maximum allowed length for a file name. |
| 151 |
* When that happens, a URL hash is used instead. Paths returned by this function |
| 152 |
* can be later used with tempfn_random() which adds 16 bytes to the resulting name. */ |
| 153 |
if (strlen(path) >= HASH_URL_THRESHOLD_LENGTH) { |
| 154 |
_cleanup_free_ char *hash = NULL; |
| 155 |
int r; |
| 156 |
|
| 157 |
free(path); |
| 158 |
|
| 159 |
r = hash_url(url, &hash); |
| 160 |
if (r < 0) |
| 161 |
return r; |
| 162 |
|
| 163 |
path = strjoin(image_root, "/", strempty(prefix), hash, escaped_etag ? "." : "", |
| 164 |
strempty(escaped_etag), strempty(suffix)); |
| 165 |
if (!path) |
| 166 |
return -ENOMEM; |
| 167 |
} |
| 168 |
|
| 169 |
*ret = path; |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
int pull_make_auxiliary_job( |
| 174 |
PullJob **ret, |
| 175 |
const char *url, |
| 176 |
int (*strip_suffixes)(const char *name, char **ret), |
| 177 |
const char *suffix, |
| 178 |
ImportVerify verify, |
| 179 |
CurlGlue *glue, |
| 180 |
PullJobOpenDisk on_open_disk, |
| 181 |
PullJobFinished on_finished, |
| 182 |
void *userdata) { |
| 183 |
|
| 184 |
_cleanup_free_ char *last_component = NULL, *ll = NULL, *auxiliary_url = NULL; |
| 185 |
_cleanup_(pull_job_unrefp) PullJob *job = NULL; |
| 186 |
const char *q; |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(ret); |
| 190 |
assert(url); |
| 191 |
assert(strip_suffixes); |
| 192 |
assert(glue); |
| 193 |
|
| 194 |
r = import_url_last_component(url, &last_component); |
| 195 |
if (r < 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
r = strip_suffixes(last_component, &ll); |
| 199 |
if (r < 0) |
| 200 |
return r; |
| 201 |
|
| 202 |
q = strjoina(ll, suffix); |
| 203 |
|
| 204 |
r = import_url_change_last_component(url, q, &auxiliary_url); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
|
| 208 |
r = pull_job_new(&job, auxiliary_url, glue, userdata); |
| 209 |
if (r < 0) |
| 210 |
return r; |
| 211 |
|
| 212 |
job->on_open_disk = on_open_disk; |
| 213 |
job->on_finished = on_finished; |
| 214 |
job->compressed_max = job->uncompressed_max = 1ULL * 1024ULL * 1024ULL; |
| 215 |
job->calc_checksum = IN_SET(verify, IMPORT_VERIFY_CHECKSUM, IMPORT_VERIFY_SIGNATURE); |
| 216 |
|
| 217 |
*ret = TAKE_PTR(job); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
static bool is_checksum_file(const char *fn) { |
| 222 |
/* Returns true if the specified filename refers to a checksum file we grok */ |
| 223 |
|
| 224 |
if (!fn) |
| 225 |
return false; |
| 226 |
|
| 227 |
return streq(fn, "SHA256SUMS") || endswith(fn, ".sha256"); |
| 228 |
} |
| 229 |
|
| 230 |
static SignatureStyle signature_style_from_filename(const char *fn) { |
| 231 |
/* Returns true if the specified filename refers to a signature file we grok */ |
| 232 |
|
| 233 |
if (!fn) |
| 234 |
return _SIGNATURE_STYLE_INVALID; |
| 235 |
|
| 236 |
if (streq(fn, "SHA256SUMS.gpg")) |
| 237 |
return SIGNATURE_GPG_PER_DIRECTORY; |
| 238 |
|
| 239 |
if (streq(fn, "SHA256SUMS.asc")) |
| 240 |
return SIGNATURE_ASC_PER_DIRECTORY; |
| 241 |
|
| 242 |
if (endswith(fn, ".sha256.gpg")) |
| 243 |
return SIGNATURE_GPG_PER_FILE; |
| 244 |
|
| 245 |
if (endswith(fn, ".sha256.asc")) |
| 246 |
return SIGNATURE_ASC_PER_FILE; |
| 247 |
|
| 248 |
return _SIGNATURE_STYLE_INVALID; |
| 249 |
} |
| 250 |
|
| 251 |
int pull_make_verification_jobs( |
| 252 |
PullJob **ret_checksum_job, |
| 253 |
PullJob **ret_signature_job, |
| 254 |
ImportVerify verify, |
| 255 |
const char *url, |
| 256 |
CurlGlue *glue, |
| 257 |
PullJobFinished on_finished, |
| 258 |
void *userdata) { |
| 259 |
|
| 260 |
_cleanup_(pull_job_unrefp) PullJob *checksum_job = NULL, *signature_job = NULL; |
| 261 |
_cleanup_free_ char *fn = NULL; |
| 262 |
int r; |
| 263 |
|
| 264 |
assert(ret_checksum_job); |
| 265 |
assert(ret_signature_job); |
| 266 |
assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX); |
| 267 |
assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0); |
| 268 |
assert(url); |
| 269 |
assert(glue); |
| 270 |
|
| 271 |
/* If verification is turned off, or if the checksum to validate is already specified we don't need |
| 272 |
* to download a checksum file or signature, hence shortcut things */ |
| 273 |
if (verify < 0 || /* verification already done (via literal checksum) */ |
| 274 |
verify == IMPORT_VERIFY_NO) { /* verification turned off */ |
| 275 |
*ret_checksum_job = *ret_signature_job = NULL; |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
r = import_url_last_component(url, &fn); |
| 280 |
if (r < 0 && r != -EADDRNOTAVAIL) /* EADDRNOTAVAIL means there was no last component, which is OK for |
| 281 |
* us, we'll just assume it's not a checksum/signature file */ |
| 282 |
return r; |
| 283 |
|
| 284 |
/* Acquire the checksum file if verification or signature verification is requested and the main file |
| 285 |
* to acquire isn't a checksum or signature file anyway */ |
| 286 |
if (verify != IMPORT_VERIFY_NO && !is_checksum_file(fn) && signature_style_from_filename(fn) < 0) { |
| 287 |
_cleanup_free_ char *checksum_url = NULL; |
| 288 |
const char *suffixed = NULL; |
| 289 |
|
| 290 |
/* Queue jobs for the checksum file for the image. */ |
| 291 |
|
| 292 |
if (fn) |
| 293 |
suffixed = strjoina(fn, ".sha256"); /* Start with the suse-style checksum (if there's a base filename) */ |
| 294 |
else |
| 295 |
suffixed = "SHA256SUMS"; |
| 296 |
|
| 297 |
r = import_url_change_last_component(url, suffixed, &checksum_url); |
| 298 |
if (r < 0) |
| 299 |
return r; |
| 300 |
|
| 301 |
r = pull_job_new(&checksum_job, checksum_url, glue, userdata); |
| 302 |
if (r < 0) |
| 303 |
return r; |
| 304 |
|
| 305 |
checksum_job->on_finished = on_finished; |
| 306 |
checksum_job->uncompressed_max = checksum_job->compressed_max = 1ULL * 1024ULL * 1024ULL; |
| 307 |
checksum_job->on_not_found = pull_job_restart_with_sha256sum; /* if this fails, look for ubuntu-style checksum */ |
| 308 |
} |
| 309 |
|
| 310 |
if (verify == IMPORT_VERIFY_SIGNATURE && signature_style_from_filename(fn) < 0) { |
| 311 |
_cleanup_free_ char *signature_url = NULL; |
| 312 |
const char *suffixed = NULL; |
| 313 |
|
| 314 |
if (fn) |
| 315 |
suffixed = strjoina(fn, ".sha256.asc"); /* Start with the suse-style checksum (if there's a base filename) */ |
| 316 |
else |
| 317 |
suffixed = "SHA256SUMS.gpg"; |
| 318 |
|
| 319 |
/* Queue job for the signature file for the image. */ |
| 320 |
r = import_url_change_last_component(url, suffixed, &signature_url); |
| 321 |
if (r < 0) |
| 322 |
return r; |
| 323 |
|
| 324 |
r = pull_job_new(&signature_job, signature_url, glue, userdata); |
| 325 |
if (r < 0) |
| 326 |
return r; |
| 327 |
|
| 328 |
signature_job->on_finished = on_finished; |
| 329 |
signature_job->uncompressed_max = signature_job->compressed_max = 1ULL * 1024ULL * 1024ULL; |
| 330 |
signature_job->on_not_found = pull_job_restart_with_signature; |
| 331 |
} |
| 332 |
|
| 333 |
*ret_checksum_job = TAKE_PTR(checksum_job); |
| 334 |
*ret_signature_job = TAKE_PTR(signature_job); |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
static int verify_one(PullJob *checksum_job, PullJob *job) { |
| 339 |
_cleanup_free_ char *fn = NULL; |
| 340 |
int r; |
| 341 |
|
| 342 |
assert(checksum_job); |
| 343 |
|
| 344 |
if (!job) |
| 345 |
return 0; |
| 346 |
|
| 347 |
assert(IN_SET(job->state, PULL_JOB_DONE, PULL_JOB_FAILED)); |
| 348 |
|
| 349 |
/* Don't verify the checksum if we didn't actually successfully download something new */ |
| 350 |
if (job->state != PULL_JOB_DONE) |
| 351 |
return 0; |
| 352 |
if (job->error != 0) |
| 353 |
return 0; |
| 354 |
if (job->etag_exists) |
| 355 |
return 0; |
| 356 |
|
| 357 |
assert(job->calc_checksum); |
| 358 |
assert(iovec_is_set(&job->checksum)); |
| 359 |
|
| 360 |
r = import_url_last_component(job->url, &fn); |
| 361 |
if (r < 0) |
| 362 |
return log_error_errno(r, "Failed to extract filename from URL '%s': %m", job->url); |
| 363 |
|
| 364 |
if (!filename_is_valid(fn)) |
| 365 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 366 |
"Cannot verify checksum, could not determine server-side file name."); |
| 367 |
|
| 368 |
if (is_checksum_file(fn) || signature_style_from_filename(fn) >= 0) /* We cannot verify checksum files or signature files with a checksum file */ |
| 369 |
return log_error_errno(SYNTHETIC_ERRNO(ELOOP), |
| 370 |
"Cannot verify checksum/signature files via themselves."); |
| 371 |
|
| 372 |
_cleanup_free_ char *cs = hexmem(job->checksum.iov_base, job->checksum.iov_len); |
| 373 |
if (!cs) |
| 374 |
return log_oom(); |
| 375 |
|
| 376 |
const char *p = NULL; |
| 377 |
FOREACH_STRING(separator, |
| 378 |
" *", /* separator for binary mode */ |
| 379 |
" ", /* separator for text mode */ |
| 380 |
" " /* non-standard separator used by linuxcontainers.org */) { |
| 381 |
_cleanup_free_ char *line = NULL; |
| 382 |
|
| 383 |
line = strjoin(cs, separator, fn, "\n"); |
| 384 |
if (!line) |
| 385 |
return log_oom(); |
| 386 |
|
| 387 |
p = memmem_safe(checksum_job->payload.iov_base, |
| 388 |
checksum_job->payload.iov_len, |
| 389 |
line, |
| 390 |
strlen(line)); |
| 391 |
if (p) |
| 392 |
break; |
| 393 |
} |
| 394 |
|
| 395 |
/* Only counts if found at beginning of a line */ |
| 396 |
if (!p || (p != (char*) checksum_job->payload.iov_base && p[-1] != '\n')) |
| 397 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 398 |
"DOWNLOAD INVALID: Checksum of %s file did not check out, file has been tampered with.", fn); |
| 399 |
|
| 400 |
log_info("SHA256 checksum of %s is valid.", job->url); |
| 401 |
return 1; |
| 402 |
} |
| 403 |
|
| 404 |
static int verify_gpg( |
| 405 |
const struct iovec *payload, |
| 406 |
const struct iovec *signature) { |
| 407 |
|
| 408 |
_cleanup_close_pair_ int gpg_pipe[2] = EBADF_PAIR; |
| 409 |
_cleanup_(rm_rf_physical_and_freep) char *gpg_home = NULL; |
| 410 |
char sig_file_path[] = "/tmp/sigXXXXXX"; |
| 411 |
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL; |
| 412 |
int r; |
| 413 |
|
| 414 |
assert(iovec_is_valid(payload)); |
| 415 |
assert(iovec_is_valid(signature)); |
| 416 |
|
| 417 |
r = pipe2(gpg_pipe, O_CLOEXEC); |
| 418 |
if (r < 0) |
| 419 |
return log_error_errno(errno, "Failed to create pipe for gpg: %m"); |
| 420 |
|
| 421 |
if (iovec_is_set(signature)) { |
| 422 |
_cleanup_close_ int sig_file = -EBADF; |
| 423 |
|
| 424 |
sig_file = mkostemp(sig_file_path, O_RDWR); |
| 425 |
if (sig_file < 0) |
| 426 |
return log_error_errno(errno, "Failed to create temporary file: %m"); |
| 427 |
|
| 428 |
r = loop_write(sig_file, signature->iov_base, signature->iov_len); |
| 429 |
if (r < 0) { |
| 430 |
log_error_errno(r, "Failed to write to temporary file: %m"); |
| 431 |
goto finish; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
r = mkdtemp_malloc("/tmp/gpghomeXXXXXX", &gpg_home); |
| 436 |
if (r < 0) { |
| 437 |
log_error_errno(r, "Failed to create temporary home for gpg: %m"); |
| 438 |
goto finish; |
| 439 |
} |
| 440 |
|
| 441 |
r = pidref_safe_fork_full( |
| 442 |
"(gpg)", |
| 443 |
(int[]) { gpg_pipe[0], -EBADF, STDERR_FILENO }, |
| 444 |
/* except_fds= */ NULL, /* n_except_fds= */ 0, |
| 445 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE, |
| 446 |
&pidref); |
| 447 |
if (r < 0) |
| 448 |
return r; |
| 449 |
if (r == 0) { |
| 450 |
const char *cmd[] = { |
| 451 |
"gpg", |
| 452 |
"--no-options", |
| 453 |
"--no-default-keyring", |
| 454 |
"--no-auto-key-locate", |
| 455 |
"--no-auto-check-trustdb", |
| 456 |
"--batch", |
| 457 |
"--trust-model=always", |
| 458 |
NULL, /* --homedir= */ |
| 459 |
NULL, /* --keyring= */ |
| 460 |
NULL, /* --verify */ |
| 461 |
NULL, /* signature file */ |
| 462 |
NULL, /* dash */ |
| 463 |
NULL /* trailing NULL */ |
| 464 |
}; |
| 465 |
size_t k = ELEMENTSOF(cmd) - 6; |
| 466 |
|
| 467 |
/* Child */ |
| 468 |
|
| 469 |
cmd[k++] = strjoina("--homedir=", gpg_home); |
| 470 |
|
| 471 |
/* We add the user keyring only to the command line arguments, if it's around since gpg fails |
| 472 |
* otherwise. */ |
| 473 |
if (access(USER_KEYRING_PATH, F_OK) >= 0) |
| 474 |
cmd[k++] = "--keyring=" USER_KEYRING_PATH; |
| 475 |
else if (access(USER_KEYRING_PATH_LEGACY, F_OK) >= 0) |
| 476 |
cmd[k++] = "--keyring=" USER_KEYRING_PATH_LEGACY; |
| 477 |
else |
| 478 |
cmd[k++] = "--keyring=" VENDOR_KEYRING_PATH; |
| 479 |
|
| 480 |
cmd[k++] = "--verify"; |
| 481 |
if (signature) { |
| 482 |
cmd[k++] = sig_file_path; |
| 483 |
cmd[k++] = "-"; |
| 484 |
cmd[k++] = NULL; |
| 485 |
} |
| 486 |
|
| 487 |
execvp("gpg2", (char * const *) cmd); |
| 488 |
execvp("gpg", (char * const *) cmd); |
| 489 |
log_error_errno(errno, "Failed to execute gpg: %m"); |
| 490 |
_exit(EXIT_FAILURE); |
| 491 |
} |
| 492 |
|
| 493 |
gpg_pipe[0] = safe_close(gpg_pipe[0]); |
| 494 |
|
| 495 |
if (iovec_is_set(payload)) { |
| 496 |
r = loop_write(gpg_pipe[1], payload->iov_base, payload->iov_len); |
| 497 |
if (r < 0) { |
| 498 |
log_error_errno(r, "Failed to write to pipe: %m"); |
| 499 |
goto finish; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
gpg_pipe[1] = safe_close(gpg_pipe[1]); |
| 504 |
|
| 505 |
r = pidref_wait_for_terminate_and_check("gpg", &pidref, WAIT_LOG_ABNORMAL); |
| 506 |
if (r < 0) |
| 507 |
goto finish; |
| 508 |
pidref_done(&pidref); |
| 509 |
if (r != EXIT_SUCCESS) |
| 510 |
r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 511 |
"DOWNLOAD INVALID: Signature verification failed."); |
| 512 |
else { |
| 513 |
log_info("Signature verification succeeded."); |
| 514 |
r = 0; |
| 515 |
} |
| 516 |
|
| 517 |
finish: |
| 518 |
if (iovec_is_set(signature)) |
| 519 |
(void) unlink(sig_file_path); |
| 520 |
|
| 521 |
return r; |
| 522 |
} |
| 523 |
|
| 524 |
int pull_verify(ImportVerify verify, |
| 525 |
PullJob *main_job, |
| 526 |
PullJob *checksum_job, |
| 527 |
PullJob *signature_job, |
| 528 |
PullJob *settings_job, |
| 529 |
PullJob *roothash_job, |
| 530 |
PullJob *roothash_signature_job, |
| 531 |
PullJob *verity_job) { |
| 532 |
|
| 533 |
_cleanup_free_ char *fn = NULL; |
| 534 |
PullJob *verify_job; |
| 535 |
int r; |
| 536 |
|
| 537 |
assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX); |
| 538 |
assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0); |
| 539 |
assert(main_job); |
| 540 |
assert(main_job->state == PULL_JOB_DONE); |
| 541 |
|
| 542 |
if (verify < 0 || /* verification already done (via literal checksum) */ |
| 543 |
verify == IMPORT_VERIFY_NO) /* verification turned off */ |
| 544 |
return 0; |
| 545 |
|
| 546 |
r = import_url_last_component(main_job->url, &fn); |
| 547 |
if (r < 0) |
| 548 |
return log_error_errno(r, "Failed to extract filename from URL '%s': %m", main_job->url); |
| 549 |
|
| 550 |
if (signature_style_from_filename(fn) >= 0) |
| 551 |
return log_error_errno(SYNTHETIC_ERRNO(ELOOP), |
| 552 |
"Main download is a signature file, can't verify it."); |
| 553 |
|
| 554 |
if (is_checksum_file(fn)) { |
| 555 |
log_debug("Main download is a checksum file, can't validate its checksum with itself, skipping."); |
| 556 |
verify_job = main_job; |
| 557 |
} else { |
| 558 |
assert(main_job->calc_checksum); |
| 559 |
assert(iovec_is_set(&main_job->checksum)); |
| 560 |
assert(checksum_job); |
| 561 |
assert(checksum_job->state == PULL_JOB_DONE); |
| 562 |
|
| 563 |
if (!iovec_is_set(&checksum_job->payload)) |
| 564 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 565 |
"Checksum is empty, cannot verify."); |
| 566 |
|
| 567 |
PullJob *j; |
| 568 |
FOREACH_ARGUMENT(j, main_job, settings_job, roothash_job, roothash_signature_job, verity_job) { |
| 569 |
r = verify_one(checksum_job, j); |
| 570 |
if (r < 0) |
| 571 |
return r; |
| 572 |
} |
| 573 |
|
| 574 |
verify_job = checksum_job; |
| 575 |
} |
| 576 |
|
| 577 |
if (verify != IMPORT_VERIFY_SIGNATURE) |
| 578 |
return 0; |
| 579 |
|
| 580 |
assert(verify_job); |
| 581 |
assert(signature_job); |
| 582 |
assert(signature_job->state == PULL_JOB_DONE); |
| 583 |
|
| 584 |
if (!iovec_is_set(&signature_job->payload)) |
| 585 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 586 |
"Signature is empty, cannot verify."); |
| 587 |
|
| 588 |
return verify_gpg(&verify_job->payload, &signature_job->payload); |
| 589 |
} |
| 590 |
|
| 591 |
int verification_style_from_url(const char *url, VerificationStyle *ret) { |
| 592 |
_cleanup_free_ char *last = NULL; |
| 593 |
int r; |
| 594 |
|
| 595 |
assert(url); |
| 596 |
assert(ret); |
| 597 |
|
| 598 |
/* Determines which kind of verification style is appropriate for this url */ |
| 599 |
|
| 600 |
r = import_url_last_component(url, &last); |
| 601 |
if (r < 0) |
| 602 |
return r; |
| 603 |
|
| 604 |
if (streq(last, "SHA256SUMS")) { |
| 605 |
*ret = VERIFICATION_PER_DIRECTORY; |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
if (endswith(last, ".sha256")) { |
| 610 |
*ret = VERIFICATION_PER_FILE; |
| 611 |
return 0; |
| 612 |
} |
| 613 |
|
| 614 |
return -EINVAL; |
| 615 |
} |
| 616 |
|
| 617 |
int pull_job_restart_with_sha256sum(PullJob *j, char **ret) { |
| 618 |
VerificationStyle style; |
| 619 |
int r; |
| 620 |
|
| 621 |
assert(j); |
| 622 |
assert(ret); |
| 623 |
|
| 624 |
/* Generic implementation of a PullJobNotFound handler, that restarts the job requesting SHA256SUMS */ |
| 625 |
|
| 626 |
r = verification_style_from_url(j->url, &style); |
| 627 |
if (r < 0) |
| 628 |
return log_error_errno(r, "Failed to determine verification style of URL '%s': %m", j->url); |
| 629 |
|
| 630 |
if (style == VERIFICATION_PER_DIRECTORY) { /* Nothing to do anymore */ |
| 631 |
*ret = NULL; |
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
assert(style == VERIFICATION_PER_FILE); /* This must have been .sha256 style URL before */ |
| 636 |
|
| 637 |
log_debug("Got 404 for '%s', now trying to get SHA256SUMS instead.", j->url); |
| 638 |
|
| 639 |
r = import_url_change_last_component(j->url, "SHA256SUMS", ret); |
| 640 |
if (r < 0) |
| 641 |
return log_error_errno(r, "Failed to replace SHA256SUMS suffix: %m"); |
| 642 |
|
| 643 |
return 1; |
| 644 |
} |
| 645 |
|
| 646 |
int signature_style_from_url(const char *url, SignatureStyle *ret, char **ret_filename) { |
| 647 |
_cleanup_free_ char *last = NULL; |
| 648 |
SignatureStyle style; |
| 649 |
int r; |
| 650 |
|
| 651 |
assert(url); |
| 652 |
assert(ret); |
| 653 |
assert(ret_filename); |
| 654 |
|
| 655 |
/* Determines which kind of signature style is appropriate for this url */ |
| 656 |
|
| 657 |
r = import_url_last_component(url, &last); |
| 658 |
if (r < 0) |
| 659 |
return r; |
| 660 |
|
| 661 |
style = signature_style_from_filename(last); |
| 662 |
if (style < 0) |
| 663 |
return style; |
| 664 |
|
| 665 |
*ret_filename = TAKE_PTR(last); |
| 666 |
*ret = style; |
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
int pull_job_restart_with_signature(PullJob *j, char **ret) { |
| 671 |
_cleanup_free_ char *last = NULL; |
| 672 |
SignatureStyle style; |
| 673 |
int r; |
| 674 |
|
| 675 |
assert(j); |
| 676 |
assert(ret); |
| 677 |
|
| 678 |
/* Generic implementation of a PullJobNotFound handler, that restarts the job requesting a different |
| 679 |
* signature file. After the initial file, additional *.sha256.gpg, SHA256SUMS.gpg and SHA256SUMS.asc |
| 680 |
* are tried in this order. */ |
| 681 |
|
| 682 |
r = signature_style_from_url(j->url, &style, &last); |
| 683 |
if (r < 0) |
| 684 |
return log_error_errno(r, "Failed to determine signature style of URL '%s': %m", j->url); |
| 685 |
|
| 686 |
switch (style) { |
| 687 |
|
| 688 |
case SIGNATURE_ASC_PER_DIRECTORY: /* Nothing to do anymore */ |
| 689 |
*ret = NULL; |
| 690 |
return 0; |
| 691 |
|
| 692 |
case SIGNATURE_ASC_PER_FILE: { /* Try .sha256.gpg next */ |
| 693 |
char *ext; |
| 694 |
|
| 695 |
log_debug("Got 404 for '%s', now trying to get .sha256.gpg instead.", j->url); |
| 696 |
|
| 697 |
ext = endswith(last, ".asc"); |
| 698 |
assert(ext); |
| 699 |
strcpy(ext, ".gpg"); |
| 700 |
|
| 701 |
r = import_url_change_last_component(j->url, last, ret); |
| 702 |
if (r < 0) |
| 703 |
return log_error_errno(r, "Failed to replace .sha256.asc suffix: %m"); |
| 704 |
break; |
| 705 |
} |
| 706 |
|
| 707 |
case SIGNATURE_GPG_PER_FILE: /* Try SHA256SUMS.gpg next */ |
| 708 |
log_debug("Got 404 for '%s', now trying to get SHA256SUMS.gpg instead.", j->url); |
| 709 |
r = import_url_change_last_component(j->url, "SHA256SUMS.gpg", ret); |
| 710 |
if (r < 0) |
| 711 |
return log_error_errno(r, "Failed to replace SHA256SUMS suffix: %m"); |
| 712 |
break; |
| 713 |
|
| 714 |
case SIGNATURE_GPG_PER_DIRECTORY: |
| 715 |
log_debug("Got 404 for '%s', now trying to get SHA256SUMS.asc instead.", j->url); |
| 716 |
r = import_url_change_last_component(j->url, "SHA256SUMS.asc", ret); |
| 717 |
if (r < 0) |
| 718 |
return log_error_errno(r, "Failed to replace SHA256SUMS.gpg suffix: %m"); |
| 719 |
break; |
| 720 |
|
| 721 |
default: |
| 722 |
assert_not_reached(); |
| 723 |
} |
| 724 |
|
| 725 |
return 1; |
| 726 |
} |
| 727 |
|
| 728 |
bool pull_validate_local(const char *name, ImportFlags flags) { |
| 729 |
|
| 730 |
if (FLAGS_SET(flags, IMPORT_DIRECT)) |
| 731 |
return path_is_valid(name); |
| 732 |
|
| 733 |
return image_name_is_valid(name); |
| 734 |
} |
| 735 |
|
| 736 |
int pull_url_needs_checksum(const char *url) { |
| 737 |
_cleanup_free_ char *fn = NULL; |
| 738 |
int r; |
| 739 |
|
| 740 |
/* Returns true if we need to validate this resource via a hash value. This returns true for all |
| 741 |
* files — except for gpg signature files and SHA256SUMS files and the like, which are validated with |
| 742 |
* a validation tool like gpg. */ |
| 743 |
|
| 744 |
r = import_url_last_component(url, &fn); |
| 745 |
if (r == -EADDRNOTAVAIL) /* no last component? then let's assume it's not a signature/checksum file */ |
| 746 |
return false; |
| 747 |
if (r < 0) |
| 748 |
return r; |
| 749 |
|
| 750 |
return !is_checksum_file(fn) && signature_style_from_filename(fn) < 0; |
| 751 |
} |