]>
Commit | Line | Data |
---|---|---|
769ce76d AG |
1 | /* |
2 | * QEMU Block driver for CURL images | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
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 | */ | |
80c71a24 | 24 | #include "qemu/osdep.h" |
da34e65c | 25 | #include "qapi/error.h" |
769ce76d | 26 | #include "qemu-common.h" |
796a060b | 27 | #include "qemu/error-report.h" |
737e150e | 28 | #include "block/block_int.h" |
97a3ea57 | 29 | #include "qapi/qmp/qbool.h" |
d49b6836 | 30 | #include "qapi/qmp/qstring.h" |
1bff9606 | 31 | #include "crypto/secret.h" |
769ce76d | 32 | #include <curl/curl.h> |
f348b6d1 | 33 | #include "qemu/cutils.h" |
769ce76d | 34 | |
41c23467 | 35 | // #define DEBUG_CURL |
769ce76d AG |
36 | // #define DEBUG_VERBOSE |
37 | ||
38 | #ifdef DEBUG_CURL | |
ed79f37d | 39 | #define DEBUG_CURL_PRINT 1 |
769ce76d | 40 | #else |
ed79f37d | 41 | #define DEBUG_CURL_PRINT 0 |
769ce76d | 42 | #endif |
ed79f37d ZJ |
43 | #define DPRINTF(fmt, ...) \ |
44 | do { \ | |
45 | if (DEBUG_CURL_PRINT) { \ | |
46 | fprintf(stderr, fmt, ## __VA_ARGS__); \ | |
47 | } \ | |
48 | } while (0) | |
769ce76d | 49 | |
031fd1be PM |
50 | #if LIBCURL_VERSION_NUM >= 0x071000 |
51 | /* The multi interface timer callback was introduced in 7.16.0 */ | |
52 | #define NEED_CURL_TIMER_CALLBACK | |
9aedd5a5 MB |
53 | #define HAVE_SOCKET_ACTION |
54 | #endif | |
55 | ||
56 | #ifndef HAVE_SOCKET_ACTION | |
57 | /* If curl_multi_socket_action isn't available, define it statically here in | |
58 | * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is | |
59 | * less efficient but still safe. */ | |
60 | static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, | |
61 | curl_socket_t sockfd, | |
62 | int ev_bitmask, | |
63 | int *running_handles) | |
64 | { | |
65 | return curl_multi_socket(multi_handle, sockfd, running_handles); | |
66 | } | |
67 | #define curl_multi_socket_action __curl_multi_socket_action | |
031fd1be PM |
68 | #endif |
69 | ||
fb6d1bbd | 70 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
23dce387 | 71 | CURLPROTO_FTP | CURLPROTO_FTPS) |
fb6d1bbd | 72 | |
769ce76d AG |
73 | #define CURL_NUM_STATES 8 |
74 | #define CURL_NUM_ACB 8 | |
e3542c67 | 75 | #define READ_AHEAD_DEFAULT (256 * 1024) |
212aefaa | 76 | #define CURL_TIMEOUT_DEFAULT 5 |
f76faeda | 77 | #define CURL_TIMEOUT_MAX 10000 |
769ce76d AG |
78 | |
79 | #define FIND_RET_NONE 0 | |
80 | #define FIND_RET_OK 1 | |
81 | #define FIND_RET_WAIT 2 | |
82 | ||
e3542c67 MB |
83 | #define CURL_BLOCK_OPT_URL "url" |
84 | #define CURL_BLOCK_OPT_READAHEAD "readahead" | |
97a3ea57 | 85 | #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" |
212aefaa | 86 | #define CURL_BLOCK_OPT_TIMEOUT "timeout" |
a94f83d9 | 87 | #define CURL_BLOCK_OPT_COOKIE "cookie" |
1bff9606 DB |
88 | #define CURL_BLOCK_OPT_USERNAME "username" |
89 | #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" | |
90 | #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" | |
91 | #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" | |
e3542c67 | 92 | |
769ce76d AG |
93 | struct BDRVCURLState; |
94 | ||
95 | typedef struct CURLAIOCB { | |
7c84b1b8 | 96 | BlockAIOCB common; |
769ce76d | 97 | QEMUIOVector *qiov; |
363c3c85 NT |
98 | |
99 | int64_t sector_num; | |
100 | int nb_sectors; | |
101 | ||
769ce76d AG |
102 | size_t start; |
103 | size_t end; | |
104 | } CURLAIOCB; | |
105 | ||
ff5ca166 HR |
106 | typedef struct CURLSocket { |
107 | int fd; | |
108 | QLIST_ENTRY(CURLSocket) next; | |
109 | } CURLSocket; | |
110 | ||
769ce76d AG |
111 | typedef struct CURLState |
112 | { | |
113 | struct BDRVCURLState *s; | |
114 | CURLAIOCB *acb[CURL_NUM_ACB]; | |
115 | CURL *curl; | |
ff5ca166 | 116 | QLIST_HEAD(, CURLSocket) sockets; |
769ce76d AG |
117 | char *orig_buf; |
118 | size_t buf_start; | |
119 | size_t buf_off; | |
120 | size_t buf_len; | |
121 | char range[128]; | |
122 | char errmsg[CURL_ERROR_SIZE]; | |
123 | char in_use; | |
124 | } CURLState; | |
125 | ||
126 | typedef struct BDRVCURLState { | |
127 | CURLM *multi; | |
031fd1be | 128 | QEMUTimer timer; |
769ce76d AG |
129 | size_t len; |
130 | CURLState states[CURL_NUM_STATES]; | |
131 | char *url; | |
c76f4952 | 132 | size_t readahead_size; |
97a3ea57 | 133 | bool sslverify; |
f76faeda | 134 | uint64_t timeout; |
a94f83d9 | 135 | char *cookie; |
3494d650 | 136 | bool accept_range; |
63f0f45f | 137 | AioContext *aio_context; |
ba3186c4 | 138 | QemuMutex mutex; |
1bff9606 DB |
139 | char *username; |
140 | char *password; | |
141 | char *proxyusername; | |
142 | char *proxypassword; | |
769ce76d AG |
143 | } BDRVCURLState; |
144 | ||
145 | static void curl_clean_state(CURLState *s); | |
146 | static void curl_multi_do(void *arg); | |
838ef602 | 147 | static void curl_multi_read(void *arg); |
769ce76d | 148 | |
031fd1be PM |
149 | #ifdef NEED_CURL_TIMER_CALLBACK |
150 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) | |
151 | { | |
152 | BDRVCURLState *s = opaque; | |
153 | ||
154 | DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms); | |
155 | if (timeout_ms == -1) { | |
156 | timer_del(&s->timer); | |
157 | } else { | |
158 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; | |
159 | timer_mod(&s->timer, | |
160 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); | |
161 | } | |
162 | return 0; | |
163 | } | |
164 | #endif | |
165 | ||
769ce76d | 166 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
63f0f45f | 167 | void *userp, void *sp) |
769ce76d | 168 | { |
63f0f45f | 169 | BDRVCURLState *s; |
838ef602 | 170 | CURLState *state = NULL; |
ff5ca166 HR |
171 | CURLSocket *socket; |
172 | ||
838ef602 | 173 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); |
63f0f45f | 174 | s = state->s; |
838ef602 | 175 | |
ff5ca166 HR |
176 | QLIST_FOREACH(socket, &state->sockets, next) { |
177 | if (socket->fd == fd) { | |
178 | if (action == CURL_POLL_REMOVE) { | |
179 | QLIST_REMOVE(socket, next); | |
180 | g_free(socket); | |
181 | } | |
182 | break; | |
183 | } | |
184 | } | |
185 | if (!socket) { | |
186 | socket = g_new0(CURLSocket, 1); | |
187 | socket->fd = fd; | |
188 | QLIST_INSERT_HEAD(&state->sockets, socket, next); | |
189 | } | |
190 | socket = NULL; | |
191 | ||
92b6a160 | 192 | DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, (int)fd); |
769ce76d AG |
193 | switch (action) { |
194 | case CURL_POLL_IN: | |
dca21ef2 | 195 | aio_set_fd_handler(s->aio_context, fd, false, |
f6a51c84 | 196 | curl_multi_read, NULL, NULL, state); |
769ce76d AG |
197 | break; |
198 | case CURL_POLL_OUT: | |
dca21ef2 | 199 | aio_set_fd_handler(s->aio_context, fd, false, |
f6a51c84 | 200 | NULL, curl_multi_do, NULL, state); |
769ce76d AG |
201 | break; |
202 | case CURL_POLL_INOUT: | |
dca21ef2 | 203 | aio_set_fd_handler(s->aio_context, fd, false, |
f6a51c84 | 204 | curl_multi_read, curl_multi_do, NULL, state); |
769ce76d AG |
205 | break; |
206 | case CURL_POLL_REMOVE: | |
dca21ef2 | 207 | aio_set_fd_handler(s->aio_context, fd, false, |
f6a51c84 | 208 | NULL, NULL, NULL, NULL); |
769ce76d AG |
209 | break; |
210 | } | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
3494d650 | 215 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
769ce76d | 216 | { |
3494d650 | 217 | BDRVCURLState *s = opaque; |
769ce76d | 218 | size_t realsize = size * nmemb; |
3494d650 | 219 | const char *accept_line = "Accept-Ranges: bytes"; |
769ce76d | 220 | |
3494d650 FZ |
221 | if (realsize >= strlen(accept_line) |
222 | && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { | |
223 | s->accept_range = true; | |
0bfcd599 | 224 | } |
769ce76d AG |
225 | |
226 | return realsize; | |
227 | } | |
228 | ||
229 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) | |
230 | { | |
231 | CURLState *s = ((CURLState*)opaque); | |
232 | size_t realsize = size * nmemb; | |
233 | int i; | |
234 | ||
0bfcd599 | 235 | DPRINTF("CURL: Just reading %zd bytes\n", realsize); |
769ce76d | 236 | |
4e767657 HR |
237 | if (!s || !s->orig_buf) { |
238 | goto read_end; | |
239 | } | |
769ce76d | 240 | |
6d4b9e55 FZ |
241 | if (s->buf_off >= s->buf_len) { |
242 | /* buffer full, read nothing */ | |
4e767657 | 243 | goto read_end; |
6d4b9e55 FZ |
244 | } |
245 | realsize = MIN(realsize, s->buf_len - s->buf_off); | |
769ce76d AG |
246 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
247 | s->buf_off += realsize; | |
248 | ||
249 | for(i=0; i<CURL_NUM_ACB; i++) { | |
250 | CURLAIOCB *acb = s->acb[i]; | |
251 | ||
252 | if (!acb) | |
253 | continue; | |
254 | ||
255 | if ((s->buf_off >= acb->end)) { | |
4e504535 HR |
256 | size_t request_length = acb->nb_sectors * BDRV_SECTOR_SIZE; |
257 | ||
03396148 MT |
258 | qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start, |
259 | acb->end - acb->start); | |
4e504535 HR |
260 | |
261 | if (acb->end - acb->start < request_length) { | |
262 | size_t offset = acb->end - acb->start; | |
263 | qemu_iovec_memset(acb->qiov, offset, 0, | |
264 | request_length - offset); | |
265 | } | |
266 | ||
769ce76d | 267 | acb->common.cb(acb->common.opaque, 0); |
8007429a | 268 | qemu_aio_unref(acb); |
769ce76d AG |
269 | s->acb[i] = NULL; |
270 | } | |
271 | } | |
272 | ||
4e767657 HR |
273 | read_end: |
274 | /* curl will error out if we do not return this value */ | |
275 | return size * nmemb; | |
769ce76d AG |
276 | } |
277 | ||
278 | static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len, | |
279 | CURLAIOCB *acb) | |
280 | { | |
281 | int i; | |
282 | size_t end = start + len; | |
4e504535 HR |
283 | size_t clamped_end = MIN(end, s->len); |
284 | size_t clamped_len = clamped_end - start; | |
769ce76d AG |
285 | |
286 | for (i=0; i<CURL_NUM_STATES; i++) { | |
287 | CURLState *state = &s->states[i]; | |
288 | size_t buf_end = (state->buf_start + state->buf_off); | |
289 | size_t buf_fend = (state->buf_start + state->buf_len); | |
290 | ||
291 | if (!state->orig_buf) | |
292 | continue; | |
293 | if (!state->buf_off) | |
294 | continue; | |
295 | ||
296 | // Does the existing buffer cover our section? | |
297 | if ((start >= state->buf_start) && | |
298 | (start <= buf_end) && | |
4e504535 HR |
299 | (clamped_end >= state->buf_start) && |
300 | (clamped_end <= buf_end)) | |
769ce76d AG |
301 | { |
302 | char *buf = state->orig_buf + (start - state->buf_start); | |
303 | ||
4e504535 HR |
304 | qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); |
305 | if (clamped_len < len) { | |
306 | qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); | |
307 | } | |
769ce76d AG |
308 | acb->common.cb(acb->common.opaque, 0); |
309 | ||
310 | return FIND_RET_OK; | |
311 | } | |
312 | ||
313 | // Wait for unfinished chunks | |
b7079df4 MB |
314 | if (state->in_use && |
315 | (start >= state->buf_start) && | |
769ce76d | 316 | (start <= buf_fend) && |
4e504535 HR |
317 | (clamped_end >= state->buf_start) && |
318 | (clamped_end <= buf_fend)) | |
769ce76d AG |
319 | { |
320 | int j; | |
321 | ||
322 | acb->start = start - state->buf_start; | |
4e504535 | 323 | acb->end = acb->start + clamped_len; |
769ce76d AG |
324 | |
325 | for (j=0; j<CURL_NUM_ACB; j++) { | |
326 | if (!state->acb[j]) { | |
327 | state->acb[j] = acb; | |
328 | return FIND_RET_WAIT; | |
329 | } | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | return FIND_RET_NONE; | |
335 | } | |
336 | ||
ba3186c4 | 337 | /* Called with s->mutex held. */ |
838ef602 | 338 | static void curl_multi_check_completion(BDRVCURLState *s) |
769ce76d | 339 | { |
769ce76d AG |
340 | int msgs_in_queue; |
341 | ||
769ce76d AG |
342 | /* Try to find done transfers, so we can free the easy |
343 | * handle again. */ | |
1f2cead3 | 344 | for (;;) { |
769ce76d AG |
345 | CURLMsg *msg; |
346 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); | |
347 | ||
1f2cead3 | 348 | /* Quit when there are no more completions */ |
769ce76d AG |
349 | if (!msg) |
350 | break; | |
769ce76d | 351 | |
1f2cead3 MB |
352 | if (msg->msg == CURLMSG_DONE) { |
353 | CURLState *state = NULL; | |
354 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, | |
355 | (char **)&state); | |
356 | ||
357 | /* ACBs for successful messages get completed in curl_read_cb */ | |
358 | if (msg->data.result != CURLE_OK) { | |
359 | int i; | |
796a060b RJ |
360 | static int errcount = 100; |
361 | ||
362 | /* Don't lose the original error message from curl, since | |
363 | * it contains extra data. | |
364 | */ | |
365 | if (errcount > 0) { | |
366 | error_report("curl: %s", state->errmsg); | |
367 | if (--errcount == 0) { | |
368 | error_report("curl: further errors suppressed"); | |
369 | } | |
370 | } | |
371 | ||
1f2cead3 MB |
372 | for (i = 0; i < CURL_NUM_ACB; i++) { |
373 | CURLAIOCB *acb = state->acb[i]; | |
374 | ||
375 | if (acb == NULL) { | |
376 | continue; | |
f785a5ae | 377 | } |
f785a5ae | 378 | |
ba3186c4 | 379 | qemu_mutex_unlock(&s->mutex); |
796a060b | 380 | acb->common.cb(acb->common.opaque, -EPROTO); |
ba3186c4 | 381 | qemu_mutex_lock(&s->mutex); |
8007429a | 382 | qemu_aio_unref(acb); |
1f2cead3 MB |
383 | state->acb[i] = NULL; |
384 | } | |
769ce76d | 385 | } |
1f2cead3 MB |
386 | |
387 | curl_clean_state(state); | |
388 | break; | |
769ce76d | 389 | } |
1f2cead3 | 390 | } |
769ce76d AG |
391 | } |
392 | ||
ba3186c4 | 393 | /* Called with s->mutex held. */ |
9d456654 | 394 | static void curl_multi_do_locked(CURLState *s) |
031fd1be | 395 | { |
ff5ca166 | 396 | CURLSocket *socket, *next_socket; |
031fd1be PM |
397 | int running; |
398 | int r; | |
399 | ||
838ef602 | 400 | if (!s->s->multi) { |
031fd1be PM |
401 | return; |
402 | } | |
403 | ||
ff5ca166 HR |
404 | /* Need to use _SAFE because curl_multi_socket_action() may trigger |
405 | * curl_sock_cb() which might modify this list */ | |
406 | QLIST_FOREACH_SAFE(socket, &s->sockets, next, next_socket) { | |
407 | do { | |
408 | r = curl_multi_socket_action(s->s->multi, socket->fd, 0, &running); | |
409 | } while (r == CURLM_CALL_MULTI_PERFORM); | |
410 | } | |
838ef602 MB |
411 | } |
412 | ||
9d456654 PB |
413 | static void curl_multi_do(void *arg) |
414 | { | |
415 | CURLState *s = (CURLState *)arg; | |
416 | ||
ba3186c4 | 417 | qemu_mutex_lock(&s->s->mutex); |
9d456654 | 418 | curl_multi_do_locked(s); |
ba3186c4 | 419 | qemu_mutex_unlock(&s->s->mutex); |
9d456654 PB |
420 | } |
421 | ||
838ef602 MB |
422 | static void curl_multi_read(void *arg) |
423 | { | |
424 | CURLState *s = (CURLState *)arg; | |
425 | ||
ba3186c4 | 426 | qemu_mutex_lock(&s->s->mutex); |
9d456654 | 427 | curl_multi_do_locked(s); |
838ef602 | 428 | curl_multi_check_completion(s->s); |
ba3186c4 | 429 | qemu_mutex_unlock(&s->s->mutex); |
031fd1be PM |
430 | } |
431 | ||
432 | static void curl_multi_timeout_do(void *arg) | |
433 | { | |
434 | #ifdef NEED_CURL_TIMER_CALLBACK | |
435 | BDRVCURLState *s = (BDRVCURLState *)arg; | |
436 | int running; | |
437 | ||
438 | if (!s->multi) { | |
439 | return; | |
440 | } | |
441 | ||
ba3186c4 | 442 | qemu_mutex_lock(&s->mutex); |
031fd1be PM |
443 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
444 | ||
838ef602 | 445 | curl_multi_check_completion(s); |
ba3186c4 | 446 | qemu_mutex_unlock(&s->mutex); |
031fd1be PM |
447 | #else |
448 | abort(); | |
449 | #endif | |
450 | } | |
451 | ||
a2f468e4 | 452 | static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s) |
769ce76d AG |
453 | { |
454 | CURLState *state = NULL; | |
455 | int i, j; | |
456 | ||
457 | do { | |
458 | for (i=0; i<CURL_NUM_STATES; i++) { | |
459 | for (j=0; j<CURL_NUM_ACB; j++) | |
460 | if (s->states[i].acb[j]) | |
461 | continue; | |
462 | if (s->states[i].in_use) | |
463 | continue; | |
464 | ||
465 | state = &s->states[i]; | |
466 | state->in_use = 1; | |
467 | break; | |
468 | } | |
469 | if (!state) { | |
a2f468e4 | 470 | aio_poll(bdrv_get_aio_context(bs), true); |
769ce76d AG |
471 | } |
472 | } while(!state); | |
473 | ||
9e550b32 MB |
474 | if (!state->curl) { |
475 | state->curl = curl_easy_init(); | |
476 | if (!state->curl) { | |
477 | return NULL; | |
478 | } | |
479 | curl_easy_setopt(state->curl, CURLOPT_URL, s->url); | |
97a3ea57 MB |
480 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, |
481 | (long) s->sslverify); | |
a94f83d9 RJ |
482 | if (s->cookie) { |
483 | curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); | |
484 | } | |
f76faeda | 485 | curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); |
9e550b32 MB |
486 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, |
487 | (void *)curl_read_cb); | |
488 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); | |
489 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); | |
490 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); | |
491 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); | |
492 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); | |
493 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); | |
494 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); | |
495 | ||
1bff9606 DB |
496 | if (s->username) { |
497 | curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); | |
498 | } | |
499 | if (s->password) { | |
500 | curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); | |
501 | } | |
502 | if (s->proxyusername) { | |
503 | curl_easy_setopt(state->curl, | |
504 | CURLOPT_PROXYUSERNAME, s->proxyusername); | |
505 | } | |
506 | if (s->proxypassword) { | |
507 | curl_easy_setopt(state->curl, | |
508 | CURLOPT_PROXYPASSWORD, s->proxypassword); | |
509 | } | |
510 | ||
9e550b32 MB |
511 | /* Restrict supported protocols to avoid security issues in the more |
512 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see | |
513 | * CVE-2013-0249. | |
514 | * | |
515 | * Restricting protocols is only supported from 7.19.4 upwards. | |
516 | */ | |
8a8f5840 | 517 | #if LIBCURL_VERSION_NUM >= 0x071304 |
9e550b32 MB |
518 | curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); |
519 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); | |
8a8f5840 | 520 | #endif |
fb6d1bbd | 521 | |
769ce76d | 522 | #ifdef DEBUG_VERBOSE |
9e550b32 | 523 | curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); |
769ce76d | 524 | #endif |
9e550b32 | 525 | } |
769ce76d | 526 | |
ff5ca166 | 527 | QLIST_INIT(&state->sockets); |
769ce76d AG |
528 | state->s = s; |
529 | ||
530 | return state; | |
531 | } | |
532 | ||
533 | static void curl_clean_state(CURLState *s) | |
534 | { | |
535 | if (s->s->multi) | |
536 | curl_multi_remove_handle(s->s->multi, s->curl); | |
ff5ca166 HR |
537 | |
538 | while (!QLIST_EMPTY(&s->sockets)) { | |
539 | CURLSocket *socket = QLIST_FIRST(&s->sockets); | |
540 | ||
541 | QLIST_REMOVE(socket, next); | |
542 | g_free(socket); | |
543 | } | |
544 | ||
769ce76d AG |
545 | s->in_use = 0; |
546 | } | |
547 | ||
8e6d58cd KW |
548 | static void curl_parse_filename(const char *filename, QDict *options, |
549 | Error **errp) | |
769ce76d | 550 | { |
e3542c67 | 551 | qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename)); |
8e6d58cd KW |
552 | } |
553 | ||
63f0f45f SH |
554 | static void curl_detach_aio_context(BlockDriverState *bs) |
555 | { | |
556 | BDRVCURLState *s = bs->opaque; | |
557 | int i; | |
558 | ||
559 | for (i = 0; i < CURL_NUM_STATES; i++) { | |
560 | if (s->states[i].in_use) { | |
561 | curl_clean_state(&s->states[i]); | |
562 | } | |
563 | if (s->states[i].curl) { | |
564 | curl_easy_cleanup(s->states[i].curl); | |
565 | s->states[i].curl = NULL; | |
566 | } | |
f7047c2d MA |
567 | g_free(s->states[i].orig_buf); |
568 | s->states[i].orig_buf = NULL; | |
63f0f45f SH |
569 | } |
570 | if (s->multi) { | |
571 | curl_multi_cleanup(s->multi); | |
572 | s->multi = NULL; | |
573 | } | |
574 | ||
575 | timer_del(&s->timer); | |
576 | } | |
577 | ||
578 | static void curl_attach_aio_context(BlockDriverState *bs, | |
579 | AioContext *new_context) | |
580 | { | |
581 | BDRVCURLState *s = bs->opaque; | |
582 | ||
583 | aio_timer_init(new_context, &s->timer, | |
584 | QEMU_CLOCK_REALTIME, SCALE_NS, | |
585 | curl_multi_timeout_do, s); | |
586 | ||
587 | assert(!s->multi); | |
588 | s->multi = curl_multi_init(); | |
589 | s->aio_context = new_context; | |
590 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); | |
591 | #ifdef NEED_CURL_TIMER_CALLBACK | |
592 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); | |
593 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); | |
594 | #endif | |
595 | } | |
596 | ||
8e6d58cd KW |
597 | static QemuOptsList runtime_opts = { |
598 | .name = "curl", | |
599 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
600 | .desc = { | |
601 | { | |
e3542c67 | 602 | .name = CURL_BLOCK_OPT_URL, |
8e6d58cd KW |
603 | .type = QEMU_OPT_STRING, |
604 | .help = "URL to open", | |
605 | }, | |
606 | { | |
e3542c67 | 607 | .name = CURL_BLOCK_OPT_READAHEAD, |
8e6d58cd KW |
608 | .type = QEMU_OPT_SIZE, |
609 | .help = "Readahead size", | |
610 | }, | |
97a3ea57 MB |
611 | { |
612 | .name = CURL_BLOCK_OPT_SSLVERIFY, | |
613 | .type = QEMU_OPT_BOOL, | |
614 | .help = "Verify SSL certificate" | |
615 | }, | |
212aefaa DHB |
616 | { |
617 | .name = CURL_BLOCK_OPT_TIMEOUT, | |
618 | .type = QEMU_OPT_NUMBER, | |
619 | .help = "Curl timeout" | |
620 | }, | |
a94f83d9 RJ |
621 | { |
622 | .name = CURL_BLOCK_OPT_COOKIE, | |
623 | .type = QEMU_OPT_STRING, | |
624 | .help = "Pass the cookie or list of cookies with each request" | |
625 | }, | |
1bff9606 DB |
626 | { |
627 | .name = CURL_BLOCK_OPT_USERNAME, | |
628 | .type = QEMU_OPT_STRING, | |
629 | .help = "Username for HTTP auth" | |
630 | }, | |
631 | { | |
632 | .name = CURL_BLOCK_OPT_PASSWORD_SECRET, | |
633 | .type = QEMU_OPT_STRING, | |
634 | .help = "ID of secret used as password for HTTP auth", | |
635 | }, | |
636 | { | |
637 | .name = CURL_BLOCK_OPT_PROXY_USERNAME, | |
638 | .type = QEMU_OPT_STRING, | |
639 | .help = "Username for HTTP proxy auth" | |
640 | }, | |
641 | { | |
642 | .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, | |
643 | .type = QEMU_OPT_STRING, | |
644 | .help = "ID of secret used as password for HTTP proxy auth", | |
645 | }, | |
8e6d58cd KW |
646 | { /* end of list */ } |
647 | }, | |
648 | }; | |
649 | ||
1bff9606 | 650 | |
015a1036 HR |
651 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
652 | Error **errp) | |
8e6d58cd KW |
653 | { |
654 | BDRVCURLState *s = bs->opaque; | |
655 | CURLState *state = NULL; | |
656 | QemuOpts *opts; | |
657 | Error *local_err = NULL; | |
658 | const char *file; | |
a94f83d9 | 659 | const char *cookie; |
8e6d58cd | 660 | double d; |
1bff9606 | 661 | const char *secretid; |
8e6d58cd KW |
662 | |
663 | static int inited = 0; | |
664 | ||
a7cea2ba | 665 | if (flags & BDRV_O_RDWR) { |
2a94fee3 | 666 | error_setg(errp, "curl block device does not support writes"); |
a7cea2ba RJ |
667 | return -EROFS; |
668 | } | |
669 | ||
87ea75d5 | 670 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
8e6d58cd | 671 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 672 | if (local_err) { |
2a94fee3 | 673 | error_propagate(errp, local_err); |
8e6d58cd KW |
674 | goto out_noclean; |
675 | } | |
676 | ||
e3542c67 MB |
677 | s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, |
678 | READ_AHEAD_DEFAULT); | |
c76f4952 | 679 | if ((s->readahead_size & 0x1ff) != 0) { |
2a94fee3 PB |
680 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
681 | s->readahead_size); | |
c76f4952 N |
682 | goto out_noclean; |
683 | } | |
684 | ||
212aefaa DHB |
685 | s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, |
686 | CURL_TIMEOUT_DEFAULT); | |
f76faeda RJ |
687 | if (s->timeout > CURL_TIMEOUT_MAX) { |
688 | error_setg(errp, "timeout parameter is too large or negative"); | |
689 | goto out_noclean; | |
690 | } | |
212aefaa | 691 | |
97a3ea57 MB |
692 | s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true); |
693 | ||
a94f83d9 RJ |
694 | cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); |
695 | s->cookie = g_strdup(cookie); | |
696 | ||
e3542c67 | 697 | file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); |
8e6d58cd | 698 | if (file == NULL) { |
2a94fee3 | 699 | error_setg(errp, "curl block driver requires an 'url' option"); |
8e6d58cd KW |
700 | goto out_noclean; |
701 | } | |
702 | ||
1bff9606 DB |
703 | s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); |
704 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); | |
705 | ||
706 | if (secretid) { | |
707 | s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
708 | if (!s->password) { | |
709 | goto out_noclean; | |
710 | } | |
711 | } | |
712 | ||
713 | s->proxyusername = g_strdup( | |
714 | qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); | |
715 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); | |
716 | if (secretid) { | |
717 | s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
718 | if (!s->proxypassword) { | |
719 | goto out_noclean; | |
720 | } | |
721 | } | |
722 | ||
769ce76d AG |
723 | if (!inited) { |
724 | curl_global_init(CURL_GLOBAL_ALL); | |
725 | inited = 1; | |
726 | } | |
727 | ||
d0f2c4c6 | 728 | DPRINTF("CURL: Opening %s\n", file); |
63f0f45f | 729 | s->aio_context = bdrv_get_aio_context(bs); |
8e6d58cd | 730 | s->url = g_strdup(file); |
a2f468e4 | 731 | state = curl_init_state(bs, s); |
769ce76d AG |
732 | if (!state) |
733 | goto out_noclean; | |
734 | ||
735 | // Get file size | |
736 | ||
3494d650 | 737 | s->accept_range = false; |
769ce76d | 738 | curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); |
3494d650 FZ |
739 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, |
740 | curl_header_cb); | |
741 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); | |
769ce76d AG |
742 | if (curl_easy_perform(state->curl)) |
743 | goto out; | |
a41c4578 | 744 | if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { |
769ce76d | 745 | goto out; |
a41c4578 TG |
746 | } |
747 | /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not | |
748 | * know or the size is zero. From 7.19.4 CURL returns -1 if size is not | |
749 | * known and zero if it is realy zero-length file. */ | |
750 | #if LIBCURL_VERSION_NUM >= 0x071304 | |
751 | if (d < 0) { | |
752 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
753 | "Server didn't report file size."); | |
754 | goto out; | |
755 | } | |
756 | #else | |
757 | if (d <= 0) { | |
758 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
759 | "Unknown file size or zero-length file."); | |
760 | goto out; | |
761 | } | |
762 | #endif | |
763 | ||
764 | s->len = (size_t)d; | |
765 | ||
3494d650 FZ |
766 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
767 | || !strncasecmp(s->url, "https://", strlen("https://"))) | |
768 | && !s->accept_range) { | |
769 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
770 | "Server does not support 'range' (byte ranges)."); | |
771 | goto out; | |
772 | } | |
0bfcd599 | 773 | DPRINTF("CURL: Size = %zd\n", s->len); |
769ce76d AG |
774 | |
775 | curl_clean_state(state); | |
776 | curl_easy_cleanup(state->curl); | |
777 | state->curl = NULL; | |
778 | ||
ba3186c4 | 779 | qemu_mutex_init(&s->mutex); |
63f0f45f | 780 | curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
769ce76d | 781 | |
8e6d58cd | 782 | qemu_opts_del(opts); |
769ce76d AG |
783 | return 0; |
784 | ||
785 | out: | |
acd7fdc6 | 786 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
769ce76d AG |
787 | curl_easy_cleanup(state->curl); |
788 | state->curl = NULL; | |
789 | out_noclean: | |
a94f83d9 | 790 | g_free(s->cookie); |
8e6d58cd KW |
791 | g_free(s->url); |
792 | qemu_opts_del(opts); | |
769ce76d AG |
793 | return -EINVAL; |
794 | } | |
795 | ||
d7331bed | 796 | static const AIOCBInfo curl_aiocb_info = { |
c16b5a2c | 797 | .aiocb_size = sizeof(CURLAIOCB), |
c16b5a2c CH |
798 | }; |
799 | ||
363c3c85 NT |
800 | |
801 | static void curl_readv_bh_cb(void *p) | |
769ce76d | 802 | { |
769ce76d | 803 | CURLState *state; |
b69cdef8 | 804 | int running; |
1919631e | 805 | int ret = -EINPROGRESS; |
769ce76d | 806 | |
363c3c85 | 807 | CURLAIOCB *acb = p; |
1919631e PB |
808 | BlockDriverState *bs = acb->common.bs; |
809 | BDRVCURLState *s = bs->opaque; | |
769ce76d | 810 | |
9054d9f6 | 811 | size_t start = acb->sector_num * BDRV_SECTOR_SIZE; |
363c3c85 | 812 | size_t end; |
769ce76d | 813 | |
ba3186c4 | 814 | qemu_mutex_lock(&s->mutex); |
1919631e | 815 | |
769ce76d AG |
816 | // In case we have the requested data already (e.g. read-ahead), |
817 | // we can just call the callback and be done. | |
9054d9f6 | 818 | switch (curl_find_buf(s, start, acb->nb_sectors * BDRV_SECTOR_SIZE, acb)) { |
769ce76d | 819 | case FIND_RET_OK: |
8007429a | 820 | qemu_aio_unref(acb); |
769ce76d AG |
821 | // fall through |
822 | case FIND_RET_WAIT: | |
1919631e | 823 | goto out; |
769ce76d AG |
824 | default: |
825 | break; | |
826 | } | |
827 | ||
828 | // No cache found, so let's start a new request | |
a2f468e4 | 829 | state = curl_init_state(acb->common.bs, s); |
363c3c85 | 830 | if (!state) { |
1919631e PB |
831 | ret = -EIO; |
832 | goto out; | |
363c3c85 | 833 | } |
769ce76d AG |
834 | |
835 | acb->start = 0; | |
4e504535 | 836 | acb->end = MIN(acb->nb_sectors * BDRV_SECTOR_SIZE, s->len - start); |
769ce76d AG |
837 | |
838 | state->buf_off = 0; | |
f7047c2d | 839 | g_free(state->orig_buf); |
769ce76d | 840 | state->buf_start = start; |
4e504535 HR |
841 | state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); |
842 | end = start + state->buf_len - 1; | |
8dc7a772 KW |
843 | state->orig_buf = g_try_malloc(state->buf_len); |
844 | if (state->buf_len && state->orig_buf == NULL) { | |
845 | curl_clean_state(state); | |
1919631e PB |
846 | ret = -ENOMEM; |
847 | goto out; | |
8dc7a772 | 848 | } |
769ce76d AG |
849 | state->acb[0] = acb; |
850 | ||
0bfcd599 | 851 | snprintf(state->range, 127, "%zd-%zd", start, end); |
9054d9f6 HR |
852 | DPRINTF("CURL (AIO): Reading %llu at %zd (%s)\n", |
853 | (acb->nb_sectors * BDRV_SECTOR_SIZE), start, state->range); | |
769ce76d AG |
854 | curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); |
855 | ||
856 | curl_multi_add_handle(s->multi, state->curl); | |
769ce76d | 857 | |
b69cdef8 MB |
858 | /* Tell curl it needs to kick things off */ |
859 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
1919631e PB |
860 | |
861 | out: | |
ba3186c4 | 862 | qemu_mutex_unlock(&s->mutex); |
1919631e PB |
863 | if (ret != -EINPROGRESS) { |
864 | acb->common.cb(acb->common.opaque, ret); | |
865 | qemu_aio_unref(acb); | |
866 | } | |
363c3c85 NT |
867 | } |
868 | ||
7c84b1b8 | 869 | static BlockAIOCB *curl_aio_readv(BlockDriverState *bs, |
363c3c85 | 870 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
097310b5 | 871 | BlockCompletionFunc *cb, void *opaque) |
363c3c85 NT |
872 | { |
873 | CURLAIOCB *acb; | |
874 | ||
d7331bed | 875 | acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque); |
363c3c85 | 876 | |
363c3c85 NT |
877 | acb->qiov = qiov; |
878 | acb->sector_num = sector_num; | |
879 | acb->nb_sectors = nb_sectors; | |
880 | ||
fffb6e12 | 881 | aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb); |
769ce76d AG |
882 | return &acb->common; |
883 | } | |
884 | ||
769ce76d AG |
885 | static void curl_close(BlockDriverState *bs) |
886 | { | |
887 | BDRVCURLState *s = bs->opaque; | |
769ce76d | 888 | |
d0f2c4c6 | 889 | DPRINTF("CURL: Close\n"); |
63f0f45f | 890 | curl_detach_aio_context(bs); |
ba3186c4 | 891 | qemu_mutex_destroy(&s->mutex); |
031fd1be | 892 | |
a94f83d9 | 893 | g_free(s->cookie); |
45724d6d | 894 | g_free(s->url); |
769ce76d AG |
895 | } |
896 | ||
897 | static int64_t curl_getlength(BlockDriverState *bs) | |
898 | { | |
899 | BDRVCURLState *s = bs->opaque; | |
900 | return s->len; | |
901 | } | |
902 | ||
903 | static BlockDriver bdrv_http = { | |
63f0f45f SH |
904 | .format_name = "http", |
905 | .protocol_name = "http", | |
906 | ||
907 | .instance_size = sizeof(BDRVCURLState), | |
908 | .bdrv_parse_filename = curl_parse_filename, | |
909 | .bdrv_file_open = curl_open, | |
910 | .bdrv_close = curl_close, | |
911 | .bdrv_getlength = curl_getlength, | |
769ce76d | 912 | |
63f0f45f | 913 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d | 914 | |
63f0f45f SH |
915 | .bdrv_detach_aio_context = curl_detach_aio_context, |
916 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
769ce76d AG |
917 | }; |
918 | ||
919 | static BlockDriver bdrv_https = { | |
63f0f45f SH |
920 | .format_name = "https", |
921 | .protocol_name = "https", | |
769ce76d | 922 | |
63f0f45f SH |
923 | .instance_size = sizeof(BDRVCURLState), |
924 | .bdrv_parse_filename = curl_parse_filename, | |
925 | .bdrv_file_open = curl_open, | |
926 | .bdrv_close = curl_close, | |
927 | .bdrv_getlength = curl_getlength, | |
769ce76d | 928 | |
63f0f45f SH |
929 | .bdrv_aio_readv = curl_aio_readv, |
930 | ||
931 | .bdrv_detach_aio_context = curl_detach_aio_context, | |
932 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
769ce76d AG |
933 | }; |
934 | ||
935 | static BlockDriver bdrv_ftp = { | |
63f0f45f SH |
936 | .format_name = "ftp", |
937 | .protocol_name = "ftp", | |
938 | ||
939 | .instance_size = sizeof(BDRVCURLState), | |
940 | .bdrv_parse_filename = curl_parse_filename, | |
941 | .bdrv_file_open = curl_open, | |
942 | .bdrv_close = curl_close, | |
943 | .bdrv_getlength = curl_getlength, | |
769ce76d | 944 | |
63f0f45f | 945 | .bdrv_aio_readv = curl_aio_readv, |
769ce76d | 946 | |
63f0f45f SH |
947 | .bdrv_detach_aio_context = curl_detach_aio_context, |
948 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
769ce76d AG |
949 | }; |
950 | ||
951 | static BlockDriver bdrv_ftps = { | |
63f0f45f SH |
952 | .format_name = "ftps", |
953 | .protocol_name = "ftps", | |
769ce76d | 954 | |
63f0f45f SH |
955 | .instance_size = sizeof(BDRVCURLState), |
956 | .bdrv_parse_filename = curl_parse_filename, | |
957 | .bdrv_file_open = curl_open, | |
958 | .bdrv_close = curl_close, | |
959 | .bdrv_getlength = curl_getlength, | |
769ce76d | 960 | |
63f0f45f SH |
961 | .bdrv_aio_readv = curl_aio_readv, |
962 | ||
963 | .bdrv_detach_aio_context = curl_detach_aio_context, | |
964 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
769ce76d AG |
965 | }; |
966 | ||
769ce76d AG |
967 | static void curl_block_init(void) |
968 | { | |
969 | bdrv_register(&bdrv_http); | |
970 | bdrv_register(&bdrv_https); | |
971 | bdrv_register(&bdrv_ftp); | |
972 | bdrv_register(&bdrv_ftps); | |
769ce76d AG |
973 | } |
974 | ||
975 | block_init(curl_block_init); |