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