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