occurlfunctions.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #include "ocinternal.h"
  5. #include "ocdebug.h"
  6. #include "ocdata.h"
  7. #include "occontent.h"
  8. #include "ocrc.h"
  9. /* Condition on libcurl version */
  10. /* Set up an alias as needed */
  11. #ifndef HAVE_CURLOPT_KEYPASSWD
  12. #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
  13. #endif
  14. static char* combinecredentials(const char* user, const char* pwd);
  15. /* Set various general curl flags */
  16. int
  17. ocset_curl_flags(OCstate* state)
  18. {
  19. CURLcode cstat = CURLE_OK;
  20. CURL* curl = state->curl;
  21. struct OCcurlflags* flags = &state->curlflags;
  22. #ifdef CURLOPT_ENCODING
  23. if (flags->compress) {
  24. cstat = curl_easy_setopt(curl, CURLOPT_ENCODING,"deflate, gzip");
  25. if(cstat != CURLE_OK) goto done;
  26. OCDBG(1,"CURLOPT_ENCODING=deflate, gzip");
  27. }
  28. #endif
  29. if (flags->cookiejar || flags->cookiefile) {
  30. cstat = curl_easy_setopt(curl, CURLOPT_COOKIESESSION, 1);
  31. if (cstat != CURLE_OK) goto done;
  32. OCDBG(1,"CURLOPT_COOKIESESSION=1");
  33. }
  34. if (flags->cookiejar) {
  35. cstat = curl_easy_setopt(curl, CURLOPT_COOKIEJAR, flags->cookiejar);
  36. if (cstat != CURLE_OK) goto done;
  37. OCDBG1(1,"CURLOPT_COOKIEJAR=%s",flags->cookiejar);
  38. }
  39. if (flags->cookiefile) {
  40. cstat = curl_easy_setopt(curl, CURLOPT_COOKIEFILE, flags->cookiefile);
  41. if (cstat != CURLE_OK) goto done;
  42. OCDBG1(1,"CURLOPT_COOKIEFILE=%s",flags->cookiefile);
  43. }
  44. if (flags->verbose) {
  45. cstat = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  46. if (cstat != CURLE_OK) goto done;
  47. OCDBG1(1,"CURLOPT_VERBOSE=%ld",1L);
  48. }
  49. if (flags->timeout) {
  50. cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)flags->timeout);
  51. if (cstat != CURLE_OK) goto done;
  52. OCDBG1(1,"CURLOPT_TIMEOUT=%ld",1L);
  53. }
  54. /* Following are always set */
  55. cstat = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  56. OCDBG1(1,"CURLOPT_FOLLOWLOCATION=%ld",1L);
  57. cstat = curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L);
  58. OCDBG1(1,"CURLOPT_FOLLOWLOCATION=%ld",1L);
  59. cstat = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, state->error.curlerrorbuf);
  60. OCDBG1(1,"CURLOPT_ERRORBUFFER",0);
  61. done:
  62. return cstat;
  63. }
  64. int
  65. ocset_proxy(OCstate* state)
  66. {
  67. CURLcode cstat;
  68. CURL* curl = state->curl;
  69. struct OCproxy *proxy = &state->proxy;
  70. struct OCcredentials *creds = &state->creds;
  71. cstat = curl_easy_setopt(curl, CURLOPT_PROXY, proxy->host);
  72. if (cstat != CURLE_OK) return OC_ECURL;
  73. OCDBG1(1,"CURLOPT_PROXY=%s",proxy->host);
  74. cstat = curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxy->port);
  75. if (cstat != CURLE_OK) return OC_ECURL;
  76. OCDBG1(1,"CURLOPT_PROXYPORT=%d",proxy->port);
  77. if (creds->username) {
  78. char *combined = combinecredentials(creds->username,creds->password);
  79. if (!combined) return OC_ENOMEM;
  80. cstat = curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, combined);
  81. if (cstat != CURLE_OK) return OC_ECURL;
  82. OCDBG1(1,"CURLOPT_PROXYUSERPWD=%s",combined);
  83. #ifdef CURLOPT_PROXYAUTH
  84. cstat = curl_easy_setopt(curl, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
  85. if(cstat != CURLE_OK) goto fail;
  86. OCDBG1(1,"CURLOPT_PROXYAUTH=%ld",(long)CURLAUTH_ANY);
  87. #endif
  88. free(combined);
  89. }
  90. return OC_NOERR;
  91. }
  92. int
  93. ocset_ssl(OCstate* state)
  94. {
  95. CURLcode cstat = CURLE_OK;
  96. CURL* curl = state->curl;
  97. struct OCSSL* ssl = &state->ssl;
  98. long verify = (ssl->validate?1L:0L);
  99. cstat=curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, verify);
  100. if (cstat != CURLE_OK) goto fail;
  101. OCDBG1(1,"CURLOPT_SSL_VERIFYPEER=%ld",verify);
  102. cstat=curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, (verify?2L:0L));
  103. if (cstat != CURLE_OK) goto fail;
  104. OCDBG1(1,"CURLOPT_SSL_VERIFYHOST=%ld",(verify?2L:0L));
  105. #ifdef OCIGNORE
  106. if(verify)
  107. #endif
  108. {
  109. if(ssl->certificate) {
  110. cstat = curl_easy_setopt(curl, CURLOPT_SSLCERT, ssl->certificate);
  111. if(cstat != CURLE_OK) goto fail;
  112. OCDBG1(1,"CURLOPT_SSLCERT=%s",ssl->certificate);
  113. }
  114. if(ssl->key) {
  115. cstat = curl_easy_setopt(curl, CURLOPT_SSLKEY, ssl->key);
  116. if(cstat != CURLE_OK) goto fail;
  117. OCDBG1(1,"CURLOPT_SSLKEY=%s",ssl->key);
  118. }
  119. if(ssl->keypasswd) {
  120. /* libcurl prior to 7.16.4 used 'CURLOPT_SSLKEYPASSWD' */
  121. cstat = curl_easy_setopt(curl, CURLOPT_KEYPASSWD, ssl->keypasswd);
  122. if(cstat != CURLE_OK) goto fail;
  123. OCDBG1(1,"CURLOPT_SSLKEY=%s",ssl->key);
  124. }
  125. if(ssl->cainfo) {
  126. cstat = curl_easy_setopt(curl, CURLOPT_CAINFO, ssl->cainfo);
  127. if(cstat != CURLE_OK) goto fail;
  128. OCDBG1(1,"CURLOPT_CAINFO=%s",ssl->cainfo);
  129. }
  130. if(ssl->capath) {
  131. cstat = curl_easy_setopt(curl, CURLOPT_CAPATH, ssl->capath);
  132. if(cstat != CURLE_OK) goto fail;
  133. OCDBG1(1,"CURLOPT_CAPATH=%s",ssl->capath);
  134. }
  135. {
  136. cstat = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, ssl->verifypeer);
  137. if(cstat != CURLE_OK) goto fail;
  138. OCDBG1(1,"CURLOPT_SSL_VERIFYPEER=%d",ssl->verifypeer);
  139. }
  140. }
  141. return OC_NOERR;
  142. fail:
  143. return OC_ECURL;
  144. }
  145. /* This is called with arguments while the other functions in this file are
  146. * used with global values read from the.dodsrc file. The reason is that
  147. * we may have multiple password sources.
  148. */
  149. int
  150. ocset_user_password(OCstate* state)
  151. {
  152. CURLcode cstat;
  153. CURL* curl = state->curl;
  154. char* combined = NULL;
  155. const char* userC = state->creds.username;
  156. const char* passwordC = state->creds.password;
  157. if(userC == NULL || passwordC == NULL) return OC_NOERR;
  158. combined = combinecredentials(userC,passwordC);
  159. if (!combined) return OC_ENOMEM;
  160. cstat = curl_easy_setopt(curl, CURLOPT_USERPWD, combined);
  161. if (cstat != CURLE_OK) goto done;
  162. OCDBG1(1,"CURLOPT_USERPWD=%s",combined);
  163. cstat = curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long) CURLAUTH_ANY);
  164. if (cstat != CURLE_OK) goto done;
  165. OCDBG1(1,"CURLOPT_HTTPAUTH=%ld",(long)CURLAUTH_ANY);
  166. done:
  167. if(combined != NULL) free(combined);
  168. return (cstat == CURLE_OK?OC_NOERR:OC_ECURL);
  169. }
  170. static char*
  171. combinecredentials(const char* user, const char* pwd)
  172. {
  173. int userPassSize = strlen(user) + strlen(pwd) + 2;
  174. char *userPassword = malloc(sizeof(char) * userPassSize);
  175. if (!userPassword) {
  176. oc_log(LOGERR,"Out of Memory\n");
  177. return NULL;
  178. }
  179. strcpy(userPassword, user);
  180. strcat(userPassword, ":");
  181. strcat(userPassword, pwd);
  182. return userPassword;
  183. }