blob: 50e741a0d7a57a74cdd99eca4d6d0eb7fc9633ff [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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/block_int.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010031#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010032#include "qapi/qmp/qstring.h"
Daniel P. Berrange1bff9602016-01-21 14:19:20 +000033#include "crypto/secret.h"
Alexander Graf769ce762009-05-11 17:41:42 +020034#include <curl/curl.h>
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Laurent Viviered2a66d2018-12-13 17:27:25 +010036#include "trace.h"
Alexander Graf769ce762009-05-11 17:41:42 +020037
Alexander Graf769ce762009-05-11 17:41:42 +020038// #define DEBUG_VERBOSE
39
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +010040#define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
Max Reitz23dce382016-11-02 18:55:37 +010041 CURLPROTO_FTP | CURLPROTO_FTPS)
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +010042
Alexander Graf769ce762009-05-11 17:41:42 +020043#define CURL_NUM_STATES 8
44#define CURL_NUM_ACB 8
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +000045#define CURL_TIMEOUT_MAX 10000
Alexander Graf769ce762009-05-11 17:41:42 +020046
Matthew Boothe3542c62014-05-14 19:28:41 -040047#define CURL_BLOCK_OPT_URL "url"
48#define CURL_BLOCK_OPT_READAHEAD "readahead"
Matthew Booth97a3ea52014-05-14 19:28:42 -040049#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -030050#define CURL_BLOCK_OPT_TIMEOUT "timeout"
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +010051#define CURL_BLOCK_OPT_COOKIE "cookie"
Peter Krempa327c8eb2017-05-04 16:00:06 +020052#define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
Daniel P. Berrange1bff9602016-01-21 14:19:20 +000053#define CURL_BLOCK_OPT_USERNAME "username"
54#define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
55#define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
56#define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
Matthew Boothe3542c62014-05-14 19:28:41 -040057
Max Reitz712b64e2019-02-01 20:29:31 +010058#define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
59#define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
60#define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
61
Alexander Graf769ce762009-05-11 17:41:42 +020062struct BDRVCURLState;
Max Reitz04878612019-09-10 14:41:30 +020063struct CURLState;
Alexander Graf769ce762009-05-11 17:41:42 +020064
Jeff Cody2d259642017-11-07 17:27:22 -050065static bool libcurl_initialized;
66
Alexander Graf769ce762009-05-11 17:41:42 +020067typedef struct CURLAIOCB {
Paolo Bonzini28256d82017-05-15 12:00:58 +020068 Coroutine *co;
Alexander Graf769ce762009-05-11 17:41:42 +020069 QEMUIOVector *qiov;
Nick Thomas363c3c82011-09-21 11:55:50 +010070
Paolo Bonzini2125e5e2017-05-15 12:00:57 +020071 uint64_t offset;
72 uint64_t bytes;
Paolo Bonzini28256d82017-05-15 12:00:58 +020073 int ret;
Nick Thomas363c3c82011-09-21 11:55:50 +010074
Alexander Graf769ce762009-05-11 17:41:42 +020075 size_t start;
76 size_t end;
77} CURLAIOCB;
78
Max Reitzff5ca162016-10-25 04:54:30 +020079typedef struct CURLSocket {
80 int fd;
Max Reitz3663dca2021-03-09 14:05:40 +010081 struct BDRVCURLState *s;
Max Reitzff5ca162016-10-25 04:54:30 +020082} CURLSocket;
83
Alexander Graf769ce762009-05-11 17:41:42 +020084typedef struct CURLState
85{
86 struct BDRVCURLState *s;
87 CURLAIOCB *acb[CURL_NUM_ACB];
88 CURL *curl;
89 char *orig_buf;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +020090 uint64_t buf_start;
Alexander Graf769ce762009-05-11 17:41:42 +020091 size_t buf_off;
92 size_t buf_len;
93 char range[128];
94 char errmsg[CURL_ERROR_SIZE];
95 char in_use;
96} CURLState;
97
98typedef struct BDRVCURLState {
99 CURLM *multi;
Peter Maydell031fd1b2014-01-24 14:56:17 +0100100 QEMUTimer timer;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200101 uint64_t len;
Alexander Graf769ce762009-05-11 17:41:42 +0200102 CURLState states[CURL_NUM_STATES];
Max Reitz0f418a22021-03-09 14:05:41 +0100103 GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */
Alexander Graf769ce762009-05-11 17:41:42 +0200104 char *url;
Nolanc76f4952009-07-01 17:16:52 -0700105 size_t readahead_size;
Matthew Booth97a3ea52014-05-14 19:28:42 -0400106 bool sslverify;
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000107 uint64_t timeout;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100108 char *cookie;
Fam Zheng3494d652013-07-02 15:19:21 +0800109 bool accept_range;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200110 AioContext *aio_context;
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100111 QemuMutex mutex;
Paolo Bonzini709f2132018-02-03 10:39:35 -0500112 CoQueue free_state_waitq;
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000113 char *username;
114 char *password;
115 char *proxyusername;
116 char *proxypassword;
Alexander Graf769ce762009-05-11 17:41:42 +0200117} BDRVCURLState;
118
119static void curl_clean_state(CURLState *s);
120static void curl_multi_do(void *arg);
121
Max Reitz0f418a22021-03-09 14:05:41 +0100122static gboolean curl_drop_socket(void *key, void *value, void *opaque)
123{
124 CURLSocket *socket = value;
125 BDRVCURLState *s = socket->s;
126
127 aio_set_fd_handler(s->aio_context, socket->fd, false,
128 NULL, NULL, NULL, NULL);
129 return true;
130}
131
132static void curl_drop_all_sockets(GHashTable *sockets)
133{
134 g_hash_table_foreach_remove(sockets, curl_drop_socket, NULL);
135}
136
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200137/* Called from curl_multi_do_locked, with s->mutex held. */
Peter Maydell031fd1b2014-01-24 14:56:17 +0100138static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
139{
140 BDRVCURLState *s = opaque;
141
Laurent Viviered2a66d2018-12-13 17:27:25 +0100142 trace_curl_timer_cb(timeout_ms);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100143 if (timeout_ms == -1) {
144 timer_del(&s->timer);
145 } else {
146 int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
147 timer_mod(&s->timer,
148 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
149 }
150 return 0;
151}
Peter Maydell031fd1b2014-01-24 14:56:17 +0100152
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200153/* Called from curl_multi_do_locked, with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200154static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200155 void *userp, void *sp)
Alexander Graf769ce762009-05-11 17:41:42 +0200156{
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200157 BDRVCURLState *s;
Matthew Booth838ef602014-04-29 16:03:30 +0100158 CURLState *state = NULL;
Max Reitzff5ca162016-10-25 04:54:30 +0200159 CURLSocket *socket;
160
Matthew Booth838ef602014-04-29 16:03:30 +0100161 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200162 s = state->s;
Matthew Booth838ef602014-04-29 16:03:30 +0100163
Max Reitz0f418a22021-03-09 14:05:41 +0100164 socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd));
Max Reitzff5ca162016-10-25 04:54:30 +0200165 if (!socket) {
166 socket = g_new0(CURLSocket, 1);
167 socket->fd = fd;
Max Reitz3663dca2021-03-09 14:05:40 +0100168 socket->s = s;
Max Reitz0f418a22021-03-09 14:05:41 +0100169 g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket);
Max Reitzff5ca162016-10-25 04:54:30 +0200170 }
Max Reitzff5ca162016-10-25 04:54:30 +0200171
Laurent Viviered2a66d2018-12-13 17:27:25 +0100172 trace_curl_sock_cb(action, (int)fd);
Alexander Graf769ce762009-05-11 17:41:42 +0200173 switch (action) {
174 case CURL_POLL_IN:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800175 aio_set_fd_handler(s->aio_context, fd, false,
Max Reitz9dbad872019-09-10 14:41:33 +0200176 curl_multi_do, NULL, NULL, socket);
Alexander Graf769ce762009-05-11 17:41:42 +0200177 break;
178 case CURL_POLL_OUT:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800179 aio_set_fd_handler(s->aio_context, fd, false,
Max Reitz9dbad872019-09-10 14:41:33 +0200180 NULL, curl_multi_do, NULL, socket);
Alexander Graf769ce762009-05-11 17:41:42 +0200181 break;
182 case CURL_POLL_INOUT:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800183 aio_set_fd_handler(s->aio_context, fd, false,
Max Reitz9dbad872019-09-10 14:41:33 +0200184 curl_multi_do, curl_multi_do, NULL, socket);
Alexander Graf769ce762009-05-11 17:41:42 +0200185 break;
186 case CURL_POLL_REMOVE:
Fam Zhengdca21ef2015-10-23 11:08:05 +0800187 aio_set_fd_handler(s->aio_context, fd, false,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000188 NULL, NULL, NULL, NULL);
Alexander Graf769ce762009-05-11 17:41:42 +0200189 break;
190 }
191
Max Reitz007f3392019-09-10 14:41:31 +0200192 if (action == CURL_POLL_REMOVE) {
Max Reitz0f418a22021-03-09 14:05:41 +0100193 g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd));
Max Reitz007f3392019-09-10 14:41:31 +0200194 }
195
Alexander Graf769ce762009-05-11 17:41:42 +0200196 return 0;
197}
198
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200199/* Called from curl_multi_do_locked, with s->mutex held. */
Fam Zheng3494d652013-07-02 15:19:21 +0800200static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
Alexander Graf769ce762009-05-11 17:41:42 +0200201{
Fam Zheng3494d652013-07-02 15:19:21 +0800202 BDRVCURLState *s = opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200203 size_t realsize = size * nmemb;
David Edmondson7788a312020-02-24 10:13:09 +0000204 const char *header = (char *)ptr;
205 const char *end = header + realsize;
David Edmondson69032252020-02-24 10:13:10 +0000206 const char *accept_ranges = "accept-ranges:";
David Edmondson7788a312020-02-24 10:13:09 +0000207 const char *bytes = "bytes";
Alexander Graf769ce762009-05-11 17:41:42 +0200208
David Edmondson7788a312020-02-24 10:13:09 +0000209 if (realsize >= strlen(accept_ranges)
David Edmondson69032252020-02-24 10:13:10 +0000210 && g_ascii_strncasecmp(header, accept_ranges,
211 strlen(accept_ranges)) == 0) {
David Edmondson7788a312020-02-24 10:13:09 +0000212
213 char *p = strchr(header, ':') + 1;
214
215 /* Skip whitespace between the header name and value. */
216 while (p < end && *p && g_ascii_isspace(*p)) {
217 p++;
218 }
219
220 if (end - p >= strlen(bytes)
221 && strncmp(p, bytes, strlen(bytes)) == 0) {
222
223 /* Check that there is nothing but whitespace after the value. */
224 p += strlen(bytes);
225 while (p < end && *p && g_ascii_isspace(*p)) {
226 p++;
227 }
228
229 if (p == end || !*p) {
230 s->accept_range = true;
231 }
232 }
Blue Swirl0bfcd592010-05-22 08:02:12 +0000233 }
Alexander Graf769ce762009-05-11 17:41:42 +0200234
235 return realsize;
236}
237
Paolo Bonzini34db05e2017-05-15 12:00:54 +0200238/* Called from curl_multi_do_locked, with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200239static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
240{
241 CURLState *s = ((CURLState*)opaque);
242 size_t realsize = size * nmemb;
Alexander Graf769ce762009-05-11 17:41:42 +0200243
Laurent Viviered2a66d2018-12-13 17:27:25 +0100244 trace_curl_read_cb(realsize);
Alexander Graf769ce762009-05-11 17:41:42 +0200245
Max Reitz4e767652016-10-25 04:54:29 +0200246 if (!s || !s->orig_buf) {
247 goto read_end;
248 }
Alexander Graf769ce762009-05-11 17:41:42 +0200249
Fam Zheng6d4b9e52014-03-26 13:05:40 +0100250 if (s->buf_off >= s->buf_len) {
251 /* buffer full, read nothing */
Max Reitz4e767652016-10-25 04:54:29 +0200252 goto read_end;
Fam Zheng6d4b9e52014-03-26 13:05:40 +0100253 }
254 realsize = MIN(realsize, s->buf_len - s->buf_off);
Alexander Graf769ce762009-05-11 17:41:42 +0200255 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
256 s->buf_off += realsize;
257
Max Reitz4e767652016-10-25 04:54:29 +0200258read_end:
259 /* curl will error out if we do not return this value */
260 return size * nmemb;
Alexander Graf769ce762009-05-11 17:41:42 +0200261}
262
Paolo Bonzini456af342017-05-15 12:00:55 +0200263/* Called with s->mutex held. */
Paolo Bonzini28256d82017-05-15 12:00:58 +0200264static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len,
265 CURLAIOCB *acb)
Alexander Graf769ce762009-05-11 17:41:42 +0200266{
267 int i;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200268 uint64_t end = start + len;
269 uint64_t clamped_end = MIN(end, s->len);
270 uint64_t clamped_len = clamped_end - start;
Alexander Graf769ce762009-05-11 17:41:42 +0200271
272 for (i=0; i<CURL_NUM_STATES; i++) {
273 CURLState *state = &s->states[i];
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200274 uint64_t buf_end = (state->buf_start + state->buf_off);
275 uint64_t buf_fend = (state->buf_start + state->buf_len);
Alexander Graf769ce762009-05-11 17:41:42 +0200276
277 if (!state->orig_buf)
278 continue;
279 if (!state->buf_off)
280 continue;
281
282 // Does the existing buffer cover our section?
283 if ((start >= state->buf_start) &&
284 (start <= buf_end) &&
Max Reitz4e504532016-10-25 04:54:31 +0200285 (clamped_end >= state->buf_start) &&
286 (clamped_end <= buf_end))
Alexander Graf769ce762009-05-11 17:41:42 +0200287 {
288 char *buf = state->orig_buf + (start - state->buf_start);
289
Max Reitz4e504532016-10-25 04:54:31 +0200290 qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len);
291 if (clamped_len < len) {
292 qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len);
293 }
Paolo Bonzini28256d82017-05-15 12:00:58 +0200294 acb->ret = 0;
295 return true;
Alexander Graf769ce762009-05-11 17:41:42 +0200296 }
297
298 // Wait for unfinished chunks
Matthew Boothb7079df2014-04-29 16:03:32 +0100299 if (state->in_use &&
300 (start >= state->buf_start) &&
Alexander Graf769ce762009-05-11 17:41:42 +0200301 (start <= buf_fend) &&
Max Reitz4e504532016-10-25 04:54:31 +0200302 (clamped_end >= state->buf_start) &&
303 (clamped_end <= buf_fend))
Alexander Graf769ce762009-05-11 17:41:42 +0200304 {
305 int j;
306
307 acb->start = start - state->buf_start;
Max Reitz4e504532016-10-25 04:54:31 +0200308 acb->end = acb->start + clamped_len;
Alexander Graf769ce762009-05-11 17:41:42 +0200309
310 for (j=0; j<CURL_NUM_ACB; j++) {
311 if (!state->acb[j]) {
312 state->acb[j] = acb;
Paolo Bonzini28256d82017-05-15 12:00:58 +0200313 return true;
Alexander Graf769ce762009-05-11 17:41:42 +0200314 }
315 }
316 }
317 }
318
Paolo Bonzini28256d82017-05-15 12:00:58 +0200319 return false;
Alexander Graf769ce762009-05-11 17:41:42 +0200320}
321
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100322/* Called with s->mutex held. */
Matthew Booth838ef602014-04-29 16:03:30 +0100323static void curl_multi_check_completion(BDRVCURLState *s)
Alexander Graf769ce762009-05-11 17:41:42 +0200324{
Alexander Graf769ce762009-05-11 17:41:42 +0200325 int msgs_in_queue;
326
Alexander Graf769ce762009-05-11 17:41:42 +0200327 /* Try to find done transfers, so we can free the easy
328 * handle again. */
Matthew Booth1f2cead2014-04-29 16:03:31 +0100329 for (;;) {
Alexander Graf769ce762009-05-11 17:41:42 +0200330 CURLMsg *msg;
331 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
332
Matthew Booth1f2cead2014-04-29 16:03:31 +0100333 /* Quit when there are no more completions */
Alexander Graf769ce762009-05-11 17:41:42 +0200334 if (!msg)
335 break;
Alexander Graf769ce762009-05-11 17:41:42 +0200336
Matthew Booth1f2cead2014-04-29 16:03:31 +0100337 if (msg->msg == CURLMSG_DONE) {
Max Reitzbfb23b42019-09-10 14:41:35 +0200338 int i;
Matthew Booth1f2cead2014-04-29 16:03:31 +0100339 CURLState *state = NULL;
Max Reitzbfb23b42019-09-10 14:41:35 +0200340 bool error = msg->data.result != CURLE_OK;
341
Matthew Booth1f2cead2014-04-29 16:03:31 +0100342 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
343 (char **)&state);
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100344
Max Reitzbfb23b42019-09-10 14:41:35 +0200345 if (error) {
Richard W.M. Jones796a0602015-07-08 14:37:48 +0100346 static int errcount = 100;
347
348 /* Don't lose the original error message from curl, since
349 * it contains extra data.
350 */
351 if (errcount > 0) {
352 error_report("curl: %s", state->errmsg);
353 if (--errcount == 0) {
354 error_report("curl: further errors suppressed");
355 }
356 }
Max Reitzbfb23b42019-09-10 14:41:35 +0200357 }
Richard W.M. Jones796a0602015-07-08 14:37:48 +0100358
Max Reitzbfb23b42019-09-10 14:41:35 +0200359 for (i = 0; i < CURL_NUM_ACB; i++) {
360 CURLAIOCB *acb = state->acb[i];
Nicholas Thomasf785a5a2011-08-15 10:00:34 +0100361
Max Reitzbfb23b42019-09-10 14:41:35 +0200362 if (acb == NULL) {
363 continue;
Matthew Booth1f2cead2014-04-29 16:03:31 +0100364 }
Max Reitzbfb23b42019-09-10 14:41:35 +0200365
366 if (!error) {
367 /* Assert that we have read all data */
368 assert(state->buf_off >= acb->end);
369
370 qemu_iovec_from_buf(acb->qiov, 0,
371 state->orig_buf + acb->start,
372 acb->end - acb->start);
373
374 if (acb->end - acb->start < acb->bytes) {
375 size_t offset = acb->end - acb->start;
376 qemu_iovec_memset(acb->qiov, offset, 0,
377 acb->bytes - offset);
378 }
379 }
380
381 acb->ret = error ? -EIO : 0;
382 state->acb[i] = NULL;
383 qemu_mutex_unlock(&s->mutex);
384 aio_co_wake(acb->co);
385 qemu_mutex_lock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200386 }
Matthew Booth1f2cead2014-04-29 16:03:31 +0100387
388 curl_clean_state(state);
389 break;
Alexander Graf769ce762009-05-11 17:41:42 +0200390 }
Matthew Booth1f2cead2014-04-29 16:03:31 +0100391 }
Alexander Graf769ce762009-05-11 17:41:42 +0200392}
393
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100394/* Called with s->mutex held. */
Max Reitz9abaf9f2019-09-10 14:41:34 +0200395static void curl_multi_do_locked(CURLSocket *socket)
Peter Maydell031fd1b2014-01-24 14:56:17 +0100396{
Max Reitz3663dca2021-03-09 14:05:40 +0100397 BDRVCURLState *s = socket->s;
Peter Maydell031fd1b2014-01-24 14:56:17 +0100398 int running;
399 int r;
400
Max Reitz9abaf9f2019-09-10 14:41:34 +0200401 if (!s->multi) {
Peter Maydell031fd1b2014-01-24 14:56:17 +0100402 return;
403 }
404
Max Reitz9abaf9f2019-09-10 14:41:34 +0200405 do {
406 r = curl_multi_socket_action(s->multi, socket->fd, 0, &running);
407 } while (r == CURLM_CALL_MULTI_PERFORM);
Matthew Booth838ef602014-04-29 16:03:30 +0100408}
409
Paolo Bonzini9d456652017-02-13 14:52:30 +0100410static void curl_multi_do(void *arg)
411{
Max Reitz9dbad872019-09-10 14:41:33 +0200412 CURLSocket *socket = arg;
Max Reitz3663dca2021-03-09 14:05:40 +0100413 BDRVCURLState *s = socket->s;
Paolo Bonzini9d456652017-02-13 14:52:30 +0100414
Max Reitz9dbad872019-09-10 14:41:33 +0200415 qemu_mutex_lock(&s->mutex);
416 curl_multi_do_locked(socket);
417 curl_multi_check_completion(s);
418 qemu_mutex_unlock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100419}
420
421static void curl_multi_timeout_do(void *arg)
422{
Peter Maydell031fd1b2014-01-24 14:56:17 +0100423 BDRVCURLState *s = (BDRVCURLState *)arg;
424 int running;
425
426 if (!s->multi) {
427 return;
428 }
429
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100430 qemu_mutex_lock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100431 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
432
Matthew Booth838ef602014-04-29 16:03:30 +0100433 curl_multi_check_completion(s);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100434 qemu_mutex_unlock(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100435}
436
Paolo Bonzini456af342017-05-15 12:00:55 +0200437/* Called with s->mutex held. */
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200438static CURLState *curl_find_state(BDRVCURLState *s)
Alexander Graf769ce762009-05-11 17:41:42 +0200439{
440 CURLState *state = NULL;
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200441 int i;
Alexander Graf769ce762009-05-11 17:41:42 +0200442
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200443 for (i = 0; i < CURL_NUM_STATES; i++) {
444 if (!s->states[i].in_use) {
Alexander Graf769ce762009-05-11 17:41:42 +0200445 state = &s->states[i];
446 state->in_use = 1;
447 break;
448 }
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200449 }
450 return state;
451}
Alexander Graf769ce762009-05-11 17:41:42 +0200452
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200453static int curl_init_state(BDRVCURLState *s, CURLState *state)
454{
Matthew Booth9e550b32014-04-29 16:03:26 +0100455 if (!state->curl) {
456 state->curl = curl_easy_init();
457 if (!state->curl) {
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200458 return -EIO;
Matthew Booth9e550b32014-04-29 16:03:26 +0100459 }
460 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
Matthew Booth97a3ea52014-05-14 19:28:42 -0400461 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
462 (long) s->sslverify);
Richard W.M. Jones637fa442018-09-14 10:56:22 +0100463 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST,
464 s->sslverify ? 2L : 0L);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100465 if (s->cookie) {
466 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
467 }
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000468 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
Matthew Booth9e550b32014-04-29 16:03:26 +0100469 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
470 (void *)curl_read_cb);
471 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
472 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
473 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
474 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
475 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
476 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
477 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
Alexander Graf769ce762009-05-11 17:41:42 +0200478
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000479 if (s->username) {
480 curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username);
481 }
482 if (s->password) {
483 curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password);
484 }
485 if (s->proxyusername) {
486 curl_easy_setopt(state->curl,
487 CURLOPT_PROXYUSERNAME, s->proxyusername);
488 }
489 if (s->proxypassword) {
490 curl_easy_setopt(state->curl,
491 CURLOPT_PROXYPASSWORD, s->proxypassword);
492 }
493
Matthew Booth9e550b32014-04-29 16:03:26 +0100494 /* Restrict supported protocols to avoid security issues in the more
495 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
496 * CVE-2013-0249.
497 *
498 * Restricting protocols is only supported from 7.19.4 upwards.
499 */
Stefan Hajnoczi8a8f5842013-02-13 09:25:34 +0100500#if LIBCURL_VERSION_NUM >= 0x071304
Matthew Booth9e550b32014-04-29 16:03:26 +0100501 curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
502 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
Stefan Hajnoczi8a8f5842013-02-13 09:25:34 +0100503#endif
Stefan Hajnoczifb6d1bb2013-02-08 08:49:10 +0100504
Alexander Graf769ce762009-05-11 17:41:42 +0200505#ifdef DEBUG_VERBOSE
Matthew Booth9e550b32014-04-29 16:03:26 +0100506 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
Alexander Graf769ce762009-05-11 17:41:42 +0200507#endif
Matthew Booth9e550b32014-04-29 16:03:26 +0100508 }
Alexander Graf769ce762009-05-11 17:41:42 +0200509
510 state->s = s;
511
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200512 return 0;
Alexander Graf769ce762009-05-11 17:41:42 +0200513}
514
Paolo Bonzini456af342017-05-15 12:00:55 +0200515/* Called with s->mutex held. */
Alexander Graf769ce762009-05-11 17:41:42 +0200516static void curl_clean_state(CURLState *s)
517{
Paolo Bonzini675a7752017-05-15 12:00:53 +0200518 int j;
519 for (j = 0; j < CURL_NUM_ACB; j++) {
520 assert(!s->acb[j]);
521 }
522
Alexander Graf769ce762009-05-11 17:41:42 +0200523 if (s->s->multi)
524 curl_multi_remove_handle(s->s->multi, s->curl);
Max Reitzff5ca162016-10-25 04:54:30 +0200525
Alexander Graf769ce762009-05-11 17:41:42 +0200526 s->in_use = 0;
Paolo Bonzini2bb5c932017-05-15 12:00:59 +0200527
Paolo Bonzini709f2132018-02-03 10:39:35 -0500528 qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200529}
530
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200531static void curl_parse_filename(const char *filename, QDict *options,
532 Error **errp)
Alexander Graf769ce762009-05-11 17:41:42 +0200533{
Eric Blake46f5ac22017-04-27 16:58:17 -0500534 qdict_put_str(options, CURL_BLOCK_OPT_URL, filename);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200535}
536
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200537static void curl_detach_aio_context(BlockDriverState *bs)
538{
539 BDRVCURLState *s = bs->opaque;
540 int i;
541
Gan Qixinf5056b72020-12-03 15:50:53 +0800542 WITH_QEMU_LOCK_GUARD(&s->mutex) {
Max Reitz0f418a22021-03-09 14:05:41 +0100543 curl_drop_all_sockets(s->sockets);
Gan Qixinf5056b72020-12-03 15:50:53 +0800544 for (i = 0; i < CURL_NUM_STATES; i++) {
545 if (s->states[i].in_use) {
546 curl_clean_state(&s->states[i]);
547 }
548 if (s->states[i].curl) {
549 curl_easy_cleanup(s->states[i].curl);
550 s->states[i].curl = NULL;
551 }
552 g_free(s->states[i].orig_buf);
553 s->states[i].orig_buf = NULL;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200554 }
Gan Qixinf5056b72020-12-03 15:50:53 +0800555 if (s->multi) {
556 curl_multi_cleanup(s->multi);
557 s->multi = NULL;
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200558 }
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200559 }
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200560
561 timer_del(&s->timer);
562}
563
564static void curl_attach_aio_context(BlockDriverState *bs,
565 AioContext *new_context)
566{
567 BDRVCURLState *s = bs->opaque;
568
569 aio_timer_init(new_context, &s->timer,
570 QEMU_CLOCK_REALTIME, SCALE_NS,
571 curl_multi_timeout_do, s);
572
573 assert(!s->multi);
574 s->multi = curl_multi_init();
575 s->aio_context = new_context;
576 curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200577 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
578 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200579}
580
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200581static QemuOptsList runtime_opts = {
582 .name = "curl",
583 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
584 .desc = {
585 {
Matthew Boothe3542c62014-05-14 19:28:41 -0400586 .name = CURL_BLOCK_OPT_URL,
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200587 .type = QEMU_OPT_STRING,
588 .help = "URL to open",
589 },
590 {
Matthew Boothe3542c62014-05-14 19:28:41 -0400591 .name = CURL_BLOCK_OPT_READAHEAD,
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200592 .type = QEMU_OPT_SIZE,
593 .help = "Readahead size",
594 },
Matthew Booth97a3ea52014-05-14 19:28:42 -0400595 {
596 .name = CURL_BLOCK_OPT_SSLVERIFY,
597 .type = QEMU_OPT_BOOL,
598 .help = "Verify SSL certificate"
599 },
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300600 {
601 .name = CURL_BLOCK_OPT_TIMEOUT,
602 .type = QEMU_OPT_NUMBER,
603 .help = "Curl timeout"
604 },
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100605 {
606 .name = CURL_BLOCK_OPT_COOKIE,
607 .type = QEMU_OPT_STRING,
608 .help = "Pass the cookie or list of cookies with each request"
609 },
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000610 {
Peter Krempa327c8eb2017-05-04 16:00:06 +0200611 .name = CURL_BLOCK_OPT_COOKIE_SECRET,
612 .type = QEMU_OPT_STRING,
613 .help = "ID of secret used as cookie passed with each request"
614 },
615 {
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000616 .name = CURL_BLOCK_OPT_USERNAME,
617 .type = QEMU_OPT_STRING,
618 .help = "Username for HTTP auth"
619 },
620 {
621 .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
622 .type = QEMU_OPT_STRING,
623 .help = "ID of secret used as password for HTTP auth",
624 },
625 {
626 .name = CURL_BLOCK_OPT_PROXY_USERNAME,
627 .type = QEMU_OPT_STRING,
628 .help = "Username for HTTP proxy auth"
629 },
630 {
631 .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
632 .type = QEMU_OPT_STRING,
633 .help = "ID of secret used as password for HTTP proxy auth",
634 },
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200635 { /* end of list */ }
636 },
637};
638
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000639
Max Reitz015a1032013-09-05 14:22:29 +0200640static int curl_open(BlockDriverState *bs, QDict *options, int flags,
641 Error **errp)
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200642{
643 BDRVCURLState *s = bs->opaque;
644 CURLState *state = NULL;
645 QemuOpts *opts;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200646 const char *file;
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100647 const char *cookie;
Peter Krempa327c8eb2017-05-04 16:00:06 +0200648 const char *cookie_secret;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200649 double d;
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000650 const char *secretid;
Max Reitz34634ca2017-03-31 14:04:31 +0200651 const char *protocol_delimiter;
Jeff Cody2d259642017-11-07 17:27:22 -0500652 int ret;
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200653
Kevin Wolf6ceef362018-10-08 17:27:18 +0200654 ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes",
655 errp);
656 if (ret < 0) {
657 return ret;
Richard W.M. Jonesa7cea2b2013-06-10 12:38:43 +0100658 }
659
Jeff Cody2d259642017-11-07 17:27:22 -0500660 if (!libcurl_initialized) {
661 ret = curl_global_init(CURL_GLOBAL_ALL);
662 if (ret) {
663 error_setg(errp, "libcurl initialization failed with %d", ret);
664 return -EIO;
665 }
666 libcurl_initialized = true;
667 }
668
Paolo Bonzini456af342017-05-15 12:00:55 +0200669 qemu_mutex_init(&s->mutex);
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800670 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Markus Armbruster668f62e2020-07-07 18:06:02 +0200671 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200672 goto out_noclean;
673 }
674
Matthew Boothe3542c62014-05-14 19:28:41 -0400675 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
Max Reitz712b64e2019-02-01 20:29:31 +0100676 CURL_BLOCK_OPT_READAHEAD_DEFAULT);
Nolanc76f4952009-07-01 17:16:52 -0700677 if ((s->readahead_size & 0x1ff) != 0) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100678 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
679 s->readahead_size);
Nolanc76f4952009-07-01 17:16:52 -0700680 goto out_noclean;
681 }
682
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300683 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
Max Reitz712b64e2019-02-01 20:29:31 +0100684 CURL_BLOCK_OPT_TIMEOUT_DEFAULT);
Richard W.M. Jonesf76faed2014-10-26 11:05:27 +0000685 if (s->timeout > CURL_TIMEOUT_MAX) {
686 error_setg(errp, "timeout parameter is too large or negative");
687 goto out_noclean;
688 }
Daniel Henrique Barboza212aefa2014-08-13 12:44:27 -0300689
Max Reitz712b64e2019-02-01 20:29:31 +0100690 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY,
691 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT);
Matthew Booth97a3ea52014-05-14 19:28:42 -0400692
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100693 cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
Peter Krempa327c8eb2017-05-04 16:00:06 +0200694 cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET);
695
696 if (cookie && cookie_secret) {
697 error_setg(errp,
698 "curl driver cannot handle both cookie and cookie secret");
699 goto out_noclean;
700 }
701
702 if (cookie_secret) {
703 s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp);
704 if (!s->cookie) {
705 goto out_noclean;
706 }
707 } else {
708 s->cookie = g_strdup(cookie);
709 }
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100710
Matthew Boothe3542c62014-05-14 19:28:41 -0400711 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200712 if (file == NULL) {
Paolo Bonzini2a94fee2014-02-17 14:43:57 +0100713 error_setg(errp, "curl block driver requires an 'url' option");
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200714 goto out_noclean;
715 }
716
Max Reitz34634ca2017-03-31 14:04:31 +0200717 if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) ||
718 !strstart(protocol_delimiter, "://", NULL))
719 {
720 error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not "
721 "start with '%s://')", bs->drv->protocol_name, file,
722 bs->drv->protocol_name);
723 goto out_noclean;
724 }
725
Daniel P. Berrange1bff9602016-01-21 14:19:20 +0000726 s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
727 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
728
729 if (secretid) {
730 s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
731 if (!s->password) {
732 goto out_noclean;
733 }
734 }
735
736 s->proxyusername = g_strdup(
737 qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
738 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
739 if (secretid) {
740 s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
741 if (!s->proxypassword) {
742 goto out_noclean;
743 }
744 }
745
Laurent Viviered2a66d2018-12-13 17:27:25 +0100746 trace_curl_open(file);
Paolo Bonzini709f2132018-02-03 10:39:35 -0500747 qemu_co_queue_init(&s->free_state_waitq);
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200748 s->aio_context = bdrv_get_aio_context(bs);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200749 s->url = g_strdup(file);
Max Reitz0f418a22021-03-09 14:05:41 +0100750 s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free);
Paolo Bonzini456af342017-05-15 12:00:55 +0200751 qemu_mutex_lock(&s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200752 state = curl_find_state(s);
Paolo Bonzini456af342017-05-15 12:00:55 +0200753 qemu_mutex_unlock(&s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200754 if (!state) {
Alexander Graf769ce762009-05-11 17:41:42 +0200755 goto out_noclean;
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200756 }
Alexander Graf769ce762009-05-11 17:41:42 +0200757
758 // Get file size
759
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200760 if (curl_init_state(s, state) < 0) {
761 goto out;
762 }
763
Fam Zheng3494d652013-07-02 15:19:21 +0800764 s->accept_range = false;
Alexander Graf769ce762009-05-11 17:41:42 +0200765 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
Fam Zheng3494d652013-07-02 15:19:21 +0800766 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
767 curl_header_cb);
768 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
Alexander Graf769ce762009-05-11 17:41:42 +0200769 if (curl_easy_perform(state->curl))
770 goto out;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200771 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
Alexander Graf769ce762009-05-11 17:41:42 +0200772 goto out;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200773 }
774 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
775 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
Stefan Weil50d6a8a2018-07-12 21:51:20 +0200776 * known and zero if it is really zero-length file. */
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200777#if LIBCURL_VERSION_NUM >= 0x071304
778 if (d < 0) {
779 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
780 "Server didn't report file size.");
781 goto out;
782 }
783#else
784 if (d <= 0) {
785 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
786 "Unknown file size or zero-length file.");
787 goto out;
788 }
789#endif
790
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200791 s->len = d;
Tomáš Golembiovskýa41c4572016-07-08 13:18:09 +0200792
Fam Zheng3494d652013-07-02 15:19:21 +0800793 if ((!strncasecmp(s->url, "http://", strlen("http://"))
794 || !strncasecmp(s->url, "https://", strlen("https://")))
795 && !s->accept_range) {
796 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
797 "Server does not support 'range' (byte ranges).");
798 goto out;
799 }
Laurent Viviered2a66d2018-12-13 17:27:25 +0100800 trace_curl_open_size(s->len);
Alexander Graf769ce762009-05-11 17:41:42 +0200801
Paolo Bonzini456af342017-05-15 12:00:55 +0200802 qemu_mutex_lock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200803 curl_clean_state(state);
Paolo Bonzini456af342017-05-15 12:00:55 +0200804 qemu_mutex_unlock(&s->mutex);
Alexander Graf769ce762009-05-11 17:41:42 +0200805 curl_easy_cleanup(state->curl);
806 state->curl = NULL;
807
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200808 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
Alexander Graf769ce762009-05-11 17:41:42 +0200809
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200810 qemu_opts_del(opts);
Alexander Graf769ce762009-05-11 17:41:42 +0200811 return 0;
812
813out:
Maria Kustovaacd7fdc2014-03-18 09:59:18 +0400814 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
Alexander Graf769ce762009-05-11 17:41:42 +0200815 curl_easy_cleanup(state->curl);
816 state->curl = NULL;
817out_noclean:
Paolo Bonzini456af342017-05-15 12:00:55 +0200818 qemu_mutex_destroy(&s->mutex);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100819 g_free(s->cookie);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200820 g_free(s->url);
Jeff Cody996922d2017-11-07 17:27:23 -0500821 g_free(s->username);
822 g_free(s->proxyusername);
823 g_free(s->proxypassword);
Max Reitz0f418a22021-03-09 14:05:41 +0100824 curl_drop_all_sockets(s->sockets);
825 g_hash_table_destroy(s->sockets);
Kevin Wolf8e6d58c2013-04-10 15:31:33 +0200826 qemu_opts_del(opts);
Alexander Graf769ce762009-05-11 17:41:42 +0200827 return -EINVAL;
828}
829
Paolo Bonzini28256d82017-05-15 12:00:58 +0200830static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)
Alexander Graf769ce762009-05-11 17:41:42 +0200831{
Alexander Graf769ce762009-05-11 17:41:42 +0200832 CURLState *state;
Matthew Boothb69cdef2014-04-29 16:03:29 +0100833 int running;
Alexander Graf769ce762009-05-11 17:41:42 +0200834
Paolo Bonzini19196312017-02-13 14:52:31 +0100835 BDRVCURLState *s = bs->opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200836
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200837 uint64_t start = acb->offset;
838 uint64_t end;
Alexander Graf769ce762009-05-11 17:41:42 +0200839
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100840 qemu_mutex_lock(&s->mutex);
Paolo Bonzini19196312017-02-13 14:52:31 +0100841
Alexander Graf769ce762009-05-11 17:41:42 +0200842 // In case we have the requested data already (e.g. read-ahead),
843 // we can just call the callback and be done.
Paolo Bonzini28256d82017-05-15 12:00:58 +0200844 if (curl_find_buf(s, start, acb->bytes, acb)) {
845 goto out;
Alexander Graf769ce762009-05-11 17:41:42 +0200846 }
847
848 // No cache found, so let's start a new request
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200849 for (;;) {
850 state = curl_find_state(s);
851 if (state) {
852 break;
853 }
Paolo Bonzini709f2132018-02-03 10:39:35 -0500854 qemu_co_queue_wait(&s->free_state_waitq, &s->mutex);
Paolo Bonzini3ce6a722017-05-15 12:00:56 +0200855 }
856
857 if (curl_init_state(s, state) < 0) {
858 curl_clean_state(state);
Paolo Bonzini28256d82017-05-15 12:00:58 +0200859 acb->ret = -EIO;
Paolo Bonzini19196312017-02-13 14:52:31 +0100860 goto out;
Nick Thomas363c3c82011-09-21 11:55:50 +0100861 }
Alexander Graf769ce762009-05-11 17:41:42 +0200862
863 acb->start = 0;
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200864 acb->end = MIN(acb->bytes, s->len - start);
Alexander Graf769ce762009-05-11 17:41:42 +0200865
866 state->buf_off = 0;
Markus Armbrusterf7047c22014-06-06 18:25:12 +0200867 g_free(state->orig_buf);
Alexander Graf769ce762009-05-11 17:41:42 +0200868 state->buf_start = start;
Max Reitz4e504532016-10-25 04:54:31 +0200869 state->buf_len = MIN(acb->end + s->readahead_size, s->len - start);
870 end = start + state->buf_len - 1;
Kevin Wolf8dc7a772014-05-20 13:26:40 +0200871 state->orig_buf = g_try_malloc(state->buf_len);
872 if (state->buf_len && state->orig_buf == NULL) {
873 curl_clean_state(state);
Paolo Bonzini28256d82017-05-15 12:00:58 +0200874 acb->ret = -ENOMEM;
Paolo Bonzini19196312017-02-13 14:52:31 +0100875 goto out;
Kevin Wolf8dc7a772014-05-20 13:26:40 +0200876 }
Alexander Graf769ce762009-05-11 17:41:42 +0200877 state->acb[0] = acb;
878
Paolo Bonzini2125e5e2017-05-15 12:00:57 +0200879 snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end);
Laurent Viviered2a66d2018-12-13 17:27:25 +0100880 trace_curl_setup_preadv(acb->bytes, start, state->range);
Alexander Graf769ce762009-05-11 17:41:42 +0200881 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
882
Max Reitzc34dc072019-09-10 14:41:36 +0200883 if (curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) {
884 state->acb[0] = NULL;
885 acb->ret = -EIO;
886
887 curl_clean_state(state);
888 goto out;
889 }
Alexander Graf769ce762009-05-11 17:41:42 +0200890
Matthew Boothb69cdef2014-04-29 16:03:29 +0100891 /* Tell curl it needs to kick things off */
892 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
Paolo Bonzini19196312017-02-13 14:52:31 +0100893
894out:
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100895 qemu_mutex_unlock(&s->mutex);
Nick Thomas363c3c82011-09-21 11:55:50 +0100896}
897
Paolo Bonzini28256d82017-05-15 12:00:58 +0200898static int coroutine_fn curl_co_preadv(BlockDriverState *bs,
899 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
Nick Thomas363c3c82011-09-21 11:55:50 +0100900{
Paolo Bonzini28256d82017-05-15 12:00:58 +0200901 CURLAIOCB acb = {
902 .co = qemu_coroutine_self(),
903 .ret = -EINPROGRESS,
904 .qiov = qiov,
905 .offset = offset,
906 .bytes = bytes
907 };
Nick Thomas363c3c82011-09-21 11:55:50 +0100908
Paolo Bonzini28256d82017-05-15 12:00:58 +0200909 curl_setup_preadv(bs, &acb);
910 while (acb.ret == -EINPROGRESS) {
911 qemu_coroutine_yield();
912 }
913 return acb.ret;
Alexander Graf769ce762009-05-11 17:41:42 +0200914}
915
Alexander Graf769ce762009-05-11 17:41:42 +0200916static void curl_close(BlockDriverState *bs)
917{
918 BDRVCURLState *s = bs->opaque;
Alexander Graf769ce762009-05-11 17:41:42 +0200919
Laurent Viviered2a66d2018-12-13 17:27:25 +0100920 trace_curl_close();
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200921 curl_detach_aio_context(bs);
Paolo Bonziniba3186c2017-02-22 19:07:23 +0100922 qemu_mutex_destroy(&s->mutex);
Peter Maydell031fd1b2014-01-24 14:56:17 +0100923
Max Reitz0f418a22021-03-09 14:05:41 +0100924 g_hash_table_destroy(s->sockets);
Richard W.M. Jonesa94f83d2014-08-29 16:03:12 +0100925 g_free(s->cookie);
Stefan Weil45724d62012-09-01 11:06:45 +0200926 g_free(s->url);
Jeff Cody996922d2017-11-07 17:27:23 -0500927 g_free(s->username);
928 g_free(s->proxyusername);
929 g_free(s->proxypassword);
Alexander Graf769ce762009-05-11 17:41:42 +0200930}
931
932static int64_t curl_getlength(BlockDriverState *bs)
933{
934 BDRVCURLState *s = bs->opaque;
935 return s->len;
936}
937
Max Reitz937c0072019-02-01 20:29:32 +0100938static void curl_refresh_filename(BlockDriverState *bs)
939{
940 BDRVCURLState *s = bs->opaque;
941
942 /* "readahead" and "timeout" do not change the guest-visible data,
943 * so ignore them */
944 if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT ||
945 s->cookie || s->username || s->password || s->proxyusername ||
946 s->proxypassword)
947 {
948 return;
949 }
950
951 pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url);
952}
953
954
Max Reitz26542672019-02-01 20:29:25 +0100955static const char *const curl_strong_runtime_opts[] = {
956 CURL_BLOCK_OPT_URL,
957 CURL_BLOCK_OPT_SSLVERIFY,
958 CURL_BLOCK_OPT_COOKIE,
959 CURL_BLOCK_OPT_COOKIE_SECRET,
960 CURL_BLOCK_OPT_USERNAME,
961 CURL_BLOCK_OPT_PASSWORD_SECRET,
962 CURL_BLOCK_OPT_PROXY_USERNAME,
963 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
964
965 NULL
966};
967
Alexander Graf769ce762009-05-11 17:41:42 +0200968static BlockDriver bdrv_http = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200969 .format_name = "http",
970 .protocol_name = "http",
Alexander Graf769ce762009-05-11 17:41:42 +0200971
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200972 .instance_size = sizeof(BDRVCURLState),
973 .bdrv_parse_filename = curl_parse_filename,
974 .bdrv_file_open = curl_open,
975 .bdrv_close = curl_close,
976 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +0200977
Paolo Bonzini28256d82017-05-15 12:00:58 +0200978 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200979
980 .bdrv_detach_aio_context = curl_detach_aio_context,
981 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +0100982
Max Reitz937c0072019-02-01 20:29:32 +0100983 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +0100984 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +0200985};
986
987static BlockDriver bdrv_https = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200988 .format_name = "https",
989 .protocol_name = "https",
Alexander Graf769ce762009-05-11 17:41:42 +0200990
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200991 .instance_size = sizeof(BDRVCURLState),
992 .bdrv_parse_filename = curl_parse_filename,
993 .bdrv_file_open = curl_open,
994 .bdrv_close = curl_close,
995 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +0200996
Paolo Bonzini28256d82017-05-15 12:00:58 +0200997 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +0200998
999 .bdrv_detach_aio_context = curl_detach_aio_context,
1000 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001001
Max Reitz937c0072019-02-01 20:29:32 +01001002 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001003 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001004};
1005
1006static BlockDriver bdrv_ftp = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001007 .format_name = "ftp",
1008 .protocol_name = "ftp",
Alexander Graf769ce762009-05-11 17:41:42 +02001009
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001010 .instance_size = sizeof(BDRVCURLState),
1011 .bdrv_parse_filename = curl_parse_filename,
1012 .bdrv_file_open = curl_open,
1013 .bdrv_close = curl_close,
1014 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001015
Paolo Bonzini28256d82017-05-15 12:00:58 +02001016 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001017
1018 .bdrv_detach_aio_context = curl_detach_aio_context,
1019 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001020
Max Reitz937c0072019-02-01 20:29:32 +01001021 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001022 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001023};
1024
1025static BlockDriver bdrv_ftps = {
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001026 .format_name = "ftps",
1027 .protocol_name = "ftps",
Alexander Graf769ce762009-05-11 17:41:42 +02001028
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001029 .instance_size = sizeof(BDRVCURLState),
1030 .bdrv_parse_filename = curl_parse_filename,
1031 .bdrv_file_open = curl_open,
1032 .bdrv_close = curl_close,
1033 .bdrv_getlength = curl_getlength,
Alexander Graf769ce762009-05-11 17:41:42 +02001034
Paolo Bonzini28256d82017-05-15 12:00:58 +02001035 .bdrv_co_preadv = curl_co_preadv,
Stefan Hajnoczi63f0f452014-05-08 16:34:40 +02001036
1037 .bdrv_detach_aio_context = curl_detach_aio_context,
1038 .bdrv_attach_aio_context = curl_attach_aio_context,
Max Reitz26542672019-02-01 20:29:25 +01001039
Max Reitz937c0072019-02-01 20:29:32 +01001040 .bdrv_refresh_filename = curl_refresh_filename,
Max Reitz26542672019-02-01 20:29:25 +01001041 .strong_runtime_opts = curl_strong_runtime_opts,
Alexander Graf769ce762009-05-11 17:41:42 +02001042};
1043
Alexander Graf769ce762009-05-11 17:41:42 +02001044static void curl_block_init(void)
1045{
1046 bdrv_register(&bdrv_http);
1047 bdrv_register(&bdrv_https);
1048 bdrv_register(&bdrv_ftp);
1049 bdrv_register(&bdrv_ftps);
Alexander Graf769ce762009-05-11 17:41:42 +02001050}
1051
1052block_init(curl_block_init);