The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rest.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/**
19 * $Id: d85c90ee61626d54fe3b3a6cd60ca6ace3eadc75 $
20 *
21 * @brief Function prototypes and datatypes for the REST (HTTP) transport.
22 * @file rest.h
23 *
24 * @copyright 2012-2016 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25 */
26RCSIDH(rest_h, "$Id: d85c90ee61626d54fe3b3a6cd60ca6ace3eadc75 $")
27
28#include <freeradius-devel/curl/base.h>
29#include <freeradius-devel/curl/config.h>
30#include <freeradius-devel/server/pairmove.h>
31#include <freeradius-devel/util/slab.h>
32
33/*
34 * The common JSON library (also tells us if we have json-c)
35 */
36#include <freeradius-devel/json/base.h>
37
38#define REST_URI_MAX_LEN 2048
39#define REST_BODY_MAX_LEN 8192
40#define REST_BODY_ALLOC_CHUNK 1024
41#define REST_BODY_MAX_ATTRS 256
42
52
69
84
85/** Magic pointer value for determining if we should disable proxying
86 */
87extern char const *rest_no_proxy;
88
89/*
90 * Must be updated (in rest.c) if additional values are added to
91 * http_body_type_t
92 */
94
95extern const unsigned long http_curl_auth[REST_HTTP_AUTH_NUM_ENTRIES];
96
98extern size_t http_auth_table_len;
99
101extern size_t http_method_table_len;
102
104extern size_t http_body_type_table_len;
105
107extern size_t http_content_type_table_len;
108
109typedef struct {
110 char const *proxy; //!< Send request via this proxy.
111
112 char const *method_str; //!< The string version of the HTTP method.
113 http_method_t method; //!< What HTTP method should be used, GET, POST etc...
114
115 char const *body_str; //!< The string version of the encoding/content type.
116 http_body_type_t body; //!< What encoding type should be used.
117
118 bool auth_is_set; //!< Whether a value was provided for auth_str.
119
120 http_auth_type_t auth; //!< HTTP auth type.
121
122 bool require_auth; //!< Whether HTTP-Auth is required or not.
123
124 uint32_t chunk; //!< Max chunk-size (mainly for testing the encoders)
126
127typedef struct {
128 char const *force_to_str; //!< Force decoding with this decoder.
129 http_body_type_t force_to; //!< Override the Content-Type header in the response
130 //!< to force decoding as a particular type.
131
132 size_t max_body_in; //!< Maximum size of incoming data.
134
135/*
136 * Structure for section configuration
137 */
138typedef struct {
139 char const *name; //!< Section name.
140
141 fr_time_delta_t timeout; //!< Timeout timeval.
142
143 rlm_rest_section_request_t request; //!< Request configuration.
144 rlm_rest_section_response_t response; //!< Response configuration.
145
148
149/*
150 * Structure for module configuration
151 */
152typedef struct {
153 char const *connect_proxy; //!< Send request via this proxy.
154
155 int http_negotiation; //!< What HTTP version to negotiate, and how to
156 ///< negotiate it. One or the CURL_HTTP_VERSION_ macros.
157
158 bool multiplex; //!< Whether to perform multiple requests using a single
159 ///< connection.
160
161 fr_curl_conn_config_t conn_config; //!< Configuration of slab allocated connection handles.
162
163 rlm_rest_section_t xlat; //!< Configuration specific to xlat.
164 rlm_rest_section_t authorize; //!< Configuration specific to authorisation.
165 rlm_rest_section_t authenticate; //!< Configuration specific to authentication.
166 rlm_rest_section_t accounting; //!< Configuration specific to accounting.
167 rlm_rest_section_t post_auth; //!< Configuration specific to Post-auth
168
169#ifndef NDEBUG
170 bool fail_header_decode; //!< Force header decoding to fail for debugging purposes.
171 bool fail_body_decode; //!< Force body decoding to fail for debugging purposes.
172#endif
173} rlm_rest_t;
174
177
178/** Thread specific rlm_rest instance data
179 *
180 */
181typedef struct {
182 rlm_rest_t const *inst; //!< Instance of rlm_rest.
183 rest_slab_list_t *slab; //!< Slab list for connection handles.
184 fr_curl_handle_t *mhandle; //!< Thread specific multi handle. Serves as the dispatch
185 //!< and coralling structure for REST requests.
187
188/*
189 * States for stream based attribute encoders
190 */
197
198/*
199 * States for the response parser
200 */
207
208/*
209 * Outbound data context (passed to CURLOPT_READFUNCTION as CURLOPT_READDATA)
210 */
211typedef struct {
212 rlm_rest_t const *instance; //!< This instance of rlm_rest.
213 rlm_rest_section_t const *section; //!< Section configuration.
214
215 request_t *request; //!< Current request.
216 read_state_t state; //!< Encoder state
217
218 fr_dcursor_t cursor; //!< Cursor pointing to the start of the list to encode.
219
220 size_t chunk; //!< Chunk size
221
222 void *encoder; //!< Encoder specific data.
224
225/*
226 * Curl inbound data context (passed to CURLOPT_WRITEFUNCTION and
227 * CURLOPT_HEADERFUNCTION as CURLOPT_WRITEDATA and CURLOPT_HEADERDATA)
228 */
229typedef struct {
230 rlm_rest_t const *instance; //!< This instance of rlm_rest.
231 rlm_rest_section_t const *section; //!< Section configuration.
232
233 request_t *request; //!< Current request.
234 write_state_t state; //!< Decoder state.
235
236 char *buffer; //!< Raw incoming HTTP data.
237 size_t alloc; //!< Space allocated for buffer.
238 size_t used; //!< Space used in buffer.
239
240 int code; //!< HTTP Status Code.
241 http_body_type_t type; //!< HTTP Content Type.
242 http_body_type_t force_to; //!< Force decoding the body type as a particular encoding.
243
244 tmpl_t *header; //!< Where to create pairs representing HTTP response headers.
245 ///< If NULL no headers will be parsed other than content-type.
246
247 void *decoder; //!< Decoder specific data.
249
250/*
251 * Curl context data
252 */
253typedef struct {
254 struct curl_slist *headers; //!< Any HTTP headers which will be sent with the
255 //!< request.
256
257 char *body; //!< Pointer to the buffer which contains body data/
258 //!< Only used when not performing chunked encoding.
259
260 rlm_rest_request_t request; //!< Request context data.
261 rlm_rest_response_t response; //!< Response context data.
263
264/** Stores the state of a yielded xlat
265 *
266 */
267typedef struct {
268 rlm_rest_section_t section; //!< Our mutated section config.
269 fr_curl_io_request_t *handle; //!< curl easy handle servicing our request.
271
272typedef struct {
273 struct {
274 fr_value_box_t *uri; //!< URI to send HTTP request to.
275 fr_value_box_list_t *header; //!< Headers to place in the request
276 fr_value_box_t *data; //!< Custom data to send in requests.
277 fr_value_box_t *username; //!< Username to use for authentication
278 fr_value_box_t *password; //!< Password to use for authentication
279 } request;
280
281 struct {
282 tmpl_t *header; //!< Where to write response headers
283 } response;
285
286extern HIDDEN fr_dict_t const *dict_freeradius;
287
291
292/*
293 * Function prototype for rest_read_wrapper. Matches CURL's
294 * CURLOPT_READFUNCTION prototype.
295 */
296typedef size_t (*rest_read_t)(void *ptr, size_t size, size_t nmemb,
297 void *userdata);
298
299
300void *rest_mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout);
301
302/*
303 * Request processing API
304 */
305
307 char const *header, bool validate) CC_HINT(nonnull(1,2,3));
308
309int rest_request_config(module_ctx_t const *mctx, rlm_rest_section_t const *section,
310 request_t *request, fr_curl_io_request_t *randle, http_method_t method,
312 char const *uri, char const *body_data) CC_HINT(nonnull (1,2,4,7));
313
315 UNUSED rlm_rest_section_t const *section, request_t *request,
316 fr_curl_io_request_t *randle);
317
320
321#define rest_get_handle_code(_handle)(((rlm_rest_curl_context_t*)((fr_curl_io_request_t*)(_handle))->uctx)->response.code)
322
323#define rest_get_handle_type(_handle)(((rlm_rest_curl_context_t*)((fr_curl_io_request_t*)(_handle))->uctx)->response.type)
324
325size_t rest_get_handle_data(char const **out, fr_curl_io_request_t *handle);
326
327/*
328 * Helper functions
329 */
330size_t rest_uri_escape(UNUSED request_t *request, char *out, size_t outlen, char const *raw, UNUSED void *arg);
331ssize_t rest_uri_host_unescape(char **out, UNUSED rlm_rest_t const *mod_inst, request_t *request,
332 fr_curl_io_request_t *randle, char const *uri);
333
334/*
335 * Async IO helpers
336 */
337void rest_io_module_signal(module_ctx_t const *mctx, request_t *request, fr_signal_t action);
338void rest_io_xlat_signal(xlat_ctx_t const *xctx, request_t *request, fr_signal_t action);
#define RCSIDH(h, id)
Definition build.h:486
#define HIDDEN
Definition build.h:316
#define UNUSED
Definition build.h:317
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
unsigned int uint32_t
long int ssize_t
unsigned long int size_t
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
rlm_rest_t const * instance
This instance of rlm_rest.
Definition rest.h:230
read_state_t state
Encoder state.
Definition rest.h:216
HIDDEN fr_dict_attr_t const * attr_rest_http_header
Definition rlm_rest.c:285
http_auth_type_t auth
HTTP auth type.
Definition rest.h:120
size_t rest_get_handle_data(char const **out, fr_curl_io_request_t *handle)
Extracts pointer to buffer containing response data.
Definition rest.c:1622
struct curl_slist * headers
Any HTTP headers which will be sent with the request.
Definition rest.h:254
tmpl_t * header
Where to create pairs representing HTTP response headers.
Definition rest.h:244
request_t * request
Current request.
Definition rest.h:233
char * buffer
Raw incoming HTTP data.
Definition rest.h:236
fr_table_num_sorted_t const http_auth_table[]
Definition rest.c:163
int code
HTTP Status Code.
Definition rest.h:240
fr_curl_handle_t * mhandle
Thread specific multi handle.
Definition rest.h:184
write_state_t state
Decoder state.
Definition rest.h:234
void rest_io_module_signal(module_ctx_t const *mctx, request_t *request, fr_signal_t action)
bool fail_header_decode
Force header decoding to fail for debugging purposes.
Definition rest.h:170
char const * proxy
Send request via this proxy.
Definition rest.h:110
size_t used
Space used in buffer.
Definition rest.h:238
int rest_response_decode(rlm_rest_t const *instance, UNUSED rlm_rest_section_t const *section, request_t *request, fr_curl_io_request_t *randle)
http_body_type_t type
HTTP Content Type.
Definition rest.h:241
char * body
Pointer to the buffer which contains body data/ Only used when not performing chunked encoding.
Definition rest.h:257
fr_curl_tls_t tls
Definition rest.h:146
bool fail_body_decode
Force body decoding to fail for debugging purposes.
Definition rest.h:171
http_body_type_t force_to
Override the Content-Type header in the response to force decoding as a particular type.
Definition rest.h:129
http_method_t
Definition rest.h:43
@ REST_HTTP_METHOD_PATCH
Definition rest.h:48
@ REST_HTTP_METHOD_DELETE
Definition rest.h:49
@ REST_HTTP_METHOD_PUT
Definition rest.h:47
@ REST_HTTP_METHOD_POST
Definition rest.h:46
@ REST_HTTP_METHOD_UNKNOWN
Definition rest.h:44
@ REST_HTTP_METHOD_CUSTOM
Must always come last, should not be in method table.
Definition rest.h:50
@ REST_HTTP_METHOD_GET
Definition rest.h:45
size_t max_body_in
Maximum size of incoming data.
Definition rest.h:132
fr_dcursor_t cursor
Cursor pointing to the start of the list to encode.
Definition rest.h:218
http_body_type_t force_to
Force decoding the body type as a particular encoding.
Definition rest.h:242
http_body_type_t
Definition rest.h:53
@ REST_HTTP_BODY_HTML
Definition rest.h:64
@ REST_HTTP_BODY_PLAIN
Definition rest.h:65
@ REST_HTTP_BODY_JSON
Definition rest.h:61
@ REST_HTTP_BODY_INVALID
Definition rest.h:57
@ REST_HTTP_BODY_XML
Definition rest.h:62
@ REST_HTTP_BODY_UNSUPPORTED
Definition rest.h:55
@ REST_HTTP_BODY_YAML
Definition rest.h:63
@ REST_HTTP_BODY_POST
Definition rest.h:60
@ REST_HTTP_BODY_CUSTOM
Definition rest.h:59
@ REST_HTTP_BODY_NUM_ENTRIES
Definition rest.h:67
@ REST_HTTP_BODY_CRL
Definition rest.h:66
@ REST_HTTP_BODY_UNKNOWN
Definition rest.h:54
@ REST_HTTP_BODY_NONE
Definition rest.h:58
@ REST_HTTP_BODY_UNAVAILABLE
Definition rest.h:56
void * rest_mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout)
char const * method_str
The string version of the HTTP method.
Definition rest.h:112
read_state_t
Definition rest.h:191
@ READ_STATE_ATTR_CONT
Definition rest.h:194
@ READ_STATE_ATTR_BEGIN
Definition rest.h:193
@ READ_STATE_END
Definition rest.h:195
@ READ_STATE_INIT
Definition rest.h:192
void rest_io_xlat_signal(xlat_ctx_t const *xctx, request_t *request, fr_signal_t action)
Handle asynchronous cancellation of a request.
Definition io.c:56
rlm_rest_section_t const * section
Section configuration.
Definition rest.h:231
fr_table_num_sorted_t const http_body_type_table[]
Conversion table for type config values.
Definition rest.c:147
fr_curl_conn_config_t conn_config
Configuration of slab allocated connection handles.
Definition rest.h:161
int http_negotiation
What HTTP version to negotiate, and how to.
Definition rest.h:155
HIDDEN fr_dict_t const * dict_freeradius
Definition base.c:77
char const * body_str
The string version of the encoding/content type.
Definition rest.h:115
HIDDEN fr_dict_attr_t const * attr_rest_http_body
Definition rlm_rest.c:284
fr_table_num_sorted_t const http_method_table[]
Conversion table for method config values.
Definition rest.c:128
ssize_t rest_uri_host_unescape(char **out, UNUSED rlm_rest_t const *mod_inst, request_t *request, fr_curl_io_request_t *randle, char const *uri)
Unescapes the host portion of a URI string.
Definition rest.c:2165
char const * force_to_str
Force decoding with this decoder.
Definition rest.h:128
rlm_rest_section_request_t request
Request configuration.
Definition rest.h:143
rlm_rest_response_t response
Response context data.
Definition rest.h:261
size_t(* rest_read_t)(void *ptr, size_t size, size_t nmemb, void *userdata)
Definition rest.h:296
void * decoder
Decoder specific data.
Definition rest.h:247
bool auth_is_set
Whether a value was provided for auth_str.
Definition rest.h:118
int rest_request_config_add_header(request_t *request, fr_curl_io_request_t *randle, char const *header, bool validate))
Adds an additional header to a handle to use in the next reques.
Definition rest.c:1706
rlm_rest_section_t const * section
Section configuration.
Definition rest.h:213
int rest_request_config(module_ctx_t const *mctx, rlm_rest_section_t const *section, request_t *request, fr_curl_io_request_t *randle, http_method_t method, http_body_type_t type, char const *uri, char const *body_data))
Configures request curlopts.
Definition rest.c:1786
void * encoder
Encoder specific data.
Definition rest.h:222
rlm_rest_section_t xlat
Configuration specific to xlat.
Definition rest.h:163
HIDDEN fr_dict_attr_t const * attr_rest_http_status_code
Definition rlm_rest.c:286
void rest_response_debug(request_t *request, fr_curl_io_request_t *handle)
Print out the response text.
Definition rest.c:1565
rlm_rest_request_t request
Request context data.
Definition rest.h:260
http_method_t method
What HTTP method should be used, GET, POST etc...
Definition rest.h:113
fr_table_num_sorted_t const http_content_type_table[]
Conversion table for "Content-Type" header values.
Definition rest.c:191
const unsigned long http_curl_auth[REST_HTTP_AUTH_NUM_ENTRIES]
Definition rest.c:102
rlm_rest_section_response_t response
Response configuration.
Definition rest.h:144
rlm_rest_section_t section
Our mutated section config.
Definition rest.h:268
const http_body_type_t http_body_type_supported[REST_HTTP_BODY_NUM_ENTRIES]
Table of encoder/decoder support.
Definition rest.c:52
fr_time_delta_t timeout
Timeout timeval.
Definition rest.h:141
size_t rest_uri_escape(UNUSED request_t *request, char *out, size_t outlen, char const *raw, UNUSED void *arg)
URL encodes a string.
Definition rest.c:2139
write_state_t
Definition rest.h:201
@ WRITE_STATE_INIT
Definition rest.h:202
@ WRITE_STATE_PARSE_HEADERS
Definition rest.h:203
@ WRITE_STATE_PARSE_CONTENT
Definition rest.h:204
@ WRITE_STATE_DISCARD
Definition rest.h:205
bool multiplex
Whether to perform multiple requests using a single connection.
Definition rest.h:158
rlm_rest_t const * inst
Instance of rlm_rest.
Definition rest.h:182
rlm_rest_section_t accounting
Configuration specific to accounting.
Definition rest.h:166
char const * name
Section name.
Definition rest.h:139
http_auth_type_t
Definition rest.h:70
@ REST_HTTP_AUTH_NTLM_WB
Definition rest.h:79
@ REST_HTTP_AUTH_NUM_ENTRIES
Definition rest.h:82
@ REST_HTTP_AUTH_BASIC
Definition rest.h:74
@ REST_HTTP_AUTH_NTLM
Definition rest.h:78
@ REST_HTTP_AUTH_DIGEST
Definition rest.h:75
@ REST_HTTP_AUTH_TLS_SRP
Definition rest.h:73
@ REST_HTTP_AUTH_UNKNOWN
Definition rest.h:71
@ REST_HTTP_AUTH_GSSNEGOTIATE
Definition rest.h:77
@ REST_HTTP_AUTH_ANY
Definition rest.h:80
@ REST_HTTP_AUTH_NONE
Definition rest.h:72
@ REST_HTTP_AUTH_DIGEST_IE
Definition rest.h:76
@ REST_HTTP_AUTH_ANY_SAFE
Definition rest.h:81
void rest_response_error(request_t *request, fr_curl_io_request_t *handle)
Print out the response text as error lines.
Definition rest.c:1540
size_t alloc
Space allocated for buffer.
Definition rest.h:237
rest_slab_list_t * slab
Slab list for connection handles.
Definition rest.h:183
size_t http_body_type_table_len
Definition rest.c:161
char const * connect_proxy
Send request via this proxy.
Definition rest.h:153
rlm_rest_section_t post_auth
Configuration specific to Post-auth.
Definition rest.h:167
size_t http_auth_table_len
Definition rest.c:175
size_t http_content_type_table_len
Definition rest.c:203
request_t * request
Current request.
Definition rest.h:215
http_body_type_t body
What encoding type should be used.
Definition rest.h:116
rlm_rest_section_t authorize
Configuration specific to authorisation.
Definition rest.h:164
size_t http_method_table_len
Definition rest.c:136
fr_curl_io_request_t * handle
curl easy handle servicing our request.
Definition rest.h:269
rlm_rest_t const * instance
This instance of rlm_rest.
Definition rest.h:212
char const * rest_no_proxy
Magic pointer value for determining if we should disable proxying.
Definition rlm_rest.c:95
rlm_rest_section_t authenticate
Configuration specific to authentication.
Definition rest.h:165
uint32_t chunk
Max chunk-size (mainly for testing the encoders)
Definition rest.h:124
bool require_auth
Whether HTTP-Auth is required or not.
Definition rest.h:122
size_t chunk
Chunk size.
Definition rest.h:220
Thread specific rlm_rest instance data.
Definition rest.h:181
Stores the state of a yielded xlat.
Definition rest.h:267
username
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
#define FR_SLAB_FUNCS(_name, _type)
Define type specific wrapper functions for slabs and slab elements.
Definition slab.h:120
#define FR_SLAB_TYPES(_name, _type)
Define type specific wrapper structs for slabs and slab elements.
Definition slab.h:72
fr_aka_sim_id_type_t type
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
static fr_slen_t data
Definition value.h:1288
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1020
An xlat calling ctx.
Definition xlat_ctx.h:49