1769ce76dSAlexander Graf /* 2769ce76dSAlexander Graf * QEMU Block driver for CURL images 3769ce76dSAlexander Graf * 4769ce76dSAlexander Graf * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 5769ce76dSAlexander Graf * 6769ce76dSAlexander Graf * Permission is hereby granted, free of charge, to any person obtaining a copy 7769ce76dSAlexander Graf * of this software and associated documentation files (the "Software"), to deal 8769ce76dSAlexander Graf * in the Software without restriction, including without limitation the rights 9769ce76dSAlexander Graf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10769ce76dSAlexander Graf * copies of the Software, and to permit persons to whom the Software is 11769ce76dSAlexander Graf * furnished to do so, subject to the following conditions: 12769ce76dSAlexander Graf * 13769ce76dSAlexander Graf * The above copyright notice and this permission notice shall be included in 14769ce76dSAlexander Graf * all copies or substantial portions of the Software. 15769ce76dSAlexander Graf * 16769ce76dSAlexander Graf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17769ce76dSAlexander Graf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18769ce76dSAlexander Graf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19769ce76dSAlexander Graf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20769ce76dSAlexander Graf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21769ce76dSAlexander Graf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22769ce76dSAlexander Graf * THE SOFTWARE. 23769ce76dSAlexander Graf */ 24452fcdbcSMarkus Armbruster 2580c71a24SPeter Maydell #include "qemu/osdep.h" 26da34e65cSMarkus Armbruster #include "qapi/error.h" 27796a060bSRichard W.M. Jones #include "qemu/error-report.h" 280b8fa32fSMarkus Armbruster #include "qemu/module.h" 29922a01a0SMarkus Armbruster #include "qemu/option.h" 30737e150eSPaolo Bonzini #include "block/block_int.h" 31452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h" 32d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h" 331bff9606SDaniel P. Berrange #include "crypto/secret.h" 34769ce76dSAlexander Graf #include <curl/curl.h> 35f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 36ed2a66deSLaurent Vivier #include "trace.h" 37769ce76dSAlexander Graf 38769ce76dSAlexander Graf // #define DEBUG_VERBOSE 39769ce76dSAlexander Graf 40031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000 41031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */ 42031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK 439aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION 449aedd5a5SMatthew Booth #endif 459aedd5a5SMatthew Booth 469aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION 479aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in 489aedd5a5SMatthew Booth * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is 499aedd5a5SMatthew Booth * less efficient but still safe. */ 509aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle, 519aedd5a5SMatthew Booth curl_socket_t sockfd, 529aedd5a5SMatthew Booth int ev_bitmask, 539aedd5a5SMatthew Booth int *running_handles) 549aedd5a5SMatthew Booth { 559aedd5a5SMatthew Booth return curl_multi_socket(multi_handle, sockfd, running_handles); 569aedd5a5SMatthew Booth } 579aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action 58031fd1beSPeter Maydell #endif 59031fd1beSPeter Maydell 60fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ 6123dce387SMax Reitz CURLPROTO_FTP | CURLPROTO_FTPS) 62fb6d1bbdSStefan Hajnoczi 63769ce76dSAlexander Graf #define CURL_NUM_STATES 8 64769ce76dSAlexander Graf #define CURL_NUM_ACB 8 65f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000 66769ce76dSAlexander Graf 67e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL "url" 68e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead" 6997a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" 70212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout" 71a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE "cookie" 72327c8ebdSPeter Krempa #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" 731bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_USERNAME "username" 741bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" 751bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" 761bff9606SDaniel P. Berrange #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" 77e3542c67SMatthew Booth 78712b64e8SMax Reitz #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024) 79712b64e8SMax Reitz #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true 80712b64e8SMax Reitz #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5 81712b64e8SMax Reitz 82769ce76dSAlexander Graf struct BDRVCURLState; 8304878616SMax Reitz struct CURLState; 84769ce76dSAlexander Graf 852d25964dSJeff Cody static bool libcurl_initialized; 862d25964dSJeff Cody 87769ce76dSAlexander Graf typedef struct CURLAIOCB { 8828256d82SPaolo Bonzini Coroutine *co; 89769ce76dSAlexander Graf QEMUIOVector *qiov; 90363c3c85SNick Thomas 912125e5eaSPaolo Bonzini uint64_t offset; 922125e5eaSPaolo Bonzini uint64_t bytes; 9328256d82SPaolo Bonzini int ret; 94363c3c85SNick Thomas 95769ce76dSAlexander Graf size_t start; 96769ce76dSAlexander Graf size_t end; 97769ce76dSAlexander Graf } CURLAIOCB; 98769ce76dSAlexander Graf 99ff5ca166SMax Reitz typedef struct CURLSocket { 100ff5ca166SMax Reitz int fd; 10104878616SMax Reitz struct CURLState *state; 102ff5ca166SMax Reitz QLIST_ENTRY(CURLSocket) next; 103ff5ca166SMax Reitz } CURLSocket; 104ff5ca166SMax Reitz 105769ce76dSAlexander Graf typedef struct CURLState 106769ce76dSAlexander Graf { 107769ce76dSAlexander Graf struct BDRVCURLState *s; 108769ce76dSAlexander Graf CURLAIOCB *acb[CURL_NUM_ACB]; 109769ce76dSAlexander Graf CURL *curl; 110ff5ca166SMax Reitz QLIST_HEAD(, CURLSocket) sockets; 111769ce76dSAlexander Graf char *orig_buf; 1122125e5eaSPaolo Bonzini uint64_t buf_start; 113769ce76dSAlexander Graf size_t buf_off; 114769ce76dSAlexander Graf size_t buf_len; 115769ce76dSAlexander Graf char range[128]; 116769ce76dSAlexander Graf char errmsg[CURL_ERROR_SIZE]; 117769ce76dSAlexander Graf char in_use; 118769ce76dSAlexander Graf } CURLState; 119769ce76dSAlexander Graf 120769ce76dSAlexander Graf typedef struct BDRVCURLState { 121769ce76dSAlexander Graf CURLM *multi; 122031fd1beSPeter Maydell QEMUTimer timer; 1232125e5eaSPaolo Bonzini uint64_t len; 124769ce76dSAlexander Graf CURLState states[CURL_NUM_STATES]; 125769ce76dSAlexander Graf char *url; 126c76f4952SNolan size_t readahead_size; 12797a3ea57SMatthew Booth bool sslverify; 128f76faedaSRichard W.M. Jones uint64_t timeout; 129a94f83d9SRichard W.M. Jones char *cookie; 1303494d650SFam Zheng bool accept_range; 13163f0f45fSStefan Hajnoczi AioContext *aio_context; 132ba3186c4SPaolo Bonzini QemuMutex mutex; 133709f2132SPaolo Bonzini CoQueue free_state_waitq; 1341bff9606SDaniel P. Berrange char *username; 1351bff9606SDaniel P. Berrange char *password; 1361bff9606SDaniel P. Berrange char *proxyusername; 1371bff9606SDaniel P. Berrange char *proxypassword; 138769ce76dSAlexander Graf } BDRVCURLState; 139769ce76dSAlexander Graf 140769ce76dSAlexander Graf static void curl_clean_state(CURLState *s); 141769ce76dSAlexander Graf static void curl_multi_do(void *arg); 142769ce76dSAlexander Graf 143031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 14434db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 145031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) 146031fd1beSPeter Maydell { 147031fd1beSPeter Maydell BDRVCURLState *s = opaque; 148031fd1beSPeter Maydell 149ed2a66deSLaurent Vivier trace_curl_timer_cb(timeout_ms); 150031fd1beSPeter Maydell if (timeout_ms == -1) { 151031fd1beSPeter Maydell timer_del(&s->timer); 152031fd1beSPeter Maydell } else { 153031fd1beSPeter Maydell int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; 154031fd1beSPeter Maydell timer_mod(&s->timer, 155031fd1beSPeter Maydell qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); 156031fd1beSPeter Maydell } 157031fd1beSPeter Maydell return 0; 158031fd1beSPeter Maydell } 159031fd1beSPeter Maydell #endif 160031fd1beSPeter Maydell 16134db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 162769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, 16363f0f45fSStefan Hajnoczi void *userp, void *sp) 164769ce76dSAlexander Graf { 16563f0f45fSStefan Hajnoczi BDRVCURLState *s; 166838ef602SMatthew Booth CURLState *state = NULL; 167ff5ca166SMax Reitz CURLSocket *socket; 168ff5ca166SMax Reitz 169838ef602SMatthew Booth curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); 17063f0f45fSStefan Hajnoczi s = state->s; 171838ef602SMatthew Booth 172ff5ca166SMax Reitz QLIST_FOREACH(socket, &state->sockets, next) { 173ff5ca166SMax Reitz if (socket->fd == fd) { 174ff5ca166SMax Reitz break; 175ff5ca166SMax Reitz } 176ff5ca166SMax Reitz } 177ff5ca166SMax Reitz if (!socket) { 178ff5ca166SMax Reitz socket = g_new0(CURLSocket, 1); 179ff5ca166SMax Reitz socket->fd = fd; 18004878616SMax Reitz socket->state = state; 181ff5ca166SMax Reitz QLIST_INSERT_HEAD(&state->sockets, socket, next); 182ff5ca166SMax Reitz } 183ff5ca166SMax Reitz 184ed2a66deSLaurent Vivier trace_curl_sock_cb(action, (int)fd); 185769ce76dSAlexander Graf switch (action) { 186769ce76dSAlexander Graf case CURL_POLL_IN: 187dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 1889dbad87dSMax Reitz curl_multi_do, NULL, NULL, socket); 189769ce76dSAlexander Graf break; 190769ce76dSAlexander Graf case CURL_POLL_OUT: 191dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 1929dbad87dSMax Reitz NULL, curl_multi_do, NULL, socket); 193769ce76dSAlexander Graf break; 194769ce76dSAlexander Graf case CURL_POLL_INOUT: 195dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 1969dbad87dSMax Reitz curl_multi_do, curl_multi_do, NULL, socket); 197769ce76dSAlexander Graf break; 198769ce76dSAlexander Graf case CURL_POLL_REMOVE: 199dca21ef2SFam Zheng aio_set_fd_handler(s->aio_context, fd, false, 200f6a51c84SStefan Hajnoczi NULL, NULL, NULL, NULL); 201769ce76dSAlexander Graf break; 202769ce76dSAlexander Graf } 203769ce76dSAlexander Graf 204007f339bSMax Reitz if (action == CURL_POLL_REMOVE) { 205007f339bSMax Reitz QLIST_REMOVE(socket, next); 206007f339bSMax Reitz g_free(socket); 207007f339bSMax Reitz } 208007f339bSMax Reitz 209769ce76dSAlexander Graf return 0; 210769ce76dSAlexander Graf } 211769ce76dSAlexander Graf 21234db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 2133494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 214769ce76dSAlexander Graf { 2153494d650SFam Zheng BDRVCURLState *s = opaque; 216769ce76dSAlexander Graf size_t realsize = size * nmemb; 2173494d650SFam Zheng const char *accept_line = "Accept-Ranges: bytes"; 218769ce76dSAlexander Graf 2193494d650SFam Zheng if (realsize >= strlen(accept_line) 2203494d650SFam Zheng && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) { 2213494d650SFam Zheng s->accept_range = true; 2220bfcd599SBlue Swirl } 223769ce76dSAlexander Graf 224769ce76dSAlexander Graf return realsize; 225769ce76dSAlexander Graf } 226769ce76dSAlexander Graf 22734db05e7SPaolo Bonzini /* Called from curl_multi_do_locked, with s->mutex held. */ 228769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) 229769ce76dSAlexander Graf { 230769ce76dSAlexander Graf CURLState *s = ((CURLState*)opaque); 231769ce76dSAlexander Graf size_t realsize = size * nmemb; 232769ce76dSAlexander Graf 233ed2a66deSLaurent Vivier trace_curl_read_cb(realsize); 234769ce76dSAlexander Graf 2354e767657SMax Reitz if (!s || !s->orig_buf) { 2364e767657SMax Reitz goto read_end; 2374e767657SMax Reitz } 238769ce76dSAlexander Graf 2396d4b9e55SFam Zheng if (s->buf_off >= s->buf_len) { 2406d4b9e55SFam Zheng /* buffer full, read nothing */ 2414e767657SMax Reitz goto read_end; 2426d4b9e55SFam Zheng } 2436d4b9e55SFam Zheng realsize = MIN(realsize, s->buf_len - s->buf_off); 244769ce76dSAlexander Graf memcpy(s->orig_buf + s->buf_off, ptr, realsize); 245769ce76dSAlexander Graf s->buf_off += realsize; 246769ce76dSAlexander Graf 2474e767657SMax Reitz read_end: 2484e767657SMax Reitz /* curl will error out if we do not return this value */ 2494e767657SMax Reitz return size * nmemb; 250769ce76dSAlexander Graf } 251769ce76dSAlexander Graf 252456af346SPaolo Bonzini /* Called with s->mutex held. */ 25328256d82SPaolo Bonzini static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, 254769ce76dSAlexander Graf CURLAIOCB *acb) 255769ce76dSAlexander Graf { 256769ce76dSAlexander Graf int i; 2572125e5eaSPaolo Bonzini uint64_t end = start + len; 2582125e5eaSPaolo Bonzini uint64_t clamped_end = MIN(end, s->len); 2592125e5eaSPaolo Bonzini uint64_t clamped_len = clamped_end - start; 260769ce76dSAlexander Graf 261769ce76dSAlexander Graf for (i=0; i<CURL_NUM_STATES; i++) { 262769ce76dSAlexander Graf CURLState *state = &s->states[i]; 2632125e5eaSPaolo Bonzini uint64_t buf_end = (state->buf_start + state->buf_off); 2642125e5eaSPaolo Bonzini uint64_t buf_fend = (state->buf_start + state->buf_len); 265769ce76dSAlexander Graf 266769ce76dSAlexander Graf if (!state->orig_buf) 267769ce76dSAlexander Graf continue; 268769ce76dSAlexander Graf if (!state->buf_off) 269769ce76dSAlexander Graf continue; 270769ce76dSAlexander Graf 271769ce76dSAlexander Graf // Does the existing buffer cover our section? 272769ce76dSAlexander Graf if ((start >= state->buf_start) && 273769ce76dSAlexander Graf (start <= buf_end) && 2744e504535SMax Reitz (clamped_end >= state->buf_start) && 2754e504535SMax Reitz (clamped_end <= buf_end)) 276769ce76dSAlexander Graf { 277769ce76dSAlexander Graf char *buf = state->orig_buf + (start - state->buf_start); 278769ce76dSAlexander Graf 2794e504535SMax Reitz qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); 2804e504535SMax Reitz if (clamped_len < len) { 2814e504535SMax Reitz qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); 2824e504535SMax Reitz } 28328256d82SPaolo Bonzini acb->ret = 0; 28428256d82SPaolo Bonzini return true; 285769ce76dSAlexander Graf } 286769ce76dSAlexander Graf 287769ce76dSAlexander Graf // Wait for unfinished chunks 288b7079df4SMatthew Booth if (state->in_use && 289b7079df4SMatthew Booth (start >= state->buf_start) && 290769ce76dSAlexander Graf (start <= buf_fend) && 2914e504535SMax Reitz (clamped_end >= state->buf_start) && 2924e504535SMax Reitz (clamped_end <= buf_fend)) 293769ce76dSAlexander Graf { 294769ce76dSAlexander Graf int j; 295769ce76dSAlexander Graf 296769ce76dSAlexander Graf acb->start = start - state->buf_start; 2974e504535SMax Reitz acb->end = acb->start + clamped_len; 298769ce76dSAlexander Graf 299769ce76dSAlexander Graf for (j=0; j<CURL_NUM_ACB; j++) { 300769ce76dSAlexander Graf if (!state->acb[j]) { 301769ce76dSAlexander Graf state->acb[j] = acb; 30228256d82SPaolo Bonzini return true; 303769ce76dSAlexander Graf } 304769ce76dSAlexander Graf } 305769ce76dSAlexander Graf } 306769ce76dSAlexander Graf } 307769ce76dSAlexander Graf 30828256d82SPaolo Bonzini return false; 309769ce76dSAlexander Graf } 310769ce76dSAlexander Graf 311ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 312838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s) 313769ce76dSAlexander Graf { 314769ce76dSAlexander Graf int msgs_in_queue; 315769ce76dSAlexander Graf 316769ce76dSAlexander Graf /* Try to find done transfers, so we can free the easy 317769ce76dSAlexander Graf * handle again. */ 3181f2cead3SMatthew Booth for (;;) { 319769ce76dSAlexander Graf CURLMsg *msg; 320769ce76dSAlexander Graf msg = curl_multi_info_read(s->multi, &msgs_in_queue); 321769ce76dSAlexander Graf 3221f2cead3SMatthew Booth /* Quit when there are no more completions */ 323769ce76dSAlexander Graf if (!msg) 324769ce76dSAlexander Graf break; 325769ce76dSAlexander Graf 3261f2cead3SMatthew Booth if (msg->msg == CURLMSG_DONE) { 327*bfb23b48SMax Reitz int i; 328769ce76dSAlexander Graf CURLState *state = NULL; 329*bfb23b48SMax Reitz bool error = msg->data.result != CURLE_OK; 330*bfb23b48SMax Reitz 331f6246509SMatthew Booth curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, 332f6246509SMatthew Booth (char **)&state); 333f785a5aeSNicholas Thomas 334*bfb23b48SMax Reitz if (error) { 335796a060bSRichard W.M. Jones static int errcount = 100; 336796a060bSRichard W.M. Jones 337796a060bSRichard W.M. Jones /* Don't lose the original error message from curl, since 338796a060bSRichard W.M. Jones * it contains extra data. 339796a060bSRichard W.M. Jones */ 340796a060bSRichard W.M. Jones if (errcount > 0) { 341796a060bSRichard W.M. Jones error_report("curl: %s", state->errmsg); 342796a060bSRichard W.M. Jones if (--errcount == 0) { 343796a060bSRichard W.M. Jones error_report("curl: further errors suppressed"); 344796a060bSRichard W.M. Jones } 345796a060bSRichard W.M. Jones } 346*bfb23b48SMax Reitz } 347796a060bSRichard W.M. Jones 348f785a5aeSNicholas Thomas for (i = 0; i < CURL_NUM_ACB; i++) { 349f785a5aeSNicholas Thomas CURLAIOCB *acb = state->acb[i]; 350f785a5aeSNicholas Thomas 351f785a5aeSNicholas Thomas if (acb == NULL) { 352f785a5aeSNicholas Thomas continue; 353f785a5aeSNicholas Thomas } 354f785a5aeSNicholas Thomas 355*bfb23b48SMax Reitz if (!error) { 356*bfb23b48SMax Reitz /* Assert that we have read all data */ 357*bfb23b48SMax Reitz assert(state->buf_off >= acb->end); 358*bfb23b48SMax Reitz 359*bfb23b48SMax Reitz qemu_iovec_from_buf(acb->qiov, 0, 360*bfb23b48SMax Reitz state->orig_buf + acb->start, 361*bfb23b48SMax Reitz acb->end - acb->start); 362*bfb23b48SMax Reitz 363*bfb23b48SMax Reitz if (acb->end - acb->start < acb->bytes) { 364*bfb23b48SMax Reitz size_t offset = acb->end - acb->start; 365*bfb23b48SMax Reitz qemu_iovec_memset(acb->qiov, offset, 0, 366*bfb23b48SMax Reitz acb->bytes - offset); 367*bfb23b48SMax Reitz } 368*bfb23b48SMax Reitz } 369*bfb23b48SMax Reitz 370*bfb23b48SMax Reitz acb->ret = error ? -EIO : 0; 371f785a5aeSNicholas Thomas state->acb[i] = NULL; 37228256d82SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 37328256d82SPaolo Bonzini aio_co_wake(acb->co); 37428256d82SPaolo Bonzini qemu_mutex_lock(&s->mutex); 375f785a5aeSNicholas Thomas } 376f785a5aeSNicholas Thomas 377769ce76dSAlexander Graf curl_clean_state(state); 378769ce76dSAlexander Graf break; 379769ce76dSAlexander Graf } 380769ce76dSAlexander Graf } 381769ce76dSAlexander Graf } 382769ce76dSAlexander Graf 383ba3186c4SPaolo Bonzini /* Called with s->mutex held. */ 3849abaf9fcSMax Reitz static void curl_multi_do_locked(CURLSocket *socket) 385031fd1beSPeter Maydell { 3869abaf9fcSMax Reitz BDRVCURLState *s = socket->state->s; 387031fd1beSPeter Maydell int running; 388031fd1beSPeter Maydell int r; 389031fd1beSPeter Maydell 3909abaf9fcSMax Reitz if (!s->multi) { 391031fd1beSPeter Maydell return; 392031fd1beSPeter Maydell } 393031fd1beSPeter Maydell 394031fd1beSPeter Maydell do { 3959abaf9fcSMax Reitz r = curl_multi_socket_action(s->multi, socket->fd, 0, &running); 396031fd1beSPeter Maydell } while (r == CURLM_CALL_MULTI_PERFORM); 397ff5ca166SMax Reitz } 398838ef602SMatthew Booth 3999d456654SPaolo Bonzini static void curl_multi_do(void *arg) 4009d456654SPaolo Bonzini { 4019dbad87dSMax Reitz CURLSocket *socket = arg; 4029dbad87dSMax Reitz BDRVCURLState *s = socket->state->s; 4039d456654SPaolo Bonzini 4049dbad87dSMax Reitz qemu_mutex_lock(&s->mutex); 4059dbad87dSMax Reitz curl_multi_do_locked(socket); 4069dbad87dSMax Reitz curl_multi_check_completion(s); 4079dbad87dSMax Reitz qemu_mutex_unlock(&s->mutex); 408031fd1beSPeter Maydell } 409031fd1beSPeter Maydell 410031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg) 411031fd1beSPeter Maydell { 412031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK 413031fd1beSPeter Maydell BDRVCURLState *s = (BDRVCURLState *)arg; 414031fd1beSPeter Maydell int running; 415031fd1beSPeter Maydell 416031fd1beSPeter Maydell if (!s->multi) { 417031fd1beSPeter Maydell return; 418031fd1beSPeter Maydell } 419031fd1beSPeter Maydell 420ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 421031fd1beSPeter Maydell curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 422031fd1beSPeter Maydell 423838ef602SMatthew Booth curl_multi_check_completion(s); 424ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 425031fd1beSPeter Maydell #else 426031fd1beSPeter Maydell abort(); 427031fd1beSPeter Maydell #endif 428031fd1beSPeter Maydell } 429031fd1beSPeter Maydell 430456af346SPaolo Bonzini /* Called with s->mutex held. */ 4313ce6a729SPaolo Bonzini static CURLState *curl_find_state(BDRVCURLState *s) 432769ce76dSAlexander Graf { 433769ce76dSAlexander Graf CURLState *state = NULL; 4343ce6a729SPaolo Bonzini int i; 435769ce76dSAlexander Graf 436769ce76dSAlexander Graf for (i = 0; i < CURL_NUM_STATES; i++) { 4373ce6a729SPaolo Bonzini if (!s->states[i].in_use) { 438769ce76dSAlexander Graf state = &s->states[i]; 439769ce76dSAlexander Graf state->in_use = 1; 440769ce76dSAlexander Graf break; 441769ce76dSAlexander Graf } 442769ce76dSAlexander Graf } 4433ce6a729SPaolo Bonzini return state; 4443ce6a729SPaolo Bonzini } 445769ce76dSAlexander Graf 4463ce6a729SPaolo Bonzini static int curl_init_state(BDRVCURLState *s, CURLState *state) 4473ce6a729SPaolo Bonzini { 4489e550b32SMatthew Booth if (!state->curl) { 449769ce76dSAlexander Graf state->curl = curl_easy_init(); 4509e550b32SMatthew Booth if (!state->curl) { 4513ce6a729SPaolo Bonzini return -EIO; 4529e550b32SMatthew Booth } 453769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_URL, s->url); 45497a3ea57SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, 45597a3ea57SMatthew Booth (long) s->sslverify); 456637fa44aSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST, 457637fa44aSRichard W.M. Jones s->sslverify ? 2L : 0L); 458a94f83d9SRichard W.M. Jones if (s->cookie) { 459a94f83d9SRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie); 460a94f83d9SRichard W.M. Jones } 461f76faedaSRichard W.M. Jones curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout); 4629e550b32SMatthew Booth curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, 4639e550b32SMatthew Booth (void *)curl_read_cb); 464769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state); 465769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state); 466769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1); 467769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1); 468769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1); 469769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg); 470f785a5aeSNicholas Thomas curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1); 471769ce76dSAlexander Graf 4721bff9606SDaniel P. Berrange if (s->username) { 4731bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username); 4741bff9606SDaniel P. Berrange } 4751bff9606SDaniel P. Berrange if (s->password) { 4761bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password); 4771bff9606SDaniel P. Berrange } 4781bff9606SDaniel P. Berrange if (s->proxyusername) { 4791bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 4801bff9606SDaniel P. Berrange CURLOPT_PROXYUSERNAME, s->proxyusername); 4811bff9606SDaniel P. Berrange } 4821bff9606SDaniel P. Berrange if (s->proxypassword) { 4831bff9606SDaniel P. Berrange curl_easy_setopt(state->curl, 4841bff9606SDaniel P. Berrange CURLOPT_PROXYPASSWORD, s->proxypassword); 4851bff9606SDaniel P. Berrange } 4861bff9606SDaniel P. Berrange 487fb6d1bbdSStefan Hajnoczi /* Restrict supported protocols to avoid security issues in the more 488fb6d1bbdSStefan Hajnoczi * obscure protocols. For example, do not allow POP3/SMTP/IMAP see 489fb6d1bbdSStefan Hajnoczi * CVE-2013-0249. 4908a8f5840SStefan Hajnoczi * 4918a8f5840SStefan Hajnoczi * Restricting protocols is only supported from 7.19.4 upwards. 492fb6d1bbdSStefan Hajnoczi */ 4938a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304 494fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS); 495fb6d1bbdSStefan Hajnoczi curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS); 4968a8f5840SStefan Hajnoczi #endif 497fb6d1bbdSStefan Hajnoczi 498769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE 499769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1); 500769ce76dSAlexander Graf #endif 5019e550b32SMatthew Booth } 502769ce76dSAlexander Graf 503ff5ca166SMax Reitz QLIST_INIT(&state->sockets); 504769ce76dSAlexander Graf state->s = s; 505769ce76dSAlexander Graf 5063ce6a729SPaolo Bonzini return 0; 507769ce76dSAlexander Graf } 508769ce76dSAlexander Graf 509456af346SPaolo Bonzini /* Called with s->mutex held. */ 510769ce76dSAlexander Graf static void curl_clean_state(CURLState *s) 511769ce76dSAlexander Graf { 512675a7756SPaolo Bonzini int j; 513675a7756SPaolo Bonzini for (j = 0; j < CURL_NUM_ACB; j++) { 514675a7756SPaolo Bonzini assert(!s->acb[j]); 515675a7756SPaolo Bonzini } 516675a7756SPaolo Bonzini 517769ce76dSAlexander Graf if (s->s->multi) 518769ce76dSAlexander Graf curl_multi_remove_handle(s->s->multi, s->curl); 519ff5ca166SMax Reitz 520ff5ca166SMax Reitz while (!QLIST_EMPTY(&s->sockets)) { 521ff5ca166SMax Reitz CURLSocket *socket = QLIST_FIRST(&s->sockets); 522ff5ca166SMax Reitz 523ff5ca166SMax Reitz QLIST_REMOVE(socket, next); 524ff5ca166SMax Reitz g_free(socket); 525ff5ca166SMax Reitz } 526ff5ca166SMax Reitz 527769ce76dSAlexander Graf s->in_use = 0; 5282bb5c936SPaolo Bonzini 529709f2132SPaolo Bonzini qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); 530769ce76dSAlexander Graf } 531769ce76dSAlexander Graf 5328e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options, 5338e6d58cdSKevin Wolf Error **errp) 534769ce76dSAlexander Graf { 53546f5ac20SEric Blake qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); 5368e6d58cdSKevin Wolf } 5378e6d58cdSKevin Wolf 53863f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs) 53963f0f45fSStefan Hajnoczi { 54063f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 54163f0f45fSStefan Hajnoczi int i; 54263f0f45fSStefan Hajnoczi 543456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 54463f0f45fSStefan Hajnoczi for (i = 0; i < CURL_NUM_STATES; i++) { 54563f0f45fSStefan Hajnoczi if (s->states[i].in_use) { 54663f0f45fSStefan Hajnoczi curl_clean_state(&s->states[i]); 54763f0f45fSStefan Hajnoczi } 54863f0f45fSStefan Hajnoczi if (s->states[i].curl) { 54963f0f45fSStefan Hajnoczi curl_easy_cleanup(s->states[i].curl); 55063f0f45fSStefan Hajnoczi s->states[i].curl = NULL; 55163f0f45fSStefan Hajnoczi } 55263f0f45fSStefan Hajnoczi g_free(s->states[i].orig_buf); 55363f0f45fSStefan Hajnoczi s->states[i].orig_buf = NULL; 55463f0f45fSStefan Hajnoczi } 55563f0f45fSStefan Hajnoczi if (s->multi) { 55663f0f45fSStefan Hajnoczi curl_multi_cleanup(s->multi); 55763f0f45fSStefan Hajnoczi s->multi = NULL; 55863f0f45fSStefan Hajnoczi } 559456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 56063f0f45fSStefan Hajnoczi 56163f0f45fSStefan Hajnoczi timer_del(&s->timer); 56263f0f45fSStefan Hajnoczi } 56363f0f45fSStefan Hajnoczi 56463f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs, 56563f0f45fSStefan Hajnoczi AioContext *new_context) 56663f0f45fSStefan Hajnoczi { 56763f0f45fSStefan Hajnoczi BDRVCURLState *s = bs->opaque; 56863f0f45fSStefan Hajnoczi 56963f0f45fSStefan Hajnoczi aio_timer_init(new_context, &s->timer, 57063f0f45fSStefan Hajnoczi QEMU_CLOCK_REALTIME, SCALE_NS, 57163f0f45fSStefan Hajnoczi curl_multi_timeout_do, s); 57263f0f45fSStefan Hajnoczi 57363f0f45fSStefan Hajnoczi assert(!s->multi); 57463f0f45fSStefan Hajnoczi s->multi = curl_multi_init(); 57563f0f45fSStefan Hajnoczi s->aio_context = new_context; 57663f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); 57763f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK 57863f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); 57963f0f45fSStefan Hajnoczi curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); 58063f0f45fSStefan Hajnoczi #endif 58163f0f45fSStefan Hajnoczi } 58263f0f45fSStefan Hajnoczi 5838e6d58cdSKevin Wolf static QemuOptsList runtime_opts = { 5848e6d58cdSKevin Wolf .name = "curl", 5858e6d58cdSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 5868e6d58cdSKevin Wolf .desc = { 5878e6d58cdSKevin Wolf { 588e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_URL, 5898e6d58cdSKevin Wolf .type = QEMU_OPT_STRING, 5908e6d58cdSKevin Wolf .help = "URL to open", 5918e6d58cdSKevin Wolf }, 5928e6d58cdSKevin Wolf { 593e3542c67SMatthew Booth .name = CURL_BLOCK_OPT_READAHEAD, 5948e6d58cdSKevin Wolf .type = QEMU_OPT_SIZE, 5958e6d58cdSKevin Wolf .help = "Readahead size", 5968e6d58cdSKevin Wolf }, 59797a3ea57SMatthew Booth { 59897a3ea57SMatthew Booth .name = CURL_BLOCK_OPT_SSLVERIFY, 59997a3ea57SMatthew Booth .type = QEMU_OPT_BOOL, 60097a3ea57SMatthew Booth .help = "Verify SSL certificate" 60197a3ea57SMatthew Booth }, 602212aefaaSDaniel Henrique Barboza { 603212aefaaSDaniel Henrique Barboza .name = CURL_BLOCK_OPT_TIMEOUT, 604212aefaaSDaniel Henrique Barboza .type = QEMU_OPT_NUMBER, 605212aefaaSDaniel Henrique Barboza .help = "Curl timeout" 606212aefaaSDaniel Henrique Barboza }, 607a94f83d9SRichard W.M. Jones { 608a94f83d9SRichard W.M. Jones .name = CURL_BLOCK_OPT_COOKIE, 609a94f83d9SRichard W.M. Jones .type = QEMU_OPT_STRING, 610a94f83d9SRichard W.M. Jones .help = "Pass the cookie or list of cookies with each request" 611a94f83d9SRichard W.M. Jones }, 6121bff9606SDaniel P. Berrange { 613327c8ebdSPeter Krempa .name = CURL_BLOCK_OPT_COOKIE_SECRET, 614327c8ebdSPeter Krempa .type = QEMU_OPT_STRING, 615327c8ebdSPeter Krempa .help = "ID of secret used as cookie passed with each request" 616327c8ebdSPeter Krempa }, 617327c8ebdSPeter Krempa { 6181bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_USERNAME, 6191bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6201bff9606SDaniel P. Berrange .help = "Username for HTTP auth" 6211bff9606SDaniel P. Berrange }, 6221bff9606SDaniel P. Berrange { 6231bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PASSWORD_SECRET, 6241bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6251bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP auth", 6261bff9606SDaniel P. Berrange }, 6271bff9606SDaniel P. Berrange { 6281bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_USERNAME, 6291bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6301bff9606SDaniel P. Berrange .help = "Username for HTTP proxy auth" 6311bff9606SDaniel P. Berrange }, 6321bff9606SDaniel P. Berrange { 6331bff9606SDaniel P. Berrange .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 6341bff9606SDaniel P. Berrange .type = QEMU_OPT_STRING, 6351bff9606SDaniel P. Berrange .help = "ID of secret used as password for HTTP proxy auth", 6361bff9606SDaniel P. Berrange }, 6378e6d58cdSKevin Wolf { /* end of list */ } 6388e6d58cdSKevin Wolf }, 6398e6d58cdSKevin Wolf }; 6408e6d58cdSKevin Wolf 6411bff9606SDaniel P. Berrange 642015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags, 643015a1036SMax Reitz Error **errp) 6448e6d58cdSKevin Wolf { 6458e6d58cdSKevin Wolf BDRVCURLState *s = bs->opaque; 6468e6d58cdSKevin Wolf CURLState *state = NULL; 6478e6d58cdSKevin Wolf QemuOpts *opts; 6488e6d58cdSKevin Wolf Error *local_err = NULL; 6498e6d58cdSKevin Wolf const char *file; 650a94f83d9SRichard W.M. Jones const char *cookie; 651327c8ebdSPeter Krempa const char *cookie_secret; 6528e6d58cdSKevin Wolf double d; 6531bff9606SDaniel P. Berrange const char *secretid; 65434634ca2SMax Reitz const char *protocol_delimiter; 6552d25964dSJeff Cody int ret; 6568e6d58cdSKevin Wolf 6576ceef36aSKevin Wolf ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes", 6586ceef36aSKevin Wolf errp); 6596ceef36aSKevin Wolf if (ret < 0) { 6606ceef36aSKevin Wolf return ret; 661a7cea2baSRichard W.M. Jones } 662a7cea2baSRichard W.M. Jones 6632d25964dSJeff Cody if (!libcurl_initialized) { 6642d25964dSJeff Cody ret = curl_global_init(CURL_GLOBAL_ALL); 6652d25964dSJeff Cody if (ret) { 6662d25964dSJeff Cody error_setg(errp, "libcurl initialization failed with %d", ret); 6672d25964dSJeff Cody return -EIO; 6682d25964dSJeff Cody } 6692d25964dSJeff Cody libcurl_initialized = true; 6702d25964dSJeff Cody } 6712d25964dSJeff Cody 672456af346SPaolo Bonzini qemu_mutex_init(&s->mutex); 67387ea75d5SPeter Crosthwaite opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 6748e6d58cdSKevin Wolf qemu_opts_absorb_qdict(opts, options, &local_err); 67584d18f06SMarkus Armbruster if (local_err) { 6762a94fee3SPaolo Bonzini error_propagate(errp, local_err); 6778e6d58cdSKevin Wolf goto out_noclean; 6788e6d58cdSKevin Wolf } 6798e6d58cdSKevin Wolf 680e3542c67SMatthew Booth s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, 681712b64e8SMax Reitz CURL_BLOCK_OPT_READAHEAD_DEFAULT); 682c76f4952SNolan if ((s->readahead_size & 0x1ff) != 0) { 6832a94fee3SPaolo Bonzini error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", 684c76f4952SNolan s->readahead_size); 685c76f4952SNolan goto out_noclean; 686c76f4952SNolan } 687c76f4952SNolan 688212aefaaSDaniel Henrique Barboza s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, 689712b64e8SMax Reitz CURL_BLOCK_OPT_TIMEOUT_DEFAULT); 690f76faedaSRichard W.M. Jones if (s->timeout > CURL_TIMEOUT_MAX) { 691f76faedaSRichard W.M. Jones error_setg(errp, "timeout parameter is too large or negative"); 692f76faedaSRichard W.M. Jones goto out_noclean; 693f76faedaSRichard W.M. Jones } 694212aefaaSDaniel Henrique Barboza 695712b64e8SMax Reitz s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, 696712b64e8SMax Reitz CURL_BLOCK_OPT_SSLVERIFY_DEFAULT); 69797a3ea57SMatthew Booth 698a94f83d9SRichard W.M. Jones cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); 699327c8ebdSPeter Krempa cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); 700327c8ebdSPeter Krempa 701327c8ebdSPeter Krempa if (cookie && cookie_secret) { 702327c8ebdSPeter Krempa error_setg(errp, 703327c8ebdSPeter Krempa "curl driver cannot handle both cookie and cookie secret"); 704327c8ebdSPeter Krempa goto out_noclean; 705327c8ebdSPeter Krempa } 706327c8ebdSPeter Krempa 707327c8ebdSPeter Krempa if (cookie_secret) { 708327c8ebdSPeter Krempa s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); 709327c8ebdSPeter Krempa if (!s->cookie) { 710327c8ebdSPeter Krempa goto out_noclean; 711327c8ebdSPeter Krempa } 712327c8ebdSPeter Krempa } else { 713a94f83d9SRichard W.M. Jones s->cookie = g_strdup(cookie); 714327c8ebdSPeter Krempa } 715a94f83d9SRichard W.M. Jones 716e3542c67SMatthew Booth file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); 7178e6d58cdSKevin Wolf if (file == NULL) { 7182a94fee3SPaolo Bonzini error_setg(errp, "curl block driver requires an 'url' option"); 7198e6d58cdSKevin Wolf goto out_noclean; 7208e6d58cdSKevin Wolf } 7218e6d58cdSKevin Wolf 72234634ca2SMax Reitz if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || 72334634ca2SMax Reitz !strstart(protocol_delimiter, "://", NULL)) 72434634ca2SMax Reitz { 72534634ca2SMax Reitz error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " 72634634ca2SMax Reitz "start with '%s://')", bs->drv->protocol_name, file, 72734634ca2SMax Reitz bs->drv->protocol_name); 72834634ca2SMax Reitz goto out_noclean; 72934634ca2SMax Reitz } 73034634ca2SMax Reitz 7311bff9606SDaniel P. Berrange s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); 7321bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); 7331bff9606SDaniel P. Berrange 7341bff9606SDaniel P. Berrange if (secretid) { 7351bff9606SDaniel P. Berrange s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); 7361bff9606SDaniel P. Berrange if (!s->password) { 7371bff9606SDaniel P. Berrange goto out_noclean; 7381bff9606SDaniel P. Berrange } 7391bff9606SDaniel P. Berrange } 7401bff9606SDaniel P. Berrange 7411bff9606SDaniel P. Berrange s->proxyusername = g_strdup( 7421bff9606SDaniel P. Berrange qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); 7431bff9606SDaniel P. Berrange secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); 7441bff9606SDaniel P. Berrange if (secretid) { 7451bff9606SDaniel P. Berrange s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); 7461bff9606SDaniel P. Berrange if (!s->proxypassword) { 7471bff9606SDaniel P. Berrange goto out_noclean; 7481bff9606SDaniel P. Berrange } 7491bff9606SDaniel P. Berrange } 7501bff9606SDaniel P. Berrange 751ed2a66deSLaurent Vivier trace_curl_open(file); 752709f2132SPaolo Bonzini qemu_co_queue_init(&s->free_state_waitq); 75363f0f45fSStefan Hajnoczi s->aio_context = bdrv_get_aio_context(bs); 7548e6d58cdSKevin Wolf s->url = g_strdup(file); 755456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 7563ce6a729SPaolo Bonzini state = curl_find_state(s); 757456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 7583ce6a729SPaolo Bonzini if (!state) { 759769ce76dSAlexander Graf goto out_noclean; 7603ce6a729SPaolo Bonzini } 761769ce76dSAlexander Graf 762769ce76dSAlexander Graf // Get file size 763769ce76dSAlexander Graf 7643ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 7653ce6a729SPaolo Bonzini goto out; 7663ce6a729SPaolo Bonzini } 7673ce6a729SPaolo Bonzini 7683494d650SFam Zheng s->accept_range = false; 769769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); 7703494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, 7713494d650SFam Zheng curl_header_cb); 7723494d650SFam Zheng curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); 773769ce76dSAlexander Graf if (curl_easy_perform(state->curl)) 774769ce76dSAlexander Graf goto out; 775a41c4578STomáš Golembiovský if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { 776769ce76dSAlexander Graf goto out; 777a41c4578STomáš Golembiovský } 778a41c4578STomáš Golembiovský /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not 779a41c4578STomáš Golembiovský * know or the size is zero. From 7.19.4 CURL returns -1 if size is not 78050d6a8a3SStefan Weil * known and zero if it is really zero-length file. */ 781a41c4578STomáš Golembiovský #if LIBCURL_VERSION_NUM >= 0x071304 782a41c4578STomáš Golembiovský if (d < 0) { 783a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 784a41c4578STomáš Golembiovský "Server didn't report file size."); 785a41c4578STomáš Golembiovský goto out; 786a41c4578STomáš Golembiovský } 787a41c4578STomáš Golembiovský #else 788a41c4578STomáš Golembiovský if (d <= 0) { 789a41c4578STomáš Golembiovský pstrcpy(state->errmsg, CURL_ERROR_SIZE, 790a41c4578STomáš Golembiovský "Unknown file size or zero-length file."); 791a41c4578STomáš Golembiovský goto out; 792a41c4578STomáš Golembiovský } 793a41c4578STomáš Golembiovský #endif 794a41c4578STomáš Golembiovský 7952125e5eaSPaolo Bonzini s->len = d; 796a41c4578STomáš Golembiovský 7973494d650SFam Zheng if ((!strncasecmp(s->url, "http://", strlen("http://")) 7983494d650SFam Zheng || !strncasecmp(s->url, "https://", strlen("https://"))) 7993494d650SFam Zheng && !s->accept_range) { 8003494d650SFam Zheng pstrcpy(state->errmsg, CURL_ERROR_SIZE, 8013494d650SFam Zheng "Server does not support 'range' (byte ranges)."); 8023494d650SFam Zheng goto out; 8033494d650SFam Zheng } 804ed2a66deSLaurent Vivier trace_curl_open_size(s->len); 805769ce76dSAlexander Graf 806456af346SPaolo Bonzini qemu_mutex_lock(&s->mutex); 807769ce76dSAlexander Graf curl_clean_state(state); 808456af346SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 809769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 810769ce76dSAlexander Graf state->curl = NULL; 811769ce76dSAlexander Graf 81263f0f45fSStefan Hajnoczi curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); 813769ce76dSAlexander Graf 8148e6d58cdSKevin Wolf qemu_opts_del(opts); 815769ce76dSAlexander Graf return 0; 816769ce76dSAlexander Graf 817769ce76dSAlexander Graf out: 818acd7fdc6SMaria Kustova error_setg(errp, "CURL: Error opening file: %s", state->errmsg); 819769ce76dSAlexander Graf curl_easy_cleanup(state->curl); 820769ce76dSAlexander Graf state->curl = NULL; 821769ce76dSAlexander Graf out_noclean: 822456af346SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 823a94f83d9SRichard W.M. Jones g_free(s->cookie); 8248e6d58cdSKevin Wolf g_free(s->url); 825996922deSJeff Cody g_free(s->username); 826996922deSJeff Cody g_free(s->proxyusername); 827996922deSJeff Cody g_free(s->proxypassword); 8288e6d58cdSKevin Wolf qemu_opts_del(opts); 829769ce76dSAlexander Graf return -EINVAL; 830769ce76dSAlexander Graf } 831769ce76dSAlexander Graf 83228256d82SPaolo Bonzini static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) 833769ce76dSAlexander Graf { 834769ce76dSAlexander Graf CURLState *state; 835b69cdef8SMatthew Booth int running; 836769ce76dSAlexander Graf 8371919631eSPaolo Bonzini BDRVCURLState *s = bs->opaque; 838769ce76dSAlexander Graf 8392125e5eaSPaolo Bonzini uint64_t start = acb->offset; 8402125e5eaSPaolo Bonzini uint64_t end; 841769ce76dSAlexander Graf 842ba3186c4SPaolo Bonzini qemu_mutex_lock(&s->mutex); 8431919631eSPaolo Bonzini 844769ce76dSAlexander Graf // In case we have the requested data already (e.g. read-ahead), 845769ce76dSAlexander Graf // we can just call the callback and be done. 84628256d82SPaolo Bonzini if (curl_find_buf(s, start, acb->bytes, acb)) { 84734db05e7SPaolo Bonzini goto out; 848769ce76dSAlexander Graf } 849769ce76dSAlexander Graf 850769ce76dSAlexander Graf // No cache found, so let's start a new request 8513ce6a729SPaolo Bonzini for (;;) { 8523ce6a729SPaolo Bonzini state = curl_find_state(s); 8533ce6a729SPaolo Bonzini if (state) { 8543ce6a729SPaolo Bonzini break; 8553ce6a729SPaolo Bonzini } 856709f2132SPaolo Bonzini qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); 8573ce6a729SPaolo Bonzini } 8583ce6a729SPaolo Bonzini 8593ce6a729SPaolo Bonzini if (curl_init_state(s, state) < 0) { 8603ce6a729SPaolo Bonzini curl_clean_state(state); 86128256d82SPaolo Bonzini acb->ret = -EIO; 8621919631eSPaolo Bonzini goto out; 863363c3c85SNick Thomas } 864769ce76dSAlexander Graf 865769ce76dSAlexander Graf acb->start = 0; 8662125e5eaSPaolo Bonzini acb->end = MIN(acb->bytes, s->len - start); 867769ce76dSAlexander Graf 868769ce76dSAlexander Graf state->buf_off = 0; 8697267c094SAnthony Liguori g_free(state->orig_buf); 870769ce76dSAlexander Graf state->buf_start = start; 8714e504535SMax Reitz state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); 8724e504535SMax Reitz end = start + state->buf_len - 1; 8738dc7a772SKevin Wolf state->orig_buf = g_try_malloc(state->buf_len); 8748dc7a772SKevin Wolf if (state->buf_len && state->orig_buf == NULL) { 8758dc7a772SKevin Wolf curl_clean_state(state); 87628256d82SPaolo Bonzini acb->ret = -ENOMEM; 8771919631eSPaolo Bonzini goto out; 8788dc7a772SKevin Wolf } 879769ce76dSAlexander Graf state->acb[0] = acb; 880769ce76dSAlexander Graf 8812125e5eaSPaolo Bonzini snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); 882ed2a66deSLaurent Vivier trace_curl_setup_preadv(acb->bytes, start, state->range); 883769ce76dSAlexander Graf curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range); 884769ce76dSAlexander Graf 885769ce76dSAlexander Graf curl_multi_add_handle(s->multi, state->curl); 886769ce76dSAlexander Graf 887b69cdef8SMatthew Booth /* Tell curl it needs to kick things off */ 888b69cdef8SMatthew Booth curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); 8891919631eSPaolo Bonzini 8901919631eSPaolo Bonzini out: 891ba3186c4SPaolo Bonzini qemu_mutex_unlock(&s->mutex); 892363c3c85SNick Thomas } 893363c3c85SNick Thomas 89428256d82SPaolo Bonzini static int coroutine_fn curl_co_preadv(BlockDriverState *bs, 89528256d82SPaolo Bonzini uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) 896363c3c85SNick Thomas { 89728256d82SPaolo Bonzini CURLAIOCB acb = { 89828256d82SPaolo Bonzini .co = qemu_coroutine_self(), 89928256d82SPaolo Bonzini .ret = -EINPROGRESS, 90028256d82SPaolo Bonzini .qiov = qiov, 90128256d82SPaolo Bonzini .offset = offset, 90228256d82SPaolo Bonzini .bytes = bytes 90328256d82SPaolo Bonzini }; 904363c3c85SNick Thomas 90528256d82SPaolo Bonzini curl_setup_preadv(bs, &acb); 90628256d82SPaolo Bonzini while (acb.ret == -EINPROGRESS) { 90728256d82SPaolo Bonzini qemu_coroutine_yield(); 90828256d82SPaolo Bonzini } 90928256d82SPaolo Bonzini return acb.ret; 910769ce76dSAlexander Graf } 911769ce76dSAlexander Graf 912769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs) 913769ce76dSAlexander Graf { 914769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 915769ce76dSAlexander Graf 916ed2a66deSLaurent Vivier trace_curl_close(); 91763f0f45fSStefan Hajnoczi curl_detach_aio_context(bs); 918ba3186c4SPaolo Bonzini qemu_mutex_destroy(&s->mutex); 919031fd1beSPeter Maydell 920a94f83d9SRichard W.M. Jones g_free(s->cookie); 92145724d6dSStefan Weil g_free(s->url); 922996922deSJeff Cody g_free(s->username); 923996922deSJeff Cody g_free(s->proxyusername); 924996922deSJeff Cody g_free(s->proxypassword); 925769ce76dSAlexander Graf } 926769ce76dSAlexander Graf 927769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs) 928769ce76dSAlexander Graf { 929769ce76dSAlexander Graf BDRVCURLState *s = bs->opaque; 930769ce76dSAlexander Graf return s->len; 931769ce76dSAlexander Graf } 932769ce76dSAlexander Graf 933937c007bSMax Reitz static void curl_refresh_filename(BlockDriverState *bs) 934937c007bSMax Reitz { 935937c007bSMax Reitz BDRVCURLState *s = bs->opaque; 936937c007bSMax Reitz 937937c007bSMax Reitz /* "readahead" and "timeout" do not change the guest-visible data, 938937c007bSMax Reitz * so ignore them */ 939937c007bSMax Reitz if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT || 940937c007bSMax Reitz s->cookie || s->username || s->password || s->proxyusername || 941937c007bSMax Reitz s->proxypassword) 942937c007bSMax Reitz { 943937c007bSMax Reitz return; 944937c007bSMax Reitz } 945937c007bSMax Reitz 946937c007bSMax Reitz pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url); 947937c007bSMax Reitz } 948937c007bSMax Reitz 949937c007bSMax Reitz 9502654267cSMax Reitz static const char *const curl_strong_runtime_opts[] = { 9512654267cSMax Reitz CURL_BLOCK_OPT_URL, 9522654267cSMax Reitz CURL_BLOCK_OPT_SSLVERIFY, 9532654267cSMax Reitz CURL_BLOCK_OPT_COOKIE, 9542654267cSMax Reitz CURL_BLOCK_OPT_COOKIE_SECRET, 9552654267cSMax Reitz CURL_BLOCK_OPT_USERNAME, 9562654267cSMax Reitz CURL_BLOCK_OPT_PASSWORD_SECRET, 9572654267cSMax Reitz CURL_BLOCK_OPT_PROXY_USERNAME, 9582654267cSMax Reitz CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, 9592654267cSMax Reitz 9602654267cSMax Reitz NULL 9612654267cSMax Reitz }; 9622654267cSMax Reitz 963769ce76dSAlexander Graf static BlockDriver bdrv_http = { 964769ce76dSAlexander Graf .format_name = "http", 965769ce76dSAlexander Graf .protocol_name = "http", 966769ce76dSAlexander Graf 967769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9688e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 96966f82ceeSKevin Wolf .bdrv_file_open = curl_open, 970769ce76dSAlexander Graf .bdrv_close = curl_close, 971769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 972769ce76dSAlexander Graf 97328256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 97463f0f45fSStefan Hajnoczi 97563f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 97663f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 9772654267cSMax Reitz 978937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 9792654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 980769ce76dSAlexander Graf }; 981769ce76dSAlexander Graf 982769ce76dSAlexander Graf static BlockDriver bdrv_https = { 983769ce76dSAlexander Graf .format_name = "https", 984769ce76dSAlexander Graf .protocol_name = "https", 985769ce76dSAlexander Graf 986769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 9878e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 98866f82ceeSKevin Wolf .bdrv_file_open = curl_open, 989769ce76dSAlexander Graf .bdrv_close = curl_close, 990769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 991769ce76dSAlexander Graf 99228256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 99363f0f45fSStefan Hajnoczi 99463f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 99563f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 9962654267cSMax Reitz 997937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 9982654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 999769ce76dSAlexander Graf }; 1000769ce76dSAlexander Graf 1001769ce76dSAlexander Graf static BlockDriver bdrv_ftp = { 1002769ce76dSAlexander Graf .format_name = "ftp", 1003769ce76dSAlexander Graf .protocol_name = "ftp", 1004769ce76dSAlexander Graf 1005769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10068e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 100766f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1008769ce76dSAlexander Graf .bdrv_close = curl_close, 1009769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1010769ce76dSAlexander Graf 101128256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 101263f0f45fSStefan Hajnoczi 101363f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 101463f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 10152654267cSMax Reitz 1016937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 10172654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 1018769ce76dSAlexander Graf }; 1019769ce76dSAlexander Graf 1020769ce76dSAlexander Graf static BlockDriver bdrv_ftps = { 1021769ce76dSAlexander Graf .format_name = "ftps", 1022769ce76dSAlexander Graf .protocol_name = "ftps", 1023769ce76dSAlexander Graf 1024769ce76dSAlexander Graf .instance_size = sizeof(BDRVCURLState), 10258e6d58cdSKevin Wolf .bdrv_parse_filename = curl_parse_filename, 102666f82ceeSKevin Wolf .bdrv_file_open = curl_open, 1027769ce76dSAlexander Graf .bdrv_close = curl_close, 1028769ce76dSAlexander Graf .bdrv_getlength = curl_getlength, 1029769ce76dSAlexander Graf 103028256d82SPaolo Bonzini .bdrv_co_preadv = curl_co_preadv, 103163f0f45fSStefan Hajnoczi 103263f0f45fSStefan Hajnoczi .bdrv_detach_aio_context = curl_detach_aio_context, 103363f0f45fSStefan Hajnoczi .bdrv_attach_aio_context = curl_attach_aio_context, 10342654267cSMax Reitz 1035937c007bSMax Reitz .bdrv_refresh_filename = curl_refresh_filename, 10362654267cSMax Reitz .strong_runtime_opts = curl_strong_runtime_opts, 1037769ce76dSAlexander Graf }; 1038769ce76dSAlexander Graf 1039769ce76dSAlexander Graf static void curl_block_init(void) 1040769ce76dSAlexander Graf { 1041769ce76dSAlexander Graf bdrv_register(&bdrv_http); 1042769ce76dSAlexander Graf bdrv_register(&bdrv_https); 1043769ce76dSAlexander Graf bdrv_register(&bdrv_ftp); 1044769ce76dSAlexander Graf bdrv_register(&bdrv_ftps); 1045769ce76dSAlexander Graf } 1046769ce76dSAlexander Graf 1047769ce76dSAlexander Graf block_init(curl_block_init); 1048