Browse Source

Support authentication for fetching a remote share.

Allow use of HTTP authentication not only when uploading a share to a
remote server but also when downloading.
master
Damien Goutte-Gattat 2 months ago
parent
commit
228cdf24a1
  1. 27
      src/scheme-libcurl.c

27
src/scheme-libcurl.c

@ -32,6 +32,17 @@
#include <curl/curl.h>
static int
check_authentication(CURL *curl)
{
long http_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
return http_code == 401 ? 1 : 0;
}
struct getfile_cb_data {
unsigned char *buffer;
size_t written;
@ -79,6 +90,8 @@ gfsec_scheme_libcurl_get_file(gfsec_scheme_t scheme,
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_REQUIRED);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
if ( curl_easy_setopt(curl, CURLOPT_URL, url) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
@ -86,6 +99,9 @@ gfsec_scheme_libcurl_get_file(gfsec_scheme_t scheme,
if ( ret == 0 && curl_easy_perform(curl) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
if ( ret == 0 && check_authentication(curl) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
if ( ret == 0 ) {
*buffer = data.buffer;
*len = data.written;
@ -155,6 +171,7 @@ gfsec_scheme_libcurl_put_file(gfsec_scheme_t scheme,
curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dummy_write_cb);
curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_REQUIRED);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
if ( curl_easy_setopt(curl, CURLOPT_URL, url) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
@ -165,14 +182,8 @@ gfsec_scheme_libcurl_put_file(gfsec_scheme_t scheme,
if ( ret == 0 && curl_easy_perform(curl) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
/* Check for authentication error. */
if ( ret == 0 ) {
long http_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if ( http_code == 401 )
ret = GFSEC_SCHEME_STATUS_ERROR;
}
if ( ret == 0 && check_authentication(curl) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
curl_easy_cleanup(curl);
free(url);

Loading…
Cancel
Save