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