The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
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: 61410083f6986333cb55bb7a39af7872848035a7 $
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: 61410083f6986333cb55bb7a39af7872848035a7 $")
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
96
97extern const unsigned long http_curl_auth[REST_HTTP_AUTH_NUM_ENTRIES];
98
100extern size_t http_auth_table_len;
101
103extern size_t http_method_table_len;
104
106extern size_t http_body_type_table_len;
107
109extern size_t http_content_type_table_len;
110
111typedef struct {
112 char const *proxy; //!< Send request via this proxy.
113
114 char const *method_str; //!< The string version of the HTTP method.
115 http_method_t method; //!< What HTTP method should be used, GET, POST etc...
116
117 char const *body_str; //!< The string version of the encoding/content type.
118 http_body_type_t body; //!< What encoding type should be used.
119
120 bool auth_is_set; //!< Whether a value was provided for auth_str.
121
122 http_auth_type_t auth; //!< HTTP auth type.
123
124 bool require_auth; //!< Whether HTTP-Auth is required or not.
125
126 uint32_t chunk; //!< Max chunk-size (mainly for testing the encoders)
128
129typedef struct {
130 bool do_xlat; //!< Section default for the per-attribute "do_xlat"
131 //!< flag in JSON responses. Defaults to false, the
132 //!< API must opt in to expansion.
133 bool is_json; //!< Section default for the per-attribute "is_json"
134 //!< flag in JSON responses. If true, nested JSON data
135 //!< is copied to the attribute in string form.
136 //!< Defaults to false.
138
139typedef struct {
140 bool do_xlat; //!< If true, values in POST responses are xlat
141 //!< expanded before being assigned to attributes.
142 //!< Defaults to false.
144
145typedef struct {
146 char const *force_to_str; //!< Force decoding with this decoder.
147 http_body_type_t force_to; //!< Override the Content-Type header in the response
148 //!< to force decoding as a particular type.
149 bool accept_all; //!< Accept all content types.
150
151 size_t max_body_in; //!< Maximum size of incoming data.
152
153 rlm_rest_section_response_json_t json; //!< Configuration for the JSON decoder.
154 rlm_rest_section_response_post_t post; //!< Configuration for the POST decoder.
156
157/*
158 * Structure for section configuration
159 */
160typedef struct {
161 char const *name; //!< Section name.
162
163 fr_time_delta_t timeout; //!< Timeout timeval.
164 bool binary; //!< Do we expect binary data - so xlat will output octets.
165
166 rlm_rest_section_request_t request; //!< Request configuration.
167 rlm_rest_section_response_t response; //!< Response configuration.
168
171
172/*
173 * Structure for call_env found module calls
174 */
175typedef struct {
176 rlm_rest_section_t section; //!< Parsed section config
177 CONF_SECTION *cs; //!< Conf section found for this call
178 fr_rb_node_t node; //!< In tree of calls
180
181/*
182 * Structure for module configuration
183 */
184typedef struct {
185 char const *connect_proxy; //!< Send request via this proxy.
186
187 int http_negotiation; //!< What HTTP version to negotiate, and how to
188 ///< negotiate it. One or the CURL_HTTP_VERSION_ macros.
189
190 bool multiplex; //!< Whether to perform multiple requests using a single
191 ///< connection.
192
193 fr_curl_conn_config_t conn_config; //!< Configuration of slab allocated connection handles.
194
195 rlm_rest_section_t xlat; //!< Configuration specific to xlat.
196
197 fr_rb_tree_t sections; //!< Tree of sections with module call found by call_env parsing
198 bool sections_init; //!< Has the tree been initialised.
199
200#ifndef NDEBUG
201 bool fail_header_decode; //!< Force header decoding to fail for debugging purposes.
202 bool fail_body_decode; //!< Force body decoding to fail for debugging purposes.
203#endif
204} rlm_rest_t;
205
208
209/** Thread specific rlm_rest instance data
210 *
211 */
212typedef struct {
213 rlm_rest_t const *inst; //!< Instance of rlm_rest.
214 rest_slab_list_t *slab; //!< Slab list for connection handles.
215 fr_curl_handle_t *mhandle; //!< Thread specific multi handle. Serves as the dispatch
216 //!< and coralling structure for REST requests.
218
219/*
220 * States for stream based attribute encoders
221 */
228
229/*
230 * States for the response parser
231 */
238
239/*
240 * Outbound data context (passed to CURLOPT_READFUNCTION as CURLOPT_READDATA)
241 */
242typedef struct {
243 rlm_rest_t const *instance; //!< This instance of rlm_rest.
244 rlm_rest_section_t const *section; //!< Section configuration.
245
246 request_t *request; //!< Current request.
247 read_state_t state; //!< Encoder state
248
249 fr_dcursor_t cursor; //!< Cursor pointing to the start of the list to encode.
250
251 size_t chunk; //!< Chunk size
252
253 void *encoder; //!< Encoder specific data.
255
256/*
257 * Curl inbound data context (passed to CURLOPT_WRITEFUNCTION and
258 * CURLOPT_HEADERFUNCTION as CURLOPT_WRITEDATA and CURLOPT_HEADERDATA)
259 */
260typedef struct {
261 rlm_rest_t const *instance; //!< This instance of rlm_rest.
262 rlm_rest_section_t const *section; //!< Section configuration.
263
264 request_t *request; //!< Current request.
265 write_state_t state; //!< Decoder state.
266
267 char *buffer; //!< Raw incoming HTTP data.
268 size_t alloc; //!< Space allocated for buffer.
269 size_t used; //!< Space used in buffer.
270
271 int code; //!< HTTP Status Code.
272 http_body_type_t type; //!< HTTP Content Type.
273 http_body_type_t force_to; //!< Force decoding the body type as a particular encoding.
274
275 tmpl_t *header; //!< Where to create pairs representing HTTP response headers.
276 ///< If NULL no headers will be parsed other than content-type.
277
278 void *decoder; //!< Decoder specific data.
280
281/*
282 * Curl context data
283 */
284typedef struct {
285 struct curl_slist *headers; //!< Any HTTP headers which will be sent with the
286 //!< request.
287
288 char *body; //!< Pointer to the buffer which contains body data/
289 //!< Only used when not performing chunked encoding.
290
291 rlm_rest_request_t request; //!< Request context data.
292 rlm_rest_response_t response; //!< Response context data.
294
295/** Stores the state of a yielded xlat
296 *
297 */
298typedef struct {
299 rlm_rest_section_t section; //!< Our mutated section config.
300 fr_curl_io_request_t *handle; //!< curl easy handle servicing our request.
302
303typedef struct {
304 rlm_rest_section_conf_t *section; //!< Section config.
305 struct {
306 fr_value_box_t *uri; //!< URI to send HTTP request to.
307 fr_value_box_list_t *header; //!< Headers to place in the request
308 fr_value_box_t *data; //!< Custom data to send in requests.
309 fr_value_box_t *username; //!< Username to use for authentication
310 fr_value_box_t *password; //!< Password to use for authentication
311 } request;
312
313 struct {
314 tmpl_t *header; //!< Where to write response headers
315 } response;
317
318extern HIDDEN fr_dict_t const *dict_freeradius;
319
323
324/*
325 * Function prototype for rest_read_wrapper. Matches CURL's
326 * CURLOPT_READFUNCTION prototype.
327 */
328typedef size_t (*rest_read_t)(void *ptr, size_t size, size_t nmemb,
329 void *userdata);
330
331
332void *rest_mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout);
333
334/*
335 * Request processing API
336 */
337
339 char const *header, bool validate) CC_HINT(nonnull(1,2,3));
340
341int rest_request_config(module_ctx_t const *mctx, rlm_rest_section_t const *section,
342 request_t *request, fr_curl_io_request_t *randle, http_method_t method,
344 char const *uri, char const *body_data) CC_HINT(nonnull (1,2,4,7));
345
347 UNUSED rlm_rest_section_t const *section, request_t *request,
348 fr_curl_io_request_t *randle);
349
352
353#define rest_get_handle_code(_handle)(((rlm_rest_curl_context_t*)((fr_curl_io_request_t*)(_handle))->uctx)->response.code)
354
355#define rest_get_handle_type(_handle)(((rlm_rest_curl_context_t*)((fr_curl_io_request_t*)(_handle))->uctx)->response.type)
356
357size_t rest_get_handle_data(char const **out, fr_curl_io_request_t *handle);
358
360
361/*
362 * Helper functions
363 */
364size_t rest_uri_escape(UNUSED request_t *request, char *out, size_t outlen, char const *raw, UNUSED void *arg);
365ssize_t rest_uri_host_unescape(char **out, UNUSED rlm_rest_t const *mod_inst, request_t *request,
366 fr_curl_io_request_t *randle, char const *uri);
367
368/*
369 * Async IO helpers
370 */
371void rest_io_module_signal(module_ctx_t const *mctx, request_t *request, fr_signal_t action);
372void rest_io_xlat_signal(xlat_ctx_t const *xctx, request_t *request, fr_signal_t action);
#define RCSIDH(h, id)
Definition build.h:561
#define HIDDEN
Definition build.h:383
#define UNUSED
Definition build.h:384
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
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
The main red black tree structure.
Definition rb.h:71
rlm_rest_t const * instance
This instance of rlm_rest.
Definition rest.h:261
read_state_t state
Encoder state.
Definition rest.h:247
HIDDEN fr_dict_attr_t const * attr_rest_http_header
Definition rlm_rest.c:276
bool is_json
Section default for the per-attribute "is_json" flag in JSON responses.
Definition rest.h:133
http_auth_type_t auth
HTTP auth type.
Definition rest.h:122
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:1650
struct curl_slist * headers
Any HTTP headers which will be sent with the request.
Definition rest.h:285
rlm_rest_section_response_json_t json
Configuration for the JSON decoder.
Definition rest.h:153
tmpl_t * header
Where to create pairs representing HTTP response headers.
Definition rest.h:275
request_t * request
Current request.
Definition rest.h:264
char * buffer
Raw incoming HTTP data.
Definition rest.h:267
fr_table_num_sorted_t const http_auth_table[]
Definition rest.c:165
int code
HTTP Status Code.
Definition rest.h:271
fr_curl_handle_t * mhandle
Thread specific multi handle.
Definition rest.h:215
rlm_rest_section_response_post_t post
Configuration for the POST decoder.
Definition rest.h:154
write_state_t state
Decoder state.
Definition rest.h:265
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:201
char const * proxy
Send request via this proxy.
Definition rest.h:112
size_t used
Space used in buffer.
Definition rest.h:269
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:272
char * body
Pointer to the buffer which contains body data/ Only used when not performing chunked encoding.
Definition rest.h:288
fr_curl_tls_t tls
Definition rest.h:169
fr_rb_tree_t sections
Tree of sections with module call found by call_env parsing.
Definition rest.h:197
bool fail_body_decode
Force body decoding to fail for debugging purposes.
Definition rest.h:202
http_body_type_t force_to
Override the Content-Type header in the response to force decoding as a particular type.
Definition rest.h:147
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:151
fr_dcursor_t cursor
Cursor pointing to the start of the list to encode.
Definition rest.h:249
http_body_type_t force_to
Force decoding the body type as a particular encoding.
Definition rest.h:273
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:114
http_body_type_t rest_response_body_type_get(fr_curl_io_request_t *randle)
Return the body type of a HTTP response.
Definition rest.c:1666
read_state_t
Definition rest.h:222
@ READ_STATE_ATTR_CONT
Definition rest.h:225
@ READ_STATE_ATTR_BEGIN
Definition rest.h:224
@ READ_STATE_END
Definition rest.h:226
@ READ_STATE_INIT
Definition rest.h:223
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:262
fr_table_num_sorted_t const http_body_type_table[]
Conversion table for type config values.
Definition rest.c:149
fr_curl_conn_config_t conn_config
Configuration of slab allocated connection handles.
Definition rest.h:193
int http_negotiation
What HTTP version to negotiate, and how to.
Definition rest.h:187
HIDDEN fr_dict_t const * dict_freeradius
Definition base.c:75
char const * body_str
The string version of the encoding/content type.
Definition rest.h:117
HIDDEN fr_dict_attr_t const * attr_rest_http_body
Definition rlm_rest.c:275
rlm_rest_section_t section
Parsed section config.
Definition rest.h:176
fr_rb_node_t node
In tree of calls.
Definition rest.h:178
fr_table_num_sorted_t const http_method_table[]
Conversion table for method config values.
Definition rest.c:130
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:2206
char const * force_to_str
Force decoding with this decoder.
Definition rest.h:146
rlm_rest_section_conf_t * section
Section config.
Definition rest.h:304
rlm_rest_section_request_t request
Request configuration.
Definition rest.h:166
bool binary
Do we expect binary data - so xlat will output octets.
Definition rest.h:164
rlm_rest_response_t response
Response context data.
Definition rest.h:292
size_t(* rest_read_t)(void *ptr, size_t size, size_t nmemb, void *userdata)
Definition rest.h:328
void * decoder
Decoder specific data.
Definition rest.h:278
bool do_xlat
If true, values in POST responses are xlat expanded before being assigned to attributes.
Definition rest.h:140
bool auth_is_set
Whether a value was provided for auth_str.
Definition rest.h:120
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:1747
rlm_rest_section_t const * section
Section configuration.
Definition rest.h:244
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:1827
void * encoder
Encoder specific data.
Definition rest.h:253
rlm_rest_section_t xlat
Configuration specific to xlat.
Definition rest.h:195
HIDDEN fr_dict_attr_t const * attr_rest_http_status_code
Definition rlm_rest.c:277
void rest_response_debug(request_t *request, fr_curl_io_request_t *handle)
Print out the response text.
Definition rest.c:1593
bool accept_all
Accept all content types.
Definition rest.h:149
rlm_rest_request_t request
Request context data.
Definition rest.h:291
http_method_t method
What HTTP method should be used, GET, POST etc...
Definition rest.h:115
fr_table_num_sorted_t const http_content_type_table[]
Conversion table for "Content-Type" header values.
Definition rest.c:193
const unsigned long http_curl_auth[REST_HTTP_AUTH_NUM_ENTRIES]
Definition rest.c:104
bool sections_init
Has the tree been initialised.
Definition rest.h:198
rlm_rest_section_response_t response
Response configuration.
Definition rest.h:167
rlm_rest_section_t section
Our mutated section config.
Definition rest.h:299
const http_body_type_t http_body_type_supported[REST_HTTP_BODY_NUM_ENTRIES]
Table of encoder/decoder support.
Definition rest.c:47
fr_time_delta_t timeout
Timeout timeval.
Definition rest.h:163
bool do_xlat
Section default for the per-attribute "do_xlat" flag in JSON responses.
Definition rest.h:130
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:2180
write_state_t
Definition rest.h:232
@ WRITE_STATE_INIT
Definition rest.h:233
@ WRITE_STATE_PARSE_HEADERS
Definition rest.h:234
@ WRITE_STATE_PARSE_CONTENT
Definition rest.h:235
@ WRITE_STATE_DISCARD
Definition rest.h:236
bool multiplex
Whether to perform multiple requests using a single connection.
Definition rest.h:190
rlm_rest_t const * inst
Instance of rlm_rest.
Definition rest.h:213
char const * name
Section name.
Definition rest.h:161
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:1568
size_t alloc
Space allocated for buffer.
Definition rest.h:268
rest_slab_list_t * slab
Slab list for connection handles.
Definition rest.h:214
size_t http_body_type_table_len
Definition rest.c:163
char const * connect_proxy
Send request via this proxy.
Definition rest.h:185
size_t http_auth_table_len
Definition rest.c:177
size_t http_content_type_table_len
Definition rest.c:206
request_t * request
Current request.
Definition rest.h:246
http_body_type_t body
What encoding type should be used.
Definition rest.h:118
const bool http_body_type_binary[REST_HTTP_BODY_NUM_ENTRIES]
Table of which known body types are expected to give binary data.
Definition rest.c:69
size_t http_method_table_len
Definition rest.c:138
fr_curl_io_request_t * handle
curl easy handle servicing our request.
Definition rest.h:300
rlm_rest_t const * instance
This instance of rlm_rest.
Definition rest.h:243
CONF_SECTION * cs
Conf section found for this call.
Definition rest.h:177
char const * rest_no_proxy
Magic pointer value for determining if we should disable proxying.
Definition rlm_rest.c:78
uint32_t chunk
Max chunk-size (mainly for testing the encoders)
Definition rest.h:126
bool require_auth
Whether HTTP-Auth is required or not.
Definition rest.h:124
size_t chunk
Chunk size.
Definition rest.h:251
Definition rest.h:139
Thread specific rlm_rest instance data.
Definition rest.h:212
Stores the state of a yielded xlat.
Definition rest.h:298
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:124
#define FR_SLAB_TYPES(_name, _type)
Define type specific wrapper structs for slabs and slab elements.
Definition slab.h:75
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:1340
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030
An xlat calling ctx.
Definition xlat_ctx.h:49