The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
base.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /** Main include file for our libcurl extension API
19  *
20  * @file src/lib/curl/base.h
21  *
22  * @copyright 2019 The FreeRADIUS project
23  * @copyright 2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24  */
25 
26 RCSIDH(curl_h, "$Id: 2bbce6775e3d55b80e7ac288af50420d6e323d0c $")
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define CURL_NO_OLDIES 1
33 
34 #include <curl/curl.h>
35 #include <freeradius-devel/server/global_lib.h>
36 #include <freeradius-devel/server/module.h>
37 #include <freeradius-devel/server/request.h>
38 #include <freeradius-devel/util/event.h>
39 #include <freeradius-devel/util/slab.h>
40 #include <freeradius-devel/unlang/xlat.h>
41 
43 DIAG_OFF(disabled-macro-expansion)
45 #define FR_CURL_SET_OPTION(_x, _y)\
46 do {\
47  int _ret;\
48  if ((_ret = curl_easy_setopt(randle->candle, _x, _y)) != CURLE_OK) {\
49  char const *_option;\
50  _option = STRINGIFY(_x);\
51  ERROR("Failed setting curl option %s: %s (%i)", _option, curl_easy_strerror(_ret), _ret);\
52  goto error;\
53  }\
54 } while (0)
55 
56 #define FR_CURL_ROPTIONAL_SET_OPTION(_x, _y)\
57 do {\
58  int _ret;\
59  if ((_ret = curl_easy_setopt(randle->candle, _x, _y)) != CURLE_OK) {\
60  char const *_option;\
61  _option = STRINGIFY(_x);\
62  ROPTIONAL(RERROR, ERROR, "Failed setting curl option %s: %s (%i)", _option, curl_easy_strerror(_ret), _ret);\
63  goto error;\
64  }\
65 } while (0)
66 
67 #define FR_CURL_REQUEST_SET_OPTION(_x, _y)\
68 do {\
69  int _ret;\
70  if ((_ret = curl_easy_setopt(randle->candle, _x, _y)) != CURLE_OK) {\
71  char const *_option;\
72  _option = STRINGIFY(_x);\
73  RERROR("Failed setting curl option %s: %s (%i)", _option, curl_easy_strerror(_ret), _ret);\
74  goto error;\
75  }\
76 } while (0)
77 
78 /*
79  * We have to use this as curl uses lots of enums
80  */
81 #ifndef CURL_AT_LEAST_VERSION
82 # define CURL_VERSION_BITS(x, y, z) ((x) << 16 | (y) << 8 | (z))
83 # define CURL_AT_LEAST_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= CURL_VERSION_BITS(x, y, z))
84 #endif
85 
86 /** Uctx data for timer and I/O functions
87  *
88  * Seems like overkill for a single field, but I'm sure we'll need to
89  * pass around other things later.
90  */
91 typedef struct {
92  fr_event_list_t *el; //!< Event list servicing I/O events.
93  fr_event_timer_t const *ev; //!< Multi-Handle timer.
94  uint64_t transfers; //!< How many transfers are current in progress.
95  CURLM *mandle; //!< The multi handle.
97 
98 /** Structure representing an individual request being passed to curl for processing
99  *
100  */
101 typedef struct {
102  CURL *candle; //!< Request specific handle.
103  CURLcode result; //!< Result of executing the request.
104  request_t *request; //!< Current request.
105  void *uctx; //!< Private data for the module using the API.
107 
108 typedef struct {
109  char const *certificate_file;
110  char const *private_key_file;
111  char const *private_key_password;
112  char const *ca_file;
113  char const *ca_issuer_file;
114  char const *ca_path;
115  char const *random_file;
120  char const *keylog_file; //!< Where to log SSL keying material like the pre-master secret.
121  ///< The log file can then be used to decrypt the SSL session
122  ///< with Wireshark.
123 } fr_curl_tls_t;
124 
125 typedef struct {
129 
133 
135  request_t *request, fr_curl_io_request_t *creq);
136 
138 
139 fr_curl_handle_t *fr_curl_io_init(TALLOC_CTX *ctx, fr_event_list_t *el, bool multiplex);
140 
142 
144 
145 CURL *fr_curl_tmp_handle(void);
146 #ifdef __cplusplus
147 }
148 #endif
#define DIAG_UNKNOWN_PRAGMAS
Definition: build.h:454
#define DIAG_ON(_x)
Definition: build.h:456
#define RCSIDH(h, id)
Definition: build.h:482
#define DIAG_OFF(_x)
Definition: build.h:455
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:564
long * require_cert
Definition: base.h:116
char const * private_key_password
Definition: base.h:111
fr_event_list_t * el
Event list servicing I/O events.
Definition: base.h:92
char const * private_key_file
Definition: base.h:110
char const * certificate_file
Definition: base.h:109
int fr_curl_response_certinfo(request_t *request, fr_curl_io_request_t *randle)
Definition: base.c:172
char const * keylog_file
Where to log SSL keying material like the pre-master secret.
Definition: base.h:120
fr_curl_handle_t * fr_curl_io_init(TALLOC_CTX *ctx, fr_event_list_t *el, bool multiplex)
CURLcode result
Result of executing the request.
Definition: base.h:103
int fr_curl_easy_tls_init(fr_curl_io_request_t *randle, fr_curl_tls_t const *conf)
Definition: base.c:141
bool check_cert_cn
Definition: base.h:118
char const * ca_issuer_file
Definition: base.h:113
uint64_t transfers
How many transfers are current in progress.
Definition: base.h:94
bool check_cert
Definition: base.h:117
fr_slab_config_t reuse
Definition: base.h:126
char const * ca_path
Definition: base.h:114
char const * ca_file
Definition: base.h:112
bool extract_cert_attrs
Definition: base.h:119
global_lib_autoinst_t fr_curl_autoinst
Definition: base.c:400
CURLM * mandle
The multi handle.
Definition: base.h:95
request_t * request
Current request.
Definition: base.h:104
fr_event_timer_t const * ev
Multi-Handle timer.
Definition: base.h:93
conf_parser_t fr_curl_conn_config[]
Definition: base.c:99
char const * random_file
Definition: base.h:115
fr_curl_io_request_t * fr_curl_io_request_alloc(TALLOC_CTX *ctx)
Allocate a new curl easy request and wrapper struct.
Definition: io.c:546
fr_time_delta_t connect_timeout
Definition: base.h:127
CURL * fr_curl_tmp_handle(void)
Return a thread local curl easy handle.
Definition: base.c:280
void * uctx
Private data for the module using the API.
Definition: base.h:105
conf_parser_t fr_curl_tls_config[]
Definition: base.c:68
int fr_curl_io_request_enqueue(fr_curl_handle_t *mhandle, request_t *request, fr_curl_io_request_t *creq)
Sends a request using libcurl.
Definition: io.c:482
CURL * candle
Request specific handle.
Definition: base.h:102
Uctx data for timer and I/O functions.
Definition: base.h:91
Structure representing an individual request being passed to curl for processing.
Definition: base.h:101
Structure to define how to initialise libraries with global configuration.
Definition: global_lib.h:38
Stores all information relating to an event list.
Definition: event.c:411
A timer event.
Definition: event.c:102
static rs_t * conf
Definition: radsniff.c:53
Tuneable parameters for slabs.
Definition: slab.h:42
A time delta, a difference in time measured in nanoseconds.
Definition: time.h:80
static fr_event_list_t * el