诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

389 lines
12 KiB

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_GSSAPI
  24. #ifdef HAVE_OLD_GSSMIT
  25. #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
  26. #define NCOMPAT 1
  27. #endif
  28. #ifndef CURL_DISABLE_HTTP
  29. #include "urldata.h"
  30. #include "sendf.h"
  31. #include "curl_gssapi.h"
  32. #include "rawstr.h"
  33. #include "curl_base64.h"
  34. #include "http_negotiate.h"
  35. #include "curl_memory.h"
  36. #include "url.h"
  37. #ifdef HAVE_SPNEGO
  38. # include <spnegohelp.h>
  39. # ifdef USE_SSLEAY
  40. # ifdef USE_OPENSSL
  41. # include <openssl/objects.h>
  42. # else
  43. # include <objects.h>
  44. # endif
  45. # else
  46. # error "Can't compile SPNEGO support without OpenSSL."
  47. # endif
  48. #endif
  49. #define _MPRINTF_REPLACE /* use our functions only */
  50. #include <curl/mprintf.h>
  51. /* The last #include file should be: */
  52. #include "memdebug.h"
  53. static int
  54. get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
  55. {
  56. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  57. &conn->data->state.negotiate;
  58. OM_uint32 major_status, minor_status;
  59. gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
  60. char name[2048];
  61. const char* service;
  62. /* GSSAPI implementation by Globus (known as GSI) requires the name to be
  63. of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
  64. of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
  65. Change following lines if you want to use GSI */
  66. /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
  67. if(neg_ctx->gss)
  68. service = "KHTTP";
  69. else
  70. service = "HTTP";
  71. token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
  72. conn->host.name) + 1;
  73. if(token.length + 1 > sizeof(name))
  74. return EMSGSIZE;
  75. snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
  76. conn->host.name);
  77. token.value = (void *) name;
  78. major_status = gss_import_name(&minor_status,
  79. &token,
  80. GSS_C_NT_HOSTBASED_SERVICE,
  81. server);
  82. return GSS_ERROR(major_status) ? -1 : 0;
  83. }
  84. static void
  85. log_gss_error(struct connectdata *conn, OM_uint32 error_status,
  86. const char *prefix)
  87. {
  88. OM_uint32 maj_stat, min_stat;
  89. OM_uint32 msg_ctx = 0;
  90. gss_buffer_desc status_string;
  91. char buf[1024];
  92. size_t len;
  93. snprintf(buf, sizeof(buf), "%s", prefix);
  94. len = strlen(buf);
  95. do {
  96. maj_stat = gss_display_status(&min_stat,
  97. error_status,
  98. GSS_C_MECH_CODE,
  99. GSS_C_NO_OID,
  100. &msg_ctx,
  101. &status_string);
  102. if(sizeof(buf) > len + status_string.length + 1) {
  103. snprintf(buf + len, sizeof(buf) - len,
  104. ": %s", (char*) status_string.value);
  105. len += status_string.length;
  106. }
  107. gss_release_buffer(&min_stat, &status_string);
  108. } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
  109. infof(conn->data, "%s\n", buf);
  110. }
  111. /* returning zero (0) means success, everything else is treated as "failure"
  112. with no care exactly what the failure was */
  113. int Curl_input_negotiate(struct connectdata *conn, bool proxy,
  114. const char *header)
  115. {
  116. struct SessionHandle *data = conn->data;
  117. struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
  118. &data->state.negotiate;
  119. OM_uint32 major_status, minor_status, discard_st;
  120. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  121. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  122. int ret;
  123. size_t len;
  124. size_t rawlen = 0;
  125. bool gss;
  126. const char* protocol;
  127. CURLcode error;
  128. while(*header && ISSPACE(*header))
  129. header++;
  130. if(checkprefix("GSS-Negotiate", header)) {
  131. protocol = "GSS-Negotiate";
  132. gss = TRUE;
  133. }
  134. else if(checkprefix("Negotiate", header)) {
  135. protocol = "Negotiate";
  136. gss = FALSE;
  137. }
  138. else
  139. return -1;
  140. if(neg_ctx->context) {
  141. if(neg_ctx->gss != gss) {
  142. return -1;
  143. }
  144. }
  145. else {
  146. neg_ctx->protocol = protocol;
  147. neg_ctx->gss = gss;
  148. }
  149. if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
  150. /* We finished successfully our part of authentication, but server
  151. * rejected it (since we're again here). Exit with an error since we
  152. * can't invent anything better */
  153. Curl_cleanup_negotiate(data);
  154. return -1;
  155. }
  156. if(neg_ctx->server_name == NULL &&
  157. (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
  158. return ret;
  159. header += strlen(neg_ctx->protocol);
  160. while(*header && ISSPACE(*header))
  161. header++;
  162. len = strlen(header);
  163. if(len > 0) {
  164. error = Curl_base64_decode(header,
  165. (unsigned char **)&input_token.value, &rawlen);
  166. if(error || rawlen == 0)
  167. return -1;
  168. input_token.length = rawlen;
  169. DEBUGASSERT(input_token.value != NULL);
  170. #ifdef HAVE_SPNEGO /* Handle SPNEGO */
  171. if(checkprefix("Negotiate", header)) {
  172. unsigned char *spnegoToken = NULL;
  173. size_t spnegoTokenLength = 0;
  174. gss_buffer_desc mechToken = GSS_C_EMPTY_BUFFER;
  175. spnegoToken = malloc(input_token.length);
  176. if(spnegoToken == NULL) {
  177. Curl_safefree(input_token.value);
  178. return CURLE_OUT_OF_MEMORY;
  179. }
  180. memcpy(spnegoToken, input_token.value, input_token.length);
  181. spnegoTokenLength = input_token.length;
  182. if(!parseSpnegoTargetToken(spnegoToken,
  183. spnegoTokenLength,
  184. NULL,
  185. NULL,
  186. (unsigned char**)&mechToken.value,
  187. &mechToken.length,
  188. NULL,
  189. NULL)) {
  190. Curl_safefree(spnegoToken);
  191. infof(data, "Parse SPNEGO Target Token failed\n");
  192. }
  193. else if(!mechToken.value || !mechToken.length) {
  194. Curl_safefree(spnegoToken);
  195. if(mechToken.value)
  196. gss_release_buffer(&discard_st, &mechToken);
  197. infof(data, "Parse SPNEGO Target Token succeeded (NULL token)\n");
  198. }
  199. else {
  200. Curl_safefree(spnegoToken);
  201. Curl_safefree(input_token.value);
  202. input_token.value = malloc(mechToken.length);
  203. if(input_token.value == NULL) {
  204. gss_release_buffer(&discard_st, &mechToken);
  205. return CURLE_OUT_OF_MEMORY;
  206. }
  207. memcpy(input_token.value, mechToken.value, mechToken.length);
  208. input_token.length = mechToken.length;
  209. gss_release_buffer(&discard_st, &mechToken);
  210. infof(data, "Parse SPNEGO Target Token succeeded\n");
  211. }
  212. }
  213. #endif
  214. }
  215. major_status = Curl_gss_init_sec_context(data,
  216. &minor_status,
  217. &neg_ctx->context,
  218. neg_ctx->server_name,
  219. GSS_C_NO_CHANNEL_BINDINGS,
  220. &input_token,
  221. &output_token,
  222. NULL);
  223. Curl_safefree(input_token.value);
  224. neg_ctx->status = major_status;
  225. if(GSS_ERROR(major_status)) {
  226. if(output_token.value)
  227. gss_release_buffer(&discard_st, &output_token);
  228. log_gss_error(conn, minor_status, "gss_init_sec_context() failed: ");
  229. return -1;
  230. }
  231. if(!output_token.value || !output_token.length) {
  232. if(output_token.value)
  233. gss_release_buffer(&discard_st, &output_token);
  234. return -1;
  235. }
  236. neg_ctx->output_token = output_token;
  237. return 0;
  238. }
  239. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  240. {
  241. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  242. &conn->data->state.negotiate;
  243. char *encoded = NULL;
  244. size_t len = 0;
  245. char *userp;
  246. CURLcode error;
  247. OM_uint32 discard_st;
  248. #ifdef HAVE_SPNEGO /* Handle SPNEGO */
  249. if(checkprefix("Negotiate", neg_ctx->protocol)) {
  250. ASN1_OBJECT *object = NULL;
  251. unsigned char *responseToken = NULL;
  252. size_t responseTokenLength = 0;
  253. gss_buffer_desc spnegoToken = GSS_C_EMPTY_BUFFER;
  254. responseToken = malloc(neg_ctx->output_token.length);
  255. if(responseToken == NULL)
  256. return CURLE_OUT_OF_MEMORY;
  257. memcpy(responseToken, neg_ctx->output_token.value,
  258. neg_ctx->output_token.length);
  259. responseTokenLength = neg_ctx->output_token.length;
  260. object = OBJ_txt2obj("1.2.840.113554.1.2.2", 1);
  261. if(!object) {
  262. Curl_safefree(responseToken);
  263. return CURLE_OUT_OF_MEMORY;
  264. }
  265. if(!makeSpnegoInitialToken(object,
  266. responseToken,
  267. responseTokenLength,
  268. (unsigned char**)&spnegoToken.value,
  269. &spnegoToken.length)) {
  270. Curl_safefree(responseToken);
  271. ASN1_OBJECT_free(object);
  272. infof(conn->data, "Make SPNEGO Initial Token failed\n");
  273. }
  274. else if(!spnegoToken.value || !spnegoToken.length) {
  275. Curl_safefree(responseToken);
  276. ASN1_OBJECT_free(object);
  277. if(spnegoToken.value)
  278. gss_release_buffer(&discard_st, &spnegoToken);
  279. infof(conn->data, "Make SPNEGO Initial Token succeeded (NULL token)\n");
  280. }
  281. else {
  282. Curl_safefree(responseToken);
  283. ASN1_OBJECT_free(object);
  284. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  285. neg_ctx->output_token.value = spnegoToken.value;
  286. neg_ctx->output_token.length = spnegoToken.length;
  287. infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
  288. }
  289. }
  290. #endif
  291. error = Curl_base64_encode(conn->data,
  292. neg_ctx->output_token.value,
  293. neg_ctx->output_token.length,
  294. &encoded, &len);
  295. if(error) {
  296. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  297. neg_ctx->output_token.value = NULL;
  298. neg_ctx->output_token.length = 0;
  299. return error;
  300. }
  301. if(!encoded || !len) {
  302. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  303. neg_ctx->output_token.value = NULL;
  304. neg_ctx->output_token.length = 0;
  305. return CURLE_REMOTE_ACCESS_DENIED;
  306. }
  307. userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
  308. neg_ctx->protocol, encoded);
  309. if(proxy) {
  310. Curl_safefree(conn->allocptr.proxyuserpwd);
  311. conn->allocptr.proxyuserpwd = userp;
  312. }
  313. else {
  314. Curl_safefree(conn->allocptr.userpwd);
  315. conn->allocptr.userpwd = userp;
  316. }
  317. Curl_safefree(encoded);
  318. Curl_cleanup_negotiate(conn->data);
  319. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  320. }
  321. static void cleanup(struct negotiatedata *neg_ctx)
  322. {
  323. OM_uint32 minor_status;
  324. if(neg_ctx->context != GSS_C_NO_CONTEXT)
  325. gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
  326. if(neg_ctx->output_token.value)
  327. gss_release_buffer(&minor_status, &neg_ctx->output_token);
  328. if(neg_ctx->server_name != GSS_C_NO_NAME)
  329. gss_release_name(&minor_status, &neg_ctx->server_name);
  330. memset(neg_ctx, 0, sizeof(*neg_ctx));
  331. }
  332. void Curl_cleanup_negotiate(struct SessionHandle *data)
  333. {
  334. cleanup(&data->state.negotiate);
  335. cleanup(&data->state.proxyneg);
  336. }
  337. #endif
  338. #endif