xref: /openbmc/qemu/block/curl.c (revision 93e0932b)
1 /*
2  * QEMU Block driver for CURL images
3  *
4  * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/error-report.h"
28 #include "qemu/module.h"
29 #include "qemu/option.h"
30 #include "block/block-io.h"
31 #include "block/block_int.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qapi/qmp/qstring.h"
34 #include "crypto/secret.h"
35 #include <curl/curl.h>
36 #include "qemu/cutils.h"
37 #include "trace.h"
38 
39 // #define DEBUG_VERBOSE
40 
41 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
42                    CURLPROTO_FTP | CURLPROTO_FTPS)
43 
44 #define CURL_NUM_STATES 8
45 #define CURL_NUM_ACB    8
46 #define CURL_TIMEOUT_MAX 10000
47 
48 #define CURL_BLOCK_OPT_URL       "url"
49 #define CURL_BLOCK_OPT_READAHEAD "readahead"
50 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
51 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
52 #define CURL_BLOCK_OPT_COOKIE    "cookie"
53 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
54 #define CURL_BLOCK_OPT_USERNAME "username"
55 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
56 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
57 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
58 
59 #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
60 #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
61 #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
62 
63 struct BDRVCURLState;
64 struct CURLState;
65 
66 static bool libcurl_initialized;
67 
68 typedef struct CURLAIOCB {
69     Coroutine *co;
70     QEMUIOVector *qiov;
71 
72     uint64_t offset;
73     uint64_t bytes;
74     int ret;
75 
76     size_t start;
77     size_t end;
78 } CURLAIOCB;
79 
80 typedef struct CURLSocket {
81     int fd;
82     struct BDRVCURLState *s;
83 } CURLSocket;
84 
85 typedef struct CURLState
86 {
87     struct BDRVCURLState *s;
88     CURLAIOCB *acb[CURL_NUM_ACB];
89     CURL *curl;
90     char *orig_buf;
91     uint64_t buf_start;
92     size_t buf_off;
93     size_t buf_len;
94     char range[128];
95     char errmsg[CURL_ERROR_SIZE];
96     char in_use;
97 } CURLState;
98 
99 typedef struct BDRVCURLState {
100     CURLM *multi;
101     QEMUTimer timer;
102     uint64_t len;
103     CURLState states[CURL_NUM_STATES];
104     GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */
105     char *url;
106     size_t readahead_size;
107     bool sslverify;
108     uint64_t timeout;
109     char *cookie;
110     bool accept_range;
111     AioContext *aio_context;
112     QemuMutex mutex;
113     CoQueue free_state_waitq;
114     char *username;
115     char *password;
116     char *proxyusername;
117     char *proxypassword;
118 } BDRVCURLState;
119 
120 static void curl_clean_state(CURLState *s);
121 static void curl_multi_do(void *arg);
122 
123 static gboolean curl_drop_socket(void *key, void *value, void *opaque)
124 {
125     CURLSocket *socket = value;
126     BDRVCURLState *s = socket->s;
127 
128     aio_set_fd_handler(s->aio_context, socket->fd, false,
129                        NULL, NULL, NULL, NULL, NULL);
130     return true;
131 }
132 
133 static void curl_drop_all_sockets(GHashTable *sockets)
134 {
135     g_hash_table_foreach_remove(sockets, curl_drop_socket, NULL);
136 }
137 
138 /* Called from curl_multi_do_locked, with s->mutex held.  */
139 static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
140 {
141     BDRVCURLState *s = opaque;
142 
143     trace_curl_timer_cb(timeout_ms);
144     if (timeout_ms == -1) {
145         timer_del(&s->timer);
146     } else {
147         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
148         timer_mod(&s->timer,
149                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
150     }
151     return 0;
152 }
153 
154 /* Called from curl_multi_do_locked, with s->mutex held.  */
155 static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
156                         void *userp, void *sp)
157 {
158     BDRVCURLState *s;
159     CURLState *state = NULL;
160     CURLSocket *socket;
161 
162     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
163     s = state->s;
164 
165     socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd));
166     if (!socket) {
167         socket = g_new0(CURLSocket, 1);
168         socket->fd = fd;
169         socket->s = s;
170         g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket);
171     }
172 
173     trace_curl_sock_cb(action, (int)fd);
174     switch (action) {
175         case CURL_POLL_IN:
176             aio_set_fd_handler(s->aio_context, fd, false,
177                                curl_multi_do, NULL, NULL, NULL, socket);
178             break;
179         case CURL_POLL_OUT:
180             aio_set_fd_handler(s->aio_context, fd, false,
181                                NULL, curl_multi_do, NULL, NULL, socket);
182             break;
183         case CURL_POLL_INOUT:
184             aio_set_fd_handler(s->aio_context, fd, false,
185                                curl_multi_do, curl_multi_do,
186                                NULL, NULL, socket);
187             break;
188         case CURL_POLL_REMOVE:
189             aio_set_fd_handler(s->aio_context, fd, false,
190                                NULL, NULL, NULL, NULL, NULL);
191             break;
192     }
193 
194     if (action == CURL_POLL_REMOVE) {
195         g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd));
196     }
197 
198     return 0;
199 }
200 
201 /* Called from curl_multi_do_locked, with s->mutex held.  */
202 static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
203 {
204     BDRVCURLState *s = opaque;
205     size_t realsize = size * nmemb;
206     const char *header = (char *)ptr;
207     const char *end = header + realsize;
208     const char *accept_ranges = "accept-ranges:";
209     const char *bytes = "bytes";
210 
211     if (realsize >= strlen(accept_ranges)
212         && g_ascii_strncasecmp(header, accept_ranges,
213                                strlen(accept_ranges)) == 0) {
214 
215         char *p = strchr(header, ':') + 1;
216 
217         /* Skip whitespace between the header name and value. */
218         while (p < end && *p && g_ascii_isspace(*p)) {
219             p++;
220         }
221 
222         if (end - p >= strlen(bytes)
223             && strncmp(p, bytes, strlen(bytes)) == 0) {
224 
225             /* Check that there is nothing but whitespace after the value. */
226             p += strlen(bytes);
227             while (p < end && *p && g_ascii_isspace(*p)) {
228                 p++;
229             }
230 
231             if (p == end || !*p) {
232                 s->accept_range = true;
233             }
234         }
235     }
236 
237     return realsize;
238 }
239 
240 /* Called from curl_multi_do_locked, with s->mutex held.  */
241 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
242 {
243     CURLState *s = ((CURLState*)opaque);
244     size_t realsize = size * nmemb;
245 
246     trace_curl_read_cb(realsize);
247 
248     if (!s || !s->orig_buf) {
249         goto read_end;
250     }
251 
252     if (s->buf_off >= s->buf_len) {
253         /* buffer full, read nothing */
254         goto read_end;
255     }
256     realsize = MIN(realsize, s->buf_len - s->buf_off);
257     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
258     s->buf_off += realsize;
259 
260 read_end:
261     /* curl will error out if we do not return this value */
262     return size * nmemb;
263 }
264 
265 /* Called with s->mutex held.  */
266 static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
267                           CURLAIOCB *acb)
268 {
269     int i;
270     uint64_t end = start + len;
271     uint64_t clamped_end = MIN(end, s->len);
272     uint64_t clamped_len = clamped_end - start;
273 
274     for (i=0; i<CURL_NUM_STATES; i++) {
275         CURLState *state = &s->states[i];
276         uint64_t buf_end = (state->buf_start + state->buf_off);
277         uint64_t buf_fend = (state->buf_start + state->buf_len);
278 
279         if (!state->orig_buf)
280             continue;
281         if (!state->buf_off)
282             continue;
283 
284         // Does the existing buffer cover our section?
285         if ((start >= state->buf_start) &&
286             (start <= buf_end) &&
287             (clamped_end >= state->buf_start) &&
288             (clamped_end <= buf_end))
289         {
290             char *buf = state->orig_buf + (start - state->buf_start);
291 
292             qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
293             if (clamped_len < len) {
294                 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
295             }
296             acb->ret = 0;
297             return true;
298         }
299 
300         // Wait for unfinished chunks
301         if (state->in_use &&
302             (start >= state->buf_start) &&
303             (start <= buf_fend) &&
304             (clamped_end >= state->buf_start) &&
305             (clamped_end <= buf_fend))
306         {
307             int j;
308 
309             acb->start = start - state->buf_start;
310             acb->end = acb->start + clamped_len;
311 
312             for (j=0; j<CURL_NUM_ACB; j++) {
313                 if (!state->acb[j]) {
314                     state->acb[j] = acb;
315                     return true;
316                 }
317             }
318         }
319     }
320 
321     return false;
322 }
323 
324 /* Called with s->mutex held.  */
325 static void curl_multi_check_completion(BDRVCURLState *s)
326 {
327     int msgs_in_queue;
328 
329     /* Try to find done transfers, so we can free the easy
330      * handle again. */
331     for (;;) {
332         CURLMsg *msg;
333         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
334 
335         /* Quit when there are no more completions */
336         if (!msg)
337             break;
338 
339         if (msg->msg == CURLMSG_DONE) {
340             int i;
341             CURLState *state = NULL;
342             bool error = msg->data.result != CURLE_OK;
343 
344             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
345                               (char **)&state);
346 
347             if (error) {
348                 static int errcount = 100;
349 
350                 /* Don't lose the original error message from curl, since
351                  * it contains extra data.
352                  */
353                 if (errcount > 0) {
354                     error_report("curl: %s", state->errmsg);
355                     if (--errcount == 0) {
356                         error_report("curl: further errors suppressed");
357                     }
358                 }
359             }
360 
361             for (i = 0; i < CURL_NUM_ACB; i++) {
362                 CURLAIOCB *acb = state->acb[i];
363 
364                 if (acb == NULL) {
365                     continue;
366                 }
367 
368                 if (!error) {
369                     /* Assert that we have read all data */
370                     assert(state->buf_off >= acb->end);
371 
372                     qemu_iovec_from_buf(acb->qiov, 0,
373                                         state->orig_buf + acb->start,
374                                         acb->end - acb->start);
375 
376                     if (acb->end - acb->start < acb->bytes) {
377                         size_t offset = acb->end - acb->start;
378                         qemu_iovec_memset(acb->qiov, offset, 0,
379                                           acb->bytes - offset);
380                     }
381                 }
382 
383                 acb->ret = error ? -EIO : 0;
384                 state->acb[i] = NULL;
385                 qemu_mutex_unlock(&s->mutex);
386                 aio_co_wake(acb->co);
387                 qemu_mutex_lock(&s->mutex);
388             }
389 
390             curl_clean_state(state);
391             break;
392         }
393     }
394 }
395 
396 /* Called with s->mutex held.  */
397 static void curl_multi_do_locked(CURLSocket *socket)
398 {
399     BDRVCURLState *s = socket->s;
400     int running;
401     int r;
402 
403     if (!s->multi) {
404         return;
405     }
406 
407     do {
408         r = curl_multi_socket_action(s->multi, socket->fd, 0, &running);
409     } while (r == CURLM_CALL_MULTI_PERFORM);
410 }
411 
412 static void curl_multi_do(void *arg)
413 {
414     CURLSocket *socket = arg;
415     BDRVCURLState *s = socket->s;
416 
417     qemu_mutex_lock(&s->mutex);
418     curl_multi_do_locked(socket);
419     curl_multi_check_completion(s);
420     qemu_mutex_unlock(&s->mutex);
421 }
422 
423 static void curl_multi_timeout_do(void *arg)
424 {
425     BDRVCURLState *s = (BDRVCURLState *)arg;
426     int running;
427 
428     if (!s->multi) {
429         return;
430     }
431 
432     qemu_mutex_lock(&s->mutex);
433     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
434 
435     curl_multi_check_completion(s);
436     qemu_mutex_unlock(&s->mutex);
437 }
438 
439 /* Called with s->mutex held.  */
440 static CURLState *curl_find_state(BDRVCURLState *s)
441 {
442     CURLState *state = NULL;
443     int i;
444 
445     for (i = 0; i < CURL_NUM_STATES; i++) {
446         if (!s->states[i].in_use) {
447             state = &s->states[i];
448             state->in_use = 1;
449             break;
450         }
451     }
452     return state;
453 }
454 
455 static int curl_init_state(BDRVCURLState *s, CURLState *state)
456 {
457     if (!state->curl) {
458         state->curl = curl_easy_init();
459         if (!state->curl) {
460             return -EIO;
461         }
462         if (curl_easy_setopt(state->curl, CURLOPT_URL, s->url) ||
463             curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
464                              (long) s->sslverify) ||
465             curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST,
466                              s->sslverify ? 2L : 0L)) {
467             goto err;
468         }
469         if (s->cookie) {
470             if (curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie)) {
471                 goto err;
472             }
473         }
474         if (curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout) ||
475             curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
476                              (void *)curl_read_cb) ||
477             curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state) ||
478             curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state) ||
479             curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1) ||
480             curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1) ||
481             curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1) ||
482             curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg) ||
483             curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1)) {
484             goto err;
485         }
486         if (s->username) {
487             if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) {
488                 goto err;
489             }
490         }
491         if (s->password) {
492             if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) {
493                 goto err;
494             }
495         }
496         if (s->proxyusername) {
497             if (curl_easy_setopt(state->curl,
498                                  CURLOPT_PROXYUSERNAME, s->proxyusername)) {
499                 goto err;
500             }
501         }
502         if (s->proxypassword) {
503             if (curl_easy_setopt(state->curl,
504                                  CURLOPT_PROXYPASSWORD, s->proxypassword)) {
505                 goto err;
506             }
507         }
508 
509         /* Restrict supported protocols to avoid security issues in the more
510          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
511          * CVE-2013-0249.
512          *
513          * Restricting protocols is only supported from 7.19.4 upwards.
514          */
515 #if LIBCURL_VERSION_NUM >= 0x071304
516         if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) ||
517             curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) {
518             goto err;
519         }
520 #endif
521 
522 #ifdef DEBUG_VERBOSE
523         if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) {
524             goto err;
525         }
526 #endif
527     }
528 
529     state->s = s;
530 
531     return 0;
532 
533 err:
534     curl_easy_cleanup(state->curl);
535     state->curl = NULL;
536     return -EIO;
537 }
538 
539 /* Called with s->mutex held.  */
540 static void curl_clean_state(CURLState *s)
541 {
542     int j;
543     for (j = 0; j < CURL_NUM_ACB; j++) {
544         assert(!s->acb[j]);
545     }
546 
547     if (s->s->multi)
548         curl_multi_remove_handle(s->s->multi, s->curl);
549 
550     s->in_use = 0;
551 
552     qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
553 }
554 
555 static void curl_parse_filename(const char *filename, QDict *options,
556                                 Error **errp)
557 {
558     qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
559 }
560 
561 static void curl_detach_aio_context(BlockDriverState *bs)
562 {
563     BDRVCURLState *s = bs->opaque;
564     int i;
565 
566     WITH_QEMU_LOCK_GUARD(&s->mutex) {
567         curl_drop_all_sockets(s->sockets);
568         for (i = 0; i < CURL_NUM_STATES; i++) {
569             if (s->states[i].in_use) {
570                 curl_clean_state(&s->states[i]);
571             }
572             if (s->states[i].curl) {
573                 curl_easy_cleanup(s->states[i].curl);
574                 s->states[i].curl = NULL;
575             }
576             g_free(s->states[i].orig_buf);
577             s->states[i].orig_buf = NULL;
578         }
579         if (s->multi) {
580             curl_multi_cleanup(s->multi);
581             s->multi = NULL;
582         }
583     }
584 
585     timer_del(&s->timer);
586 }
587 
588 static void curl_attach_aio_context(BlockDriverState *bs,
589                                     AioContext *new_context)
590 {
591     BDRVCURLState *s = bs->opaque;
592 
593     aio_timer_init(new_context, &s->timer,
594                    QEMU_CLOCK_REALTIME, SCALE_NS,
595                    curl_multi_timeout_do, s);
596 
597     assert(!s->multi);
598     s->multi = curl_multi_init();
599     s->aio_context = new_context;
600     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
601     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
602     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
603 }
604 
605 static QemuOptsList runtime_opts = {
606     .name = "curl",
607     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
608     .desc = {
609         {
610             .name = CURL_BLOCK_OPT_URL,
611             .type = QEMU_OPT_STRING,
612             .help = "URL to open",
613         },
614         {
615             .name = CURL_BLOCK_OPT_READAHEAD,
616             .type = QEMU_OPT_SIZE,
617             .help = "Readahead size",
618         },
619         {
620             .name = CURL_BLOCK_OPT_SSLVERIFY,
621             .type = QEMU_OPT_BOOL,
622             .help = "Verify SSL certificate"
623         },
624         {
625             .name = CURL_BLOCK_OPT_TIMEOUT,
626             .type = QEMU_OPT_NUMBER,
627             .help = "Curl timeout"
628         },
629         {
630             .name = CURL_BLOCK_OPT_COOKIE,
631             .type = QEMU_OPT_STRING,
632             .help = "Pass the cookie or list of cookies with each request"
633         },
634         {
635             .name = CURL_BLOCK_OPT_COOKIE_SECRET,
636             .type = QEMU_OPT_STRING,
637             .help = "ID of secret used as cookie passed with each request"
638         },
639         {
640             .name = CURL_BLOCK_OPT_USERNAME,
641             .type = QEMU_OPT_STRING,
642             .help = "Username for HTTP auth"
643         },
644         {
645             .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
646             .type = QEMU_OPT_STRING,
647             .help = "ID of secret used as password for HTTP auth",
648         },
649         {
650             .name = CURL_BLOCK_OPT_PROXY_USERNAME,
651             .type = QEMU_OPT_STRING,
652             .help = "Username for HTTP proxy auth"
653         },
654         {
655             .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
656             .type = QEMU_OPT_STRING,
657             .help = "ID of secret used as password for HTTP proxy auth",
658         },
659         { /* end of list */ }
660     },
661 };
662 
663 
664 static int curl_open(BlockDriverState *bs, QDict *options, int flags,
665                      Error **errp)
666 {
667     BDRVCURLState *s = bs->opaque;
668     CURLState *state = NULL;
669     QemuOpts *opts;
670     const char *file;
671     const char *cookie;
672     const char *cookie_secret;
673     double d;
674     const char *secretid;
675     const char *protocol_delimiter;
676     int ret;
677 
678     ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
679                                     errp);
680     if (ret < 0) {
681         return ret;
682     }
683 
684     if (!libcurl_initialized) {
685         ret = curl_global_init(CURL_GLOBAL_ALL);
686         if (ret) {
687             error_setg(errp, "libcurl initialization failed with %d", ret);
688             return -EIO;
689         }
690         libcurl_initialized = true;
691     }
692 
693     qemu_mutex_init(&s->mutex);
694     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
695     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
696         goto out_noclean;
697     }
698 
699     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
700                                           CURL_BLOCK_OPT_READAHEAD_DEFAULT);
701     if ((s->readahead_size & 0x1ff) != 0) {
702         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
703                    s->readahead_size);
704         goto out_noclean;
705     }
706 
707     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
708                                      CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
709     if (s->timeout > CURL_TIMEOUT_MAX) {
710         error_setg(errp, "timeout parameter is too large or negative");
711         goto out_noclean;
712     }
713 
714     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
715                                      CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
716 
717     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
718     cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
719 
720     if (cookie && cookie_secret) {
721         error_setg(errp,
722                    "curl driver cannot handle both cookie and cookie secret");
723         goto out_noclean;
724     }
725 
726     if (cookie_secret) {
727         s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
728         if (!s->cookie) {
729             goto out_noclean;
730         }
731     } else {
732         s->cookie = g_strdup(cookie);
733     }
734 
735     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
736     if (file == NULL) {
737         error_setg(errp, "curl block driver requires an 'url' option");
738         goto out_noclean;
739     }
740 
741     if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
742         !strstart(protocol_delimiter, "://", NULL))
743     {
744         error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
745                    "start with '%s://')", bs->drv->protocol_name, file,
746                    bs->drv->protocol_name);
747         goto out_noclean;
748     }
749 
750     s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
751     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
752 
753     if (secretid) {
754         s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
755         if (!s->password) {
756             goto out_noclean;
757         }
758     }
759 
760     s->proxyusername = g_strdup(
761         qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
762     secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
763     if (secretid) {
764         s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
765         if (!s->proxypassword) {
766             goto out_noclean;
767         }
768     }
769 
770     trace_curl_open(file);
771     qemu_co_queue_init(&s->free_state_waitq);
772     s->aio_context = bdrv_get_aio_context(bs);
773     s->url = g_strdup(file);
774     s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free);
775     qemu_mutex_lock(&s->mutex);
776     state = curl_find_state(s);
777     qemu_mutex_unlock(&s->mutex);
778     if (!state) {
779         goto out_noclean;
780     }
781 
782     // Get file size
783 
784     if (curl_init_state(s, state) < 0) {
785         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
786                 "curl library initialization failed.");
787         goto out;
788     }
789 
790     s->accept_range = false;
791     if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) ||
792         curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) ||
793         curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) {
794         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
795                 "curl library initialization failed.");
796         goto out;
797     }
798     if (curl_easy_perform(state->curl))
799         goto out;
800     if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
801         goto out;
802     }
803     /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
804      * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
805      * known and zero if it is really zero-length file. */
806 #if LIBCURL_VERSION_NUM >= 0x071304
807     if (d < 0) {
808         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
809                 "Server didn't report file size.");
810         goto out;
811     }
812 #else
813     if (d <= 0) {
814         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
815                 "Unknown file size or zero-length file.");
816         goto out;
817     }
818 #endif
819 
820     s->len = d;
821 
822     if ((!strncasecmp(s->url, "http://", strlen("http://"))
823         || !strncasecmp(s->url, "https://", strlen("https://")))
824         && !s->accept_range) {
825         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
826                 "Server does not support 'range' (byte ranges).");
827         goto out;
828     }
829     trace_curl_open_size(s->len);
830 
831     qemu_mutex_lock(&s->mutex);
832     curl_clean_state(state);
833     qemu_mutex_unlock(&s->mutex);
834     curl_easy_cleanup(state->curl);
835     state->curl = NULL;
836 
837     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
838 
839     qemu_opts_del(opts);
840     return 0;
841 
842 out:
843     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
844     curl_easy_cleanup(state->curl);
845     state->curl = NULL;
846 out_noclean:
847     qemu_mutex_destroy(&s->mutex);
848     g_free(s->cookie);
849     g_free(s->url);
850     g_free(s->username);
851     g_free(s->proxyusername);
852     g_free(s->proxypassword);
853     curl_drop_all_sockets(s->sockets);
854     g_hash_table_destroy(s->sockets);
855     qemu_opts_del(opts);
856     return -EINVAL;
857 }
858 
859 static void coroutine_fn curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
860 {
861     CURLState *state;
862     int running;
863 
864     BDRVCURLState *s = bs->opaque;
865 
866     uint64_t start = acb->offset;
867     uint64_t end;
868 
869     qemu_mutex_lock(&s->mutex);
870 
871     // In case we have the requested data already (e.g. read-ahead),
872     // we can just call the callback and be done.
873     if (curl_find_buf(s, start, acb->bytes, acb)) {
874         goto out;
875     }
876 
877     // No cache found, so let's start a new request
878     for (;;) {
879         state = curl_find_state(s);
880         if (state) {
881             break;
882         }
883         qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
884     }
885 
886     if (curl_init_state(s, state) < 0) {
887         curl_clean_state(state);
888         acb->ret = -EIO;
889         goto out;
890     }
891 
892     acb->start = 0;
893     acb->end = MIN(acb->bytes, s->len - start);
894 
895     state->buf_off = 0;
896     g_free(state->orig_buf);
897     state->buf_start = start;
898     state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
899     end = start + state->buf_len - 1;
900     state->orig_buf = g_try_malloc(state->buf_len);
901     if (state->buf_len && state->orig_buf == NULL) {
902         curl_clean_state(state);
903         acb->ret = -ENOMEM;
904         goto out;
905     }
906     state->acb[0] = acb;
907 
908     snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
909     trace_curl_setup_preadv(acb->bytes, start, state->range);
910     if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) ||
911         curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) {
912         state->acb[0] = NULL;
913         acb->ret = -EIO;
914 
915         curl_clean_state(state);
916         goto out;
917     }
918 
919     /* Tell curl it needs to kick things off */
920     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
921 
922 out:
923     qemu_mutex_unlock(&s->mutex);
924 }
925 
926 static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
927         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
928         BdrvRequestFlags flags)
929 {
930     CURLAIOCB acb = {
931         .co = qemu_coroutine_self(),
932         .ret = -EINPROGRESS,
933         .qiov = qiov,
934         .offset = offset,
935         .bytes = bytes
936     };
937 
938     curl_setup_preadv(bs, &acb);
939     while (acb.ret == -EINPROGRESS) {
940         qemu_coroutine_yield();
941     }
942     return acb.ret;
943 }
944 
945 static void curl_close(BlockDriverState *bs)
946 {
947     BDRVCURLState *s = bs->opaque;
948 
949     trace_curl_close();
950     curl_detach_aio_context(bs);
951     qemu_mutex_destroy(&s->mutex);
952 
953     g_hash_table_destroy(s->sockets);
954     g_free(s->cookie);
955     g_free(s->url);
956     g_free(s->username);
957     g_free(s->proxyusername);
958     g_free(s->proxypassword);
959 }
960 
961 static int64_t coroutine_fn curl_co_getlength(BlockDriverState *bs)
962 {
963     BDRVCURLState *s = bs->opaque;
964     return s->len;
965 }
966 
967 static void curl_refresh_filename(BlockDriverState *bs)
968 {
969     BDRVCURLState *s = bs->opaque;
970 
971     /* "readahead" and "timeout" do not change the guest-visible data,
972      * so ignore them */
973     if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
974         s->cookie || s->username || s->password || s->proxyusername ||
975         s->proxypassword)
976     {
977         return;
978     }
979 
980     pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
981 }
982 
983 
984 static const char *const curl_strong_runtime_opts[] = {
985     CURL_BLOCK_OPT_URL,
986     CURL_BLOCK_OPT_SSLVERIFY,
987     CURL_BLOCK_OPT_COOKIE,
988     CURL_BLOCK_OPT_COOKIE_SECRET,
989     CURL_BLOCK_OPT_USERNAME,
990     CURL_BLOCK_OPT_PASSWORD_SECRET,
991     CURL_BLOCK_OPT_PROXY_USERNAME,
992     CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
993 
994     NULL
995 };
996 
997 static BlockDriver bdrv_http = {
998     .format_name                = "http",
999     .protocol_name              = "http",
1000 
1001     .instance_size              = sizeof(BDRVCURLState),
1002     .bdrv_parse_filename        = curl_parse_filename,
1003     .bdrv_file_open             = curl_open,
1004     .bdrv_close                 = curl_close,
1005     .bdrv_co_getlength          = curl_co_getlength,
1006 
1007     .bdrv_co_preadv             = curl_co_preadv,
1008 
1009     .bdrv_detach_aio_context    = curl_detach_aio_context,
1010     .bdrv_attach_aio_context    = curl_attach_aio_context,
1011 
1012     .bdrv_refresh_filename      = curl_refresh_filename,
1013     .strong_runtime_opts        = curl_strong_runtime_opts,
1014 };
1015 
1016 static BlockDriver bdrv_https = {
1017     .format_name                = "https",
1018     .protocol_name              = "https",
1019 
1020     .instance_size              = sizeof(BDRVCURLState),
1021     .bdrv_parse_filename        = curl_parse_filename,
1022     .bdrv_file_open             = curl_open,
1023     .bdrv_close                 = curl_close,
1024     .bdrv_co_getlength          = curl_co_getlength,
1025 
1026     .bdrv_co_preadv             = curl_co_preadv,
1027 
1028     .bdrv_detach_aio_context    = curl_detach_aio_context,
1029     .bdrv_attach_aio_context    = curl_attach_aio_context,
1030 
1031     .bdrv_refresh_filename      = curl_refresh_filename,
1032     .strong_runtime_opts        = curl_strong_runtime_opts,
1033 };
1034 
1035 static BlockDriver bdrv_ftp = {
1036     .format_name                = "ftp",
1037     .protocol_name              = "ftp",
1038 
1039     .instance_size              = sizeof(BDRVCURLState),
1040     .bdrv_parse_filename        = curl_parse_filename,
1041     .bdrv_file_open             = curl_open,
1042     .bdrv_close                 = curl_close,
1043     .bdrv_co_getlength          = curl_co_getlength,
1044 
1045     .bdrv_co_preadv             = curl_co_preadv,
1046 
1047     .bdrv_detach_aio_context    = curl_detach_aio_context,
1048     .bdrv_attach_aio_context    = curl_attach_aio_context,
1049 
1050     .bdrv_refresh_filename      = curl_refresh_filename,
1051     .strong_runtime_opts        = curl_strong_runtime_opts,
1052 };
1053 
1054 static BlockDriver bdrv_ftps = {
1055     .format_name                = "ftps",
1056     .protocol_name              = "ftps",
1057 
1058     .instance_size              = sizeof(BDRVCURLState),
1059     .bdrv_parse_filename        = curl_parse_filename,
1060     .bdrv_file_open             = curl_open,
1061     .bdrv_close                 = curl_close,
1062     .bdrv_co_getlength          = curl_co_getlength,
1063 
1064     .bdrv_co_preadv             = curl_co_preadv,
1065 
1066     .bdrv_detach_aio_context    = curl_detach_aio_context,
1067     .bdrv_attach_aio_context    = curl_attach_aio_context,
1068 
1069     .bdrv_refresh_filename      = curl_refresh_filename,
1070     .strong_runtime_opts        = curl_strong_runtime_opts,
1071 };
1072 
1073 static void curl_block_init(void)
1074 {
1075     bdrv_register(&bdrv_http);
1076     bdrv_register(&bdrv_https);
1077     bdrv_register(&bdrv_ftp);
1078     bdrv_register(&bdrv_ftps);
1079 }
1080 
1081 block_init(curl_block_init);
1082