]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/pull-job.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 n = -errno;
240 }
241 if (n < 0)
242 return log_error_errno((int) n, "Failed to write file: %m");
243 if ((size_t) n < sz) {
244 log_error("Short write");
245 return -EIO;
246 }
247 } else {
248
249 if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz))
250 return log_oom();
251
252 memcpy(j->payload + j->payload_size, p, sz);
253 j->payload_size += sz;
254 }
255
256 j->written_uncompressed += sz;
257 j->written_since_last_grow += 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 log_error("File too large, overflow");
273 return -EOVERFLOW;
274 }
275
276 if (j->written_compressed + sz > j->compressed_max) {
277 log_error("File overly large, refusing.");
278 return -EFBIG;
279 }
280
281 if (j->content_length != (uint64_t) -1 &&
282 j->written_compressed + sz > j->content_length) {
283 log_error("Content length incorrect.");
284 return -EFBIG;
285 }
286
287 if (j->checksum_context)
288 gcry_md_write(j->checksum_context, p, sz);
289
290 r = import_uncompress(&j->compress, p, sz, pull_job_write_uncompressed, j);
291 if (r < 0)
292 return r;
293
294 j->written_compressed += sz;
295
296 return 0;
297 }
298
299 static int pull_job_open_disk(PullJob *j) {
300 int r;
301
302 assert(j);
303
304 if (j->on_open_disk) {
305 r = j->on_open_disk(j);
306 if (r < 0)
307 return r;
308 }
309
310 if (j->disk_fd >= 0) {
311 /* Check if we can do sparse files */
312
313 if (lseek(j->disk_fd, SEEK_SET, 0) == 0)
314 j->allow_sparse = true;
315 else {
316 if (errno != ESPIPE)
317 return log_error_errno(errno, "Failed to seek on file descriptor: %m");
318
319 j->allow_sparse = false;
320 }
321 }
322
323 if (j->calc_checksum) {
324 initialize_libgcrypt(false);
325
326 if (gcry_md_open(&j->checksum_context, GCRY_MD_SHA256, 0) != 0) {
327 log_error("Failed to initialize hash context.");
328 return -EIO;
329 }
330 }
331
332 return 0;
333 }
334
335 static int pull_job_detect_compression(PullJob *j) {
336 _cleanup_free_ uint8_t *stub = NULL;
337 size_t stub_size;
338
339 int r;
340
341 assert(j);
342
343 r = import_uncompress_detect(&j->compress, j->payload, j->payload_size);
344 if (r < 0)
345 return log_error_errno(r, "Failed to initialize compressor: %m");
346 if (r == 0)
347 return 0;
348
349 log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type));
350
351 r = pull_job_open_disk(j);
352 if (r < 0)
353 return r;
354
355 /* Now, take the payload we read so far, and decompress it */
356 stub = j->payload;
357 stub_size = j->payload_size;
358
359 j->payload = NULL;
360 j->payload_size = 0;
361 j->payload_allocated = 0;
362
363 j->state = PULL_JOB_RUNNING;
364
365 r = pull_job_write_compressed(j, stub, stub_size);
366 if (r < 0)
367 return r;
368
369 return 0;
370 }
371
372 static size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
373 PullJob *j = userdata;
374 size_t sz = size * nmemb;
375 int r;
376
377 assert(contents);
378 assert(j);
379
380 switch (j->state) {
381
382 case PULL_JOB_ANALYZING:
383 /* Let's first check what it actually is */
384
385 if (!GREEDY_REALLOC(j->payload, j->payload_allocated, j->payload_size + sz)) {
386 r = log_oom();
387 goto fail;
388 }
389
390 memcpy(j->payload + j->payload_size, contents, sz);
391 j->payload_size += sz;
392
393 r = pull_job_detect_compression(j);
394 if (r < 0)
395 goto fail;
396
397 break;
398
399 case PULL_JOB_RUNNING:
400
401 r = pull_job_write_compressed(j, contents, sz);
402 if (r < 0)
403 goto fail;
404
405 break;
406
407 case PULL_JOB_DONE:
408 case PULL_JOB_FAILED:
409 r = -ESTALE;
410 goto fail;
411
412 default:
413 assert_not_reached("Impossible state.");
414 }
415
416 return sz;
417
418 fail:
419 pull_job_finish(j, r);
420 return 0;
421 }
422
423 static size_t pull_job_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
424 PullJob *j = userdata;
425 size_t sz = size * nmemb;
426 _cleanup_free_ char *length = NULL, *last_modified = NULL;
427 char *etag;
428 int r;
429
430 assert(contents);
431 assert(j);
432
433 if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) {
434 r = -ESTALE;
435 goto fail;
436 }
437
438 assert(j->state == PULL_JOB_ANALYZING);
439
440 r = curl_header_strdup(contents, sz, "ETag:", &etag);
441 if (r < 0) {
442 log_oom();
443 goto fail;
444 }
445 if (r > 0) {
446 free(j->etag);
447 j->etag = etag;
448
449 if (strv_contains(j->old_etags, j->etag)) {
450 log_info("Image already downloaded. Skipping download.");
451 j->etag_exists = true;
452 pull_job_finish(j, 0);
453 return sz;
454 }
455
456 return sz;
457 }
458
459 r = curl_header_strdup(contents, sz, "Content-Length:", &length);
460 if (r < 0) {
461 log_oom();
462 goto fail;
463 }
464 if (r > 0) {
465 (void) safe_atou64(length, &j->content_length);
466
467 if (j->content_length != (uint64_t) -1) {
468 char bytes[FORMAT_BYTES_MAX];
469
470 if (j->content_length > j->compressed_max) {
471 log_error("Content too large.");
472 r = -EFBIG;
473 goto fail;
474 }
475
476 log_info("Downloading %s for %s.", format_bytes(bytes, sizeof(bytes), j->content_length), j->url);
477 }
478
479 return sz;
480 }
481
482 r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified);
483 if (r < 0) {
484 log_oom();
485 goto fail;
486 }
487 if (r > 0) {
488 (void) curl_parse_http_time(last_modified, &j->mtime);
489 return sz;
490 }
491
492 if (j->on_header) {
493 r = j->on_header(j, contents, sz);
494 if (r < 0)
495 goto fail;
496 }
497
498 return sz;
499
500 fail:
501 pull_job_finish(j, r);
502 return 0;
503 }
504
505 static int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
506 PullJob *j = userdata;
507 unsigned percent;
508 usec_t n;
509
510 assert(j);
511
512 if (dltotal <= 0)
513 return 0;
514
515 percent = ((100 * dlnow) / dltotal);
516 n = now(CLOCK_MONOTONIC);
517
518 if (n > j->last_status_usec + USEC_PER_SEC &&
519 percent != j->progress_percent &&
520 dlnow < dltotal) {
521 char buf[FORMAT_TIMESPAN_MAX];
522
523 if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) {
524 char y[FORMAT_BYTES_MAX];
525 usec_t left, done;
526
527 done = n - j->start_usec;
528 left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done;
529
530 log_info("Got %u%% of %s. %s left at %s/s.",
531 percent,
532 j->url,
533 format_timespan(buf, sizeof(buf), left, USEC_PER_SEC),
534 format_bytes(y, sizeof(y), (uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC))));
535 } else
536 log_info("Got %u%% of %s.", percent, j->url);
537
538 j->progress_percent = percent;
539 j->last_status_usec = n;
540
541 if (j->on_progress)
542 j->on_progress(j);
543 }
544
545 return 0;
546 }
547
548 int pull_job_new(PullJob **ret, const char *url, CurlGlue *glue, void *userdata) {
549 _cleanup_(pull_job_unrefp) PullJob *j = NULL;
550
551 assert(url);
552 assert(glue);
553 assert(ret);
554
555 j = new0(PullJob, 1);
556 if (!j)
557 return -ENOMEM;
558
559 j->state = PULL_JOB_INIT;
560 j->disk_fd = -1;
561 j->userdata = userdata;
562 j->glue = glue;
563 j->content_length = (uint64_t) -1;
564 j->start_usec = now(CLOCK_MONOTONIC);
565 j->compressed_max = j->uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU; /* 64GB safety limit */
566 j->style = VERIFICATION_STYLE_UNSET;
567
568 j->url = strdup(url);
569 if (!j->url)
570 return -ENOMEM;
571
572 *ret = TAKE_PTR(j);
573
574 return 0;
575 }
576
577 int pull_job_begin(PullJob *j) {
578 int r;
579
580 assert(j);
581
582 if (j->state != PULL_JOB_INIT)
583 return -EBUSY;
584
585 if (j->grow_machine_directory)
586 grow_machine_directory();
587
588 r = curl_glue_make(&j->curl, j->url, j);
589 if (r < 0)
590 return r;
591
592 if (!strv_isempty(j->old_etags)) {
593 _cleanup_free_ char *cc = NULL, *hdr = NULL;
594
595 cc = strv_join(j->old_etags, ", ");
596 if (!cc)
597 return -ENOMEM;
598
599 hdr = strappend("If-None-Match: ", cc);
600 if (!hdr)
601 return -ENOMEM;
602
603 if (!j->request_header) {
604 j->request_header = curl_slist_new(hdr, NULL);
605 if (!j->request_header)
606 return -ENOMEM;
607 } else {
608 struct curl_slist *l;
609
610 l = curl_slist_append(j->request_header, hdr);
611 if (!l)
612 return -ENOMEM;
613
614 j->request_header = l;
615 }
616 }
617
618 if (j->request_header) {
619 if (curl_easy_setopt(j->curl, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK)
620 return -EIO;
621 }
622
623 if (curl_easy_setopt(j->curl, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK)
624 return -EIO;
625
626 if (curl_easy_setopt(j->curl, CURLOPT_WRITEDATA, j) != CURLE_OK)
627 return -EIO;
628
629 if (curl_easy_setopt(j->curl, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK)
630 return -EIO;
631
632 if (curl_easy_setopt(j->curl, CURLOPT_HEADERDATA, j) != CURLE_OK)
633 return -EIO;
634
635 if (curl_easy_setopt(j->curl, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK)
636 return -EIO;
637
638 if (curl_easy_setopt(j->curl, CURLOPT_XFERINFODATA, j) != CURLE_OK)
639 return -EIO;
640
641 if (curl_easy_setopt(j->curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK)
642 return -EIO;
643
644 r = curl_glue_add(j->glue, j->curl);
645 if (r < 0)
646 return r;
647
648 j->state = PULL_JOB_ANALYZING;
649
650 return 0;
651 }