dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 1c543722b2b1c55b06c3cc02ace987fc68bc26d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 28 Feb 2017 13:32:31 +0100
Subject: [PATCH 78/90] tcurl: add support for http basic auth

Reviewed-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
(cherry picked from commit c2ea75da72b426d98ba489039e220d417bfb4c2a)
---
 src/tests/tcurl_test_tool.c | 14 ++++++++++++++
 src/util/tev_curl.c         | 24 ++++++++++++++++++++++++
 src/util/tev_curl.h         | 15 +++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/src/tests/tcurl_test_tool.c b/src/tests/tcurl_test_tool.c
index 9cec000fbf2e4eca2fdc5213c8b3b4cb10f1df1b..4ceef8e06040ea0abd4d112a5b7845f436c69488 100644
--- a/src/tests/tcurl_test_tool.c
+++ b/src/tests/tcurl_test_tool.c
@@ -45,6 +45,9 @@ struct tool_options {
     const char *socket_path;
     const char *capath;
     const char *cacert;
+
+    const char *username;
+    const char *password;
 };
 
 static void request_done(struct tevent_req *req)
@@ -194,6 +197,14 @@ prepare_requests(TALLOC_CTX *mem_ctx,
             }
         }
 
+        if (opts->username != NULL && opts->password != NULL) {
+            ret = tcurl_req_http_basic_auth(requests[i], opts->username,
+                                            opts->password);
+            if (ret != EOK) {
+                goto done;
+            }
+        }
+
         i++;
     }
 
@@ -299,6 +310,9 @@ int main(int argc, const char *argv[])
         { "verify-host", '\0', POPT_ARG_NONE, &opts.verify_host, '\0', "Verify host when TLS is enabled", NULL },
         { "capath", '\0', POPT_ARG_STRING, &opts.capath, '\0', "Path to CA directory where peer certificate is stored", NULL },
         { "cacert", '\0', POPT_ARG_STRING, &opts.cacert, '\0', "Path to CA certificate", NULL },
+        /* BASIC AUTH */
+        { "username", '\0', POPT_ARG_STRING, &opts.username, '\0', "Username for basic authentication", NULL },
+        { "password", '\0', POPT_ARG_STRING, &opts.password, '\0', "Password for basic authentication", NULL },
         POPT_TABLEEND
     };
 
diff --git a/src/util/tev_curl.c b/src/util/tev_curl.c
index c155f4c038d4215933ee30d41c694ad4a14ae132..8faf07c714b636a0351be365597de68d2f68a1be 100644
--- a/src/util/tev_curl.c
+++ b/src/util/tev_curl.c
@@ -1092,3 +1092,27 @@ errno_t tcurl_req_set_client_cert(struct tcurl_request *tcurl_req,
 
     return EOK;
 }
+
+errno_t tcurl_req_http_basic_auth(struct tcurl_request *tcurl_req,
+                                  const char *username,
+                                  const char *password)
+{
+    errno_t ret;
+
+    ret = tcurl_set_option(tcurl_req, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+    if (ret != EOK) {
+        return ret;
+    }
+
+    ret = tcurl_set_option(tcurl_req, CURLOPT_USERNAME, username);
+    if (ret != EOK) {
+        return ret;
+    }
+
+    ret = tcurl_set_option(tcurl_req, CURLOPT_PASSWORD, password);
+    if (ret != EOK) {
+        return ret;
+    }
+
+    return EOK;
+}
diff --git a/src/util/tev_curl.h b/src/util/tev_curl.h
index 933abcb9b531412737e8fcf391644d828b125cf8..c733127b3686b5665f53cf53ea72674e0d7af64e 100644
--- a/src/util/tev_curl.h
+++ b/src/util/tev_curl.h
@@ -243,4 +243,19 @@ errno_t tcurl_req_set_client_cert(struct tcurl_request *tcurl_req,
                                   const char *cert,
                                   const char *key);
 
+/**
+ * @brief Force HTTP basic authentication with @username and @password.
+ *
+ * @param[in]  tcurl_request
+ * @param[in]  username
+ * @param[in]  password
+ *
+ * @returns errno code
+ *
+ * @see tcurl_http
+ */
+errno_t tcurl_req_http_basic_auth(struct tcurl_request *tcurl_req,
+                                  const char *username,
+                                  const char *password);
+
 #endif /* __TEV_CURL_H */
-- 
2.9.3