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 "sync-util.h"
21 #include "xattr-util.h"
22 
pull_job_close_disk_fd(PullJob * j)23 void pull_job_close_disk_fd(PullJob *j) {
24         if (!j)
25                 return;
26 
27         if (j->close_disk_fd)
28                 safe_close(j->disk_fd);
29 
30         j->disk_fd = -1;
31 }
32 
pull_job_unref(PullJob * j)33 PullJob* pull_job_unref(PullJob *j) {
34         if (!j)
35                 return NULL;
36 
37         pull_job_close_disk_fd(j);
38 
39         curl_glue_remove_and_free(j->glue, j->curl);
40         curl_slist_free_all(j->request_header);
41 
42         import_compress_free(&j->compress);
43 
44         if (j->checksum_ctx)
45 #if PREFER_OPENSSL
46                 EVP_MD_CTX_free(j->checksum_ctx);
47 #else
48                 gcry_md_close(j->checksum_ctx);
49 #endif
50 
51         free(j->url);
52         free(j->etag);
53         strv_free(j->old_etags);
54         free(j->payload);
55         free(j->checksum);
56 
57         return mfree(j);
58 }
59 
pull_job_finish(PullJob * j,int ret)60 static void pull_job_finish(PullJob *j, int ret) {
61         assert(j);
62 
63         if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED))
64                 return;
65 
66         if (ret == 0) {
67                 j->state = PULL_JOB_DONE;
68                 j->progress_percent = 100;
69                 log_info("Download of %s complete.", j->url);
70         } else {
71                 j->state = PULL_JOB_FAILED;
72                 j->error = ret;
73         }
74 
75         if (j->on_finished)
76                 j->on_finished(j);
77 }
78 
pull_job_restart(PullJob * j,const char * new_url)79 static int pull_job_restart(PullJob *j, const char *new_url) {
80         int r;
81 
82         assert(j);
83         assert(new_url);
84 
85         r = free_and_strdup(&j->url, new_url);
86         if (r < 0)
87                 return r;
88 
89         j->state = PULL_JOB_INIT;
90         j->error = 0;
91         j->payload = mfree(j->payload);
92         j->payload_size = 0;
93         j->written_compressed = 0;
94         j->written_uncompressed = 0;
95         j->content_length = UINT64_MAX;
96         j->etag = mfree(j->etag);
97         j->etag_exists = false;
98         j->mtime = 0;
99         j->checksum = mfree(j->checksum);
100 
101         curl_glue_remove_and_free(j->glue, j->curl);
102         j->curl = NULL;
103 
104         curl_slist_free_all(j->request_header);
105         j->request_header = NULL;
106 
107         import_compress_free(&j->compress);
108 
109         if (j->checksum_ctx) {
110 #if PREFER_OPENSSL
111                 EVP_MD_CTX_free(j->checksum_ctx);
112 #else
113                 gcry_md_close(j->checksum_ctx);
114 #endif
115                 j->checksum_ctx = NULL;
116         }
117 
118         r = pull_job_begin(j);
119         if (r < 0)
120                 return r;
121 
122         return 0;
123 }
124 
pull_job_curl_on_finished(CurlGlue * g,CURL * curl,CURLcode result)125 void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
126         PullJob *j = NULL;
127         CURLcode code;
128         long protocol;
129         int r;
130 
131         if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK)
132                 return;
133 
134         if (!j || IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED))
135                 return;
136 
137         if (result != CURLE_OK) {
138                 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Transfer failed: %s", curl_easy_strerror(result));
139                 goto finish;
140         }
141 
142         code = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
143         if (code != CURLE_OK) {
144                 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
145                 goto finish;
146         }
147 
148         if (IN_SET(protocol, CURLPROTO_HTTP, CURLPROTO_HTTPS)) {
149                 long status;
150 
151                 code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
152                 if (code != CURLE_OK) {
153                         r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
154                         goto finish;
155                 }
156 
157                 if (status == 304) {
158                         log_info("Image already downloaded. Skipping download.");
159                         j->etag_exists = true;
160                         r = 0;
161                         goto finish;
162                 } else if (status >= 300) {
163 
164                         if (status == 404 && j->on_not_found) {
165                                 _cleanup_free_ char *new_url = NULL;
166 
167                                 /* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */
168                                 r = j->on_not_found(j, &new_url);
169                                 if (r < 0)
170                                         goto finish;
171 
172                                 if (r > 0) { /* A new url to use */
173                                         assert(new_url);
174 
175                                         r = pull_job_restart(j, new_url);
176                                         if (r < 0)
177                                                 goto finish;
178 
179                                         code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status);
180                                         if (code != CURLE_OK) {
181                                                 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
182                                                 goto finish;
183                                         }
184 
185                                         if (status == 0)
186                                                 return;
187                                 }
188                         }
189 
190                         r = log_error_errno(
191                                         status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */
192                                         "HTTP request to %s failed with code %li.", j->url, status);
193                         goto finish;
194                 } else if (status < 200) {
195                         r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status);
196                         goto finish;
197                 }
198         }
199 
200         if (j->state != PULL_JOB_RUNNING) {
201                 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Premature connection termination.");
202                 goto finish;
203         }
204 
205         if (j->content_length != UINT64_MAX &&
206             j->content_length != j->written_compressed) {
207                 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Download truncated.");
208                 goto finish;
209         }
210 
211         if (j->checksum_ctx) {
212                 unsigned checksum_len;
213 #if PREFER_OPENSSL
214                 uint8_t k[EVP_MAX_MD_SIZE];
215 
216                 r = EVP_DigestFinal_ex(j->checksum_ctx, k, &checksum_len);
217                 if (r == 0) {
218                         r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to get checksum.");
219                         goto finish;
220                 }
221                 assert(checksum_len <= sizeof k);
222 #else
223                 const uint8_t *k;
224 
225                 k = gcry_md_read(j->checksum_ctx, GCRY_MD_SHA256);
226                 if (!k) {
227                         r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to get checksum.");
228                         goto finish;
229                 }
230 
231                 checksum_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
232 #endif
233 
234                 j->checksum = hexmem(k, checksum_len);
235                 if (!j->checksum) {
236                         r = log_oom();
237                         goto finish;
238                 }
239 
240                 log_debug("SHA256 of %s is %s.", j->url, j->checksum);
241         }
242 
243         /* Do a couple of finishing disk operations, but only if we are the sole owner of the file (i.e. no
244          * offset is specified, which indicates we only own the file partially) */
245 
246         if (j->disk_fd >= 0) {
247 
248                 if (S_ISREG(j->disk_stat.st_mode)) {
249 
250                         if (j->offset == UINT64_MAX) {
251 
252                                 if (j->written_compressed > 0) {
253                                         /* Make sure the file size is right, in case the file was sparse and we just seeked
254                                          * for the last part */
255                                         if (ftruncate(j->disk_fd, j->written_uncompressed) < 0) {
256                                                 r = log_error_errno(errno, "Failed to truncate file: %m");
257                                                 goto finish;
258                                         }
259                                 }
260 
261                                 if (j->etag)
262                                         (void) fsetxattr(j->disk_fd, "user.source_etag", j->etag, strlen(j->etag), 0);
263                                 if (j->url)
264                                         (void) fsetxattr(j->disk_fd, "user.source_url", j->url, strlen(j->url), 0);
265 
266                                 if (j->mtime != 0) {
267                                         struct timespec ut;
268 
269                                         timespec_store(&ut, j->mtime);
270 
271                                         if (futimens(j->disk_fd, (struct timespec[]) { ut, ut }) < 0)
272                                                 log_debug_errno(errno, "Failed to adjust atime/mtime of created image, ignoring: %m");
273 
274                                         r = fd_setcrtime(j->disk_fd, j->mtime);
275                                         if (r < 0)
276                                                 log_debug_errno(r, "Failed to adjust crtime of created image, ignoring: %m");
277                                 }
278                         }
279 
280                         if (j->sync) {
281                                 r = fsync_full(j->disk_fd);
282                                 if (r < 0) {
283                                         log_error_errno(r, "Failed to synchronize file to disk: %m");
284                                         goto finish;
285                                 }
286                         }
287 
288                 } else if (S_ISBLK(j->disk_stat.st_mode) && j->sync) {
289 
290                         if (fsync(j->disk_fd) < 0) {
291                                 r = log_error_errno(errno, "Failed to synchronize block device: %m");
292                                 goto finish;
293                         }
294                 }
295         }
296 
297         log_info("Acquired %s.", FORMAT_BYTES(j->written_uncompressed));
298 
299         r = 0;
300 
301 finish:
302         pull_job_finish(j, r);
303 }
304 
pull_job_write_uncompressed(const void * p,size_t sz,void * userdata)305 static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata) {
306         PullJob *j = userdata;
307         bool too_much = false;
308         int r;
309 
310         assert(j);
311         assert(p);
312         assert(sz > 0);
313 
314         if (j->written_uncompressed > UINT64_MAX - sz)
315                 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow");
316 
317         if (j->written_uncompressed >= j->uncompressed_max) {
318                 too_much = true;
319                 goto finish;
320         }
321 
322         if (j->written_uncompressed + sz > j->uncompressed_max) {
323                 too_much = true;
324                 sz = j->uncompressed_max - j->written_uncompressed; /* since we have the data in memory
325                                                                      * already, we might as well write it to
326                                                                      * disk to the max */
327         }
328 
329         if (j->disk_fd >= 0) {
330 
331                 if (S_ISREG(j->disk_stat.st_mode) && j->offset == UINT64_MAX) {
332                         ssize_t n;
333 
334                         n = sparse_write(j->disk_fd, p, sz, 64);
335                         if (n < 0)
336                                 return log_error_errno((int) n, "Failed to write file: %m");
337                         if ((size_t) n < sz)
338                                 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write");
339                 } else {
340                         r = loop_write(j->disk_fd, p, sz, false);
341                         if (r < 0)
342                                 return log_error_errno(r, "Failed to write file: %m");
343                 }
344         }
345 
346         if (j->disk_fd < 0 || j->force_memory) {
347                 if (!GREEDY_REALLOC(j->payload, j->payload_size + sz))
348                         return log_oom();
349 
350                 memcpy(j->payload + j->payload_size, p, sz);
351                 j->payload_size += sz;
352         }
353 
354         j->written_uncompressed += sz;
355 
356 finish:
357         if (too_much)
358                 return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing.");
359 
360         return 0;
361 }
362 
pull_job_write_compressed(PullJob * j,void * p,size_t sz)363 static int pull_job_write_compressed(PullJob *j, void *p, size_t sz) {
364         int r;
365 
366         assert(j);
367         assert(p);
368 
369         if (sz <= 0)
370                 return 0;
371 
372         if (j->written_compressed + sz < j->written_compressed)
373                 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow");
374 
375         if (j->written_compressed + sz > j->compressed_max)
376                 return log_error_errno(SYNTHETIC_ERRNO(EFBIG), "File overly large, refusing.");
377 
378         if (j->content_length != UINT64_MAX &&
379             j->written_compressed + sz > j->content_length)
380                 return log_error_errno(SYNTHETIC_ERRNO(EFBIG),
381                                        "Content length incorrect.");
382 
383         if (j->checksum_ctx) {
384 #if PREFER_OPENSSL
385                 r = EVP_DigestUpdate(j->checksum_ctx, p, sz);
386                 if (r == 0)
387                         return log_error_errno(SYNTHETIC_ERRNO(EIO),
388                                                "Could not hash chunk.");
389 #else
390                 gcry_md_write(j->checksum_ctx, p, sz);
391 #endif
392         }
393 
394         r = import_uncompress(&j->compress, p, sz, pull_job_write_uncompressed, j);
395         if (r < 0)
396                 return r;
397 
398         j->written_compressed += sz;
399 
400         return 0;
401 }
402 
pull_job_open_disk(PullJob * j)403 static int pull_job_open_disk(PullJob *j) {
404         int r;
405 
406         assert(j);
407 
408         if (j->on_open_disk) {
409                 r = j->on_open_disk(j);
410                 if (r < 0)
411                         return r;
412         }
413 
414         if (j->disk_fd >= 0) {
415                 if (fstat(j->disk_fd, &j->disk_stat) < 0)
416                         return log_error_errno(errno, "Failed to stat disk file: %m");
417 
418                 if (j->offset != UINT64_MAX) {
419                         if (lseek(j->disk_fd, j->offset, SEEK_SET) == (off_t) -1)
420                                 return log_error_errno(errno, "Failed to seek on file descriptor: %m");
421                 }
422         }
423 
424         if (j->calc_checksum) {
425 #if PREFER_OPENSSL
426                 j->checksum_ctx = EVP_MD_CTX_new();
427                 if (!j->checksum_ctx)
428                         return log_oom();
429 
430                 r = EVP_DigestInit_ex(j->checksum_ctx, EVP_sha256(), NULL);
431                 if (r == 0)
432                         return log_error_errno(SYNTHETIC_ERRNO(EIO),
433                                                "Failed to initialize hash context.");
434 #else
435                 initialize_libgcrypt(false);
436 
437                 if (gcry_md_open(&j->checksum_ctx, GCRY_MD_SHA256, 0) != 0)
438                         return log_error_errno(SYNTHETIC_ERRNO(EIO),
439                                                "Failed to initialize hash context.");
440 #endif
441         }
442 
443         return 0;
444 }
445 
pull_job_detect_compression(PullJob * j)446 static int pull_job_detect_compression(PullJob *j) {
447         _cleanup_free_ uint8_t *stub = NULL;
448         size_t stub_size;
449 
450         int r;
451 
452         assert(j);
453 
454         r = import_uncompress_detect(&j->compress, j->payload, j->payload_size);
455         if (r < 0)
456                 return log_error_errno(r, "Failed to initialize compressor: %m");
457         if (r == 0)
458                 return 0;
459 
460         log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type));
461 
462         r = pull_job_open_disk(j);
463         if (r < 0)
464                 return r;
465 
466         /* Now, take the payload we read so far, and decompress it */
467         stub = j->payload;
468         stub_size = j->payload_size;
469 
470         j->payload = NULL;
471         j->payload_size = 0;
472 
473         j->state = PULL_JOB_RUNNING;
474 
475         r = pull_job_write_compressed(j, stub, stub_size);
476         if (r < 0)
477                 return r;
478 
479         return 0;
480 }
481 
pull_job_write_callback(void * contents,size_t size,size_t nmemb,void * userdata)482 static size_t pull_job_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
483         PullJob *j = userdata;
484         size_t sz = size * nmemb;
485         int r;
486 
487         assert(contents);
488         assert(j);
489 
490         switch (j->state) {
491 
492         case PULL_JOB_ANALYZING:
493                 /* Let's first check what it actually is */
494 
495                 if (!GREEDY_REALLOC(j->payload, j->payload_size + sz)) {
496                         r = log_oom();
497                         goto fail;
498                 }
499 
500                 memcpy(j->payload + j->payload_size, contents, sz);
501                 j->payload_size += sz;
502 
503                 r = pull_job_detect_compression(j);
504                 if (r < 0)
505                         goto fail;
506 
507                 break;
508 
509         case PULL_JOB_RUNNING:
510 
511                 r = pull_job_write_compressed(j, contents, sz);
512                 if (r < 0)
513                         goto fail;
514 
515                 break;
516 
517         case PULL_JOB_DONE:
518         case PULL_JOB_FAILED:
519                 r = -ESTALE;
520                 goto fail;
521 
522         default:
523                 assert_not_reached();
524         }
525 
526         return sz;
527 
528 fail:
529         pull_job_finish(j, r);
530         return 0;
531 }
532 
http_status_ok(CURLcode status)533 static int http_status_ok(CURLcode status) {
534         /* Consider all HTTP status code in the 2xx range as OK */
535         return status >= 200 && status <= 299;
536 }
537 
http_status_etag_exists(CURLcode status)538 static int http_status_etag_exists(CURLcode status) {
539         /* This one is special, it's triggered by our etag mgmt logic */
540         return status == 304;
541 }
542 
pull_job_header_callback(void * contents,size_t size,size_t nmemb,void * userdata)543 static size_t pull_job_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
544         _cleanup_free_ char *length = NULL, *last_modified = NULL, *etag = NULL;
545         size_t sz = size * nmemb;
546         PullJob *j = userdata;
547         CURLcode code;
548         long status;
549         int r;
550 
551         assert(contents);
552         assert(j);
553 
554         if (IN_SET(j->state, PULL_JOB_DONE, PULL_JOB_FAILED)) {
555                 r = -ESTALE;
556                 goto fail;
557         }
558 
559         assert(j->state == PULL_JOB_ANALYZING);
560 
561         code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status);
562         if (code != CURLE_OK) {
563                 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
564                 goto fail;
565         }
566 
567         if (http_status_ok(status) || http_status_etag_exists(status)) {
568                 /* Check Etag on OK and etag exists responses. */
569 
570                 r = curl_header_strdup(contents, sz, "ETag:", &etag);
571                 if (r < 0) {
572                         log_oom();
573                         goto fail;
574                 }
575                 if (r > 0) {
576                         free_and_replace(j->etag, etag);
577 
578                         if (strv_contains(j->old_etags, j->etag)) {
579                                 log_info("Image already downloaded. Skipping download. (%s)", j->etag);
580                                 j->etag_exists = true;
581                                 pull_job_finish(j, 0);
582                                 return sz;
583                         }
584 
585                         return sz;
586                 }
587         }
588 
589         if (!http_status_ok(status)) /* Let's ignore the rest here, these requests are probably redirects and
590                                       * stuff where the headers aren't interesting to us */
591                 return sz;
592 
593         r = curl_header_strdup(contents, sz, "Content-Length:", &length);
594         if (r < 0) {
595                 log_oom();
596                 goto fail;
597         }
598         if (r > 0) {
599                 (void) safe_atou64(length, &j->content_length);
600 
601                 if (j->content_length != UINT64_MAX) {
602                         if (j->content_length > j->compressed_max) {
603                                 r = log_error_errno(SYNTHETIC_ERRNO(EFBIG), "Content too large.");
604                                 goto fail;
605                         }
606 
607                         log_info("Downloading %s for %s.", FORMAT_BYTES(j->content_length), j->url);
608                 }
609 
610                 return sz;
611         }
612 
613         r = curl_header_strdup(contents, sz, "Last-Modified:", &last_modified);
614         if (r < 0) {
615                 log_oom();
616                 goto fail;
617         }
618         if (r > 0) {
619                 (void) curl_parse_http_time(last_modified, &j->mtime);
620                 return sz;
621         }
622 
623         if (j->on_header) {
624                 r = j->on_header(j, contents, sz);
625                 if (r < 0)
626                         goto fail;
627         }
628 
629         return sz;
630 
631 fail:
632         pull_job_finish(j, r);
633         return 0;
634 }
635 
pull_job_progress_callback(void * userdata,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)636 static int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
637         PullJob *j = userdata;
638         unsigned percent;
639         usec_t n;
640 
641         assert(j);
642 
643         if (dltotal <= 0)
644                 return 0;
645 
646         percent = ((100 * dlnow) / dltotal);
647         n = now(CLOCK_MONOTONIC);
648 
649         if (n > j->last_status_usec + USEC_PER_SEC &&
650             percent != j->progress_percent &&
651             dlnow < dltotal) {
652 
653                 if (n - j->start_usec > USEC_PER_SEC && dlnow > 0) {
654                         usec_t left, done;
655 
656                         done = n - j->start_usec;
657                         left = (usec_t) (((double) done * (double) dltotal) / dlnow) - done;
658 
659                         log_info("Got %u%% of %s. %s left at %s/s.",
660                                  percent,
661                                  j->url,
662                                  FORMAT_TIMESPAN(left, USEC_PER_SEC),
663                                  FORMAT_BYTES((uint64_t) ((double) dlnow / ((double) done / (double) USEC_PER_SEC))));
664                 } else
665                         log_info("Got %u%% of %s.", percent, j->url);
666 
667                 j->progress_percent = percent;
668                 j->last_status_usec = n;
669 
670                 if (j->on_progress)
671                         j->on_progress(j);
672         }
673 
674         return 0;
675 }
676 
pull_job_new(PullJob ** ret,const char * url,CurlGlue * glue,void * userdata)677 int pull_job_new(
678                 PullJob **ret,
679                 const char *url,
680                 CurlGlue *glue,
681                 void *userdata) {
682 
683         _cleanup_(pull_job_unrefp) PullJob *j = NULL;
684         _cleanup_free_ char *u = NULL;
685 
686         assert(url);
687         assert(glue);
688         assert(ret);
689 
690         u = strdup(url);
691         if (!u)
692                 return -ENOMEM;
693 
694         j = new(PullJob, 1);
695         if (!j)
696                 return -ENOMEM;
697 
698         *j = (PullJob) {
699                 .state = PULL_JOB_INIT,
700                 .disk_fd = -1,
701                 .close_disk_fd = true,
702                 .userdata = userdata,
703                 .glue = glue,
704                 .content_length = UINT64_MAX,
705                 .start_usec = now(CLOCK_MONOTONIC),
706                 .compressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
707                 .uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
708                 .url = TAKE_PTR(u),
709                 .offset = UINT64_MAX,
710                 .sync = true,
711         };
712 
713         *ret = TAKE_PTR(j);
714 
715         return 0;
716 }
717 
pull_job_begin(PullJob * j)718 int pull_job_begin(PullJob *j) {
719         int r;
720 
721         assert(j);
722 
723         if (j->state != PULL_JOB_INIT)
724                 return -EBUSY;
725 
726         r = curl_glue_make(&j->curl, j->url, j);
727         if (r < 0)
728                 return r;
729 
730         if (!strv_isempty(j->old_etags)) {
731                 _cleanup_free_ char *cc = NULL, *hdr = NULL;
732 
733                 cc = strv_join(j->old_etags, ", ");
734                 if (!cc)
735                         return -ENOMEM;
736 
737                 hdr = strjoin("If-None-Match: ", cc);
738                 if (!hdr)
739                         return -ENOMEM;
740 
741                 if (!j->request_header) {
742                         j->request_header = curl_slist_new(hdr, NULL);
743                         if (!j->request_header)
744                                 return -ENOMEM;
745                 } else {
746                         struct curl_slist *l;
747 
748                         l = curl_slist_append(j->request_header, hdr);
749                         if (!l)
750                                 return -ENOMEM;
751 
752                         j->request_header = l;
753                 }
754         }
755 
756         if (j->request_header) {
757                 if (curl_easy_setopt(j->curl, CURLOPT_HTTPHEADER, j->request_header) != CURLE_OK)
758                         return -EIO;
759         }
760 
761         if (curl_easy_setopt(j->curl, CURLOPT_WRITEFUNCTION, pull_job_write_callback) != CURLE_OK)
762                 return -EIO;
763 
764         if (curl_easy_setopt(j->curl, CURLOPT_WRITEDATA, j) != CURLE_OK)
765                 return -EIO;
766 
767         if (curl_easy_setopt(j->curl, CURLOPT_HEADERFUNCTION, pull_job_header_callback) != CURLE_OK)
768                 return -EIO;
769 
770         if (curl_easy_setopt(j->curl, CURLOPT_HEADERDATA, j) != CURLE_OK)
771                 return -EIO;
772 
773         if (curl_easy_setopt(j->curl, CURLOPT_XFERINFOFUNCTION, pull_job_progress_callback) != CURLE_OK)
774                 return -EIO;
775 
776         if (curl_easy_setopt(j->curl, CURLOPT_XFERINFODATA, j) != CURLE_OK)
777                 return -EIO;
778 
779         if (curl_easy_setopt(j->curl, CURLOPT_NOPROGRESS, 0) != CURLE_OK)
780                 return -EIO;
781 
782         r = curl_glue_add(j->glue, j->curl);
783         if (r < 0)
784                 return r;
785 
786         j->state = PULL_JOB_ANALYZING;
787 
788         return 0;
789 }
790