]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/pull-job.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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
78 r = pull_job_begin(j);
79 if (r < 0)
80 return r;
81
82 return 0;
83 }
84
85 void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
86 PullJob *j = NULL;
87 CURLcode code;
88 long status;
89 int r;
90
91 if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK)
92 return;
93
94 if (!j || IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED))
95 return;
96
97 if (result != CURLE_OK) {
98 log_error("Transfer failed: %s", curl_easy_strerror(result));
99 r = -EIO;
100 goto finish;
101 }
102
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));
106 r = -EIO;
107 goto finish;
108 } else if (status == 304) {
109 log_info("Image already downloaded. Skipping download.");
110 j->etag_exists = true;
111 r = 0;
112 goto finish;
113 } else if (status >= 300) {
114 if (status == 404 && j->style == VERIFICATION_PER_FILE) {
115
116 /* retry pull job with SHA256SUMS file */
117 r = pull_job_restart(j);
118 if (r < 0)
119 goto finish;
120
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));
124 r = -EIO;
125 goto finish;
126 }
127
128 if (status == 0) {
129 j->style = VERIFICATION_PER_DIRECTORY;
130 return;
131 }
132 }
133
134 log_error("HTTP request to %s failed with code %li.", j->url, status);
135 r = -EIO;
136 goto finish;
137 } else if (status < 200) {
138 log_error("HTTP request to %s finished with unexpected code %li.", j->url, status);
139 r = -EIO;
140 goto finish;
141 }
142
143 if (j->state != PULL_JOB_RUNNING) {
144 log_error("Premature connection termination.");
145 r = -EIO;
146 goto finish;
147 }
148
149 if (j->content_length != (uint64_t) -1 &&
150 j->content_length != j->written_compressed) {
151 log_error("Download truncated.");
152 r = -EIO;
153 goto finish;
154 }
155
156 if (j->checksum_context) {
157 uint8_t *k;
158
159 k = gcry_md_read(j->checksum_context, GCRY_MD_SHA256);
160 if (!k) {
161 log_error("Failed to get checksum.");
162 r = -EIO;
163 goto finish;
164 }
165
166 j->checksum = hexmem(k, gcry_md_get_algo_dlen(GCRY_MD_SHA256));
167 if (!j->checksum) {
168 r = log_oom();
169 goto finish;
170 }
171
172 log_debug("SHA256 of %s is %s.", j->url, j->checksum);
173 }
174
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 */
178
179 if (ftruncate(j->disk_fd, j->written_uncompressed) < 0) {
180 r = log_error_errno(errno, "Failed to truncate file: %m");
181 goto finish;
182 }
183
184 if (j->etag)
185 (void) fsetxattr(j->disk_fd, "user.source_etag", j->etag, strlen(j->etag), 0);
186 if (j->url)
187 (void) fsetxattr(j->disk_fd, "user.source_url", j->url, strlen(j->url), 0);
188
189 if (j->mtime != 0) {
190 struct timespec ut[2];
191
192 timespec_store(&ut[0], j->mtime);
193 ut[1] = ut[0];
194 (void) futimens(j->disk_fd, ut);
195
196 (void) fd_setcrtime(j->disk_fd, j->mtime);
197 }
198 }
199
200 r = 0;
201
202 finish:
203 pull_job_finish(j, r);
204 }
205
206 static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata) {
207 PullJob *j = userdata;
208 ssize_t n;
209
210 assert(j);
211 assert(p);
212
213 if (sz <= 0)
214 return 0;
215
216 if (j->written_uncompressed + sz < j->written_uncompressed)
217 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW),
218 "File too large, overflow");
219
220 if (j->written_uncompressed + sz > j->uncompressed_max)
221 return log_error_errno(SYNTHETIC_ERRNO(EFBIG),
222 "File overly large, refusing");
223
224 if (j->disk_fd >= 0) {
225
226 if (j->allow_sparse)
227 n = sparse_write(j->disk_fd, p, sz, 64);
228 else {
229 n = write(j->disk_fd, p, sz);
230 if (n < 0)
231 n = -errno;
232 }
233 if (n < 0)
234 return log_error_errno((int) n, "Failed to write file: %m");
235 if ((size_t) n < sz)
236 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write");
237 } else {
238
239 if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz))
240 return log_oom();
241
242 memcpy(j->payload + j->payload_size, p, sz);
243 j->payload_size += sz;
244 }
245
246 j->written_uncompressed += sz;
247
248 return 0;
249 }
250
251 static int pull_job_write_compressed(PullJob *j, void *p, size_t sz) {
252 int r;
253
254 assert(j);
255 assert(p);
256
257 if (sz <= 0)
258 return 0;
259
260 if (j->written_compressed + sz < j->written_compressed)
261 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow");
262
263 if (j->written_compressed + sz > j->compressed_max)
264 return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing.");
265
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.");
270
271 if (j->checksum_context)
272 gcry_md_write(j->checksum_context, p, sz);
273
274 r = import_uncompress(&j->compress, p, sz, pull_job_write_uncompressed, j);
275 if (r < 0)
276 return r;
277
278 j->written_compressed += sz;
279
280 return 0;
281 }
282
283 static int pull_job_open_disk(PullJob *j) {
284 int r;
285
286 assert(j);
287
288 if (j->on_open_disk) {
289 r = j->on_open_disk(j);
290 if (r < 0)
291 return r;
292 }
293
294 if (j->disk_fd >= 0) {
295 /* Check if we can do sparse files */
296
297 if (lseek(j->disk_fd, SEEK_SET, 0) == 0)
298 j->allow_sparse = true;
299 else {
300 if (errno != ESPIPE)
301 return log_error_errno(errno, "Failed to seek on file descriptor: %m");
302
303 j->allow_sparse = false;
304 }
305 }
306
307 if (j->calc_checksum) {
308 initialize_libgcrypt(false);
309
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.");
313 }
314
315 return 0;
316 }
317
318 static int pull_job_detect_compression(PullJob *j) {
319 _cleanup_free_ uint8_t *stub = NULL;
320 size_t stub_size;
321
322 int r;
323
324 assert(j);
325
326 r = import_uncompress_detect(&j->compress, j->payload, j->payload_size);
327 if (r < 0)
328 return log_error_errno(r, "Failed to initialize compressor: %m");
329 if (r == 0)
330 return 0;
331
332 log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type));
333
334 r = pull_job_open_disk(j);
335 if (r < 0)
336 return r;
337
338 /* Now, take the payload we read so far, and decompress it */
339 stub = j->payload;
340 stub_size = j->payload_size;
341
342 j->payload = NULL;
343 j->payload_size = 0;
344 j->payload_allocated = 0;
345
346 j->state = PULL_JOB_RUNNING;
347
348 r = pull_job_write_compressed(j, stub, stub_size);
349 if (r < 0)
350 return r;
351
352 return 0;
353 }
354
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;
358 int r;
359
360 assert(contents);
361 assert(j);
362
363 switch (j->state) {
364
365 case PULL_JOB_ANALYZING:
366 /* Let's first check what it actually is */
367
368 if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz)) {
369 r = log_oom();
370 goto fail;
371 }
372
373 memcpy(j->payload + j->payload_size, contents, sz);
374 j->payload_size += sz;
375
376 r = pull_job_detect_compression(j);
377 if (r < 0)
378 goto fail;
379
380 break;
381
382 case PULL_JOB_RUNNING:
383
384 r = pull_job_write_compressed(j, contents, sz);
385 if (r < 0)
386 goto fail;
387
388 break;
389
390 case PULL_JOB_DONE:
391 case PULL_JOB_FAILED:
392 r = -ESTALE;
393 goto fail;
394
395 default:
396 assert_not_reached("Impossible state.");
397 }
398
399 return sz;
400
401 fail:
402 pull_job_finish(j, r);
403 return 0;
404 }
405
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;
410 char *etag;
411 int r;
412
413 assert(contents);
414 assert(j);
415
416 if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) {
417 r = -ESTALE;
418 goto fail;
419 }
420
421 assert(j->state == PULL_JOB_ANALYZING);
422
423 r = curl_header_strdup(contents, sz, "ETag:", &etag);
424 if (r < 0) {
425 log_oom();
426 goto fail;
427 }
428 if (r > 0) {
429 free(j->etag);
430 j->etag = etag;
431
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);
436 return sz;
437 }
438
439 return sz;
440 }
441
442 r = curl_header_strdup(contents, sz, "Content-Length:", &length);
443 if (r < 0) {
444 log_oom();
445 goto fail;
446 }
447 if (r > 0) {
448 (void) safe_atou64(length, &j->content_length);
449
450 if (j->content_length != (uint64_t) -1) {
451 char bytes[FORMAT_BYTES_MAX];
452
453 if (j->content_length > j->compressed_max) {
454 log_error("Content too large.");
455 r = -EFBIG;
456 goto fail;
457 }
458
459 log_info("Downloading %s for %s.", format_bytes(bytes, sizeof(bytes), j->content_length), j->url);
460 }
461
462 return sz;
463 }
464
465 r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified);
466 if (r < 0) {
467 log_oom();
468 goto fail;
469 }
470 if (r > 0) {
471 (void) curl_parse_http_time(last_modified, &j->mtime);
472 return sz;
473 }
474
475 if (j->on_header) {
476 r = j->on_header(j, contents, sz);
477 if (r < 0)
478 goto fail;
479 }
480
481 return sz;
482
483 fail:
484 pull_job_finish(j, r);
485 return 0;
486 }
487
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;
490 unsigned percent;
491 usec_t n;
492
493 assert(j);
494
495 if (dltotal <= 0)
496 return 0;
497
498 percent = ((100 * dlnow) / dltotal);
499 n = now(CLOCK_MONOTONIC);
500
501 if (n > j->last_status_usec + USEC_PER_SEC &&
502 percent != j->progress_percent &&
503 dlnow < dltotal) {
504 char buf[FORMAT_TIMESPAN_MAX];
505
506 if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) {
507 char y[FORMAT_BYTES_MAX];
508 usec_t left, done;
509
510 done = n - j->start_usec;
511 left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done;
512
513 log_info("Got %u%% of %s. %s left at %s/s.",
514 percent,
515 j->url,
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))));
518 } else
519 log_info("Got %u%% of %s.", percent, j->url);
520
521 j->progress_percent = percent;
522 j->last_status_usec = n;
523
524 if (j->on_progress)
525 j->on_progress(j);
526 }
527
528 return 0;
529 }
530
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;
534
535 assert(url);
536 assert(glue);
537 assert(ret);
538
539 u = strdup(url);
540 if (!u)
541 return -ENOMEM;
542
543 j = new(PullJob, 1);
544 if (!j)
545 return -ENOMEM;
546
547 *j = (PullJob) {
548 .state = PULL_JOB_INIT,
549 .disk_fd = -1,
550 .userdata = userdata,
551 .glue = glue,
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,
557 .url = TAKE_PTR(u),
558 };
559
560 *ret = TAKE_PTR(j);
561
562 return 0;
563 }
564
565 int pull_job_begin(PullJob *j) {
566 int r;
567
568 assert(j);
569
570 if (j->state != PULL_JOB_INIT)
571 return -EBUSY;
572
573 r = curl_glue_make(&j->curl, j->url, j);
574 if (r < 0)
575 return r;
576
577 if (!strv_isempty(j->old_etags)) {
578 _cleanup_free_ char *cc = NULL, *hdr = NULL;
579
580 cc = strv_join(j->old_etags, ", ");
581 if (!cc)
582 return -ENOMEM;
583
584 hdr = strappend("If-None-Match: ", cc);
585 if (!hdr)
586 return -ENOMEM;
587
588 if (!j->request_header) {
589 j->request_header = curl_slist_new(hdr, NULL);
590 if (!j->request_header)
591 return -ENOMEM;
592 } else {
593 struct curl_slist *l;
594
595 l = curl_slist_append(j->request_header, hdr);
596 if (!l)
597 return -ENOMEM;
598
599 j->request_header = l;
600 }
601 }
602
603 if (j->request_header) {
604 if (curl_easy_setopt(j->curl, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK)
605 return -EIO;
606 }
607
608 if (curl_easy_setopt(j->curl, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK)
609 return -EIO;
610
611 if (curl_easy_setopt(j->curl, CURLOPT_WRITEDATA, j) != CURLE_OK)
612 return -EIO;
613
614 if (curl_easy_setopt(j->curl, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK)
615 return -EIO;
616
617 if (curl_easy_setopt(j->curl, CURLOPT_HEADERDATA, j) != CURLE_OK)
618 return -EIO;
619
620 if (curl_easy_setopt(j->curl, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK)
621 return -EIO;
622
623 if (curl_easy_setopt(j->curl, CURLOPT_XFERINFODATA, j) != CURLE_OK)
624 return -EIO;
625
626 if (curl_easy_setopt(j->curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK)
627 return -EIO;
628
629 r = curl_glue_add(j->glue, j->curl);
630 if (r < 0)
631 return r;
632
633 j->state = PULL_JOB_ANALYZING;
634
635 return 0;
636 }