aboutsummaryrefslogtreecommitdiff
path: root/src/curl/curl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/curl/curl.c')
-rw-r--r--src/curl/curl.c166
1 files changed, 150 insertions, 16 deletions
diff --git a/src/curl/curl.c b/src/curl/curl.c
index b7452330f..eb9dd6a29 100644
--- a/src/curl/curl.c
+++ b/src/curl/curl.c
@@ -170,10 +170,93 @@ struct GNUNET_CURL_Context
170 * Closure for @e cb. 170 * Closure for @e cb.
171 */ 171 */
172 void *cb_cls; 172 void *cb_cls;
173
174 /**
175 * USERNAME:PASSWORD to use for client-authentication
176 * with all requests of this context, or NULL.
177 */
178 char *userpass;
179
180 /**
181 * Type of the TLS client certificate used, or NULL.
182 */
183 char *certtype;
184
185 /**
186 * File with the TLS client certificate, or NULL.
187 */
188 char *certfile;
189
190 /**
191 * File with the private key to authenticate the
192 * TLS client, or NULL.
193 */
194 char *keyfile;
195
196 /**
197 * Passphrase to decrypt @e keyfile, or NULL.
198 */
199 char *keypass;
200
173}; 201};
174 202
175 203
176/** 204/**
205 * Force use of the provided username and password
206 * for client authentication for all operations performed
207 * with @a ctx.
208 *
209 * @param ctx context to set authentication data for
210 * @param userpass string with "$USERNAME:$PASSWORD"
211 */
212void
213GNUNET_CURL_set_userpass (struct GNUNET_CURL_Context *ctx,
214 const char *userpass)
215{
216 GNUNET_free (ctx->userpass);
217 if (NULL != userpass)
218 ctx->userpass = GNUNET_strdup (userpass);
219}
220
221
222/**
223 * Force use of the provided TLS client certificate
224 * for client authentication for all operations performed
225 * with @a ctx.
226 *
227 * Note that if the provided information is incorrect,
228 * the earliest operation that could fail is
229 * #GNUNET_CURL_job_add() or #GNUNET_CURL_job_add2()!
230 *
231 * @param ctx context to set authentication data for
232 * @param certtype type of the certificate
233 * @param certfile file with the certificate
234 * @param keyfile file with the private key
235 * @param keypass passphrase to decrypt @a keyfile (or NULL)
236 */
237void
238GNUNET_CURL_set_tlscert (struct GNUNET_CURL_Context *ctx,
239 const char *certtype,
240 const char *certfile,
241 const char *keyfile,
242 const char *keypass)
243{
244 GNUNET_free (ctx->certtype);
245 GNUNET_free (ctx->certfile);
246 GNUNET_free (ctx->keyfile);
247 GNUNET_free (ctx->keypass);
248 if (NULL != certtype)
249 ctx->certtype = GNUNET_strdup (certtype);
250 if (NULL != certfile)
251 ctx->certfile = GNUNET_strdup (certfile);
252 if (NULL != keyfile)
253 ctx->certtype = GNUNET_strdup (keyfile);
254 if (NULL != keypass)
255 ctx->certtype = GNUNET_strdup (keypass);
256}
257
258
259/**
177 * Initialise this library. This function should be called before using any of 260 * Initialise this library. This function should be called before using any of
178 * the following functions. 261 * the following functions.
179 * 262 *
@@ -457,6 +540,32 @@ GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx,
457 struct curl_slist *all_headers; 540 struct curl_slist *all_headers;
458 541
459 GNUNET_assert (NULL != jcc); 542 GNUNET_assert (NULL != jcc);
543 if ( (NULL != ctx->userpass) &&
544 (0 != curl_easy_setopt (eh,
545 CURLOPT_USERPWD,
546 ctx->userpass)) )
547 return NULL;
548 if ( (NULL != ctx->certfile) &&
549 (0 != curl_easy_setopt (eh,
550 CURLOPT_SSLCERT,
551 ctx->certfile)) )
552 return NULL;
553 if ( (NULL != ctx->certtype) &&
554 (0 != curl_easy_setopt (eh,
555 CURLOPT_SSLCERTTYPE,
556 ctx->certtype)) )
557 return NULL;
558 if ( (NULL != ctx->keyfile) &&
559 (0 != curl_easy_setopt (eh,
560 CURLOPT_SSLKEY,
561 ctx->keyfile)) )
562 return NULL;
563 if ( (NULL != ctx->keypass) &&
564 (0 != curl_easy_setopt (eh,
565 CURLOPT_KEYPASSWD,
566 ctx->keypass)) )
567 return NULL;
568
460 all_headers = setup_job_headers (ctx, 569 all_headers = setup_job_headers (ctx,
461 job_headers); 570 job_headers);
462 if (NULL == (job = setup_job (eh, 571 if (NULL == (job = setup_job (eh,
@@ -477,33 +586,27 @@ GNUNET_CURL_job_add2 (struct GNUNET_CURL_Context *ctx,
477 * CURLOPT_PRIVATE facility of the CURL @a eh. 586 * CURLOPT_PRIVATE facility of the CURL @a eh.
478 * 587 *
479 * This function modifies the CURL handle to add the 588 * This function modifies the CURL handle to add the
480 * "Content-Type: application/json" header if @a add_json is set. 589 * "Content-Type: application/json" header.
481 * 590 *
482 * @param ctx context to execute the job in 591 * @param ctx context to execute the job in
483 * @param eh curl easy handle for the request, will 592 * @param eh curl easy handle for the request, will
484 * be executed AND cleaned up 593 * be executed AND cleaned up
485 * @param add_json add "application/json" content type header
486 * @param jcc callback to invoke upon completion 594 * @param jcc callback to invoke upon completion
487 * @param jcc_cls closure for @a jcc 595 * @param jcc_cls closure for @a jcc
488 * @return NULL on error (in this case, @eh is still released!) 596 * @return NULL on error (in this case, @eh is still released!)
489 */ 597 */
490struct GNUNET_CURL_Job * 598struct GNUNET_CURL_Job *
491GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx, 599GNUNET_CURL_job_add_with_ct_json (struct GNUNET_CURL_Context *ctx,
492 CURL *eh, 600 CURL *eh,
493 int add_json, 601 GNUNET_CURL_JobCompletionCallback jcc,
494 GNUNET_CURL_JobCompletionCallback jcc, 602 void *jcc_cls)
495 void *jcc_cls)
496{ 603{
497 struct GNUNET_CURL_Job *job; 604 struct GNUNET_CURL_Job *job;
498 struct curl_slist *job_headers = NULL; 605 struct curl_slist *job_headers = NULL;
499 606
500 if (GNUNET_YES == add_json) 607 GNUNET_assert (NULL != (job_headers =
501 { 608 curl_slist_append (NULL,
502 GNUNET_assert ( 609 "Content-Type: application/json")));
503 NULL != (job_headers =
504 curl_slist_append (NULL, "Content-Type: application/json")));
505 }
506
507 job = GNUNET_CURL_job_add2 (ctx, 610 job = GNUNET_CURL_job_add2 (ctx,
508 eh, 611 eh,
509 job_headers, 612 job_headers,
@@ -515,6 +618,32 @@ GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
515 618
516 619
517/** 620/**
621 * Schedule a CURL request to be executed and call the given @a jcc
622 * upon its completion. Note that the context will make use of the
623 * CURLOPT_PRIVATE facility of the CURL @a eh.
624 *
625 * @param ctx context to execute the job in
626 * @param eh curl easy handle for the request, will
627 * be executed AND cleaned up
628 * @param jcc callback to invoke upon completion
629 * @param jcc_cls closure for @a jcc
630 * @return NULL on error (in this case, @eh is still released!)
631 */
632struct GNUNET_CURL_Job *
633GNUNET_CURL_job_add (struct GNUNET_CURL_Context *ctx,
634 CURL *eh,
635 GNUNET_CURL_JobCompletionCallback jcc,
636 void *jcc_cls)
637{
638 return GNUNET_CURL_job_add2 (ctx,
639 eh,
640 NULL,
641 jcc,
642 jcc_cls);
643}
644
645
646/**
518 * Cancel a job. Must only be called before the job completion 647 * Cancel a job. Must only be called before the job completion
519 * callback is called for the respective job. 648 * callback is called for the respective job.
520 * 649 *
@@ -529,7 +658,7 @@ GNUNET_CURL_job_cancel (struct GNUNET_CURL_Job *job)
529 GNUNET_break (CURLM_OK == 658 GNUNET_break (CURLM_OK ==
530 curl_multi_remove_handle (ctx->multi, job->easy_handle)); 659 curl_multi_remove_handle (ctx->multi, job->easy_handle));
531 curl_easy_cleanup (job->easy_handle); 660 curl_easy_cleanup (job->easy_handle);
532 GNUNET_free_non_null (job->db.buf); 661 GNUNET_free (job->db.buf);
533 curl_slist_free_all (job->job_headers); 662 curl_slist_free_all (job->job_headers);
534 ctx->cb (ctx->cb_cls); 663 ctx->cb (ctx->cb_cls);
535 GNUNET_free (job); 664 GNUNET_free (job);
@@ -642,7 +771,7 @@ GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db,
642 *response_code = 0; 771 *response_code = 0;
643 } 772 }
644 } 773 }
645 GNUNET_free_non_null (db->buf); 774 GNUNET_free (db->buf);
646 db->buf = NULL; 775 db->buf = NULL;
647 db->buf_size = 0; 776 db->buf_size = 0;
648 if (NULL != json) 777 if (NULL != json)
@@ -899,6 +1028,11 @@ GNUNET_CURL_fini (struct GNUNET_CURL_Context *ctx)
899 curl_share_cleanup (ctx->share); 1028 curl_share_cleanup (ctx->share);
900 curl_multi_cleanup (ctx->multi); 1029 curl_multi_cleanup (ctx->multi);
901 curl_slist_free_all (ctx->common_headers); 1030 curl_slist_free_all (ctx->common_headers);
1031 GNUNET_free (ctx->userpass);
1032 GNUNET_free (ctx->certtype);
1033 GNUNET_free (ctx->certfile);
1034 GNUNET_free (ctx->keyfile);
1035 GNUNET_free (ctx->keypass);
902 GNUNET_free (ctx); 1036 GNUNET_free (ctx);
903} 1037}
904 1038