9ae3a8
From 1eeb050a2cc6c582c390614080fbeaea019b2dac Mon Sep 17 00:00:00 2001
9ae3a8
From: Richard Jones <rjones@redhat.com>
9ae3a8
Date: Thu, 11 Jun 2015 11:40:26 +0200
9ae3a8
Subject: [PATCH 26/30] curl: Allow a cookie or cookies to be sent with
9ae3a8
 http/https requests.
9ae3a8
9ae3a8
Message-id: <1434022828-13037-20-git-send-email-rjones@redhat.com>
9ae3a8
Patchwork-id: 65854
9ae3a8
O-Subject: [RHEL-7.2 qemu-kvm v3 PATCH 19/21] curl: Allow a cookie or cookies to be sent with http/https requests.
9ae3a8
Bugzilla: 1226684
9ae3a8
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
9ae3a8
From: "Richard W.M. Jones" <rjones@redhat.com>
9ae3a8
9ae3a8
In order to access VMware ESX efficiently, we need to send a session
9ae3a8
cookie.  This patch is very simple and just allows you to send that
9ae3a8
session cookie.  It punts on the question of how you get the session
9ae3a8
cookie in the first place, but in practice you can just run a `curl'
9ae3a8
command against the server and extract the cookie that way.
9ae3a8
9ae3a8
To use it, add file.cookie to the curl URL.  For example:
9ae3a8
9ae3a8
$ qemu-img info 'json: {
9ae3a8
    "file.driver":"https",
9ae3a8
    "file.url":"https://vcenter/folder/Windows%202003/Windows%202003-flat.vmdk?dcPath=Datacenter&dsName=datastore1",
9ae3a8
    "file.sslverify":"off",
9ae3a8
    "file.cookie":"vmware_soap_session=\"52a01262-bf93-ccce-d379-8dabb3e55560\""}'
9ae3a8
image: [...]
9ae3a8
file format: raw
9ae3a8
virtual size: 8.0G (8589934592 bytes)
9ae3a8
disk size: unavailable
9ae3a8
9ae3a8
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
Upstream-status: a94f83d94fdf907680f068f1be7ad13d1f697067
9ae3a8
9ae3a8
Note this intentionally omits the documentation changes in
9ae3a8
'qemu-options.hx' from the upstream patch.
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/curl.c | 16 ++++++++++++++++
9ae3a8
 1 file changed, 16 insertions(+)
9ae3a8
9ae3a8
diff --git a/block/curl.c b/block/curl.c
9ae3a8
index 1f1df4f..ca881ee 100644
9ae3a8
--- a/block/curl.c
9ae3a8
+++ b/block/curl.c
9ae3a8
@@ -73,6 +73,7 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
9ae3a8
 #define CURL_BLOCK_OPT_READAHEAD "readahead"
9ae3a8
 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
9ae3a8
 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
9ae3a8
+#define CURL_BLOCK_OPT_COOKIE    "cookie"
9ae3a8
 
9ae3a8
 struct BDRVCURLState;
9ae3a8
 
9ae3a8
@@ -112,6 +113,7 @@ typedef struct BDRVCURLState {
9ae3a8
     size_t readahead_size;
9ae3a8
     bool sslverify;
9ae3a8
     int timeout;
9ae3a8
+    char *cookie;
9ae3a8
     bool accept_range;
9ae3a8
 } BDRVCURLState;
9ae3a8
 
9ae3a8
@@ -382,6 +384,9 @@ static CURLState *curl_init_state(BDRVCURLState *s)
9ae3a8
         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
9ae3a8
         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
9ae3a8
                          (long) s->sslverify);
9ae3a8
+        if (s->cookie) {
9ae3a8
+            curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
9ae3a8
+        }
9ae3a8
         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, s->timeout);
9ae3a8
         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
9ae3a8
                          (void *)curl_read_cb);
9ae3a8
@@ -451,6 +456,11 @@ static QemuOptsList runtime_opts = {
9ae3a8
             .type = QEMU_OPT_NUMBER,
9ae3a8
             .help = "Curl timeout"
9ae3a8
         },
9ae3a8
+        {
9ae3a8
+            .name = CURL_BLOCK_OPT_COOKIE,
9ae3a8
+            .type = QEMU_OPT_STRING,
9ae3a8
+            .help = "Pass the cookie or list of cookies with each request"
9ae3a8
+        },
9ae3a8
         { /* end of list */ }
9ae3a8
     },
9ae3a8
 };
9ae3a8
@@ -463,6 +473,7 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
     QemuOpts *opts;
9ae3a8
     Error *local_err = NULL;
9ae3a8
     const char *file;
9ae3a8
+    const char *cookie;
9ae3a8
     double d;
9ae3a8
 
9ae3a8
     static int inited = 0;
9ae3a8
@@ -488,6 +499,9 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
 
9ae3a8
     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
9ae3a8
 
9ae3a8
+    cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
9ae3a8
+    s->cookie = g_strdup(cookie);
9ae3a8
+
9ae3a8
     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
9ae3a8
     if (file == NULL) {
9ae3a8
         qerror_report(ERROR_CLASS_GENERIC_ERROR, "curl block driver requires "
9ae3a8
@@ -556,6 +570,7 @@ out:
9ae3a8
     curl_easy_cleanup(state->curl);
9ae3a8
     state->curl = NULL;
9ae3a8
 out_noclean:
9ae3a8
+    g_free(s->cookie);
9ae3a8
     g_free(s->url);
9ae3a8
     qemu_opts_del(opts);
9ae3a8
     return -EINVAL;
9ae3a8
@@ -696,6 +711,7 @@ static void curl_close(BlockDriverState *bs)
9ae3a8
     qemu_del_timer(s->timer);
9ae3a8
     qemu_free_timer(s->timer);
9ae3a8
 
9ae3a8
+    g_free(s->cookie);
9ae3a8
     g_free(s->url);
9ae3a8
 }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8