digest authentication dosen't work in libcurl 7.43.0 - c

I used the following code C to make digest authentication with my http server.
curl_easy_setopt(curl, CURLOPT_USERNAME, user_id);
curl_easy_setopt(curl, CURLOPT_PASSWORD, passwd);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST);
my http client (libcurl) recieve http 401 and do not proceed to the digest authentication.
it works for older version.
I also retried with the following command:
root#OpenWrt:~# curl --data {"red":"00"} --digest -u "admin:admin" "http://192.168.1.200:8
080/openacs/acs"
<html><head><title>JBossWeb/2.0.1.GA - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>This request requires HTTP authentication ().</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>root#OpenWrt:~#
and it dosen't work.
what i'm missing?

Related

Http get method for Splunk saved search using access token

What would be the correct HTTP get request call syntax to fetch saved search data from Splunk if we're accessing it through an access token?
My curl command is working but http.get is not.
curl command:
#os.system('curl -H "Authorization: Bearer <token>"
<baseurl>:8089/services/search/jobs/export --data search="savedsearch abc_backup_status" -d output_mode=csv')
request call ::::
BASE_URL = '<baseurl>:8089/services/search/jobs/export'
data = {"search":"savedsearch abc_backup_status"}
headers = {'Authorization': "Bearer <token>"}
auth_response = requests.get(BASE_URL, headers=headers, data = data, verify=False)
this is giving 400 errors.
The curl options -d OR --data imply a POST method by default.
From: https://man7.org/linux/man-pages/man1/curl.1.html
-d, --data <data>
(HTTP MQTT) Sends the specified data in a POST request to
the HTTP server, in the same way that a browser does when
a user has filled in an HTML form and presses the submit
button. This will cause curl to pass the data to the
server using the content-type application/x-www-form-
urlencoded. Compare to -F, --form.
It is interesting that Splunk Docs claim that search/jobs/export takes a GET, but you're creating a job to immediately export, which feels like a POST type of operation.
Also I notice that your search starts with the savedsearch command, if that's a regularly scheduled savedsearch, you may want to GET saved/searches/{name}/history to get the last execution SID, followed either by the results or events endpoint of that already executed job, instead of a new search.... but that's a use case question

Cannot set http POST headers in desired format (using libcurl)

I am working on a REST client which uses libcurl to send HTTP POST Messages.
After receiving HTTP POST packet, My REST server needs to take some action, but it is unable to do that because some headers differ from the expected format.
Required HTTP Packet Format:
Hypertext Transfer Protocol
> Accept: text/html
> Content-Type: text/plain
Line-based text data: text/plain
>test string
Current HTTP Packet Format:
Hypertext Transfer Protocol
> Accept: */*
> Content-Type: application/x-www-form-urlencoded
Line-based text data: application/x-www-form-urlencoded
>test string
I have tried the following: (Reference: http://curl.haxx.se/libcurl/c/httpcustomheader.html)
curl = curl_easy_init();
if(curl)
{
struct curl_slist *chunk = NULL;
/* Add a custom header */
chunk = curl_slist_append(chunk, "Accept: text/html");
chunk = curl_slist_append(chunk, "Content-Type: text/plain");
/* set our custom set of headers */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
res = curl_easy_perform(curl);
But this code change has NO EFFECT on the outgoing HTTP POST packet.
Can anyone please suggest me what libcurl options can help me achieve the desired format?
Thanks

Cant get response error text in Curl request

I'm able to get HTTP response code like this:
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &ResponseCode);
But how can I get response error text? I thought that CURLOPT_ERRORBUFFER could help me:
char error_buf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
But it seems empty even if ResponseCode=500 and request status text returns (I'm sure about this because JQuery ajax request shows it). So how to do this?
Solved it:
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); //<= this is important, but not obvious
char error_buf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
It turns out, that despite HTTP response code 500 CURLcode was CURLE_OK - this is default behavior, and that is why was no error message in error_buf. CURLOPT_FAILONERROR forces Curl to convert all response codes >= 300 to errors.
Return value of curl_easy_perform can be passed to curl_easy_strerror to get a text represent of the error. It may not be the 'response error' you wanted, but helps to get the reason why the request end with code like 500.

How to post http request using digest authentication with libcurl

I'm trying to perform a post request and I'm trying to do it with the digest authentication. with libcurl, I set the options:
curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_easy_setopt(curl_handle, CURLOPT_USERPWD, "username:password");
before setting all the other option (post, url and everything). The server closes my connection and I think that no digest is made. I just don't know how to automatically obtain the challenge-response behaviour of the digest. If I set HTTPAUTH to CURLAUTH_BASIC it encodes the stuff, I see with the VERBOSE option the header containing authorization = basic. With digest no headers.
Do you know how can I do it, or can you give me some example? I really searched everywhere.
For a basic POST request you should do:
curl_easy_setopt(hnd, CURLOPT_USERPWD, "user:pwd");
curl_easy_setopt(hnd, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
For a multipart POST (a.k.a multipart/form-data):
struct curl_httppost *post;
struct curl_httppost *postend;
/* setup your POST body with `curl_formadd(&post, &postend, ...)` */
curl_easy_setopt(hnd, CURLOPT_USERPWD, "user:pwd");
curl_easy_setopt(hnd, CURLOPT_HTTPPOST, post);
curl_easy_setopt(hnd, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
Pro-tip: use curl command-line tool with --libcurl request.c: it outputs into this C file the list of options used to perform the corresponding request.

How I can detect the digest authentication failure with libcurl?

When we send a http message with:
res= curl_easy_perform(curl);
How to detect the authentication failure from libcur in case that the http message was sent with wrong login or password?
I checked the value of res for both success and failure and I found it = to 0
curl_easy_perform will return 0 if the request went through successfully.
Bad authentication typically results in a 401 Unauthorized HTTP response code. However, cURL doesn't count that as a request error. An example of request error is CURLE_URL_MALFORMAT, which would be returned if your request URL was incorrectly formatted, which would mean the request didn't even hit the remote server.
Some may advise you to use CURLOPT_FAILONERROR to make curl_easy_perform fail if the response code is greater or equal to 400. However, the libcurl documentation expressly warns you about that:
This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407).
The proper way to check for authentication errors would be to use curl_easy_getinfo to fetch the HTTP response code, e.g.:
CURLcode res = curl_easy_perform(curl);
long httpCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);

Resources