1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "btrfs-util.h"
7 #include "capability-util.h"
9 #include "dirent-util.h"
13 #include "path-util.h"
14 #include "process-util.h"
15 #include "pull-common.h"
18 #include "signal-util.h"
19 #include "siphash24.h"
20 #include "string-util.h"
25 #define FILENAME_ESCAPE "/.#\"\'"
26 #define HASH_URL_THRESHOLD_LENGTH (_POSIX_PATH_MAX - 16)
28 int pull_find_old_etags(
30 const char *image_root
,
36 _cleanup_free_
char *escaped_url
= NULL
;
37 _cleanup_closedir_
DIR *d
= NULL
;
38 _cleanup_strv_free_
char **l
= NULL
;
46 image_root
= "/var/lib/machines";
48 escaped_url
= xescape(url
, FILENAME_ESCAPE
);
52 d
= opendir(image_root
);
54 if (errno
== ENOENT
) {
62 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
66 if (de
->d_type
!= DT_UNKNOWN
&&
71 a
= startswith(de
->d_name
, prefix
);
77 a
= startswith(a
, escaped_url
);
81 a
= startswith(a
, ".");
86 b
= endswith(de
->d_name
, suffix
);
90 b
= strchr(de
->d_name
, 0);
95 r
= cunescape_length(a
, b
- a
, 0, &u
);
99 if (!http_etag_is_valid(u
)) {
104 r
= strv_consume(&l
, u
);
109 *etags
= TAKE_PTR(l
);
114 int pull_make_local_copy(const char *final
, const char *image_root
, const char *local
, bool force_local
) {
122 image_root
= "/var/lib/machines";
124 p
= strjoina(image_root
, "/", local
);
127 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
129 r
= btrfs_subvol_snapshot(final
, p
,
130 BTRFS_SNAPSHOT_QUOTA
|
131 BTRFS_SNAPSHOT_FALLBACK_COPY
|
132 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
133 BTRFS_SNAPSHOT_RECURSIVE
);
135 return log_error_errno(r
, "Failed to create local image: %m");
137 log_info("Created new local image '%s'.", local
);
142 static int hash_url(const char *url
, char **ret
) {
144 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
);
148 h
= siphash24(url
, strlen(url
), k
.bytes
);
149 if (asprintf(ret
, "%"PRIx64
, h
) < 0)
155 int pull_make_path(const char *url
, const char *etag
, const char *image_root
, const char *prefix
, const char *suffix
, char **ret
) {
156 _cleanup_free_
char *escaped_url
= NULL
, *escaped_etag
= NULL
;
163 image_root
= "/var/lib/machines";
165 escaped_url
= xescape(url
, FILENAME_ESCAPE
);
170 escaped_etag
= xescape(etag
, FILENAME_ESCAPE
);
175 path
= strjoin(image_root
, "/", strempty(prefix
), escaped_url
, escaped_etag
? "." : "",
176 strempty(escaped_etag
), strempty(suffix
));
180 /* URLs might make the path longer than the maximum allowed length for a file name.
181 * When that happens, a URL hash is used instead. Paths returned by this function
182 * can be later used with tempfn_random() which adds 16 bytes to the resulting name. */
183 if (strlen(path
) >= HASH_URL_THRESHOLD_LENGTH
) {
184 _cleanup_free_
char *hash
= NULL
;
189 r
= hash_url(url
, &hash
);
193 path
= strjoin(image_root
, "/", strempty(prefix
), hash
, escaped_etag
? "." : "",
194 strempty(escaped_etag
), strempty(suffix
));
203 int pull_make_auxiliary_job(
206 int (*strip_suffixes
)(const char *name
, char **ret
),
209 PullJobFinished on_finished
,
212 _cleanup_free_
char *last_component
= NULL
, *ll
= NULL
, *auxiliary_url
= NULL
;
213 _cleanup_(pull_job_unrefp
) PullJob
*job
= NULL
;
219 assert(strip_suffixes
);
222 r
= import_url_last_component(url
, &last_component
);
226 r
= strip_suffixes(last_component
, &ll
);
230 q
= strjoina(ll
, suffix
);
232 r
= import_url_change_last_component(url
, q
, &auxiliary_url
);
236 r
= pull_job_new(&job
, auxiliary_url
, glue
, userdata
);
240 job
->on_finished
= on_finished
;
241 job
->compressed_max
= job
->uncompressed_max
= 1ULL * 1024ULL * 1024ULL;
243 *ret
= TAKE_PTR(job
);
248 int pull_make_verification_jobs(
249 PullJob
**ret_checksum_job
,
250 PullJob
**ret_signature_job
,
254 PullJobFinished on_finished
,
257 _cleanup_(pull_job_unrefp
) PullJob
*checksum_job
= NULL
, *signature_job
= NULL
;
259 const char *chksums
= NULL
;
261 assert(ret_checksum_job
);
262 assert(ret_signature_job
);
264 assert(verify
< _IMPORT_VERIFY_MAX
);
268 if (verify
!= IMPORT_VERIFY_NO
) {
269 _cleanup_free_
char *checksum_url
= NULL
, *fn
= NULL
;
271 /* Queue jobs for the checksum file for the image. */
272 r
= import_url_last_component(url
, &fn
);
276 chksums
= strjoina(fn
, ".sha256");
278 r
= import_url_change_last_component(url
, chksums
, &checksum_url
);
282 r
= pull_job_new(&checksum_job
, checksum_url
, glue
, userdata
);
286 checksum_job
->on_finished
= on_finished
;
287 checksum_job
->uncompressed_max
= checksum_job
->compressed_max
= 1ULL * 1024ULL * 1024ULL;
290 if (verify
== IMPORT_VERIFY_SIGNATURE
) {
291 _cleanup_free_
char *signature_url
= NULL
;
293 /* Queue job for the SHA256SUMS.gpg file for the image. */
294 r
= import_url_change_last_component(url
, "SHA256SUMS.gpg", &signature_url
);
298 r
= pull_job_new(&signature_job
, signature_url
, glue
, userdata
);
302 signature_job
->on_finished
= on_finished
;
303 signature_job
->uncompressed_max
= signature_job
->compressed_max
= 1ULL * 1024ULL * 1024ULL;
306 *ret_checksum_job
= checksum_job
;
307 *ret_signature_job
= signature_job
;
309 checksum_job
= signature_job
= NULL
;
314 static int verify_one(PullJob
*checksum_job
, PullJob
*job
) {
315 _cleanup_free_
char *fn
= NULL
;
316 const char *line
, *p
;
319 assert(checksum_job
);
324 assert(IN_SET(job
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
));
326 /* Don't verify the checksum if we didn't actually successfully download something new */
327 if (job
->state
!= PULL_JOB_DONE
)
331 if (job
->etag_exists
)
334 assert(job
->calc_checksum
);
335 assert(job
->checksum
);
337 r
= import_url_last_component(job
->url
, &fn
);
341 if (!filename_is_valid(fn
)) {
342 log_error("Cannot verify checksum, could not determine server-side file name.");
346 line
= strjoina(job
->checksum
, " *", fn
, "\n");
348 p
= memmem(checksum_job
->payload
,
349 checksum_job
->payload_size
,
354 line
= strjoina(job
->checksum
, " ", fn
, "\n");
356 p
= memmem(checksum_job
->payload
,
357 checksum_job
->payload_size
,
362 if (!p
|| (p
!= (char*) checksum_job
->payload
&& p
[-1] != '\n')) {
363 log_error("DOWNLOAD INVALID: Checksum of %s file did not checkout, file has been tampered with.", fn
);
367 log_info("SHA256 checksum of %s is valid.", job
->url
);
371 int pull_verify(PullJob
*main_job
,
372 PullJob
*roothash_job
,
373 PullJob
*settings_job
,
374 PullJob
*checksum_job
,
375 PullJob
*signature_job
) {
377 _cleanup_close_pair_
int gpg_pipe
[2] = { -1, -1 };
378 _cleanup_close_
int sig_file
= -1;
379 char sig_file_path
[] = "/tmp/sigXXXXXX", gpg_home
[] = "/tmp/gpghomeXXXXXX";
380 _cleanup_(sigkill_waitp
) pid_t pid
= 0;
381 bool gpg_home_created
= false;
385 assert(main_job
->state
== PULL_JOB_DONE
);
390 assert(main_job
->calc_checksum
);
391 assert(main_job
->checksum
);
393 assert(checksum_job
->state
== PULL_JOB_DONE
);
395 if (!checksum_job
->payload
|| checksum_job
->payload_size
<= 0) {
396 log_error("Checksum is empty, cannot verify.");
400 r
= verify_one(checksum_job
, main_job
);
404 r
= verify_one(checksum_job
, roothash_job
);
408 r
= verify_one(checksum_job
, settings_job
);
415 if (checksum_job
->style
== VERIFICATION_PER_FILE
)
416 signature_job
= checksum_job
;
418 assert(signature_job
->state
== PULL_JOB_DONE
);
420 if (!signature_job
->payload
|| signature_job
->payload_size
<= 0) {
421 log_error("Signature is empty, cannot verify.");
425 r
= pipe2(gpg_pipe
, O_CLOEXEC
);
427 return log_error_errno(errno
, "Failed to create pipe for gpg: %m");
429 sig_file
= mkostemp(sig_file_path
, O_RDWR
);
431 return log_error_errno(errno
, "Failed to create temporary file: %m");
433 r
= loop_write(sig_file
, signature_job
->payload
, signature_job
->payload_size
, false);
435 log_error_errno(r
, "Failed to write to temporary file: %m");
439 if (!mkdtemp(gpg_home
)) {
440 r
= log_error_errno(errno
, "Failed to create tempory home for gpg: %m");
444 gpg_home_created
= true;
446 r
= safe_fork("(gpg)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
450 const char *cmd
[] = {
453 "--no-default-keyring",
454 "--no-auto-key-locate",
455 "--no-auto-check-trustdb",
457 "--trust-model=always",
458 NULL
, /* --homedir= */
459 NULL
, /* --keyring= */
461 NULL
, /* signature file */
463 NULL
/* trailing NULL */
465 unsigned k
= ELEMENTSOF(cmd
) - 6;
469 gpg_pipe
[1] = safe_close(gpg_pipe
[1]);
471 r
= rearrange_stdio(gpg_pipe
[0], -1, STDERR_FILENO
);
473 log_error_errno(r
, "Failed to rearrange stdin/stdout: %m");
477 cmd
[k
++] = strjoina("--homedir=", gpg_home
);
479 /* We add the user keyring only to the command line
480 * arguments, if it's around since gpg fails
482 if (access(USER_KEYRING_PATH
, F_OK
) >= 0)
483 cmd
[k
++] = "--keyring=" USER_KEYRING_PATH
;
485 cmd
[k
++] = "--keyring=" VENDOR_KEYRING_PATH
;
487 cmd
[k
++] = "--verify";
488 if (checksum_job
->style
== VERIFICATION_PER_DIRECTORY
) {
489 cmd
[k
++] = sig_file_path
;
494 execvp("gpg2", (char * const *) cmd
);
495 execvp("gpg", (char * const *) cmd
);
496 log_error_errno(errno
, "Failed to execute gpg: %m");
500 gpg_pipe
[0] = safe_close(gpg_pipe
[0]);
502 r
= loop_write(gpg_pipe
[1], checksum_job
->payload
, checksum_job
->payload_size
, false);
504 log_error_errno(r
, "Failed to write to pipe: %m");
508 gpg_pipe
[1] = safe_close(gpg_pipe
[1]);
510 r
= wait_for_terminate_and_check("gpg", pid
, WAIT_LOG_ABNORMAL
);
514 if (r
!= EXIT_SUCCESS
) {
515 log_error("DOWNLOAD INVALID: Signature verification failed.");
518 log_info("Signature verification succeeded.");
523 (void) unlink(sig_file_path
);
525 if (gpg_home_created
)
526 (void) rm_rf(gpg_home
, REMOVE_ROOT
|REMOVE_PHYSICAL
);