1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
7 #include "gcrypt-util.h"
9 #include "import-util.h"
11 #include "machine-pool.h"
12 #include "parse-util.h"
13 #include "pull-common.h"
15 #include "string-util.h"
17 #include "xattr-util.h"
19 PullJob
* pull_job_unref(PullJob
*j
) {
23 curl_glue_remove_and_free(j
->glue
, j
->curl
);
24 curl_slist_free_all(j
->request_header
);
26 safe_close(j
->disk_fd
);
28 import_compress_free(&j
->compress
);
30 if (j
->checksum_context
)
31 gcry_md_close(j
->checksum_context
);
35 strv_free(j
->old_etags
);
42 static void pull_job_finish(PullJob
*j
, int ret
) {
45 if (IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
))
49 j
->state
= PULL_JOB_DONE
;
50 j
->progress_percent
= 100;
51 log_info("Download of %s complete.", j
->url
);
53 j
->state
= PULL_JOB_FAILED
;
61 static int pull_job_restart(PullJob
*j
) {
63 char *chksum_url
= NULL
;
65 r
= import_url_change_last_component(j
->url
, "SHA256SUMS", &chksum_url
);
71 j
->state
= PULL_JOB_INIT
;
72 j
->payload
= mfree(j
->payload
);
74 j
->payload_allocated
= 0;
75 j
->written_compressed
= 0;
76 j
->written_uncompressed
= 0;
78 r
= pull_job_begin(j
);
85 void pull_job_curl_on_finished(CurlGlue
*g
, CURL
*curl
, CURLcode result
) {
91 if (curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&j
) != CURLE_OK
)
94 if (!j
|| IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
))
97 if (result
!= CURLE_OK
) {
98 log_error("Transfer failed: %s", curl_easy_strerror(result
));
103 code
= curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &status
);
104 if (code
!= CURLE_OK
) {
105 log_error("Failed to retrieve response code: %s", curl_easy_strerror(code
));
108 } else if (status
== 304) {
109 log_info("Image already downloaded. Skipping download.");
110 j
->etag_exists
= true;
113 } else if (status
>= 300) {
114 if (status
== 404 && j
->style
== VERIFICATION_PER_FILE
) {
116 /* retry pull job with SHA256SUMS file */
117 r
= pull_job_restart(j
);
121 code
= curl_easy_getinfo(j
->curl
, CURLINFO_RESPONSE_CODE
, &status
);
122 if (code
!= CURLE_OK
) {
123 log_error("Failed to retrieve response code: %s", curl_easy_strerror(code
));
129 j
->style
= VERIFICATION_PER_DIRECTORY
;
134 log_error("HTTP request to %s failed with code %li.", j
->url
, status
);
137 } else if (status
< 200) {
138 log_error("HTTP request to %s finished with unexpected code %li.", j
->url
, status
);
143 if (j
->state
!= PULL_JOB_RUNNING
) {
144 log_error("Premature connection termination.");
149 if (j
->content_length
!= (uint64_t) -1 &&
150 j
->content_length
!= j
->written_compressed
) {
151 log_error("Download truncated.");
156 if (j
->checksum_context
) {
159 k
= gcry_md_read(j
->checksum_context
, GCRY_MD_SHA256
);
161 log_error("Failed to get checksum.");
166 j
->checksum
= hexmem(k
, gcry_md_get_algo_dlen(GCRY_MD_SHA256
));
172 log_debug("SHA256 of %s is %s.", j
->url
, j
->checksum
);
175 if (j
->disk_fd
>= 0 && j
->allow_sparse
) {
176 /* Make sure the file size is right, in case the file was
177 * sparse and we just seeked for the last part */
179 if (ftruncate(j
->disk_fd
, j
->written_uncompressed
) < 0) {
180 r
= log_error_errno(errno
, "Failed to truncate file: %m");
185 (void) fsetxattr(j
->disk_fd
, "user.source_etag", j
->etag
, strlen(j
->etag
), 0);
187 (void) fsetxattr(j
->disk_fd
, "user.source_url", j
->url
, strlen(j
->url
), 0);
190 struct timespec ut
[2];
192 timespec_store(&ut
[0], j
->mtime
);
194 (void) futimens(j
->disk_fd
, ut
);
196 (void) fd_setcrtime(j
->disk_fd
, j
->mtime
);
203 pull_job_finish(j
, r
);
206 static int pull_job_write_uncompressed(const void *p
, size_t sz
, void *userdata
) {
207 PullJob
*j
= userdata
;
216 if (j
->written_uncompressed
+ sz
< j
->written_uncompressed
)
217 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
),
218 "File too large, overflow");
220 if (j
->written_uncompressed
+ sz
> j
->uncompressed_max
)
221 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
222 "File overly large, refusing");
224 if (j
->disk_fd
>= 0) {
227 n
= sparse_write(j
->disk_fd
, p
, sz
, 64);
229 n
= write(j
->disk_fd
, p
, sz
);
234 return log_error_errno((int) n
, "Failed to write file: %m");
236 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short write");
239 if (!GREEDY_REALLOC(j
->payload
, j
->payload_allocated
, j
->payload_size
+ sz
))
242 memcpy(j
->payload
+ j
->payload_size
, p
, sz
);
243 j
->payload_size
+= sz
;
246 j
->written_uncompressed
+= sz
;
251 static int pull_job_write_compressed(PullJob
*j
, void *p
, size_t sz
) {
260 if (j
->written_compressed
+ sz
< j
->written_compressed
)
261 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "File too large, overflow");
263 if (j
->written_compressed
+ sz
> j
->compressed_max
)
264 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
), "File overly large, refusing.");
266 if (j
->content_length
!= (uint64_t) -1 &&
267 j
->written_compressed
+ sz
> j
->content_length
)
268 return log_error_errno(SYNTHETIC_ERRNO(EFBIG
),
269 "Content length incorrect.");
271 if (j
->checksum_context
)
272 gcry_md_write(j
->checksum_context
, p
, sz
);
274 r
= import_uncompress(&j
->compress
, p
, sz
, pull_job_write_uncompressed
, j
);
278 j
->written_compressed
+= sz
;
283 static int pull_job_open_disk(PullJob
*j
) {
288 if (j
->on_open_disk
) {
289 r
= j
->on_open_disk(j
);
294 if (j
->disk_fd
>= 0) {
295 /* Check if we can do sparse files */
297 if (lseek(j
->disk_fd
, SEEK_SET
, 0) == 0)
298 j
->allow_sparse
= true;
301 return log_error_errno(errno
, "Failed to seek on file descriptor: %m");
303 j
->allow_sparse
= false;
307 if (j
->calc_checksum
) {
308 initialize_libgcrypt(false);
310 if (gcry_md_open(&j
->checksum_context
, GCRY_MD_SHA256
, 0) != 0)
311 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
312 "Failed to initialize hash context.");
318 static int pull_job_detect_compression(PullJob
*j
) {
319 _cleanup_free_
uint8_t *stub
= NULL
;
326 r
= import_uncompress_detect(&j
->compress
, j
->payload
, j
->payload_size
);
328 return log_error_errno(r
, "Failed to initialize compressor: %m");
332 log_debug("Stream is compressed: %s", import_compress_type_to_string(j
->compress
.type
));
334 r
= pull_job_open_disk(j
);
338 /* Now, take the payload we read so far, and decompress it */
340 stub_size
= j
->payload_size
;
344 j
->payload_allocated
= 0;
346 j
->state
= PULL_JOB_RUNNING
;
348 r
= pull_job_write_compressed(j
, stub
, stub_size
);
355 static size_t pull_job_write_callback(void *contents
, size_t size
, size_t nmemb
, void *userdata
) {
356 PullJob
*j
= userdata
;
357 size_t sz
= size
* nmemb
;
365 case PULL_JOB_ANALYZING
:
366 /* Let's first check what it actually is */
368 if (!GREEDY_REALLOC(j
->payload
, j
->payload_allocated
, j
->payload_size
+ sz
)) {
373 memcpy(j
->payload
+ j
->payload_size
, contents
, sz
);
374 j
->payload_size
+= sz
;
376 r
= pull_job_detect_compression(j
);
382 case PULL_JOB_RUNNING
:
384 r
= pull_job_write_compressed(j
, contents
, sz
);
391 case PULL_JOB_FAILED
:
396 assert_not_reached("Impossible state.");
402 pull_job_finish(j
, r
);
406 static size_t pull_job_header_callback(void *contents
, size_t size
, size_t nmemb
, void *userdata
) {
407 PullJob
*j
= userdata
;
408 size_t sz
= size
* nmemb
;
409 _cleanup_free_
char *length
= NULL
, *last_modified
= NULL
;
416 if (IN_SET(j
->state
, PULL_JOB_DONE
, PULL_JOB_FAILED
)) {
421 assert(j
->state
== PULL_JOB_ANALYZING
);
423 r
= curl_header_strdup(contents
, sz
, "ETag:", &etag
);
432 if (strv_contains(j
->old_etags
, j
->etag
)) {
433 log_info("Image already downloaded. Skipping download.");
434 j
->etag_exists
= true;
435 pull_job_finish(j
, 0);
442 r
= curl_header_strdup(contents
, sz
, "Content-Length:", &length
);
448 (void) safe_atou64(length
, &j
->content_length
);
450 if (j
->content_length
!= (uint64_t) -1) {
451 char bytes
[FORMAT_BYTES_MAX
];
453 if (j
->content_length
> j
->compressed_max
) {
454 log_error("Content too large.");
459 log_info("Downloading %s for %s.", format_bytes(bytes
, sizeof(bytes
), j
->content_length
), j
->url
);
465 r
= curl_header_strdup(contents
, sz
, "Last-Modified:", &last_modified
);
471 (void) curl_parse_http_time(last_modified
, &j
->mtime
);
476 r
= j
->on_header(j
, contents
, sz
);
484 pull_job_finish(j
, r
);
488 static int pull_job_progress_callback(void *userdata
, curl_off_t dltotal
, curl_off_t dlnow
, curl_off_t ultotal
, curl_off_t ulnow
) {
489 PullJob
*j
= userdata
;
498 percent
= ((100 * dlnow
) / dltotal
);
499 n
= now(CLOCK_MONOTONIC
);
501 if (n
> j
->last_status_usec
+ USEC_PER_SEC
&&
502 percent
!= j
->progress_percent
&&
504 char buf
[FORMAT_TIMESPAN_MAX
];
506 if (n
- j
->start_usec
> USEC_PER_SEC
&& dlnow
> 0) {
507 char y
[FORMAT_BYTES_MAX
];
510 done
= n
- j
->start_usec
;
511 left
= (usec_t
) (((double) done
* (double) dltotal
) / dlnow
) - done
;
513 log_info("Got %u%% of %s. %s left at %s/s.",
516 format_timespan(buf
, sizeof(buf
), left
, USEC_PER_SEC
),
517 format_bytes(y
, sizeof(y
), (uint64_t) ((double) dlnow
/ ((double) done
/ (double) USEC_PER_SEC
))));
519 log_info("Got %u%% of %s.", percent
, j
->url
);
521 j
->progress_percent
= percent
;
522 j
->last_status_usec
= n
;
531 int pull_job_new(PullJob
**ret
, const char *url
, CurlGlue
*glue
, void *userdata
) {
532 _cleanup_(pull_job_unrefp
) PullJob
*j
= NULL
;
533 _cleanup_free_
char *u
= NULL
;
548 .state
= PULL_JOB_INIT
,
550 .userdata
= userdata
,
552 .content_length
= (uint64_t) -1,
553 .start_usec
= now(CLOCK_MONOTONIC
),
554 .compressed_max
= 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
555 .uncompressed_max
= 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
556 .style
= VERIFICATION_STYLE_UNSET
,
565 int pull_job_begin(PullJob
*j
) {
570 if (j
->state
!= PULL_JOB_INIT
)
573 r
= curl_glue_make(&j
->curl
, j
->url
, j
);
577 if (!strv_isempty(j
->old_etags
)) {
578 _cleanup_free_
char *cc
= NULL
, *hdr
= NULL
;
580 cc
= strv_join(j
->old_etags
, ", ");
584 hdr
= strappend("If-None-Match: ", cc
);
588 if (!j
->request_header
) {
589 j
->request_header
= curl_slist_new(hdr
, NULL
);
590 if (!j
->request_header
)
593 struct curl_slist
*l
;
595 l
= curl_slist_append(j
->request_header
, hdr
);
599 j
->request_header
= l
;
603 if (j
->request_header
) {
604 if (curl_easy_setopt(j
->curl
, CURLOPT_HTTPHEADER
, j
->request_header
) != CURLE_OK
)
608 if (curl_easy_setopt(j
->curl
, CURLOPT_WRITEFUNCTION
, pull_job_write_callback
) != CURLE_OK
)
611 if (curl_easy_setopt(j
->curl
, CURLOPT_WRITEDATA
, j
) != CURLE_OK
)
614 if (curl_easy_setopt(j
->curl
, CURLOPT_HEADERFUNCTION
, pull_job_header_callback
) != CURLE_OK
)
617 if (curl_easy_setopt(j
->curl
, CURLOPT_HEADERDATA
, j
) != CURLE_OK
)
620 if (curl_easy_setopt(j
->curl
, CURLOPT_XFERINFOFUNCTION
, pull_job_progress_callback
) != CURLE_OK
)
623 if (curl_easy_setopt(j
->curl
, CURLOPT_XFERINFODATA
, j
) != CURLE_OK
)
626 if (curl_easy_setopt(j
->curl
, CURLOPT_NOPROGRESS
, 0) != CURLE_OK
)
629 r
= curl_glue_add(j
->glue
, j
->curl
);
633 j
->state
= PULL_JOB_ANALYZING
;