Advertisement
JoeNotCharles

curl auth proposal example

May 3rd, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.81 KB | None | 0 0
  1. #include <curl/curl.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. char *gPromptUsername = 0;
  8. char *gPromptPassword = 0;
  9. CURL *gPausedHandle = 0;
  10. curl_auth_type gPausedType = CURLAUTH_TYPE_NONE;
  11.  
  12. void loginPrompt(int sig)
  13. {
  14.     if (gPromptUsername) {
  15.         free(gPromptUsername);
  16.         gPromptUsername = NULL;
  17.     }
  18.     if (gPromptPassword) {
  19.         free(gPromptPassword);
  20.         gPromptPassword = NULL;
  21.     }
  22.  
  23.     printf("Username for %s: ", gPausedType == CURLAUTH_TYPE_HTTP ? "host" : "proxy");
  24.     size_t bufSize;
  25.     ssize_t len = getline(&gPromptUsername, &bufSize, stdin);
  26.  
  27.     // Delete trailing newline
  28.     char* c;
  29.     for (c = gPromptUsername + len - 1; c > gPromptUsername && *c == '\n'; c--)
  30.         *c = '\0';
  31.  
  32.     printf("Password: ");
  33.     len = getline(&gPromptPassword, &bufSize, stdin);
  34.  
  35.     // Delete trailing newline
  36.     for (c = gPromptPassword + len - 1; c > gPromptPassword && *c == '\n'; c--)
  37.         *c = '\0';
  38.  
  39.     curl_easy_resume_auth(gPausedHandle, gPromptUsername, gPromptPassword);
  40. }
  41.  
  42. curl_auth_result authCallback(
  43.         curl_auth_type type,
  44.         curl_auth_scheme scheme,
  45.         const char *realm,
  46.         unsigned retryCount,
  47.         const char **usernamePtr,
  48.         const char **passwordPtr,
  49.         void *userdata)
  50. {
  51.     CURL *handle = (CURL *) userdata;
  52.  
  53.     const char *schemeStr;
  54.     switch (scheme) {
  55.         case CURLAUTH_BASIC: schemeStr = "Basic"; break;
  56.         case CURLAUTH_DIGEST: schemeStr = "Digest"; break;
  57.         case CURLAUTH_NTLM: schemeStr = "NTLM"; break;
  58.         default: schemeStr = "unknown"; break;
  59.     }
  60.  
  61.     printf("Got %s auth: scheme %s (%d), realm %s, retries %d, last username %s, last password %s\n",
  62.             type == CURLAUTH_TYPE_HTTP ? "HTTP" : "proxy",
  63.             schemeStr,
  64.             scheme,
  65.             realm ? realm : "(null)",
  66.             retryCount,
  67.             *usernamePtr ? *usernamePtr : "(null)",
  68.             *passwordPtr ? *passwordPtr : "(null)");
  69.    
  70.     if (retryCount > 3) {
  71.         // give up after 3 tries
  72.         return CURLAUTH_RESULT_CANCEL;
  73.     } else if (!*usernamePtr && !*passwordPtr) {
  74.         // on the first attempt, try an autogenerated password based on the current timestamp
  75.         char password[1024];
  76.         snprintf(password, 1024, "timestamp%d", time(NULL));
  77.         *usernamePtr = "guest";
  78.         *passwordPtr = password;;
  79.         return CURLAUTH_RESULT_CONTINUE;
  80.     } else {
  81.         // prompt the user for the username and password
  82.         // normally this would happen in a dedicated UI thread - simulate this with an alarm)
  83.         gPausedHandle = handle;
  84.         gPausedType = type;
  85.         signal(SIGALRM, loginPrompt);
  86.         alarm(1);
  87.         return CURLAUTH_RESULT_PAUSE;
  88.     }
  89. }
  90.  
  91. int main(int argc, char** argv)
  92. {
  93.     if (argc < 2 || argc > 3) {
  94.         fprintf(stderr, "Usage: curlauthtest url [proxy-address]\n");
  95.         exit(1);
  96.     }
  97.  
  98.     const char *url = argv[1];
  99.     const char *proxyAddress = argc > 2 ? argv[2] : 0;
  100.  
  101.     curl_global_init(CURL_GLOBAL_ALL);
  102.     CURL *handle = curl_easy_init();
  103.  
  104.     curl_easy_setopt(handle, CURLOPT_URL, url);
  105.     curl_easy_setopt(handle, CURLOPT_HTTP_AUTH_FUNCTION, authCallback);
  106.     curl_easy_setopt(handle, CURLOPT_HTTP_AUTH_DATA, handle);
  107.     curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  108.     if (proxyAddress) {
  109.         curl_easy_setopt(handle, CURLOPT_PROXY, proxyAddress);
  110.         curl_easy_setopt(handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  111.         curl_easy_setopt(handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
  112.     }
  113.    
  114.     curl_easy_perform(handle);
  115.  
  116.     curl_easy_cleanup(handle);
  117.     curl_global_cleanup();
  118.  
  119.     if (gPromptUsername) {
  120.         free(gPromptUsername);
  121.     }
  122.     if (gPromptPassword) {
  123.         free(gPromptPassword);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement