summaryrefslogtreecommitdiffstats
path: root/src/util/tev_curl.c
blob: c155f4c038d4215933ee30d41c694ad4a14ae132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
/*
   SSSD

   libcurl tevent integration

   Copyright (C) Red Hat, 2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

#include <talloc.h>
#include <tevent.h>

#include <curl/curl.h>

#include "util/util.h"
#include "util/tev_curl.h"

#define TCURL_IOBUF_CHUNK   1024
#define TCURL_IOBUF_MAX     4096

static bool global_is_curl_initialized;

/**
 * @brief The main structure of the tcurl module.
 *
 * Use tcurl_init() to initialize it, then pass to the request.
 * Should be kept opaque in the future.
 *
 * @see tcurl_init()
 */
struct tcurl_ctx {
    struct tevent_context *ev;
    /* See where we set CURLMOPT_TIMERFUNCTION */
    struct tevent_timer *process_timer;

    /* Since we want the API to be non-blocking, all the transfers use
     * the curl's multi interface:
     *  https://ec.haxx.se/libcurl-drive-multi.html
     * and then each transfer also uses an easy interface instance for
     * the transfer's private data
     */
    CURLM *multi_handle;
};

/**
 * @brief A tevent wrapper around curl socket
 */
struct tcurl_sock {
    struct tcurl_ctx *tctx;     /* Backchannel to the main context */

    curl_socket_t sockfd;       /* curl socket is an int typedef on Unix */
    struct tevent_fd *fde;      /* tevent tracker of the fd events */
};

static void tcurl_request_done(struct tevent_req *req,
                               errno_t process_error,
                               int response_code);

static errno_t curl_code2errno(CURLcode crv)
{
    switch (crv) {
    /* HTTP error does not fail the whole request, just returns the error
     * separately
     */
    case CURLE_HTTP_RETURNED_ERROR:
    case CURLE_OK:
        return EOK;
    case CURLE_URL_MALFORMAT:
        return EBADMSG;
    case CURLE_COULDNT_CONNECT:
        return EHOSTUNREACH;
    case CURLE_REMOTE_ACCESS_DENIED:
        return EACCES;
    case CURLE_OUT_OF_MEMORY:
        return ENOMEM;
    case CURLE_OPERATION_TIMEDOUT:
        return ETIMEDOUT;
    case CURLE_SSL_ISSUER_ERROR:
    case CURLE_SSL_CACERT_BADFILE:
    case CURLE_SSL_CACERT:
    case CURLE_SSL_CERTPROBLEM:
        return ERR_INVALID_CERT;

    case CURLE_SSL_CRL_BADFILE:
    case CURLE_SSL_SHUTDOWN_FAILED:
    case CURLE_SSL_ENGINE_INITFAILED:
    case CURLE_USE_SSL_FAILED:
    case CURLE_SSL_CIPHER:
    case CURLE_SSL_ENGINE_SETFAILED:
    case CURLE_SSL_ENGINE_NOTFOUND:
    case CURLE_SSL_CONNECT_ERROR:
        return ERR_SSL_FAILURE;
    case CURLE_PEER_FAILED_VERIFICATION:
        return ERR_UNABLE_TO_VERIFY_PEER;
    case CURLE_COULDNT_RESOLVE_HOST:
        return ERR_UNABLE_TO_RESOLVE_HOST;
    default:
        break;
    }

    return EIO;
}

static errno_t curlm_code2errno(CURLcode crv)
{
    switch (crv) {
    case CURLM_OK:
        return EOK;
    case CURLM_BAD_SOCKET:
        return EPIPE;
    case CURLM_OUT_OF_MEMORY:
        return ENOMEM;
    case CURLM_BAD_HANDLE:
    case CURLM_BAD_EASY_HANDLE:
    case CURLM_UNKNOWN_OPTION:
        return EINVAL;
    case CURLM_INTERNAL_ERROR:
        return ERR_INTERNAL;
    default:
        break;
    }

    return EIO;
}

static errno_t tcurl_global_init(void)
{
    errno_t ret;

    if (global_is_curl_initialized == false) {
        ret = curl_global_init(CURL_GLOBAL_ALL);
        if (ret != CURLE_OK) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Cannot initialize global curl options [%d]\n", ret);
            return EIO;
        }
    }

    global_is_curl_initialized = true;
    return EOK;
}

static int curl2tev_flags(int curlflags)
{
    int flags = 0;

    switch (curlflags) {
    case CURL_POLL_IN:
        flags |= TEVENT_FD_READ;
        break;
    case CURL_POLL_OUT:
        flags |= TEVENT_FD_WRITE;
        break;
    case CURL_POLL_INOUT:
        flags |= (TEVENT_FD_READ | TEVENT_FD_WRITE);
        break;
    }

    return flags;
}

static void handle_curlmsg_done(CURLMsg *message)
{
    CURL *easy_handle;
    CURLcode crv;
    struct tevent_req *req;
    long response_code = 0;
    char *done_url;
    errno_t ret;

    easy_handle = message->easy_handle;
    if (easy_handle == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "BUG: NULL handle for message %p\n", message);
        return;
    }

    if (DEBUG_IS_SET(SSSDBG_TRACE_FUNC)) {
        crv = curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &done_url);
        if (crv != CURLE_OK) {
            DEBUG(SSSDBG_MINOR_FAILURE, "Cannot get CURLINFO_EFFECTIVE_URL "
                  "[%d]: %s\n", crv, curl_easy_strerror(crv));
            /* not fatal since we need this only for debugging */
        } else {
            DEBUG(SSSDBG_TRACE_FUNC, "Handled %s\n", done_url);
        }
    }

    crv = curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, (void *) &req);
    if (crv != CURLE_OK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot get CURLINFO_PRIVATE [%d]: %s\n",
              crv, curl_easy_strerror(crv));
        ret = curl_code2errno(crv);
        goto done;
    }

    ret = curl_code2errno(message->data.result);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "CURL operation failed [%d]: %s\n",
              ret, sss_strerror(ret));
        goto done;
    }

    /* If there was no fatal error, let's read the response code
     * and mark the request as done */
    crv = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &response_code);
    if (crv != CURLE_OK) {
        DEBUG(SSSDBG_OP_FAILURE, "Cannot get response code\n");
        ret = curl_code2errno(crv);
        goto done;
    }

    ret = EOK;

done:
    tcurl_request_done(req, ret, response_code);
}

static void process_curl_activity(struct tcurl_ctx *tctx)
{
    CURLMsg *message;
    int pending;

    while ((message = curl_multi_info_read(tctx->multi_handle, &pending))) {
        switch (message->msg) {
        case CURLMSG_DONE:
            handle_curlmsg_done(message);
            break;
        default:
            DEBUG(SSSDBG_TRACE_LIBS,
                  "noop for curl msg %d\n", message->msg);
            break;
        }
    }
}

static void tcurlsock_input_available(struct tevent_context *ev,
                                      struct tevent_fd *fde,
                                      uint16_t flags,
                                      void *data)
{
    struct tcurl_ctx *tctx;
    struct tcurl_sock *tcs = NULL;
    int curl_flags = 0;
    int running_handles;

    tcs = talloc_get_type(data, struct tcurl_sock);
    if (tcs == NULL) {
        return;
    }

    if (flags & TEVENT_FD_READ) {
        curl_flags |= CURL_CSELECT_IN;
    }
    if (flags & TEVENT_FD_WRITE) {
        curl_flags |= CURL_CSELECT_OUT;
    }

    /* multi_socket_action might invalidate tcs when the transfer ends,
     * so we need to store tctx separately
     */
    tctx = tcs->tctx;

    /* https://ec.haxx.se/libcurl-drive-multi-socket.html */
    curl_multi_socket_action(tcs->tctx->multi_handle,
                             tcs->sockfd,
                             curl_flags,
                             &running_handles);

    process_curl_activity(tctx);
}

/**
 * @brief Registers a curl's socket with tevent
 *
 * Creates a private structure, registers the socket with tevent and finally
 * registers the tcurl_sock structure as a private pointer for the curl
 * socket for later
 */
static struct tcurl_sock *register_curl_socket(struct tcurl_ctx *tctx,
                                               curl_socket_t sockfd,
                                               int flags)
{
    struct tcurl_sock *tcs;

    tcs = talloc_zero(tctx, struct tcurl_sock);
    if (tcs == NULL) {
        return NULL;
    }
    tcs->sockfd = sockfd;
    tcs->tctx = tctx;

    tcs->fde = tevent_add_fd(tctx->ev, tcs, sockfd, flags,
                             tcurlsock_input_available, tcs);
    if (tcs->fde == NULL) {
        talloc_free(tcs);
        return NULL;
    }

    curl_multi_assign(tctx->multi_handle, sockfd, (void *) tcs);
    return tcs;
}

/* libcurl informs the application about socket activity to wait for with
 * this callback */
static int handle_socket(CURL *easy,
                         curl_socket_t s,
                         int action,
                         void *userp,
                         void *socketp)
{
    struct tcurl_ctx *tctx = NULL;
    struct tcurl_sock *tcsock;
    int flags = 0;

    tctx = talloc_get_type(userp, struct tcurl_ctx);
    if (tctx == NULL) {
        return 1;
    }

    DEBUG(SSSDBG_TRACE_INTERNAL,
          "Activity on curl socket %d socket data %p\n", s, socketp);

    switch (action) {
    case CURL_POLL_IN:
    case CURL_POLL_OUT:
    case CURL_POLL_INOUT:
        /* There is some activity on a socket */

        flags = curl2tev_flags(action);

        if (socketp == NULL) {
            /* If this socket doesn't have private data, it must be a new one,
             * let's start tracking it with tevent
             */
            tcsock = register_curl_socket(tctx, s, flags);
            if (tcsock == NULL) {
                return 1;
            }
        } else {
            /* If we are already tracking this socket, just set the correct
             * flags for tevent and pass the control to tevent
             */
            tcsock = talloc_get_type(socketp, struct tcurl_sock);
            if (tcsock == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE,
                      "BUG: No private data for socket %d\n", s);
                return 1;
            }
            tevent_fd_set_flags(tcsock->fde, flags);
        }
        break;

    case CURL_POLL_REMOVE:
        /* This socket is being closed by curl, so we need to.. */
        tcsock = talloc_get_type(socketp, struct tcurl_sock);
        if (tcsock == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "BUG: Trying to remove an untracked socket %d\n", s);
        }
        /* ..stop tracking the socket with the multi handle.. */
        curl_multi_assign(tctx->multi_handle, s, NULL);
        /* ..and stop tracking the fd with tevent */
        talloc_free(tcsock);
        break;

    default:
        return 1;
    }

    return 0;
}

static void check_curl_timeouts(struct tcurl_ctx *tctx)
{
    int running_handles;

    curl_multi_socket_action(tctx->multi_handle,
                             CURL_SOCKET_TIMEOUT,
                             0,
                             &running_handles);
    DEBUG(SSSDBG_TRACE_ALL,
          "Still tracking %d outstanding requests\n", running_handles);

    /* https://ec.haxx.se/libcurl-drive-multi-socket.html */
    process_curl_activity(tctx);
}

static void check_fd_activity(struct tevent_context *ev,
                              struct tevent_timer *te,
                              struct timeval current_time,
                              void *private_data)
{
    struct tcurl_ctx *tctx = talloc_get_type(private_data, struct tcurl_ctx);
    check_curl_timeouts(tctx);
}

static int schedule_fd_processing(CURLM *multi,
                                  long timeout_ms,
                                  void *userp)
{
    struct timeval tv = { 0, 0 };
    struct tcurl_ctx *tctx = talloc_get_type(userp, struct tcurl_ctx);

    DEBUG(SSSDBG_TRACE_INTERNAL, "timeout_ms: %ld\n", timeout_ms);

    if (timeout_ms == -1) {
        /* man curlmopt_timerfunction(3) says:
         *  A timeout_ms value of -1 means you should delete your timer.
         */
        talloc_zfree(tctx->process_timer);
        check_curl_timeouts(tctx);
        return 0;
    }

    tv = tevent_timeval_current_ofs(0, timeout_ms * 1000);

    /* There is only one timer per multi handle, so it makes sense to cancel
     * the previous one.
     *
     * From https://ec.haxx.se/libcurl-drive-multi-socket.html:
     * There is only one timeout for the application to handle for the
     * entire multi handle, no matter how many individual easy handles
     * that have been added or transfers that are in progress. The timer
     * callback will be updated with the current nearest-in-time period to
     * wait.
     */
    talloc_zfree(tctx->process_timer);
    tctx->process_timer = tevent_add_timer(tctx->ev, tctx, tv,
                                           check_fd_activity, tctx);
    if (tctx->process_timer == NULL) {
        return -1;
    }

    return 0;
}

static int tcurl_ctx_destroy(struct tcurl_ctx *ctx)
{
    if (ctx == NULL) {
        return 0;
    }

    curl_multi_cleanup(ctx->multi_handle);
    return 0;
}

struct tcurl_ctx *tcurl_init(TALLOC_CTX *mem_ctx,
                             struct tevent_context *ev)
{
    errno_t ret;
    struct tcurl_ctx *tctx = NULL;
    CURLMcode cmret;

    /* Per the manpage it is safe to call the initialization multiple
     * times, as long as this is done before any other curl calls to
     * make sure we don't mangle the global curl environment
     */
    ret = tcurl_global_init();
    if (ret != EOK) {
        goto fail;
    }

    tctx = talloc_zero(mem_ctx, struct tcurl_ctx);
    if (tctx == NULL) {
        goto fail;
    }
    tctx->ev = ev;

    tctx->multi_handle = curl_multi_init();
    if (tctx->multi_handle == NULL) {
        goto fail;
    }
    talloc_set_destructor(tctx, tcurl_ctx_destroy);

    cmret = curl_multi_setopt(tctx->multi_handle,
                              CURLMOPT_SOCKETDATA, tctx);
    if (cmret != CURLM_OK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Cannot set CURLMOPT_SOCKETDATA [%d]: %s\n",
              cmret, curl_multi_strerror(cmret));
        goto fail;
    }

    /*
     * When there is some activity on a socket associated with the multi
     * handle, then the handle_socket() function will be called with the
     * global context as private data
     */
    cmret = curl_multi_setopt(tctx->multi_handle,
                              CURLMOPT_SOCKETFUNCTION, handle_socket);
    if (cmret != CURLM_OK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Cannot set CURLMOPT_SOCKETFUNCTION [%d]: %s\n",
              cmret, curl_multi_strerror(cmret));
        goto fail;
    }

    /* When integrated in a mainloop, the curl multi interface must
     * kick off the communication in another eventloop tick. Similar
     * to the handle_socet function, the tcurl context is passed in
     * as private data
     */
    cmret = curl_multi_setopt(tctx->multi_handle,
                              CURLMOPT_TIMERFUNCTION, schedule_fd_processing);
    if (cmret != CURLM_OK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Cannot set CURLMOPT_TIMERFUNCTION [%d]: %s\n",
              cmret, curl_multi_strerror(cmret));
        goto fail;
    }

    cmret = curl_multi_setopt(tctx->multi_handle, CURLMOPT_TIMERDATA, tctx);
    if (cmret != CURLM_OK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Cannot set CURLMOPT_TIMERDATA [%d]: %s\n",
              cmret, curl_multi_strerror(cmret));
    }

    return tctx;

fail:
    talloc_free(tctx);
    return NULL;
}

#define tcurl_set_option(tcurl_req, option, value)                          \
({                                                                          \
    CURLcode __curl_code;                                                   \
    errno_t __ret;                                                          \
                                                                            \
    __curl_code = curl_easy_setopt((tcurl_req)->curl_easy_handle,           \
                                   (option), (value));                      \
    if (__curl_code == CURLE_OK) {                                          \
        __ret = EOK;                                                        \
    } else {                                                                \
        DEBUG(SSSDBG_OP_FAILURE, "Failed to set CURL option %s [%d]: %s\n", \
              #option, __curl_code, curl_easy_strerror(__curl_code));       \
        __ret = curl_code2errno(__curl_code);                               \
    }                                                                       \
    __ret;                                                                  \
})

static size_t tcurl_write_data(char *ptr,
                               size_t size,
                               size_t nmemb,
                               void *userdata)
{
    errno_t ret;
    size_t realsize = size * nmemb;
    struct sss_iobuf *outbuf;

    outbuf = talloc_get_type(userdata, struct sss_iobuf);

    DEBUG(SSSDBG_TRACE_INTERNAL, "---> begin libcurl data\n");
    DEBUG(SSSDBG_TRACE_INTERNAL, "%s\n", ptr);
    DEBUG(SSSDBG_TRACE_INTERNAL, "<--- end libcurl data\n");

    ret = sss_iobuf_write_len(outbuf, (uint8_t *)ptr, realsize);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to write data to buffer [%d]: %s\n",
              ret, sss_strerror(ret));
        /* zero signifies an EOF */
        return 0;
    }

    return realsize;
}

static size_t tcurl_read_data(void *ptr,
                              size_t size,
                              size_t nmemb,
                              void *userdata)
{
    errno_t ret;
    size_t readbytes;
    struct sss_iobuf *inbuf;

    inbuf = talloc_get_type(userdata, struct sss_iobuf);

    if (inbuf == NULL) {
        return CURL_READFUNC_ABORT;
    }

    ret = sss_iobuf_read(inbuf, size * nmemb, ptr, &readbytes);
    if (ret != EOK) {
        return CURL_READFUNC_ABORT;
    }

    return readbytes;
}


struct tcurl_request {
    CURL *curl_easy_handle;

    struct sss_iobuf *body;
    struct curl_slist *headers;

    const char *url;
    const char *socket;

    /* Associated tcurl context if this request is in progress. */
    struct tcurl_ctx *tcurl_ctx;
};

struct tcurl_request_state {
    struct tcurl_request *tcurl_req;
    struct sss_iobuf *response;
    int response_code;
};

struct tevent_req *
tcurl_request_send(TALLOC_CTX *mem_ctx,
                   struct tevent_context *ev,
                   struct tcurl_ctx *tcurl_ctx,
                   struct tcurl_request *tcurl_req,
                   long int timeout)
{
    struct tcurl_request_state *state;
    struct tevent_req *req;
    CURLMcode curl_code;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state, struct tcurl_request_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
        return NULL;
    }

    DEBUG(SSSDBG_TRACE_FUNC, "Sending TCURL request for %s, at socket %s\n",
          tcurl_req->url == NULL ? "<none>" : tcurl_req->url,
          tcurl_req->socket == NULL ? "<none>" : tcurl_req->socket);

    state->tcurl_req = talloc_steal(state, tcurl_req);

    state->response = sss_iobuf_init_empty(state, TCURL_IOBUF_CHUNK, TCURL_IOBUF_MAX);
    if (state->response == NULL) {
        ret = ENOMEM;
        goto done;
    }

    ret = tcurl_set_option(tcurl_req, CURLOPT_PRIVATE, req);
    if (ret != EOK) {
        goto done;
    }

    ret = tcurl_set_option(tcurl_req, CURLOPT_TIMEOUT, timeout);
    if (ret != EOK) {
        goto done;
    }

    ret = tcurl_set_option(tcurl_req, CURLOPT_WRITEFUNCTION, tcurl_write_data);
    if (ret != EOK) {
        goto done;
    }

    ret = tcurl_set_option(tcurl_req, CURLOPT_WRITEDATA, state->response);
    if (ret != EOK) {
        goto done;
    }

    if (tcurl_req->body != NULL) {
        ret = tcurl_set_option(tcurl_req, CURLOPT_READFUNCTION, tcurl_read_data);
        if (ret != EOK) {
            goto done;
        }

        ret = tcurl_set_option(tcurl_req, CURLOPT_READDATA, tcurl_req->body);
        if (ret != EOK) {
            goto done;
        }
    }

    curl_code = curl_multi_add_handle(tcurl_ctx->multi_handle,
                                      tcurl_req->curl_easy_handle);
    if (curl_code != CURLM_OK) {
        ret = curlm_code2errno(curl_code);
        goto done;
    }

    tcurl_req->tcurl_ctx = tcurl_ctx;

    ret = EAGAIN;

done:
    if (ret == EOK) {
        tevent_req_done(req);
        tevent_req_post(req, ev);
    } else if (ret != EAGAIN) {
        tevent_req_error(req, ret);
        tevent_req_post(req, ev);
    }

    return req;
}

static void tcurl_request_done(struct tevent_req *req,
                               errno_t process_error,
                               int response_code)
{
    struct tcurl_request_state *state;

    DEBUG(SSSDBG_TRACE_FUNC, "TCURL request finished [%d]: %s\n",
          process_error, sss_strerror(process_error));

    if (req == NULL) {
        /* To handle case where we fail to obtain request from private data. */
        DEBUG(SSSDBG_MINOR_FAILURE, "No tevent request provided!\n");
        return;
    }

    state = tevent_req_data(req, struct tcurl_request_state);

    curl_multi_remove_handle(state->tcurl_req->tcurl_ctx->multi_handle,
                             state->tcurl_req->curl_easy_handle);

    /* This request is no longer associated with tcurl context. */
    state->tcurl_req->tcurl_ctx = NULL;

    if (process_error != EOK) {
        tevent_req_error(req, process_error);
        return;
    }

    state->response_code = response_code;

    tevent_req_done(req);
    return;
}

errno_t tcurl_request_recv(TALLOC_CTX *mem_ctx,
                           struct tevent_req *req,
                           struct sss_iobuf **_response,
                           int *_response_code)
{
    struct tcurl_request_state *state;
    state = tevent_req_data(req, struct tcurl_request_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    if (_response != NULL) {
        *_response = talloc_steal(mem_ctx, state->response);
    }

    if (_response_code != NULL) {
        *_response_code = state->response_code;
    }

    return EOK;
}

static struct curl_slist *
tcurl_add_header(struct curl_slist *slist, const char *header)
{
    struct curl_slist *new;

    new = curl_slist_append(slist, header);
    if (new == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot add header %s\n", header);
        if (slist != NULL) {
            curl_slist_free_all(slist);
        }

        return NULL;
    }

    return new;
}

static errno_t
tcurl_construct_headers(const char **headers,
                        struct curl_slist **_slist)
{
    struct curl_slist *slist = NULL;
    int i;

    if (headers == NULL || headers[0] == NULL) {
        *_slist = NULL;
        return EOK;
    }

    for (i = 0; headers[i] != NULL; i++) {
        slist = tcurl_add_header(slist, headers[i]);
        if (slist == NULL) {
            return ENOMEM;
        }
    }

    /* Add a dummy header to suppress libcurl adding Expect 100-continue which
     * was causing libcurl to always wait for the internal timeout when sending
     * a PUT/POST request because secrets responder does not implement this.
     */
    slist = tcurl_add_header(slist, "Expect: ");
    if (slist == NULL) {
        return ENOMEM;
    }

    *_slist = slist;

    return EOK;
}

static int
tcurl_request_destructor(struct tcurl_request *tcurl_req)
{
    if (tcurl_req->tcurl_ctx != NULL) {
        DEBUG(SSSDBG_MINOR_FAILURE, "Terminating TCURL request...\n");
        curl_multi_remove_handle(tcurl_req->tcurl_ctx->multi_handle,
                                 tcurl_req->curl_easy_handle);
    }

    if (tcurl_req->headers != NULL) {
        curl_slist_free_all(tcurl_req->headers);
    }

    if (tcurl_req->curl_easy_handle != NULL) {
        curl_easy_cleanup(tcurl_req->curl_easy_handle);
    }

    return 0;
}

static struct tcurl_request *
tcurl_request_create(TALLOC_CTX *mem_ctx,
                     const char *socket_path,
                     const char *url,
                     const char **headers,
                     struct sss_iobuf *body)
{
    struct tcurl_request *tcurl_req;
    errno_t ret;

    tcurl_req = talloc_zero(mem_ctx, struct tcurl_request);
    if (tcurl_req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!\n");
        return NULL;
    }

    if (url == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "URL cannot be NULL!\n");
        ret = EINVAL;
        goto done;
    }

    /* Setup a curl easy handle. This handle contains state for the request
     * and is later associated with curl multi handle which performs
     * asynchronous processing. */
    tcurl_req->curl_easy_handle = curl_easy_init();
    if (tcurl_req->curl_easy_handle == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to initialize curl easy handle!\n");
        ret = ENOMEM;
        goto done;
    }

    tcurl_req->url = talloc_strdup(tcurl_req, url);
    if (tcurl_req->url == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!\n");
        ret = ENOMEM;
        goto done;
    }

    if (socket_path != NULL) {
        tcurl_req->socket = talloc_strdup(tcurl_req, socket_path);
        if (tcurl_req->socket == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!\n");
            ret = ENOMEM;
            goto done;
        }
    }

    ret = tcurl_construct_headers(headers, &tcurl_req->headers);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to construct headers [%d]: %s\n",
              ret, sss_strerror(ret));
        ret = ENOMEM;
        goto done;
    }

    tcurl_req->body = body;

    talloc_set_destructor(tcurl_req, tcurl_request_destructor);

    ret = tcurl_set_option(tcurl_req, CURLOPT_URL, url);
    if (ret != EOK) {
        goto done;
    }

    if (socket_path != NULL) {
        ret = tcurl_set_option(tcurl_req, CURLOPT_UNIX_SOCKET_PATH, socket_path);
        if (ret != EOK) {
            goto done;
        }
    }

    if (body != NULL) {
        /* Curl will tell the underlying protocol about incoming data length.
         * In case of HTTP it will add a sane Content-Length header. */
        ret = tcurl_set_option(tcurl_req, CURLOPT_INFILESIZE_LARGE,
                               (curl_off_t)sss_iobuf_get_size(body));
        if (ret != EOK) {
            goto done;
        }
    }

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(tcurl_req);
        return NULL;
    }

    return tcurl_req;
}

struct tcurl_request *tcurl_http(TALLOC_CTX *mem_ctx,
                                 enum tcurl_http_method method,
                                 const char *socket_path,
                                 const char *url,
                                 const char **headers,
                                 struct sss_iobuf *body)
{
    struct tcurl_request *tcurl_req;
    errno_t ret;

    tcurl_req = tcurl_request_create(mem_ctx, socket_path, url, headers, body);
    if (tcurl_req == NULL) {
        return NULL;
    }

    /* Set HTTP specific options. */

    ret = tcurl_set_option(tcurl_req, CURLOPT_HTTPHEADER, tcurl_req->headers);
    if (ret != EOK) {
        goto done;
    }

    switch (method) {
    case TCURL_HTTP_GET:
        /* Nothing to do here. GET is default. */
        break;
    case TCURL_HTTP_PUT:
        ret = tcurl_set_option(tcurl_req, CURLOPT_UPLOAD, 1L);
        if (ret != EOK) {
            goto done;
        }
        break;
    case TCURL_HTTP_POST:
        ret = tcurl_set_option(tcurl_req, CURLOPT_CUSTOMREQUEST, "POST");
        if (ret != EOK) {
            goto done;
        }
        break;
    case TCURL_HTTP_DELETE:
        ret = tcurl_set_option(tcurl_req, CURLOPT_CUSTOMREQUEST, "DELETE");
        if (ret != EOK) {
            goto done;
        }
        break;
    }

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(tcurl_req);
        return NULL;
    }

    return tcurl_req;
}

struct tevent_req *tcurl_http_send(TALLOC_CTX *mem_ctx,
                                   struct tevent_context *ev,
                                   struct tcurl_ctx *tcurl_ctx,
                                   enum tcurl_http_method method,
                                   const char *socket_path,
                                   const char *url,
                                   const char **headers,
                                   struct sss_iobuf *body,
                                   int timeout)
{
    struct tcurl_request *tcurl_req;
    struct tevent_req *req;

    tcurl_req = tcurl_http(mem_ctx, method, socket_path, url, headers, body);
    if (tcurl_req == NULL) {
        return NULL;
    }

    req = tcurl_request_send(mem_ctx, ev, tcurl_ctx, tcurl_req, timeout);
    if (req == NULL) {
        talloc_free(tcurl_req);
    }

    return req;
}

errno_t tcurl_http_recv(TALLOC_CTX *mem_ctx,
                        struct tevent_req *req,
                        int *_http_code,
                        struct sss_iobuf **_response)
{
    return tcurl_request_recv(mem_ctx, req, _response, _http_code);
}

errno_t tcurl_req_enable_rawoutput(struct tcurl_request *tcurl_req)
{
    return tcurl_set_option(tcurl_req, CURLOPT_HEADER, 1L);
}

errno_t tcurl_req_verify_peer(struct tcurl_request *tcurl_req,
                              const char *capath,
                              const char *cacert,
                              bool verify_peer,
                              bool verify_host)
{
    errno_t ret;

    long peer = verify_peer ? 1L : 0L;
    long host = verify_host ? 2L : 0L;

    ret = tcurl_set_option(tcurl_req, CURLOPT_SSL_VERIFYPEER, peer);
    if (ret != EOK) {
        return ret;
    }

    ret = tcurl_set_option(tcurl_req, CURLOPT_SSL_VERIFYHOST, host);
    if (ret != EOK) {
        return ret;
    }

    if (capath != NULL) {
        ret = tcurl_set_option(tcurl_req, CURLOPT_CAPATH, capath);
        if (ret != EOK) {
            return ret;
        }
    }

    if (cacert != NULL) {
        ret = tcurl_set_option(tcurl_req, CURLOPT_CAINFO, cacert);
        if (ret != EOK) {
            return ret;
        }
    }

    return EOK;
}

errno_t tcurl_req_set_client_cert(struct tcurl_request *tcurl_req,
                                  const char *cert,
                                  const char *key)
{
    errno_t ret;

    if (cert == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "You must specify client certificate!\n");
        return EINVAL;
    }

    ret = tcurl_set_option(tcurl_req, CURLOPT_SSLCERT, cert);
    if (ret != EOK) {
        return ret;
    }

    if (key != NULL) {
        /* If client's private key is in separate file. */
        ret = tcurl_set_option(tcurl_req, CURLOPT_SSLKEY, key);
        if (ret != EOK) {
            return ret;
        }
    }

    return EOK;
}