From: Marc Fournier Date: Tue, 24 Feb 2015 20:46:53 +0000 (+0100) Subject: avoid using CURLOPT_USERPWD when possible X-Git-Tag: collectd-5.5.0~64^2 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=38e15f5809846c009792ef9aeee800efbf3aa21e;p=collectd.git avoid using CURLOPT_USERPWD when possible CURLOPT_USERPWD chokes on colons inside usernames or passwords, so use CURLOPT_USERNAME and CURLOPT_PASSWORD if curl 7.19.1 or newer is found. Follow-up to 0af75dc13 for the rest of the plugins based on libcurl. --- diff --git a/src/apache.c b/src/apache.c index 75ef3e1b..1099248a 100644 --- a/src/apache.c +++ b/src/apache.c @@ -283,8 +283,6 @@ static int config (oconfig_item_t *ci) /* initialize curl for each host */ static int init_host (apache_t *st) /* {{{ */ { - static char credentials[1024]; - assert (st->url != NULL); /* (Assured by `config_add') */ @@ -334,6 +332,12 @@ static int init_host (apache_t *st) /* {{{ */ if (st->user != NULL) { +#ifdef HAVE_CURLOPT_USERNAME + curl_easy_setopt (st->curl, CURLOPT_USERNAME, st->user); + curl_easy_setopt (st->curl, CURLOPT_PASSWORD, + (st->pass == NULL) ? "" : st->pass); +#else + static char credentials[1024]; int status; status = ssnprintf (credentials, sizeof (credentials), "%s:%s", @@ -349,6 +353,7 @@ static int init_host (apache_t *st) /* {{{ */ } curl_easy_setopt (st->curl, CURLOPT_USERPWD, credentials); +#endif } curl_easy_setopt (st->curl, CURLOPT_URL, st->url); diff --git a/src/ascent.c b/src/ascent.c index ca0fac7f..e9d25bd5 100644 --- a/src/ascent.c +++ b/src/ascent.c @@ -524,8 +524,6 @@ static int ascent_config (const char *key, const char *value) /* {{{ */ static int ascent_init (void) /* {{{ */ { - static char credentials[1024]; - if (url == NULL) { WARNING ("ascent plugin: ascent_init: No URL configured, " @@ -551,6 +549,11 @@ static int ascent_init (void) /* {{{ */ if (user != NULL) { +#ifdef HAVE_CURLOPT_USERNAME + curl_easy_setopt (curl, CURLOPT_USERNAME, user); + curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass); +#else + static char credentials[1024]; int status; status = ssnprintf (credentials, sizeof (credentials), "%s:%s", @@ -563,6 +566,7 @@ static int ascent_init (void) /* {{{ */ } curl_easy_setopt (curl, CURLOPT_USERPWD, credentials); +#endif } curl_easy_setopt (curl, CURLOPT_URL, url); diff --git a/src/nginx.c b/src/nginx.c index e8282f23..b0daa05b 100644 --- a/src/nginx.c +++ b/src/nginx.c @@ -113,8 +113,6 @@ static int config (const char *key, const char *value) static int init (void) { - static char credentials[1024]; - if (curl != NULL) curl_easy_cleanup (curl); @@ -131,6 +129,11 @@ static int init (void) if (user != NULL) { +#ifdef HAVE_CURLOPT_USERNAME + curl_easy_setopt (curl, CURLOPT_USERNAME, user); + curl_easy_setopt (curl, CURLOPT_PASSWORD, (pass == NULL) ? "" : pass); +#else + static char credentials[1024]; int status = ssnprintf (credentials, sizeof (credentials), "%s:%s", user, pass == NULL ? "" : pass); if ((status < 0) || ((size_t) status >= sizeof (credentials))) @@ -140,6 +143,7 @@ static int init (void) } curl_easy_setopt (curl, CURLOPT_USERPWD, credentials); +#endif } if (url != NULL) diff --git a/src/write_http.c b/src/write_http.c index 9d8f30c2..8d3b85b3 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -140,6 +140,11 @@ static int wh_callback_init (wh_callback_t *cb) /* {{{ */ if (cb->user != NULL) { +#ifdef HAVE_CURLOPT_USERNAME + curl_easy_setopt (cb->curl, CURLOPT_USERNAME, cb->user); + curl_easy_setopt (cb->curl, CURLOPT_PASSWORD, + (cb->pass == NULL) ? "" : cb->pass); +#else size_t credentials_size; credentials_size = strlen (cb->user) + 2; @@ -156,6 +161,7 @@ static int wh_callback_init (wh_callback_t *cb) /* {{{ */ ssnprintf (cb->credentials, credentials_size, "%s:%s", cb->user, (cb->pass == NULL) ? "" : cb->pass); curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials); +#endif curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); }