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