Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <curl/curl.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- char *gPromptUsername = 0;
- char *gPromptPassword = 0;
- CURL *gPausedHandle = 0;
- curl_auth_type gPausedType = CURLAUTH_TYPE_NONE;
- void loginPrompt(int sig)
- {
- if (gPromptUsername) {
- free(gPromptUsername);
- gPromptUsername = NULL;
- }
- if (gPromptPassword) {
- free(gPromptPassword);
- gPromptPassword = NULL;
- }
- printf("Username for %s: ", gPausedType == CURLAUTH_TYPE_HTTP ? "host" : "proxy");
- size_t bufSize;
- ssize_t len = getline(&gPromptUsername, &bufSize, stdin);
- // Delete trailing newline
- char* c;
- for (c = gPromptUsername + len - 1; c > gPromptUsername && *c == '\n'; c--)
- *c = '\0';
- printf("Password: ");
- len = getline(&gPromptPassword, &bufSize, stdin);
- // Delete trailing newline
- for (c = gPromptPassword + len - 1; c > gPromptPassword && *c == '\n'; c--)
- *c = '\0';
- curl_easy_resume_auth(gPausedHandle, gPromptUsername, gPromptPassword);
- }
- curl_auth_result authCallback(
- curl_auth_type type,
- curl_auth_scheme scheme,
- const char *realm,
- unsigned retryCount,
- const char **usernamePtr,
- const char **passwordPtr,
- void *userdata)
- {
- CURL *handle = (CURL *) userdata;
- const char *schemeStr;
- switch (scheme) {
- case CURLAUTH_BASIC: schemeStr = "Basic"; break;
- case CURLAUTH_DIGEST: schemeStr = "Digest"; break;
- case CURLAUTH_NTLM: schemeStr = "NTLM"; break;
- default: schemeStr = "unknown"; break;
- }
- printf("Got %s auth: scheme %s (%d), realm %s, retries %d, last username %s, last password %s\n",
- type == CURLAUTH_TYPE_HTTP ? "HTTP" : "proxy",
- schemeStr,
- scheme,
- realm ? realm : "(null)",
- retryCount,
- *usernamePtr ? *usernamePtr : "(null)",
- *passwordPtr ? *passwordPtr : "(null)");
- if (retryCount > 3) {
- // give up after 3 tries
- return CURLAUTH_RESULT_CANCEL;
- } else if (!*usernamePtr && !*passwordPtr) {
- // on the first attempt, try an autogenerated password based on the current timestamp
- char password[1024];
- snprintf(password, 1024, "timestamp%d", time(NULL));
- *usernamePtr = "guest";
- *passwordPtr = password;;
- return CURLAUTH_RESULT_CONTINUE;
- } else {
- // prompt the user for the username and password
- // normally this would happen in a dedicated UI thread - simulate this with an alarm)
- gPausedHandle = handle;
- gPausedType = type;
- signal(SIGALRM, loginPrompt);
- alarm(1);
- return CURLAUTH_RESULT_PAUSE;
- }
- }
- int main(int argc, char** argv)
- {
- if (argc < 2 || argc > 3) {
- fprintf(stderr, "Usage: curlauthtest url [proxy-address]\n");
- exit(1);
- }
- const char *url = argv[1];
- const char *proxyAddress = argc > 2 ? argv[2] : 0;
- curl_global_init(CURL_GLOBAL_ALL);
- CURL *handle = curl_easy_init();
- curl_easy_setopt(handle, CURLOPT_URL, url);
- curl_easy_setopt(handle, CURLOPT_HTTP_AUTH_FUNCTION, authCallback);
- curl_easy_setopt(handle, CURLOPT_HTTP_AUTH_DATA, handle);
- curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- if (proxyAddress) {
- curl_easy_setopt(handle, CURLOPT_PROXY, proxyAddress);
- curl_easy_setopt(handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- curl_easy_setopt(handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
- }
- curl_easy_perform(handle);
- curl_easy_cleanup(handle);
- curl_global_cleanup();
- if (gPromptUsername) {
- free(gPromptUsername);
- }
- if (gPromptPassword) {
- free(gPromptPassword);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement