blob: 0fc42d03d77788d21f1b4ef14d588d6da79aec12 [file] [log] [blame]
Alexander Graf769ce762009-05-11 17:41:42 +02001/*
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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Richard W.M. Jones796a0602015-07-08 14:37:48 +010027#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010029#include "qemu/option.h"
Markus Armbrustere2c1c342022-12-21 14:35:49 +010030#include "block/block-io.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010031#include "block/block_int.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010032#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010033#include "qapi/qmp/qstring.h"
Daniel P. Berrange1bff9602016-01-21 14:19:20 +000034#include "crypto/secret.h"
Alexander Graf769ce762009-05-11 17:41:42 +020035#include <curl/curl.h>
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Laurent Viviered2a66d2018-12-13 17:27:25 +010037#include "trace.h"
Alexander Graf769ce762009-05-11 17:41:42 +020038
Alexander Graf769ce762009-05-11 17:41:42 +020039// #define DEBUG_VERBOSE
40
Anton Johanssone7b8d9d2023-01-23 21:14:31 +010041/* 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
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +010047#define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
Max Reitz23dce382016-11-02 18:55:37 +010048 CURLPROTO_FTP | CURLPROTO_FTPS)
Anton Johanssone7b8d9d2023-01-23 21:14:31 +010049#endif
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +010050
Alexander Graf769ce762009-05-11 17:41:42 +020051#define CURL_NUM_STATES 8
52#define CURL_NUM_ACB 8
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +000053#define CURL_TIMEOUT_MAX 10000
Alexander Graf769ce762009-05-11 17:41:42 +020054
Matthew Boothe3542c62014-05-14 19:28:41 -040055#define CURL_BLOCK_OPT_URL "url"
56#define CURL_BLOCK_OPT_READAHEAD "readahead"
Matthew Booth97a3ea52014-05-14 19:28:42 -040057#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -030058#define CURL_BLOCK_OPT_TIMEOUT "timeout"
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +010059#define CURL_BLOCK_OPT_COOKIE "cookie"
Peter Krempa327c8eb2017-05-04 16:00:06 +020060#define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
Daniel P. Berrange1bff9602016-01-21 14:19:20 +000061#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"
Matthew Boothe3542c62014-05-14 19:28:41 -040065
Max Reitz712b64e2019-02-01 20:29:31 +010066#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
Alexander Graf769ce762009-05-11 17:41:42 +020070struct BDRVCURLState;
Max Reitz04878612019-09-10 14:41:30 +020071struct CURLState;
Alexander Graf769ce762009-05-11 17:41:42 +020072
Jeff Cody2d259642017-11-07 17:27:22 -050073static bool libcurl_initialized;
74
Alexander Graf769ce762009-05-11 17:41:42 +020075typedef struct CURLAIOCB {
Paolo Bonzini28256d82017-05-15 12:00:58 +020076 Coroutine *co;
Alexander Graf769ce762009-05-11 17:41:42 +020077 QEMUIOVector *qiov;
Nick Thomas363c3c82011-09-21 11:55:50 +010078
Paolo Bonzini2125e5e2017-05-15 12:00:57 +020079 uint64_t offset;
80 uint64_t bytes;
Paolo Bonzini28256d82017-05-15 12:00:58 +020081 int ret;
Nick Thomas363c3c82011-09-21 11:55:50 +010082
Alexander Graf769ce762009-05-11 17:41:42 +020083 size_t start;
84 size_t end;
85} CURLAIOCB;
86
Max Reitzff5ca162016-10-25 04:54:30 +020087typedef struct CURLSocket {
88 int fd;
Max Reitz3663dca2021-03-09 14:05:40 +010089 struct BDRVCURLState *s;
Max Reitzff5ca162016-10-25 04:54:30 +020090} CURLSocket;
91
Alexander Graf769ce762009-05-11 17:41:42 +020092typedef struct CURLState
93{
94 struct BDRVCURLState *s;
95 CURLAIOCB *acb[CURL_NUM_ACB];
96 CURL *curl;
97 char *orig_buf;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +020098 uint64_t buf_start;
Alexander Graf769ce762009-05-11 17:41:42 +020099 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;
Peter Maydell031fd1b2014-01-24 14:56:17 +0100108 QEMUTimer timer;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200109 uint64_t len;
Alexander Graf769ce762009-05-11 17:41:42 +0200110 CURLState states[CURL_NUM_STATES];
Max Reitz0f418a22021-03-09 14:05:41 +0100111 GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */
Alexander Graf769ce762009-05-11 17:41:42 +0200112 char *url;
Nolanc76f4952009-07-01 17:16:52 -0700113 size_t readahead_size;
Matthew Booth97a3ea52014-05-14 19:28:42 -0400114 bool sslverify;
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000115 uint64_t timeout;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100116 char *cookie;
Fam Zheng3494d652013-07-02 15:19:21 +0800117 bool accept_range;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200118 AioContext *aio_context;
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100119 QemuMutex mutex;
Paolo Bonzini709f2132018-02-03 10:39:35 -0500120 CoQueue free_state_waitq;
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000121 char *username;
122 char *password;
123 char *proxyusername;
124 char *proxypassword;
Alexander Graf769ce762009-05-11 17:41:42 +0200125} BDRVCURLState;
126
127static void curl_clean_state(CURLState *s);
128static void curl_multi_do(void *arg);
129
Max Reitz0f418a22021-03-09 14:05:41 +0100130static gboolean curl_drop_socket(void *key, void *value, void *opaque)
131{
132 CURLSocket *socket = value;
133 BDRVCURLState *s = socket->s;
134
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400135 aio_set_fd_handler(s->aio_context, socket->fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000136 NULL, NULL, NULL, NULL, NULL);
Max Reitz0f418a22021-03-09 14:05:41 +0100137 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
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200145/* Called from curl_multi_do_locked, with s->mutex held. */
Peter Maydell031fd1b2014-01-24 14:56:17 +0100146static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
147{
148 BDRVCURLState *s = opaque;
149
Laurent Viviered2a66d2018-12-13 17:27:25 +0100150 trace_curl_timer_cb(timeout_ms);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100151 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}
Peter Maydell031fd1b2014-01-24 14:56:17 +0100160
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200161/* Called from curl_multi_do_locked, with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200162static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200163 void *userp, void *sp)
Alexander Graf769ce762009-05-11 17:41:42 +0200164{
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200165 BDRVCURLState *s;
Matthew Booth838ef602014-04-29 16:03:30 +0100166 CURLState *state = NULL;
Max Reitzff5ca162016-10-25 04:54:30 +0200167 CURLSocket *socket;
168
Matthew Booth838ef602014-04-29 16:03:30 +0100169 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200170 s = state->s;
Matthew Booth838ef602014-04-29 16:03:30 +0100171
Max Reitz0f418a22021-03-09 14:05:41 +0100172 socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd));
Max Reitzff5ca162016-10-25 04:54:30 +0200173 if (!socket) {
174 socket = g_new0(CURLSocket, 1);
175 socket->fd = fd;
Max Reitz3663dca2021-03-09 14:05:40 +0100176 socket->s = s;
Max Reitz0f418a22021-03-09 14:05:41 +0100177 g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket);
Max Reitzff5ca162016-10-25 04:54:30 +0200178 }
Max Reitzff5ca162016-10-25 04:54:30 +0200179
Laurent Viviered2a66d2018-12-13 17:27:25 +0100180 trace_curl_sock_cb(action, (int)fd);
Alexander Graf769ce762009-05-11 17:41:42 +0200181 switch (action) {
182 case CURL_POLL_IN:
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400183 aio_set_fd_handler(s->aio_context, fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000184 curl_multi_do, NULL, NULL, NULL, socket);
Alexander Graf769ce762009-05-11 17:41:42 +0200185 break;
186 case CURL_POLL_OUT:
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400187 aio_set_fd_handler(s->aio_context, fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000188 NULL, curl_multi_do, NULL, NULL, socket);
Alexander Graf769ce762009-05-11 17:41:42 +0200189 break;
190 case CURL_POLL_INOUT:
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400191 aio_set_fd_handler(s->aio_context, fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000192 curl_multi_do, curl_multi_do,
193 NULL, NULL, socket);
Alexander Graf769ce762009-05-11 17:41:42 +0200194 break;
195 case CURL_POLL_REMOVE:
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400196 aio_set_fd_handler(s->aio_context, fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000197 NULL, NULL, NULL, NULL, NULL);
Alexander Graf769ce762009-05-11 17:41:42 +0200198 break;
199 }
200
Max Reitz007f3392019-09-10 14:41:31 +0200201 if (action == CURL_POLL_REMOVE) {
Max Reitz0f418a22021-03-09 14:05:41 +0100202 g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd));
Max Reitz007f3392019-09-10 14:41:31 +0200203 }
204
Alexander Graf769ce762009-05-11 17:41:42 +0200205 return 0;
206}
207
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200208/* Called from curl_multi_do_locked, with s->mutex held. */
Fam Zheng3494d652013-07-02 15:19:21 +0800209static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
Alexander Graf769ce762009-05-11 17:41:42 +0200210{
Fam Zheng3494d652013-07-02 15:19:21 +0800211 BDRVCURLState *s = opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200212 size_t realsize = size * nmemb;
David Edmondson7788a312020-02-24 10:13:09 +0000213 const char *header = (char *)ptr;
214 const char *end = header + realsize;
David Edmondson69032252020-02-24 10:13:10 +0000215 const char *accept_ranges = "accept-ranges:";
David Edmondson7788a312020-02-24 10:13:09 +0000216 const char *bytes = "bytes";
Alexander Graf769ce762009-05-11 17:41:42 +0200217
David Edmondson7788a312020-02-24 10:13:09 +0000218 if (realsize >= strlen(accept_ranges)
David Edmondson69032252020-02-24 10:13:10 +0000219 && g_ascii_strncasecmp(header, accept_ranges,
220 strlen(accept_ranges)) == 0) {
David Edmondson7788a312020-02-24 10:13:09 +0000221
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 }
Blue Swirl0bfcd592010-05-22 08:02:12 +0000242 }
Alexander Graf769ce762009-05-11 17:41:42 +0200243
244 return realsize;
245}
246
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200247/* Called from curl_multi_do_locked, with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200248static 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;
Alexander Graf769ce762009-05-11 17:41:42 +0200252
Laurent Viviered2a66d2018-12-13 17:27:25 +0100253 trace_curl_read_cb(realsize);
Alexander Graf769ce762009-05-11 17:41:42 +0200254
Max Reitz4e767652016-10-25 04:54:29 +0200255 if (!s || !s->orig_buf) {
256 goto read_end;
257 }
Alexander Graf769ce762009-05-11 17:41:42 +0200258
Fam Zheng6d4b9e52014-03-26 13:05:40 +0100259 if (s->buf_off >= s->buf_len) {
260 /* buffer full, read nothing */
Max Reitz4e767652016-10-25 04:54:29 +0200261 goto read_end;
Fam Zheng6d4b9e52014-03-26 13:05:40 +0100262 }
263 realsize = MIN(realsize, s->buf_len - s->buf_off);
Alexander Graf769ce762009-05-11 17:41:42 +0200264 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
265 s->buf_off += realsize;
266
Max Reitz4e767652016-10-25 04:54:29 +0200267read_end:
268 /* curl will error out if we do not return this value */
269 return size * nmemb;
Alexander Graf769ce762009-05-11 17:41:42 +0200270}
271
Paolo Bonzini456af342017-05-15 12:00:55 +0200272/* Called with s->mutex held. */
Paolo Bonzini28256d82017-05-15 12:00:58 +0200273static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
274 CURLAIOCB *acb)
Alexander Graf769ce762009-05-11 17:41:42 +0200275{
276 int i;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200277 uint64_t end = start + len;
278 uint64_t clamped_end = MIN(end, s->len);
279 uint64_t clamped_len = clamped_end - start;
Alexander Graf769ce762009-05-11 17:41:42 +0200280
281 for (i=0; i<CURL_NUM_STATES; i++) {
282 CURLState *state = &s->states[i];
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200283 uint64_t buf_end = (state->buf_start + state->buf_off);
284 uint64_t buf_fend = (state->buf_start + state->buf_len);
Alexander Graf769ce762009-05-11 17:41:42 +0200285
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) &&
Max Reitz4e504532016-10-25 04:54:31 +0200294 (clamped_end >= state->buf_start) &&
295 (clamped_end <= buf_end))
Alexander Graf769ce762009-05-11 17:41:42 +0200296 {
297 char *buf = state->orig_buf + (start - state->buf_start);
298
Max Reitz4e504532016-10-25 04:54:31 +0200299 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 }
Paolo Bonzini28256d82017-05-15 12:00:58 +0200303 acb->ret = 0;
304 return true;
Alexander Graf769ce762009-05-11 17:41:42 +0200305 }
306
307 // Wait for unfinished chunks
Matthew Boothb7079df2014-04-29 16:03:32 +0100308 if (state->in_use &&
309 (start >= state->buf_start) &&
Alexander Graf769ce762009-05-11 17:41:42 +0200310 (start <= buf_fend) &&
Max Reitz4e504532016-10-25 04:54:31 +0200311 (clamped_end >= state->buf_start) &&
312 (clamped_end <= buf_fend))
Alexander Graf769ce762009-05-11 17:41:42 +0200313 {
314 int j;
315
316 acb->start = start - state->buf_start;
Max Reitz4e504532016-10-25 04:54:31 +0200317 acb->end = acb->start + clamped_len;
Alexander Graf769ce762009-05-11 17:41:42 +0200318
319 for (j=0; j<CURL_NUM_ACB; j++) {
320 if (!state->acb[j]) {
321 state->acb[j] = acb;
Paolo Bonzini28256d82017-05-15 12:00:58 +0200322 return true;
Alexander Graf769ce762009-05-11 17:41:42 +0200323 }
324 }
325 }
326 }
327
Paolo Bonzini28256d82017-05-15 12:00:58 +0200328 return false;
Alexander Graf769ce762009-05-11 17:41:42 +0200329}
330
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100331/* Called with s->mutex held. */
Matthew Booth838ef602014-04-29 16:03:30 +0100332static void curl_multi_check_completion(BDRVCURLState *s)
Alexander Graf769ce762009-05-11 17:41:42 +0200333{
Alexander Graf769ce762009-05-11 17:41:42 +0200334 int msgs_in_queue;
335
Alexander Graf769ce762009-05-11 17:41:42 +0200336 /* Try to find done transfers, so we can free the easy
337 * handle again. */
Matthew Booth1f2cead2014-04-29 16:03:31 +0100338 for (;;) {
Alexander Graf769ce762009-05-11 17:41:42 +0200339 CURLMsg *msg;
340 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
341
Matthew Booth1f2cead2014-04-29 16:03:31 +0100342 /* Quit when there are no more completions */
Alexander Graf769ce762009-05-11 17:41:42 +0200343 if (!msg)
344 break;
Alexander Graf769ce762009-05-11 17:41:42 +0200345
Matthew Booth1f2cead2014-04-29 16:03:31 +0100346 if (msg->msg == CURLMSG_DONE) {
Max Reitzbfb23b42019-09-10 14:41:35 +0200347 int i;
Matthew Booth1f2cead2014-04-29 16:03:31 +0100348 CURLState *state = NULL;
Max Reitzbfb23b42019-09-10 14:41:35 +0200349 bool error = msg->data.result != CURLE_OK;
350
Matthew Booth1f2cead2014-04-29 16:03:31 +0100351 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
352 (char **)&state);
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100353
Max Reitzbfb23b42019-09-10 14:41:35 +0200354 if (error) {
Richard W.M. Jones796a0602015-07-08 14:37:48 +0100355 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 }
Max Reitzbfb23b42019-09-10 14:41:35 +0200366 }
Richard W.M. Jones796a0602015-07-08 14:37:48 +0100367
Max Reitzbfb23b42019-09-10 14:41:35 +0200368 for (i = 0; i < CURL_NUM_ACB; i++) {
369 CURLAIOCB *acb = state->acb[i];
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100370
Max Reitzbfb23b42019-09-10 14:41:35 +0200371 if (acb == NULL) {
372 continue;
Matthew Booth1f2cead2014-04-29 16:03:31 +0100373 }
Max Reitzbfb23b42019-09-10 14:41:35 +0200374
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);
382
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 }
388 }
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);
Alexander Graf769ce762009-05-11 17:41:42 +0200395 }
Matthew Booth1f2cead2014-04-29 16:03:31 +0100396
397 curl_clean_state(state);
398 break;
Alexander Graf769ce762009-05-11 17:41:42 +0200399 }
Matthew Booth1f2cead2014-04-29 16:03:31 +0100400 }
Alexander Graf769ce762009-05-11 17:41:42 +0200401}
402
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100403/* Called with s->mutex held. */
Max Reitz9abaf9f2019-09-10 14:41:34 +0200404static void curl_multi_do_locked(CURLSocket *socket)
Peter Maydell031fd1b2014-01-24 14:56:17 +0100405{
Max Reitz3663dca2021-03-09 14:05:40 +0100406 BDRVCURLState *s = socket->s;
Peter Maydell031fd1b2014-01-24 14:56:17 +0100407 int running;
408 int r;
409
Max Reitz9abaf9f2019-09-10 14:41:34 +0200410 if (!s->multi) {
Peter Maydell031fd1b2014-01-24 14:56:17 +0100411 return;
412 }
413
Max Reitz9abaf9f2019-09-10 14:41:34 +0200414 do {
415 r = curl_multi_socket_action(s->multi, socket->fd, 0, &running);
416 } while (r == CURLM_CALL_MULTI_PERFORM);
Matthew Booth838ef602014-04-29 16:03:30 +0100417}
418
Paolo Bonzini9d456652017-02-13 14:52:30 +0100419static void curl_multi_do(void *arg)
420{
Max Reitz9dbad872019-09-10 14:41:33 +0200421 CURLSocket *socket = arg;
Max Reitz3663dca2021-03-09 14:05:40 +0100422 BDRVCURLState *s = socket->s;
Paolo Bonzini9d456652017-02-13 14:52:30 +0100423
Max Reitz9dbad872019-09-10 14:41:33 +0200424 qemu_mutex_lock(&s->mutex);
425 curl_multi_do_locked(socket);
426 curl_multi_check_completion(s);
427 qemu_mutex_unlock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100428}
429
430static void curl_multi_timeout_do(void *arg)
431{
Peter Maydell031fd1b2014-01-24 14:56:17 +0100432 BDRVCURLState *s = (BDRVCURLState *)arg;
433 int running;
434
435 if (!s->multi) {
436 return;
437 }
438
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100439 qemu_mutex_lock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100440 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
441
Matthew Booth838ef602014-04-29 16:03:30 +0100442 curl_multi_check_completion(s);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100443 qemu_mutex_unlock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100444}
445
Paolo Bonzini456af342017-05-15 12:00:55 +0200446/* Called with s->mutex held. */
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200447static CURLState *curl_find_state(BDRVCURLState *s)
Alexander Graf769ce762009-05-11 17:41:42 +0200448{
449 CURLState *state = NULL;
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200450 int i;
Alexander Graf769ce762009-05-11 17:41:42 +0200451
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200452 for (i = 0; i < CURL_NUM_STATES; i++) {
453 if (!s->states[i].in_use) {
Alexander Graf769ce762009-05-11 17:41:42 +0200454 state = &s->states[i];
455 state->in_use = 1;
456 break;
457 }
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200458 }
459 return state;
460}
Alexander Graf769ce762009-05-11 17:41:42 +0200461
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200462static int curl_init_state(BDRVCURLState *s, CURLState *state)
463{
Matthew Booth9e550b32014-04-29 16:03:26 +0100464 if (!state->curl) {
465 state->curl = curl_easy_init();
466 if (!state->curl) {
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200467 return -EIO;
Matthew Booth9e550b32014-04-29 16:03:26 +0100468 }
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000469 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;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100475 }
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000476 if (s->cookie) {
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;
492 }
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000493 if (s->username) {
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000494 if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) {
495 goto err;
496 }
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000497 }
498 if (s->password) {
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000499 if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) {
500 goto err;
501 }
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000502 }
503 if (s->proxyusername) {
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000504 if (curl_easy_setopt(state->curl,
505 CURLOPT_PROXYUSERNAME, s->proxyusername)) {
506 goto err;
507 }
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000508 }
509 if (s->proxypassword) {
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000510 if (curl_easy_setopt(state->curl,
511 CURLOPT_PROXYPASSWORD, s->proxypassword)) {
512 goto err;
513 }
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000514 }
515
Matthew Booth9e550b32014-04-29 16:03:26 +0100516 /* 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 *
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100520 * 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.
Matthew Booth9e550b32014-04-29 16:03:26 +0100523 */
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100524#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
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000532 if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) ||
533 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) {
534 goto err;
535 }
Stefan Hajnoczi8a8f5842013-02-13 09:25:34 +0100536#endif
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +0100537
Alexander Graf769ce762009-05-11 17:41:42 +0200538#ifdef DEBUG_VERBOSE
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000539 if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) {
540 goto err;
541 }
Alexander Graf769ce762009-05-11 17:41:42 +0200542#endif
Matthew Booth9e550b32014-04-29 16:03:26 +0100543 }
Alexander Graf769ce762009-05-11 17:41:42 +0200544
545 state->s = s;
546
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200547 return 0;
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000548
549err:
550 curl_easy_cleanup(state->curl);
551 state->curl = NULL;
552 return -EIO;
Alexander Graf769ce762009-05-11 17:41:42 +0200553}
554
Paolo Bonzini456af342017-05-15 12:00:55 +0200555/* Called with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200556static void curl_clean_state(CURLState *s)
557{
Paolo Bonzini675a7752017-05-15 12:00:53 +0200558 int j;
559 for (j = 0; j < CURL_NUM_ACB; j++) {
560 assert(!s->acb[j]);
561 }
562
Alexander Graf769ce762009-05-11 17:41:42 +0200563 if (s->s->multi)
564 curl_multi_remove_handle(s->s->multi, s->curl);
Max Reitzff5ca162016-10-25 04:54:30 +0200565
Alexander Graf769ce762009-05-11 17:41:42 +0200566 s->in_use = 0;
Paolo Bonzini2bb5c932017-05-15 12:00:59 +0200567
Paolo Bonzini709f2132018-02-03 10:39:35 -0500568 qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200569}
570
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200571static void curl_parse_filename(const char *filename, QDict *options,
572 Error **errp)
Alexander Graf769ce762009-05-11 17:41:42 +0200573{
Eric Blake46f5ac22017-04-27 16:58:17 -0500574 qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200575}
576
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200577static void curl_detach_aio_context(BlockDriverState *bs)
578{
579 BDRVCURLState *s = bs->opaque;
580 int i;
581
Gan Qixinf5056b72020-12-03 15:50:53 +0800582 WITH_QEMU_LOCK_GUARD(&s->mutex) {
Max Reitz0f418a22021-03-09 14:05:41 +0100583 curl_drop_all_sockets(s->sockets);
Gan Qixinf5056b72020-12-03 15:50:53 +0800584 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;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200594 }
Gan Qixinf5056b72020-12-03 15:50:53 +0800595 if (s->multi) {
596 curl_multi_cleanup(s->multi);
597 s->multi = NULL;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200598 }
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200599 }
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200600
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);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200617 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
618 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200619}
620
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200621static QemuOptsList runtime_opts = {
622 .name = "curl",
623 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
624 .desc = {
625 {
Matthew Boothe3542c62014-05-14 19:28:41 -0400626 .name = CURL_BLOCK_OPT_URL,
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200627 .type = QEMU_OPT_STRING,
628 .help = "URL to open",
629 },
630 {
Matthew Boothe3542c62014-05-14 19:28:41 -0400631 .name = CURL_BLOCK_OPT_READAHEAD,
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200632 .type = QEMU_OPT_SIZE,
633 .help = "Readahead size",
634 },
Matthew Booth97a3ea52014-05-14 19:28:42 -0400635 {
636 .name = CURL_BLOCK_OPT_SSLVERIFY,
637 .type = QEMU_OPT_BOOL,
638 .help = "Verify SSL certificate"
639 },
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300640 {
641 .name = CURL_BLOCK_OPT_TIMEOUT,
642 .type = QEMU_OPT_NUMBER,
643 .help = "Curl timeout"
644 },
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100645 {
646 .name = CURL_BLOCK_OPT_COOKIE,
647 .type = QEMU_OPT_STRING,
648 .help = "Pass the cookie or list of cookies with each request"
649 },
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000650 {
Peter Krempa327c8eb2017-05-04 16:00:06 +0200651 .name = CURL_BLOCK_OPT_COOKIE_SECRET,
652 .type = QEMU_OPT_STRING,
653 .help = "ID of secret used as cookie passed with each request"
654 },
655 {
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000656 .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 },
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200675 { /* end of list */ }
676 },
677};
678
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000679
Max Reitz015a1032013-09-05 14:22:29 +0200680static int curl_open(BlockDriverState *bs, QDict *options, int flags,
681 Error **errp)
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200682{
683 BDRVCURLState *s = bs->opaque;
684 CURLState *state = NULL;
685 QemuOpts *opts;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200686 const char *file;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100687 const char *cookie;
Peter Krempa327c8eb2017-05-04 16:00:06 +0200688 const char *cookie_secret;
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100689 /* 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
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000695 const char *secretid;
Max Reitz34634ca2017-03-31 14:04:31 +0200696 const char *protocol_delimiter;
Jeff Cody2d259642017-11-07 17:27:22 -0500697 int ret;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200698
Kevin Wolf6ceef362018-10-08 17:27:18 +0200699 ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
700 errp);
701 if (ret < 0) {
702 return ret;
Richard W.M. Jonesa7cea2b2013-06-10 12:38:43 +0100703 }
704
Jeff Cody2d259642017-11-07 17:27:22 -0500705 if (!libcurl_initialized) {
706 ret = curl_global_init(CURL_GLOBAL_ALL);
707 if (ret) {
708 error_setg(errp, "libcurl initialization failed with %d", ret);
709 return -EIO;
710 }
711 libcurl_initialized = true;
712 }
713
Paolo Bonzini456af342017-05-15 12:00:55 +0200714 qemu_mutex_init(&s->mutex);
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800715 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Markus Armbruster668f62e2020-07-07 18:06:02 +0200716 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200717 goto out_noclean;
718 }
719
Matthew Boothe3542c62014-05-14 19:28:41 -0400720 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
Max Reitz712b64e2019-02-01 20:29:31 +0100721 CURL_BLOCK_OPT_READAHEAD_DEFAULT);
Nolanc76f4952009-07-01 17:16:52 -0700722 if ((s->readahead_size & 0x1ff) != 0) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100723 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
724 s->readahead_size);
Nolanc76f4952009-07-01 17:16:52 -0700725 goto out_noclean;
726 }
727
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300728 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
Max Reitz712b64e2019-02-01 20:29:31 +0100729 CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000730 if (s->timeout > CURL_TIMEOUT_MAX) {
731 error_setg(errp, "timeout parameter is too large or negative");
732 goto out_noclean;
733 }
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300734
Max Reitz712b64e2019-02-01 20:29:31 +0100735 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
736 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
Matthew Booth97a3ea52014-05-14 19:28:42 -0400737
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100738 cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
Peter Krempa327c8eb2017-05-04 16:00:06 +0200739 cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
740
741 if (cookie && cookie_secret) {
742 error_setg(errp,
743 "curl driver cannot handle both cookie and cookie secret");
744 goto out_noclean;
745 }
746
747 if (cookie_secret) {
748 s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
749 if (!s->cookie) {
750 goto out_noclean;
751 }
752 } else {
753 s->cookie = g_strdup(cookie);
754 }
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100755
Matthew Boothe3542c62014-05-14 19:28:41 -0400756 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200757 if (file == NULL) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100758 error_setg(errp, "curl block driver requires an 'url' option");
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200759 goto out_noclean;
760 }
761
Max Reitz34634ca2017-03-31 14:04:31 +0200762 if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
763 !strstart(protocol_delimiter, "://", NULL))
764 {
765 error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
766 "start with '%s://')", bs->drv->protocol_name, file,
767 bs->drv->protocol_name);
768 goto out_noclean;
769 }
770
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000771 s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
772 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
773
774 if (secretid) {
775 s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
776 if (!s->password) {
777 goto out_noclean;
778 }
779 }
780
781 s->proxyusername = g_strdup(
782 qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
783 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
784 if (secretid) {
785 s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
786 if (!s->proxypassword) {
787 goto out_noclean;
788 }
789 }
790
Laurent Viviered2a66d2018-12-13 17:27:25 +0100791 trace_curl_open(file);
Paolo Bonzini709f2132018-02-03 10:39:35 -0500792 qemu_co_queue_init(&s->free_state_waitq);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200793 s->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200794 s->url = g_strdup(file);
Max Reitz0f418a22021-03-09 14:05:41 +0100795 s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free);
Paolo Bonzini456af342017-05-15 12:00:55 +0200796 qemu_mutex_lock(&s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200797 state = curl_find_state(s);
Paolo Bonzini456af342017-05-15 12:00:55 +0200798 qemu_mutex_unlock(&s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200799 if (!state) {
Alexander Graf769ce762009-05-11 17:41:42 +0200800 goto out_noclean;
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200801 }
Alexander Graf769ce762009-05-11 17:41:42 +0200802
803 // Get file size
804
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200805 if (curl_init_state(s, state) < 0) {
Peter Maydell2ea7dfc2022-02-22 15:23:40 +0000806 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
807 "curl library initialization failed.");
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200808 goto out;
809 }
810
Fam Zheng3494d652013-07-02 15:19:21 +0800811 s->accept_range = false;
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000812 if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) ||
813 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) ||
814 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) {
815 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
816 "curl library initialization failed.");
817 goto out;
818 }
Alexander Graf769ce762009-05-11 17:41:42 +0200819 if (curl_easy_perform(state->curl))
820 goto out;
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100821 /* CURL 7.55.0 deprecates CURLINFO_CONTENT_LENGTH_DOWNLOAD in favour of
822 * the *_T version which returns a more sensible type for content length.
823 */
824#if LIBCURL_VERSION_NUM >= 0x073700
825 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl)) {
Alexander Graf769ce762009-05-11 17:41:42 +0200826 goto out;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200827 }
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100828#else
829 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl)) {
830 goto out;
831 }
832#endif
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200833 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
834 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
Stefan Weil50d6a8a2018-07-12 21:51:20 +0200835 * known and zero if it is really zero-length file. */
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200836#if LIBCURL_VERSION_NUM >= 0x071304
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100837 if (cl < 0) {
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200838 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
839 "Server didn't report file size.");
840 goto out;
841 }
842#else
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100843 if (cl <= 0) {
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200844 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
845 "Unknown file size or zero-length file.");
846 goto out;
847 }
848#endif
849
Anton Johanssone7b8d9d2023-01-23 21:14:31 +0100850 s->len = cl;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200851
Fam Zheng3494d652013-07-02 15:19:21 +0800852 if ((!strncasecmp(s->url, "http://", strlen("http://"))
853 || !strncasecmp(s->url, "https://", strlen("https://")))
854 && !s->accept_range) {
855 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
856 "Server does not support 'range' (byte ranges).");
857 goto out;
858 }
Laurent Viviered2a66d2018-12-13 17:27:25 +0100859 trace_curl_open_size(s->len);
Alexander Graf769ce762009-05-11 17:41:42 +0200860
Paolo Bonzini456af342017-05-15 12:00:55 +0200861 qemu_mutex_lock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200862 curl_clean_state(state);
Paolo Bonzini456af342017-05-15 12:00:55 +0200863 qemu_mutex_unlock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200864 curl_easy_cleanup(state->curl);
865 state->curl = NULL;
866
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200867 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
Alexander Graf769ce762009-05-11 17:41:42 +0200868
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200869 qemu_opts_del(opts);
Alexander Graf769ce762009-05-11 17:41:42 +0200870 return 0;
871
872out:
Maria Kustovaacd7fdc2014-03-18 09:59:18 +0400873 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
Alexander Graf769ce762009-05-11 17:41:42 +0200874 curl_easy_cleanup(state->curl);
875 state->curl = NULL;
876out_noclean:
Paolo Bonzini456af342017-05-15 12:00:55 +0200877 qemu_mutex_destroy(&s->mutex);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100878 g_free(s->cookie);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200879 g_free(s->url);
Jeff Cody996922d2017-11-07 17:27:23 -0500880 g_free(s->username);
881 g_free(s->proxyusername);
882 g_free(s->proxypassword);
Hanna Czenczek1e84cf72023-02-06 14:29:49 +0100883 if (s->sockets) {
884 curl_drop_all_sockets(s->sockets);
885 g_hash_table_destroy(s->sockets);
886 }
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200887 qemu_opts_del(opts);
Alexander Graf769ce762009-05-11 17:41:42 +0200888 return -EINVAL;
889}
890
Paolo Bonzini9bae2ac2022-09-22 10:49:14 +0200891static void coroutine_fn curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
Alexander Graf769ce762009-05-11 17:41:42 +0200892{
Alexander Graf769ce762009-05-11 17:41:42 +0200893 CURLState *state;
Matthew Boothb69cdef2014-04-29 16:03:29 +0100894 int running;
Alexander Graf769ce762009-05-11 17:41:42 +0200895
Paolo Bonzini19196312017-02-13 14:52:31 +0100896 BDRVCURLState *s = bs->opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200897
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200898 uint64_t start = acb->offset;
899 uint64_t end;
Alexander Graf769ce762009-05-11 17:41:42 +0200900
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100901 qemu_mutex_lock(&s->mutex);
Paolo Bonzini19196312017-02-13 14:52:31 +0100902
Alexander Graf769ce762009-05-11 17:41:42 +0200903 // In case we have the requested data already (e.g. read-ahead),
904 // we can just call the callback and be done.
Paolo Bonzini28256d82017-05-15 12:00:58 +0200905 if (curl_find_buf(s, start, acb->bytes, acb)) {
906 goto out;
Alexander Graf769ce762009-05-11 17:41:42 +0200907 }
908
909 // No cache found, so let's start a new request
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200910 for (;;) {
911 state = curl_find_state(s);
912 if (state) {
913 break;
914 }
Paolo Bonzini709f2132018-02-03 10:39:35 -0500915 qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200916 }
917
918 if (curl_init_state(s, state) < 0) {
919 curl_clean_state(state);
Paolo Bonzini28256d82017-05-15 12:00:58 +0200920 acb->ret = -EIO;
Paolo Bonzini19196312017-02-13 14:52:31 +0100921 goto out;
Nick Thomas363c3c82011-09-21 11:55:50 +0100922 }
Alexander Graf769ce762009-05-11 17:41:42 +0200923
924 acb->start = 0;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200925 acb->end = MIN(acb->bytes, s->len - start);
Alexander Graf769ce762009-05-11 17:41:42 +0200926
927 state->buf_off = 0;
Markus Armbrusterf7047c22014-06-06 18:25:12 +0200928 g_free(state->orig_buf);
Alexander Graf769ce762009-05-11 17:41:42 +0200929 state->buf_start = start;
Max Reitz4e504532016-10-25 04:54:31 +0200930 state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
931 end = start + state->buf_len - 1;
Kevin Wolf8dc7a772014-05-20 13:26:40 +0200932 state->orig_buf = g_try_malloc(state->buf_len);
933 if (state->buf_len && state->orig_buf == NULL) {
934 curl_clean_state(state);
Paolo Bonzini28256d82017-05-15 12:00:58 +0200935 acb->ret = -ENOMEM;
Paolo Bonzini19196312017-02-13 14:52:31 +0100936 goto out;
Kevin Wolf8dc7a772014-05-20 13:26:40 +0200937 }
Alexander Graf769ce762009-05-11 17:41:42 +0200938 state->acb[0] = acb;
939
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200940 snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
Laurent Viviered2a66d2018-12-13 17:27:25 +0100941 trace_curl_setup_preadv(acb->bytes, start, state->range);
Peter Maydellb0ea6c92022-02-22 15:23:41 +0000942 if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) ||
943 curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) {
Max Reitzc34dc072019-09-10 14:41:36 +0200944 state->acb[0] = NULL;
945 acb->ret = -EIO;
946
947 curl_clean_state(state);
948 goto out;
949 }
Alexander Graf769ce762009-05-11 17:41:42 +0200950
Matthew Boothb69cdef2014-04-29 16:03:29 +0100951 /* Tell curl it needs to kick things off */
952 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
Paolo Bonzini19196312017-02-13 14:52:31 +0100953
954out:
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100955 qemu_mutex_unlock(&s->mutex);
Nick Thomas363c3c82011-09-21 11:55:50 +0100956}
957
Paolo Bonzini28256d82017-05-15 12:00:58 +0200958static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300959 int64_t offset, int64_t bytes, QEMUIOVector *qiov,
960 BdrvRequestFlags flags)
Nick Thomas363c3c82011-09-21 11:55:50 +0100961{
Paolo Bonzini28256d82017-05-15 12:00:58 +0200962 CURLAIOCB acb = {
963 .co = qemu_coroutine_self(),
964 .ret = -EINPROGRESS,
965 .qiov = qiov,
966 .offset = offset,
967 .bytes = bytes
968 };
Nick Thomas363c3c82011-09-21 11:55:50 +0100969
Paolo Bonzini28256d82017-05-15 12:00:58 +0200970 curl_setup_preadv(bs, &acb);
971 while (acb.ret == -EINPROGRESS) {
972 qemu_coroutine_yield();
973 }
974 return acb.ret;
Alexander Graf769ce762009-05-11 17:41:42 +0200975}
976
Alexander Graf769ce762009-05-11 17:41:42 +0200977static void curl_close(BlockDriverState *bs)
978{
979 BDRVCURLState *s = bs->opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200980
Laurent Viviered2a66d2018-12-13 17:27:25 +0100981 trace_curl_close();
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200982 curl_detach_aio_context(bs);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100983 qemu_mutex_destroy(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100984
Max Reitz0f418a22021-03-09 14:05:41 +0100985 g_hash_table_destroy(s->sockets);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100986 g_free(s->cookie);
Stefan Weil45724d62012-09-01 11:06:45 +0200987 g_free(s->url);
Jeff Cody996922d2017-11-07 17:27:23 -0500988 g_free(s->username);
989 g_free(s->proxyusername);
990 g_free(s->proxypassword);
Alexander Graf769ce762009-05-11 17:41:42 +0200991}
992
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +0100993static int64_t coroutine_fn curl_co_getlength(BlockDriverState *bs)
Alexander Graf769ce762009-05-11 17:41:42 +0200994{
995 BDRVCURLState *s = bs->opaque;
996 return s->len;
997}
998
Max Reitz937c0072019-02-01 20:29:32 +0100999static void curl_refresh_filename(BlockDriverState *bs)
1000{
1001 BDRVCURLState *s = bs->opaque;
1002
1003 /* "readahead" and "timeout" do not change the guest-visible data,
1004 * so ignore them */
1005 if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
1006 s->cookie || s->username || s->password || s->proxyusername ||
1007 s->proxypassword)
1008 {
1009 return;
1010 }
1011
1012 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
1013}
1014
1015
Max Reitz26542672019-02-01 20:29:25 +01001016static const char *const curl_strong_runtime_opts[] = {
1017 CURL_BLOCK_OPT_URL,
1018 CURL_BLOCK_OPT_SSLVERIFY,
1019 CURL_BLOCK_OPT_COOKIE,
1020 CURL_BLOCK_OPT_COOKIE_SECRET,
1021 CURL_BLOCK_OPT_USERNAME,
1022 CURL_BLOCK_OPT_PASSWORD_SECRET,
1023 CURL_BLOCK_OPT_PROXY_USERNAME,
1024 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
1025
1026 NULL
1027};
1028
Alexander Graf769ce762009-05-11 17:41:42 +02001029static BlockDriver bdrv_http = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001030 .format_name = "http",
1031 .protocol_name = "http",
Alexander Graf769ce762009-05-11 17:41:42 +02001032
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001033 .instance_size = sizeof(BDRVCURLState),
1034 .bdrv_parse_filename = curl_parse_filename,
1035 .bdrv_file_open = curl_open,
1036 .bdrv_close = curl_close,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001037 .bdrv_co_getlength = curl_co_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001038
Paolo Bonzini28256d82017-05-15 12:00:58 +02001039 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001040
1041 .bdrv_detach_aio_context = curl_detach_aio_context,
1042 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001043
Max Reitz937c0072019-02-01 20:29:32 +01001044 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001045 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001046};
1047
1048static BlockDriver bdrv_https = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001049 .format_name = "https",
1050 .protocol_name = "https",
Alexander Graf769ce762009-05-11 17:41:42 +02001051
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001052 .instance_size = sizeof(BDRVCURLState),
1053 .bdrv_parse_filename = curl_parse_filename,
1054 .bdrv_file_open = curl_open,
1055 .bdrv_close = curl_close,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001056 .bdrv_co_getlength = curl_co_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001057
Paolo Bonzini28256d82017-05-15 12:00:58 +02001058 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001059
1060 .bdrv_detach_aio_context = curl_detach_aio_context,
1061 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001062
Max Reitz937c0072019-02-01 20:29:32 +01001063 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001064 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001065};
1066
1067static BlockDriver bdrv_ftp = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001068 .format_name = "ftp",
1069 .protocol_name = "ftp",
Alexander Graf769ce762009-05-11 17:41:42 +02001070
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001071 .instance_size = sizeof(BDRVCURLState),
1072 .bdrv_parse_filename = curl_parse_filename,
1073 .bdrv_file_open = curl_open,
1074 .bdrv_close = curl_close,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001075 .bdrv_co_getlength = curl_co_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001076
Paolo Bonzini28256d82017-05-15 12:00:58 +02001077 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001078
1079 .bdrv_detach_aio_context = curl_detach_aio_context,
1080 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001081
Max Reitz937c0072019-02-01 20:29:32 +01001082 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001083 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001084};
1085
1086static BlockDriver bdrv_ftps = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001087 .format_name = "ftps",
1088 .protocol_name = "ftps",
Alexander Graf769ce762009-05-11 17:41:42 +02001089
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001090 .instance_size = sizeof(BDRVCURLState),
1091 .bdrv_parse_filename = curl_parse_filename,
1092 .bdrv_file_open = curl_open,
1093 .bdrv_close = curl_close,
Emanuele Giuseppe Espositoc86422c2023-01-13 21:42:04 +01001094 .bdrv_co_getlength = curl_co_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001095
Paolo Bonzini28256d82017-05-15 12:00:58 +02001096 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001097
1098 .bdrv_detach_aio_context = curl_detach_aio_context,
1099 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001100
Max Reitz937c0072019-02-01 20:29:32 +01001101 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001102 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001103};
1104
Alexander Graf769ce762009-05-11 17:41:42 +02001105static void curl_block_init(void)
1106{
1107 bdrv_register(&bdrv_http);
1108 bdrv_register(&bdrv_https);
1109 bdrv_register(&bdrv_ftp);
1110 bdrv_register(&bdrv_ftps);
Alexander Graf769ce762009-05-11 17:41:42 +02001111}
1112
1113block_init(curl_block_init);