]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
56ebfaf1 | 2 | |
98c38001 LP |
3 | #include <sys/prctl.h> |
4 | ||
b5efdb8a | 5 | #include "alloc-util.h" |
56ebfaf1 | 6 | #include "btrfs-util.h" |
430f0182 | 7 | #include "capability-util.h" |
4f5dd394 | 8 | #include "copy.h" |
a0956174 | 9 | #include "dirent-util.h" |
4f5dd394 | 10 | #include "escape.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
c40d82ab | 12 | #include "hostname-util.h" |
c004493c | 13 | #include "io-util.h" |
e8b08edc | 14 | #include "memory-util.h" |
bb15fafe | 15 | #include "path-util.h" |
0b452006 | 16 | #include "process-util.h" |
3ffd4af2 | 17 | #include "pull-common.h" |
4f5dd394 | 18 | #include "pull-job.h" |
595225af | 19 | #include "rlimit-util.h" |
4f5dd394 | 20 | #include "rm-rf.h" |
24882e06 | 21 | #include "signal-util.h" |
9818005d | 22 | #include "siphash24.h" |
07630cea | 23 | #include "string-util.h" |
4f5dd394 | 24 | #include "strv.h" |
49cf4170 | 25 | #include "web-util.h" |
56ebfaf1 LP |
26 | |
27 | #define FILENAME_ESCAPE "/.#\"\'" | |
9818005d | 28 | #define HASH_URL_THRESHOLD_LENGTH (_POSIX_PATH_MAX - 16) |
56ebfaf1 | 29 | |
9854730b LP |
30 | int pull_find_old_etags( |
31 | const char *url, | |
32 | const char *image_root, | |
33 | int dt, | |
34 | const char *prefix, | |
35 | const char *suffix, | |
36 | char ***etags) { | |
37 | ||
56ebfaf1 LP |
38 | int r; |
39 | ||
40 | assert(url); | |
41 | assert(etags); | |
42 | ||
43 | if (!image_root) | |
44 | image_root = "/var/lib/machines"; | |
45 | ||
e437538f | 46 | _cleanup_free_ char *escaped_url = xescape(url, FILENAME_ESCAPE); |
56ebfaf1 LP |
47 | if (!escaped_url) |
48 | return -ENOMEM; | |
49 | ||
e437538f | 50 | _cleanup_closedir_ DIR *d = opendir(image_root); |
56ebfaf1 LP |
51 | if (!d) { |
52 | if (errno == ENOENT) { | |
53 | *etags = NULL; | |
54 | return 0; | |
55 | } | |
56 | ||
57 | return -errno; | |
58 | } | |
59 | ||
e437538f | 60 | _cleanup_strv_free_ char **ans = NULL; |
e437538f | 61 | |
56ebfaf1 | 62 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
6abdec98 | 63 | _cleanup_free_ char *u = NULL; |
56ebfaf1 | 64 | const char *a, *b; |
56ebfaf1 LP |
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 | ||
e437538f ZJS |
95 | ssize_t l = cunescape_length(a, b - a, 0, &u); |
96 | if (l < 0) { | |
97 | assert(l >= INT8_MIN); | |
98 | return l; | |
99 | } | |
56ebfaf1 | 100 | |
6abdec98 | 101 | if (!http_etag_is_valid(u)) |
56ebfaf1 | 102 | continue; |
56ebfaf1 | 103 | |
e437538f | 104 | r = strv_consume(&ans, TAKE_PTR(u)); |
56ebfaf1 LP |
105 | if (r < 0) |
106 | return r; | |
107 | } | |
108 | ||
e437538f | 109 | *etags = TAKE_PTR(ans); |
56ebfaf1 LP |
110 | |
111 | return 0; | |
112 | } | |
113 | ||
9818005d JS |
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 | ||
933f9cae | 120 | h = siphash24(url, strlen(url), k.bytes); |
9818005d JS |
121 | if (asprintf(ret, "%"PRIx64, h) < 0) |
122 | return -ENOMEM; | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
dc2c282b | 127 | int pull_make_path(const char *url, const char *etag, const char *image_root, const char *prefix, const char *suffix, char **ret) { |
9818005d | 128 | _cleanup_free_ char *escaped_url = NULL, *escaped_etag = NULL; |
56ebfaf1 LP |
129 | char *path; |
130 | ||
131 | assert(url); | |
132 | assert(ret); | |
133 | ||
134 | if (!image_root) | |
135 | image_root = "/var/lib/machines"; | |
136 | ||
137 | escaped_url = xescape(url, FILENAME_ESCAPE); | |
138 | if (!escaped_url) | |
139 | return -ENOMEM; | |
140 | ||
141 | if (etag) { | |
56ebfaf1 LP |
142 | escaped_etag = xescape(etag, FILENAME_ESCAPE); |
143 | if (!escaped_etag) | |
144 | return -ENOMEM; | |
9818005d | 145 | } |
56ebfaf1 | 146 | |
9818005d | 147 | path = strjoin(image_root, "/", strempty(prefix), escaped_url, escaped_etag ? "." : "", |
4600a396 | 148 | strempty(escaped_etag), strempty(suffix)); |
56ebfaf1 LP |
149 | if (!path) |
150 | return -ENOMEM; | |
151 | ||
9818005d JS |
152 | /* URLs might make the path longer than the maximum allowed length for a file name. |
153 | * When that happens, a URL hash is used instead. Paths returned by this function | |
154 | * can be later used with tempfn_random() which adds 16 bytes to the resulting name. */ | |
155 | if (strlen(path) >= HASH_URL_THRESHOLD_LENGTH) { | |
156 | _cleanup_free_ char *hash = NULL; | |
157 | int r; | |
158 | ||
159 | free(path); | |
160 | ||
161 | r = hash_url(url, &hash); | |
162 | if (r < 0) | |
163 | return r; | |
164 | ||
165 | path = strjoin(image_root, "/", strempty(prefix), hash, escaped_etag ? "." : "", | |
4600a396 | 166 | strempty(escaped_etag), strempty(suffix)); |
9818005d JS |
167 | if (!path) |
168 | return -ENOMEM; | |
169 | } | |
170 | ||
56ebfaf1 LP |
171 | *ret = path; |
172 | return 0; | |
173 | } | |
85dbc41d | 174 | |
91359193 | 175 | int pull_make_auxiliary_job( |
9854730b LP |
176 | PullJob **ret, |
177 | const char *url, | |
91359193 LP |
178 | int (*strip_suffixes)(const char *name, char **ret), |
179 | const char *suffix, | |
c40d82ab | 180 | ImportVerify verify, |
9854730b | 181 | CurlGlue *glue, |
c40d82ab | 182 | PullJobOpenDisk on_open_disk, |
9854730b LP |
183 | PullJobFinished on_finished, |
184 | void *userdata) { | |
185 | ||
91359193 | 186 | _cleanup_free_ char *last_component = NULL, *ll = NULL, *auxiliary_url = NULL; |
9854730b LP |
187 | _cleanup_(pull_job_unrefp) PullJob *job = NULL; |
188 | const char *q; | |
189 | int r; | |
190 | ||
191 | assert(ret); | |
192 | assert(url); | |
91359193 | 193 | assert(strip_suffixes); |
9854730b LP |
194 | assert(glue); |
195 | ||
196 | r = import_url_last_component(url, &last_component); | |
197 | if (r < 0) | |
198 | return r; | |
199 | ||
91359193 | 200 | r = strip_suffixes(last_component, &ll); |
9854730b LP |
201 | if (r < 0) |
202 | return r; | |
203 | ||
91359193 | 204 | q = strjoina(ll, suffix); |
9854730b | 205 | |
91359193 | 206 | r = import_url_change_last_component(url, q, &auxiliary_url); |
9854730b LP |
207 | if (r < 0) |
208 | return r; | |
209 | ||
91359193 | 210 | r = pull_job_new(&job, auxiliary_url, glue, userdata); |
9854730b LP |
211 | if (r < 0) |
212 | return r; | |
213 | ||
c40d82ab | 214 | job->on_open_disk = on_open_disk; |
9854730b LP |
215 | job->on_finished = on_finished; |
216 | job->compressed_max = job->uncompressed_max = 1ULL * 1024ULL * 1024ULL; | |
c40d82ab | 217 | job->calc_checksum = IN_SET(verify, IMPORT_VERIFY_CHECKSUM, IMPORT_VERIFY_SIGNATURE); |
9854730b | 218 | |
1cc6c93a | 219 | *ret = TAKE_PTR(job); |
9854730b LP |
220 | return 0; |
221 | } | |
222 | ||
c40d82ab LP |
223 | static bool is_checksum_file(const char *fn) { |
224 | /* Returns true if the specified filename refers to a checksum file we grok */ | |
225 | ||
226 | if (!fn) | |
227 | return false; | |
228 | ||
229 | return streq(fn, "SHA256SUMS") || endswith(fn, ".sha256"); | |
230 | } | |
231 | ||
232 | static bool is_signature_file(const char *fn) { | |
233 | /* Returns true if the specified filename refers to a signature file we grok (reminder: | |
234 | * suse-style .sha256 files are inline signed) */ | |
235 | ||
236 | if (!fn) | |
237 | return false; | |
238 | ||
239 | return streq(fn, "SHA256SUMS.gpg") || endswith(fn, ".sha256"); | |
240 | } | |
241 | ||
dc2c282b LP |
242 | int pull_make_verification_jobs( |
243 | PullJob **ret_checksum_job, | |
244 | PullJob **ret_signature_job, | |
98c38001 | 245 | ImportVerify verify, |
c40d82ab | 246 | const char *checksum, /* set if literal checksum verification is requested, in which case 'verify' is set to _IMPORT_VERIFY_INVALID */ |
98c38001 LP |
247 | const char *url, |
248 | CurlGlue *glue, | |
dc2c282b | 249 | PullJobFinished on_finished, |
98c38001 LP |
250 | void *userdata) { |
251 | ||
dc2c282b | 252 | _cleanup_(pull_job_unrefp) PullJob *checksum_job = NULL, *signature_job = NULL; |
c40d82ab | 253 | _cleanup_free_ char *fn = NULL; |
98c38001 LP |
254 | int r; |
255 | ||
256 | assert(ret_checksum_job); | |
257 | assert(ret_signature_job); | |
c40d82ab LP |
258 | assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX); |
259 | assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0); | |
260 | assert((verify < 0) || !checksum); | |
98c38001 LP |
261 | assert(url); |
262 | assert(glue); | |
263 | ||
c40d82ab LP |
264 | /* If verification is turned off, or if the checksum to validate is already specified we don't need |
265 | * to download a checksum file or signature, hence shortcut things */ | |
266 | if (verify == IMPORT_VERIFY_NO || checksum) { | |
267 | *ret_checksum_job = *ret_signature_job = NULL; | |
268 | return 0; | |
269 | } | |
270 | ||
271 | r = import_url_last_component(url, &fn); | |
272 | if (r < 0 && r != -EADDRNOTAVAIL) /* EADDRNOTAVAIL means there was no last component, which is OK for | |
273 | * us, we'll just assume it's not a checksum/signature file */ | |
274 | return r; | |
275 | ||
276 | /* Acquire the checksum file if verification or signature verification is requested and the main file | |
277 | * to acquire isn't a checksum or signature file anyway */ | |
278 | if (verify != IMPORT_VERIFY_NO && !is_checksum_file(fn) && !is_signature_file(fn)) { | |
279 | _cleanup_free_ char *checksum_url = NULL; | |
280 | const char *suffixed = NULL; | |
98c38001 | 281 | |
697be0be | 282 | /* Queue jobs for the checksum file for the image. */ |
697be0be | 283 | |
c40d82ab LP |
284 | if (fn) |
285 | suffixed = strjoina(fn, ".sha256"); /* Start with the suse-style checksum (if there's a base filename) */ | |
286 | else | |
287 | suffixed = "SHA256SUMS"; | |
697be0be | 288 | |
c40d82ab | 289 | r = import_url_change_last_component(url, suffixed, &checksum_url); |
98c38001 LP |
290 | if (r < 0) |
291 | return r; | |
292 | ||
dc2c282b | 293 | r = pull_job_new(&checksum_job, checksum_url, glue, userdata); |
98c38001 LP |
294 | if (r < 0) |
295 | return r; | |
296 | ||
297 | checksum_job->on_finished = on_finished; | |
298 | checksum_job->uncompressed_max = checksum_job->compressed_max = 1ULL * 1024ULL * 1024ULL; | |
c40d82ab | 299 | checksum_job->on_not_found = pull_job_restart_with_sha256sum; /* if this fails, look for ubuntu-style checksum */ |
98c38001 LP |
300 | } |
301 | ||
c40d82ab | 302 | if (verify == IMPORT_VERIFY_SIGNATURE && !is_signature_file(fn)) { |
98c38001 LP |
303 | _cleanup_free_ char *signature_url = NULL; |
304 | ||
305 | /* Queue job for the SHA256SUMS.gpg file for the image. */ | |
306 | r = import_url_change_last_component(url, "SHA256SUMS.gpg", &signature_url); | |
307 | if (r < 0) | |
308 | return r; | |
309 | ||
dc2c282b | 310 | r = pull_job_new(&signature_job, signature_url, glue, userdata); |
98c38001 LP |
311 | if (r < 0) |
312 | return r; | |
313 | ||
314 | signature_job->on_finished = on_finished; | |
315 | signature_job->uncompressed_max = signature_job->compressed_max = 1ULL * 1024ULL * 1024ULL; | |
316 | } | |
317 | ||
c20307fd LP |
318 | *ret_checksum_job = TAKE_PTR(checksum_job); |
319 | *ret_signature_job = TAKE_PTR(signature_job); | |
98c38001 LP |
320 | return 0; |
321 | } | |
322 | ||
91359193 | 323 | static int verify_one(PullJob *checksum_job, PullJob *job) { |
98c38001 | 324 | _cleanup_free_ char *fn = NULL; |
91359193 | 325 | const char *line, *p; |
98c38001 LP |
326 | int r; |
327 | ||
91359193 | 328 | assert(checksum_job); |
98c38001 | 329 | |
91359193 | 330 | if (!job) |
98c38001 LP |
331 | return 0; |
332 | ||
91359193 | 333 | assert(IN_SET(job->state, PULL_JOB_DONE, PULL_JOB_FAILED)); |
98c38001 | 334 | |
91359193 LP |
335 | /* Don't verify the checksum if we didn't actually successfully download something new */ |
336 | if (job->state != PULL_JOB_DONE) | |
337 | return 0; | |
338 | if (job->error != 0) | |
339 | return 0; | |
340 | if (job->etag_exists) | |
341 | return 0; | |
98c38001 | 342 | |
91359193 LP |
343 | assert(job->calc_checksum); |
344 | assert(job->checksum); | |
345 | ||
346 | r = import_url_last_component(job->url, &fn); | |
98c38001 | 347 | if (r < 0) |
c40d82ab | 348 | return log_error_errno(r, "Failed to extract filename from URL '%s': %m", job->url); |
98c38001 | 349 | |
baaa35ad ZJS |
350 | if (!filename_is_valid(fn)) |
351 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
352 | "Cannot verify checksum, could not determine server-side file name."); | |
98c38001 | 353 | |
c40d82ab LP |
354 | if (is_checksum_file(fn) || is_signature_file(fn)) /* We cannot verify checksum files or signature files with a checksum file */ |
355 | return log_error_errno(SYNTHETIC_ERRNO(ELOOP), | |
356 | "Cannot verify checksum/signature files via themselves."); | |
98c38001 | 357 | |
c40d82ab | 358 | line = strjoina(job->checksum, " *", fn, "\n"); /* string for binary mode */ |
e8b08edc LP |
359 | p = memmem_safe(checksum_job->payload, |
360 | checksum_job->payload_size, | |
361 | line, | |
362 | strlen(line)); | |
697be0be | 363 | if (!p) { |
c40d82ab | 364 | line = strjoina(job->checksum, " ", fn, "\n"); /* string for text mode */ |
e8b08edc LP |
365 | p = memmem_safe(checksum_job->payload, |
366 | checksum_job->payload_size, | |
367 | line, | |
368 | strlen(line)); | |
697be0be TB |
369 | } |
370 | ||
c40d82ab | 371 | /* Only counts if found at beginning of a line */ |
baaa35ad ZJS |
372 | if (!p || (p != (char*) checksum_job->payload && p[-1] != '\n')) |
373 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
c40d82ab | 374 | "DOWNLOAD INVALID: Checksum of %s file did not check out, file has been tampered with.", fn); |
98c38001 | 375 | |
91359193 LP |
376 | log_info("SHA256 checksum of %s is valid.", job->url); |
377 | return 1; | |
378 | } | |
98c38001 | 379 | |
ac71ece3 LP |
380 | static int verify_gpg( |
381 | const void *payload, size_t payload_size, | |
382 | const void *signature, size_t signature_size) { | |
9854730b | 383 | |
34014779 | 384 | _cleanup_close_pair_ int gpg_pipe[2] = { -EBADF, -EBADF }; |
91359193 LP |
385 | char sig_file_path[] = "/tmp/sigXXXXXX", gpg_home[] = "/tmp/gpghomeXXXXXX"; |
386 | _cleanup_(sigkill_waitp) pid_t pid = 0; | |
387 | bool gpg_home_created = false; | |
388 | int r; | |
9854730b | 389 | |
ac71ece3 LP |
390 | assert(payload || payload_size == 0); |
391 | assert(signature || signature_size == 0); | |
98c38001 LP |
392 | |
393 | r = pipe2(gpg_pipe, O_CLOEXEC); | |
394 | if (r < 0) | |
0100b6e1 | 395 | return log_error_errno(errno, "Failed to create pipe for gpg: %m"); |
98c38001 | 396 | |
ac71ece3 LP |
397 | if (signature_size > 0) { |
398 | _cleanup_close_ int sig_file = -1; | |
98c38001 | 399 | |
ac71ece3 LP |
400 | sig_file = mkostemp(sig_file_path, O_RDWR); |
401 | if (sig_file < 0) | |
402 | return log_error_errno(errno, "Failed to create temporary file: %m"); | |
403 | ||
404 | r = loop_write(sig_file, signature, signature_size, false); | |
405 | if (r < 0) { | |
406 | log_error_errno(r, "Failed to write to temporary file: %m"); | |
407 | goto finish; | |
408 | } | |
98c38001 LP |
409 | } |
410 | ||
0acfdffe | 411 | if (!mkdtemp(gpg_home)) { |
5238e957 | 412 | r = log_error_errno(errno, "Failed to create temporary home for gpg: %m"); |
0acfdffe LP |
413 | goto finish; |
414 | } | |
415 | ||
416 | gpg_home_created = true; | |
417 | ||
b6e1fff1 | 418 | r = safe_fork("(gpg)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
4c253ed1 | 419 | if (r < 0) |
b6e1fff1 | 420 | return r; |
4c253ed1 | 421 | if (r == 0) { |
98c38001 LP |
422 | const char *cmd[] = { |
423 | "gpg", | |
424 | "--no-options", | |
425 | "--no-default-keyring", | |
426 | "--no-auto-key-locate", | |
427 | "--no-auto-check-trustdb", | |
428 | "--batch", | |
429 | "--trust-model=always", | |
0acfdffe LP |
430 | NULL, /* --homedir= */ |
431 | NULL, /* --keyring= */ | |
98c38001 LP |
432 | NULL, /* --verify */ |
433 | NULL, /* signature file */ | |
434 | NULL, /* dash */ | |
435 | NULL /* trailing NULL */ | |
436 | }; | |
ac71ece3 | 437 | size_t k = ELEMENTSOF(cmd) - 6; |
98c38001 LP |
438 | |
439 | /* Child */ | |
440 | ||
98c38001 LP |
441 | gpg_pipe[1] = safe_close(gpg_pipe[1]); |
442 | ||
aedec452 | 443 | r = rearrange_stdio(TAKE_FD(gpg_pipe[0]), -1, STDERR_FILENO); |
046a82c1 | 444 | if (r < 0) { |
2b33ab09 | 445 | log_error_errno(r, "Failed to rearrange stdin/stdout: %m"); |
98c38001 LP |
446 | _exit(EXIT_FAILURE); |
447 | } | |
448 | ||
595225af LP |
449 | (void) rlimit_nofile_safe(); |
450 | ||
0acfdffe LP |
451 | cmd[k++] = strjoina("--homedir=", gpg_home); |
452 | ||
ac71ece3 | 453 | /* We add the user keyring only to the command line arguments, if it's around since gpg fails |
98c38001 LP |
454 | * otherwise. */ |
455 | if (access(USER_KEYRING_PATH, F_OK) >= 0) | |
456 | cmd[k++] = "--keyring=" USER_KEYRING_PATH; | |
1c49d1ba LP |
457 | else |
458 | cmd[k++] = "--keyring=" VENDOR_KEYRING_PATH; | |
98c38001 LP |
459 | |
460 | cmd[k++] = "--verify"; | |
ac71ece3 | 461 | if (signature) { |
697be0be TB |
462 | cmd[k++] = sig_file_path; |
463 | cmd[k++] = "-"; | |
464 | cmd[k++] = NULL; | |
465 | } | |
98c38001 | 466 | |
0acfdffe | 467 | execvp("gpg2", (char * const *) cmd); |
98c38001 LP |
468 | execvp("gpg", (char * const *) cmd); |
469 | log_error_errno(errno, "Failed to execute gpg: %m"); | |
470 | _exit(EXIT_FAILURE); | |
471 | } | |
472 | ||
473 | gpg_pipe[0] = safe_close(gpg_pipe[0]); | |
474 | ||
ac71ece3 | 475 | r = loop_write(gpg_pipe[1], payload, payload_size, false); |
98c38001 LP |
476 | if (r < 0) { |
477 | log_error_errno(r, "Failed to write to pipe: %m"); | |
478 | goto finish; | |
479 | } | |
480 | ||
481 | gpg_pipe[1] = safe_close(gpg_pipe[1]); | |
482 | ||
8f03de53 | 483 | r = wait_for_terminate_and_check("gpg", TAKE_PID(pid), WAIT_LOG_ABNORMAL); |
98c38001 LP |
484 | if (r < 0) |
485 | goto finish; | |
c40d82ab LP |
486 | if (r != EXIT_SUCCESS) |
487 | r = log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
488 | "DOWNLOAD INVALID: Signature verification failed."); | |
489 | else { | |
98c38001 LP |
490 | log_info("Signature verification succeeded."); |
491 | r = 0; | |
492 | } | |
493 | ||
494 | finish: | |
ac71ece3 LP |
495 | if (signature_size > 0) |
496 | (void) unlink(sig_file_path); | |
98c38001 | 497 | |
0acfdffe | 498 | if (gpg_home_created) |
c6878637 | 499 | (void) rm_rf(gpg_home, REMOVE_ROOT|REMOVE_PHYSICAL); |
0acfdffe | 500 | |
98c38001 LP |
501 | return r; |
502 | } | |
f14717a7 | 503 | |
ac71ece3 | 504 | int pull_verify(ImportVerify verify, |
c40d82ab | 505 | const char *checksum, /* Verify with literal checksum */ |
ac71ece3 | 506 | PullJob *main_job, |
ac71ece3 | 507 | PullJob *checksum_job, |
ff2f7797 LP |
508 | PullJob *signature_job, |
509 | PullJob *settings_job, | |
510 | PullJob *roothash_job, | |
511 | PullJob *roothash_signature_job, | |
512 | PullJob *verity_job) { | |
ac71ece3 | 513 | |
c40d82ab | 514 | _cleanup_free_ char *fn = NULL; |
ac71ece3 | 515 | VerificationStyle style; |
c40d82ab | 516 | PullJob *verify_job; |
ac71ece3 LP |
517 | int r; |
518 | ||
c40d82ab LP |
519 | assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX); |
520 | assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0); | |
521 | assert((verify < 0) || !checksum); | |
ac71ece3 LP |
522 | assert(main_job); |
523 | assert(main_job->state == PULL_JOB_DONE); | |
524 | ||
c40d82ab | 525 | if (verify == IMPORT_VERIFY_NO) /* verification turned off */ |
ac71ece3 LP |
526 | return 0; |
527 | ||
c40d82ab LP |
528 | if (checksum) { |
529 | /* Verification by literal checksum */ | |
530 | assert(!checksum_job); | |
531 | assert(!signature_job); | |
532 | assert(!settings_job); | |
533 | assert(!roothash_job); | |
534 | assert(!roothash_signature_job); | |
535 | assert(!verity_job); | |
ac71ece3 | 536 | |
c40d82ab LP |
537 | assert(main_job->calc_checksum); |
538 | assert(main_job->checksum); | |
ac71ece3 | 539 | |
c40d82ab LP |
540 | if (!strcaseeq(checksum, main_job->checksum)) |
541 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
542 | "DOWNLOAD INVALID: Checksum of %s file did not check out, file has been tampered with.", | |
543 | main_job->url); | |
544 | ||
545 | return 0; | |
ff2f7797 | 546 | } |
ac71ece3 | 547 | |
c40d82ab LP |
548 | r = import_url_last_component(main_job->url, &fn); |
549 | if (r < 0) | |
550 | return log_error_errno(r, "Failed to extract filename from URL '%s': %m", main_job->url); | |
551 | ||
552 | if (is_signature_file(fn)) | |
553 | return log_error_errno(SYNTHETIC_ERRNO(ELOOP), | |
554 | "Main download is a signature file, can't verify it."); | |
555 | ||
556 | if (is_checksum_file(fn)) { | |
557 | log_debug("Main download is a checksum file, can't validate its checksum with itself, skipping."); | |
558 | verify_job = main_job; | |
559 | } else { | |
560 | PullJob *j; | |
561 | assert(main_job->calc_checksum); | |
562 | assert(main_job->checksum); | |
563 | assert(checksum_job); | |
564 | assert(checksum_job->state == PULL_JOB_DONE); | |
565 | ||
566 | if (!checksum_job->payload || checksum_job->payload_size <= 0) | |
567 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
568 | "Checksum is empty, cannot verify."); | |
569 | ||
570 | FOREACH_POINTER(j, main_job, settings_job, roothash_job, roothash_signature_job, verity_job) { | |
571 | r = verify_one(checksum_job, j); | |
572 | if (r < 0) | |
573 | return r; | |
574 | } | |
575 | ||
576 | verify_job = checksum_job; | |
577 | } | |
578 | ||
579 | if (verify != IMPORT_VERIFY_SIGNATURE) | |
ac71ece3 LP |
580 | return 0; |
581 | ||
c40d82ab LP |
582 | assert(verify_job); |
583 | ||
584 | r = verification_style_from_url(verify_job->url, &style); | |
ac71ece3 | 585 | if (r < 0) |
c40d82ab | 586 | return log_error_errno(r, "Failed to determine verification style from URL '%s': %m", verify_job->url); |
ac71ece3 LP |
587 | |
588 | if (style == VERIFICATION_PER_DIRECTORY) { | |
589 | assert(signature_job); | |
590 | assert(signature_job->state == PULL_JOB_DONE); | |
591 | ||
592 | if (!signature_job->payload || signature_job->payload_size <= 0) | |
593 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
594 | "Signature is empty, cannot verify."); | |
595 | ||
c40d82ab | 596 | return verify_gpg(verify_job->payload, verify_job->payload_size, signature_job->payload, signature_job->payload_size); |
ac71ece3 | 597 | } else |
c40d82ab | 598 | return verify_gpg(verify_job->payload, verify_job->payload_size, NULL, 0); |
ac71ece3 LP |
599 | } |
600 | ||
f14717a7 LP |
601 | int verification_style_from_url(const char *url, VerificationStyle *ret) { |
602 | _cleanup_free_ char *last = NULL; | |
603 | int r; | |
604 | ||
605 | assert(url); | |
606 | assert(ret); | |
607 | ||
608 | /* Determines which kind of verification style is appropriate for this url */ | |
609 | ||
610 | r = import_url_last_component(url, &last); | |
611 | if (r < 0) | |
612 | return r; | |
613 | ||
614 | if (streq(last, "SHA256SUMS")) { | |
615 | *ret = VERIFICATION_PER_DIRECTORY; | |
616 | return 0; | |
617 | } | |
618 | ||
619 | if (endswith(last, ".sha256")) { | |
620 | *ret = VERIFICATION_PER_FILE; | |
621 | return 0; | |
622 | } | |
623 | ||
624 | return -EINVAL; | |
625 | } | |
626 | ||
627 | int pull_job_restart_with_sha256sum(PullJob *j, char **ret) { | |
628 | VerificationStyle style; | |
629 | int r; | |
630 | ||
631 | assert(j); | |
632 | ||
633 | /* Generic implementation of a PullJobNotFound handler, that restarts the job requesting SHA256SUMS */ | |
634 | ||
635 | r = verification_style_from_url(j->url, &style); | |
636 | if (r < 0) | |
637 | return log_error_errno(r, "Failed to determine verification style of URL '%s': %m", j->url); | |
638 | ||
639 | if (style == VERIFICATION_PER_DIRECTORY) /* Nothing to do anymore */ | |
640 | return 0; | |
641 | ||
642 | assert(style == VERIFICATION_PER_FILE); /* This must have been .sha256 style URL before */ | |
643 | ||
644 | log_debug("Got 404 for %s, now trying to get SHA256SUMS instead.", j->url); | |
645 | ||
646 | r = import_url_change_last_component(j->url, "SHA256SUMS", ret); | |
647 | if (r < 0) | |
648 | return log_error_errno(r, "Failed to replace SHA256SUMS suffix: %m"); | |
649 | ||
650 | return 1; | |
651 | } | |
c40d82ab LP |
652 | |
653 | bool pull_validate_local(const char *name, PullFlags flags) { | |
654 | ||
655 | if (FLAGS_SET(flags, PULL_DIRECT)) | |
656 | return path_is_valid(name); | |
657 | ||
658 | return hostname_is_valid(name, 0); | |
659 | } | |
660 | ||
661 | int pull_url_needs_checksum(const char *url) { | |
662 | _cleanup_free_ char *fn = NULL; | |
663 | int r; | |
664 | ||
665 | /* Returns true if we need to validate this resource via a hash value. This returns true for all | |
666 | * files — except for gpg signature files and SHA256SUMS files and the like, which are validated with | |
667 | * a validation tool like gpg. */ | |
668 | ||
669 | r = import_url_last_component(url, &fn); | |
670 | if (r == -EADDRNOTAVAIL) /* no last component? then let's assume it's not a signature/checksum file */ | |
671 | return false; | |
672 | if (r < 0) | |
673 | return r; | |
674 | ||
675 | return !is_checksum_file(fn) && !is_signature_file(fn); | |
676 | } |