]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <fcntl.h> | |
4 | #include <sys/stat.h> | |
5 | #include <sys/xattr.h> | |
6 | ||
7 | #include "alloc-util.h" | |
8 | #include "fd-util.h" | |
9 | #include "format-util.h" | |
10 | #include "gcrypt-util.h" | |
11 | #include "hexdecoct.h" | |
12 | #include "import-util.h" | |
13 | #include "io-util.h" | |
14 | #include "machine-pool.h" | |
15 | #include "parse-util.h" | |
16 | #include "pull-common.h" | |
17 | #include "pull-job.h" | |
18 | #include "string-util.h" | |
19 | #include "strv.h" | |
20 | #include "xattr-util.h" | |
21 | ||
22 | PullJob* pull_job_unref(PullJob *j) { | |
23 | if (!j) | |
24 | return NULL; | |
25 | ||
26 | curl_glue_remove_and_free(j->glue, j->curl); | |
27 | curl_slist_free_all(j->request_header); | |
28 | ||
29 | safe_close(j->disk_fd); | |
30 | ||
31 | import_compress_free(&j->compress); | |
32 | ||
33 | if (j->checksum_context) | |
34 | gcry_md_close(j->checksum_context); | |
35 | ||
36 | free(j->url); | |
37 | free(j->etag); | |
38 | strv_free(j->old_etags); | |
39 | free(j->payload); | |
40 | free(j->checksum); | |
41 | ||
42 | return mfree(j); | |
43 | } | |
44 | ||
45 | static void pull_job_finish(PullJob *j, int ret) { | |
46 | assert(j); | |
47 | ||
48 | if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) | |
49 | return; | |
50 | ||
51 | if (ret == 0) { | |
52 | j->state = PULL_JOB_DONE; | |
53 | j->progress_percent = 100; | |
54 | log_info("Download of %s complete.", j->url); | |
55 | } else { | |
56 | j->state = PULL_JOB_FAILED; | |
57 | j->error = ret; | |
58 | } | |
59 | ||
60 | if (j->on_finished) | |
61 | j->on_finished(j); | |
62 | } | |
63 | ||
64 | static int pull_job_restart(PullJob *j, const char *new_url) { | |
65 | int r; | |
66 | ||
67 | assert(j); | |
68 | assert(new_url); | |
69 | ||
70 | r = free_and_strdup(&j->url, new_url); | |
71 | if (r < 0) | |
72 | return r; | |
73 | ||
74 | j->state = PULL_JOB_INIT; | |
75 | j->payload = mfree(j->payload); | |
76 | j->payload_size = 0; | |
77 | j->payload_allocated = 0; | |
78 | j->written_compressed = 0; | |
79 | j->written_uncompressed = 0; | |
80 | ||
81 | r = pull_job_begin(j); | |
82 | if (r < 0) | |
83 | return r; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) { | |
89 | PullJob *j = NULL; | |
90 | CURLcode code; | |
91 | long status; | |
92 | int r; | |
93 | ||
94 | if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK) | |
95 | return; | |
96 | ||
97 | if (!j || IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) | |
98 | return; | |
99 | ||
100 | if (result != CURLE_OK) { | |
101 | log_error("Transfer failed: %s", curl_easy_strerror(result)); | |
102 | r = -EIO; | |
103 | goto finish; | |
104 | } | |
105 | ||
106 | code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); | |
107 | if (code != CURLE_OK) { | |
108 | log_error("Failed to retrieve response code: %s", curl_easy_strerror(code)); | |
109 | r = -EIO; | |
110 | goto finish; | |
111 | } else if (status == 304) { | |
112 | log_info("Image already downloaded. Skipping download."); | |
113 | j->etag_exists = true; | |
114 | r = 0; | |
115 | goto finish; | |
116 | } else if (status >= 300) { | |
117 | ||
118 | if (status == 404 && j->on_not_found) { | |
119 | _cleanup_free_ char *new_url = NULL; | |
120 | ||
121 | /* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */ | |
122 | r = j->on_not_found(j, &new_url); | |
123 | if (r < 0) | |
124 | goto finish; | |
125 | ||
126 | if (r > 0) { /* A new url to use */ | |
127 | assert(new_url); | |
128 | ||
129 | r = pull_job_restart(j, new_url); | |
130 | if (r < 0) | |
131 | goto finish; | |
132 | ||
133 | code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status); | |
134 | if (code != CURLE_OK) { | |
135 | log_error("Failed to retrieve response code: %s", curl_easy_strerror(code)); | |
136 | r = -EIO; | |
137 | goto finish; | |
138 | } | |
139 | ||
140 | if (status == 0) | |
141 | return; | |
142 | } | |
143 | } | |
144 | ||
145 | log_error("HTTP request to %s failed with code %li.", j->url, status); | |
146 | r = -EIO; | |
147 | goto finish; | |
148 | } else if (status < 200) { | |
149 | log_error("HTTP request to %s finished with unexpected code %li.", j->url, status); | |
150 | r = -EIO; | |
151 | goto finish; | |
152 | } | |
153 | ||
154 | if (j->state != PULL_JOB_RUNNING) { | |
155 | log_error("Premature connection termination."); | |
156 | r = -EIO; | |
157 | goto finish; | |
158 | } | |
159 | ||
160 | if (j->content_length != (uint64_t) -1 && | |
161 | j->content_length != j->written_compressed) { | |
162 | log_error("Download truncated."); | |
163 | r = -EIO; | |
164 | goto finish; | |
165 | } | |
166 | ||
167 | if (j->checksum_context) { | |
168 | uint8_t *k; | |
169 | ||
170 | k = gcry_md_read(j->checksum_context, GCRY_MD_SHA256); | |
171 | if (!k) { | |
172 | log_error("Failed to get checksum."); | |
173 | r = -EIO; | |
174 | goto finish; | |
175 | } | |
176 | ||
177 | j->checksum = hexmem(k, gcry_md_get_algo_dlen(GCRY_MD_SHA256)); | |
178 | if (!j->checksum) { | |
179 | r = log_oom(); | |
180 | goto finish; | |
181 | } | |
182 | ||
183 | log_debug("SHA256 of %s is %s.", j->url, j->checksum); | |
184 | } | |
185 | ||
186 | if (j->disk_fd >= 0 && j->allow_sparse) { | |
187 | /* Make sure the file size is right, in case the file was | |
188 | * sparse and we just seeked for the last part */ | |
189 | ||
190 | if (ftruncate(j->disk_fd, j->written_uncompressed) < 0) { | |
191 | r = log_error_errno(errno, "Failed to truncate file: %m"); | |
192 | goto finish; | |
193 | } | |
194 | ||
195 | if (j->etag) | |
196 | (void) fsetxattr(j->disk_fd, "user.source_etag", j->etag, strlen(j->etag), 0); | |
197 | if (j->url) | |
198 | (void) fsetxattr(j->disk_fd, "user.source_url", j->url, strlen(j->url), 0); | |
199 | ||
200 | if (j->mtime != 0) { | |
201 | struct timespec ut[2]; | |
202 | ||
203 | timespec_store(&ut[0], j->mtime); | |
204 | ut[1] = ut[0]; | |
205 | (void) futimens(j->disk_fd, ut); | |
206 | ||
207 | (void) fd_setcrtime(j->disk_fd, j->mtime); | |
208 | } | |
209 | } | |
210 | ||
211 | r = 0; | |
212 | ||
213 | finish: | |
214 | pull_job_finish(j, r); | |
215 | } | |
216 | ||
217 | static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata) { | |
218 | PullJob *j = userdata; | |
219 | ssize_t n; | |
220 | ||
221 | assert(j); | |
222 | assert(p); | |
223 | ||
224 | if (sz <= 0) | |
225 | return 0; | |
226 | ||
227 | if (j->written_uncompressed + sz < j->written_uncompressed) | |
228 | return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), | |
229 | "File too large, overflow"); | |
230 | ||
231 | if (j->written_uncompressed + sz > j->uncompressed_max) | |
232 | return log_error_errno(SYNTHETIC_ERRNO(EFBIG), | |
233 | "File overly large, refusing"); | |
234 | ||
235 | if (j->disk_fd >= 0) { | |
236 | ||
237 | if (j->allow_sparse) | |
238 | n = sparse_write(j->disk_fd, p, sz, 64); | |
239 | else { | |
240 | n = write(j->disk_fd, p, sz); | |
241 | if (n < 0) | |
242 | n = -errno; | |
243 | } | |
244 | if (n < 0) | |
245 | return log_error_errno((int) n, "Failed to write file: %m"); | |
246 | if ((size_t) n < sz) | |
247 | return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write"); | |
248 | } else { | |
249 | ||
250 | if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz)) | |
251 | return log_oom(); | |
252 | ||
253 | memcpy(j->payload + j->payload_size, p, sz); | |
254 | j->payload_size += sz; | |
255 | } | |
256 | ||
257 | j->written_uncompressed += sz; | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | static int pull_job_write_compressed(PullJob *j, void *p, size_t sz) { | |
263 | int r; | |
264 | ||
265 | assert(j); | |
266 | assert(p); | |
267 | ||
268 | if (sz <= 0) | |
269 | return 0; | |
270 | ||
271 | if (j->written_compressed + sz < j->written_compressed) | |
272 | return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow"); | |
273 | ||
274 | if (j->written_compressed + sz > j->compressed_max) | |
275 | return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing."); | |
276 | ||
277 | if (j->content_length != (uint64_t) -1 && | |
278 | j->written_compressed + sz > j->content_length) | |
279 | return log_error_errno(SYNTHETIC_ERRNO(EFBIG), | |
280 | "Content length incorrect."); | |
281 | ||
282 | if (j->checksum_context) | |
283 | gcry_md_write(j->checksum_context, p, sz); | |
284 | ||
285 | r = import_uncompress(&j->compress, p, sz, pull_job_write_uncompressed, j); | |
286 | if (r < 0) | |
287 | return r; | |
288 | ||
289 | j->written_compressed += sz; | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | static int pull_job_open_disk(PullJob *j) { | |
295 | int r; | |
296 | ||
297 | assert(j); | |
298 | ||
299 | if (j->on_open_disk) { | |
300 | r = j->on_open_disk(j); | |
301 | if (r < 0) | |
302 | return r; | |
303 | } | |
304 | ||
305 | if (j->disk_fd >= 0) { | |
306 | /* Check if we can do sparse files */ | |
307 | ||
308 | if (lseek(j->disk_fd, SEEK_SET, 0) == 0) | |
309 | j->allow_sparse = true; | |
310 | else { | |
311 | if (errno != ESPIPE) | |
312 | return log_error_errno(errno, "Failed to seek on file descriptor: %m"); | |
313 | ||
314 | j->allow_sparse = false; | |
315 | } | |
316 | } | |
317 | ||
318 | if (j->calc_checksum) { | |
319 | initialize_libgcrypt(false); | |
320 | ||
321 | if (gcry_md_open(&j->checksum_context, GCRY_MD_SHA256, 0) != 0) | |
322 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
323 | "Failed to initialize hash context."); | |
324 | } | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
329 | static int pull_job_detect_compression(PullJob *j) { | |
330 | _cleanup_free_ uint8_t *stub = NULL; | |
331 | size_t stub_size; | |
332 | ||
333 | int r; | |
334 | ||
335 | assert(j); | |
336 | ||
337 | r = import_uncompress_detect(&j->compress, j->payload, j->payload_size); | |
338 | if (r < 0) | |
339 | return log_error_errno(r, "Failed to initialize compressor: %m"); | |
340 | if (r == 0) | |
341 | return 0; | |
342 | ||
343 | log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type)); | |
344 | ||
345 | r = pull_job_open_disk(j); | |
346 | if (r < 0) | |
347 | return r; | |
348 | ||
349 | /* Now, take the payload we read so far, and decompress it */ | |
350 | stub = j->payload; | |
351 | stub_size = j->payload_size; | |
352 | ||
353 | j->payload = NULL; | |
354 | j->payload_size = 0; | |
355 | j->payload_allocated = 0; | |
356 | ||
357 | j->state = PULL_JOB_RUNNING; | |
358 | ||
359 | r = pull_job_write_compressed(j, stub, stub_size); | |
360 | if (r < 0) | |
361 | return r; | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
366 | static size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) { | |
367 | PullJob *j = userdata; | |
368 | size_t sz = size * nmemb; | |
369 | int r; | |
370 | ||
371 | assert(contents); | |
372 | assert(j); | |
373 | ||
374 | switch (j->state) { | |
375 | ||
376 | case PULL_JOB_ANALYZING: | |
377 | /* Let's first check what it actually is */ | |
378 | ||
379 | if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz)) { | |
380 | r = log_oom(); | |
381 | goto fail; | |
382 | } | |
383 | ||
384 | memcpy(j->payload + j->payload_size, contents, sz); | |
385 | j->payload_size += sz; | |
386 | ||
387 | r = pull_job_detect_compression(j); | |
388 | if (r < 0) | |
389 | goto fail; | |
390 | ||
391 | break; | |
392 | ||
393 | case PULL_JOB_RUNNING: | |
394 | ||
395 | r = pull_job_write_compressed(j, contents, sz); | |
396 | if (r < 0) | |
397 | goto fail; | |
398 | ||
399 | break; | |
400 | ||
401 | case PULL_JOB_DONE: | |
402 | case PULL_JOB_FAILED: | |
403 | r = -ESTALE; | |
404 | goto fail; | |
405 | ||
406 | default: | |
407 | assert_not_reached("Impossible state."); | |
408 | } | |
409 | ||
410 | return sz; | |
411 | ||
412 | fail: | |
413 | pull_job_finish(j, r); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | static size_t pull_job_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) { | |
418 | PullJob *j = userdata; | |
419 | size_t sz = size * nmemb; | |
420 | _cleanup_free_ char *length = NULL, *last_modified = NULL; | |
421 | char *etag; | |
422 | int r; | |
423 | ||
424 | assert(contents); | |
425 | assert(j); | |
426 | ||
427 | if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) { | |
428 | r = -ESTALE; | |
429 | goto fail; | |
430 | } | |
431 | ||
432 | assert(j->state == PULL_JOB_ANALYZING); | |
433 | ||
434 | r = curl_header_strdup(contents, sz, "ETag:", &etag); | |
435 | if (r < 0) { | |
436 | log_oom(); | |
437 | goto fail; | |
438 | } | |
439 | if (r > 0) { | |
440 | free(j->etag); | |
441 | j->etag = etag; | |
442 | ||
443 | if (strv_contains(j->old_etags, j->etag)) { | |
444 | log_info("Image already downloaded. Skipping download."); | |
445 | j->etag_exists = true; | |
446 | pull_job_finish(j, 0); | |
447 | return sz; | |
448 | } | |
449 | ||
450 | return sz; | |
451 | } | |
452 | ||
453 | r = curl_header_strdup(contents, sz, "Content-Length:", &length); | |
454 | if (r < 0) { | |
455 | log_oom(); | |
456 | goto fail; | |
457 | } | |
458 | if (r > 0) { | |
459 | (void) safe_atou64(length, &j->content_length); | |
460 | ||
461 | if (j->content_length != (uint64_t) -1) { | |
462 | char bytes[FORMAT_BYTES_MAX]; | |
463 | ||
464 | if (j->content_length > j->compressed_max) { | |
465 | log_error("Content too large."); | |
466 | r = -EFBIG; | |
467 | goto fail; | |
468 | } | |
469 | ||
470 | log_info("Downloading %s for %s.", format_bytes(bytes, sizeof(bytes), j->content_length), j->url); | |
471 | } | |
472 | ||
473 | return sz; | |
474 | } | |
475 | ||
476 | r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified); | |
477 | if (r < 0) { | |
478 | log_oom(); | |
479 | goto fail; | |
480 | } | |
481 | if (r > 0) { | |
482 | (void) curl_parse_http_time(last_modified, &j->mtime); | |
483 | return sz; | |
484 | } | |
485 | ||
486 | if (j->on_header) { | |
487 | r = j->on_header(j, contents, sz); | |
488 | if (r < 0) | |
489 | goto fail; | |
490 | } | |
491 | ||
492 | return sz; | |
493 | ||
494 | fail: | |
495 | pull_job_finish(j, r); | |
496 | return 0; | |
497 | } | |
498 | ||
499 | static int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { | |
500 | PullJob *j = userdata; | |
501 | unsigned percent; | |
502 | usec_t n; | |
503 | ||
504 | assert(j); | |
505 | ||
506 | if (dltotal <= 0) | |
507 | return 0; | |
508 | ||
509 | percent = ((100 * dlnow) / dltotal); | |
510 | n = now(CLOCK_MONOTONIC); | |
511 | ||
512 | if (n > j->last_status_usec + USEC_PER_SEC && | |
513 | percent != j->progress_percent && | |
514 | dlnow < dltotal) { | |
515 | char buf[FORMAT_TIMESPAN_MAX]; | |
516 | ||
517 | if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) { | |
518 | char y[FORMAT_BYTES_MAX]; | |
519 | usec_t left, done; | |
520 | ||
521 | done = n - j->start_usec; | |
522 | left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done; | |
523 | ||
524 | log_info("Got %u%% of %s. %s left at %s/s.", | |
525 | percent, | |
526 | j->url, | |
527 | format_timespan(buf, sizeof(buf), left, USEC_PER_SEC), | |
528 | format_bytes(y, sizeof(y), (uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC)))); | |
529 | } else | |
530 | log_info("Got %u%% of %s.", percent, j->url); | |
531 | ||
532 | j->progress_percent = percent; | |
533 | j->last_status_usec = n; | |
534 | ||
535 | if (j->on_progress) | |
536 | j->on_progress(j); | |
537 | } | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | int pull_job_new(PullJob **ret, const char *url, CurlGlue *glue, void *userdata) { | |
543 | _cleanup_(pull_job_unrefp) PullJob *j = NULL; | |
544 | _cleanup_free_ char *u = NULL; | |
545 | ||
546 | assert(url); | |
547 | assert(glue); | |
548 | assert(ret); | |
549 | ||
550 | u = strdup(url); | |
551 | if (!u) | |
552 | return -ENOMEM; | |
553 | ||
554 | j = new(PullJob, 1); | |
555 | if (!j) | |
556 | return -ENOMEM; | |
557 | ||
558 | *j = (PullJob) { | |
559 | .state = PULL_JOB_INIT, | |
560 | .disk_fd = -1, | |
561 | .userdata = userdata, | |
562 | .glue = glue, | |
563 | .content_length = (uint64_t) -1, | |
564 | .start_usec = now(CLOCK_MONOTONIC), | |
565 | .compressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */ | |
566 | .uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */ | |
567 | .url = TAKE_PTR(u), | |
568 | }; | |
569 | ||
570 | *ret = TAKE_PTR(j); | |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
575 | int pull_job_begin(PullJob *j) { | |
576 | int r; | |
577 | ||
578 | assert(j); | |
579 | ||
580 | if (j->state != PULL_JOB_INIT) | |
581 | return -EBUSY; | |
582 | ||
583 | r = curl_glue_make(&j->curl, j->url, j); | |
584 | if (r < 0) | |
585 | return r; | |
586 | ||
587 | if (!strv_isempty(j->old_etags)) { | |
588 | _cleanup_free_ char *cc = NULL, *hdr = NULL; | |
589 | ||
590 | cc = strv_join(j->old_etags, ", "); | |
591 | if (!cc) | |
592 | return -ENOMEM; | |
593 | ||
594 | hdr = strjoin("If-None-Match: ", cc); | |
595 | if (!hdr) | |
596 | return -ENOMEM; | |
597 | ||
598 | if (!j->request_header) { | |
599 | j->request_header = curl_slist_new(hdr, NULL); | |
600 | if (!j->request_header) | |
601 | return -ENOMEM; | |
602 | } else { | |
603 | struct curl_slist *l; | |
604 | ||
605 | l = curl_slist_append(j->request_header, hdr); | |
606 | if (!l) | |
607 | return -ENOMEM; | |
608 | ||
609 | j->request_header = l; | |
610 | } | |
611 | } | |
612 | ||
613 | if (j->request_header) { | |
614 | if (curl_easy_setopt(j->curl, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK) | |
615 | return -EIO; | |
616 | } | |
617 | ||
618 | if (curl_easy_setopt(j->curl, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK) | |
619 | return -EIO; | |
620 | ||
621 | if (curl_easy_setopt(j->curl, CURLOPT_WRITEDATA, j) != CURLE_OK) | |
622 | return -EIO; | |
623 | ||
624 | if (curl_easy_setopt(j->curl, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK) | |
625 | return -EIO; | |
626 | ||
627 | if (curl_easy_setopt(j->curl, CURLOPT_HEADERDATA, j) != CURLE_OK) | |
628 | return -EIO; | |
629 | ||
630 | if (curl_easy_setopt(j->curl, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK) | |
631 | return -EIO; | |
632 | ||
633 | if (curl_easy_setopt(j->curl, CURLOPT_XFERINFODATA, j) != CURLE_OK) | |
634 | return -EIO; | |
635 | ||
636 | if (curl_easy_setopt(j->curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK) | |
637 | return -EIO; | |
638 | ||
639 | r = curl_glue_add(j->glue, j->curl); | |
640 | if (r < 0) | |
641 | return r; | |
642 | ||
643 | j->state = PULL_JOB_ANALYZING; | |
644 | ||
645 | return 0; | |
646 | } |