The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rest.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: ec936891dfb0f216f0b5a8bd8110c11f94f12d65 $
19 *
20 * @brief Functions and datatypes for the REST (HTTP) transport.
21 * @file rest.c
22 *
23 * @copyright 2012-2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25RCSID("$Id: ec936891dfb0f216f0b5a8bd8110c11f94f12d65 $")
26
27#define LOG_PREFIX mctx->mi->name
28
29#include <ctype.h>
30#include <string.h>
31#include <time.h>
32
33#include <freeradius-devel/server/base.h>
34#include <freeradius-devel/server/log.h>
35#include <freeradius-devel/server/pool.h>
36#include <freeradius-devel/server/tmpl.h>
37#include <freeradius-devel/unlang/call.h>
38#include <freeradius-devel/util/value.h>
39
40#include <talloc.h>
41
42#include "rest.h"
43
44/** Table of encoder/decoder support.
45 *
46 * Indexes in this table match the http_body_type_t enum, and should be
47 * updated if additional enum values are added.
48 *
49 * @see http_body_type_t
50 */
52 REST_HTTP_BODY_UNKNOWN, // REST_HTTP_BODY_UNKNOWN
53 REST_HTTP_BODY_UNSUPPORTED, // REST_HTTP_BODY_UNSUPPORTED
54 REST_HTTP_BODY_UNSUPPORTED, // REST_HTTP_BODY_UNAVAILABLE
55 REST_HTTP_BODY_UNSUPPORTED, // REST_HTTP_BODY_INVALID
56 REST_HTTP_BODY_NONE, // REST_HTTP_BODY_NONE
57 REST_HTTP_BODY_CUSTOM, // REST_HTTP_BODY_CUSTOM
58 REST_HTTP_BODY_POST, // REST_HTTP_BODY_POST
59#ifdef HAVE_JSON
60 REST_HTTP_BODY_JSON, // REST_HTTP_BODY_JSON
61#else
63#endif
64 REST_HTTP_BODY_UNSUPPORTED, // REST_HTTP_BODY_XML
65 REST_HTTP_BODY_UNSUPPORTED, // REST_HTTP_BODY_YAML
66 REST_HTTP_BODY_INVALID, // REST_HTTP_BODY_HTML
67 REST_HTTP_BODY_PLAIN // REST_HTTP_BODY_PLAIN
68};
69
70/*
71 * Lib CURL doesn't define symbols for unsupported auth methods
72 */
73#ifndef CURLOPT_TLSAUTH_SRP
74# define CURLOPT_TLSAUTH_SRP 0
75#endif
76#ifndef CURLAUTH_BASIC
77# define CURLAUTH_BASIC 0
78#endif
79#ifndef CURLAUTH_DIGEST
80# define CURLAUTH_DIGEST 0
81#endif
82#ifndef CURLAUTH_DIGEST_IE
83# define CURLAUTH_DIGEST_IE 0
84#endif
85#ifndef CURLAUTH_GSSNEGOTIATE
86# define CURLAUTH_GSSNEGOTIATE 0
87#endif
88#ifndef CURLAUTH_NTLM
89# define CURLAUTH_NTLM 0
90#endif
91#ifndef CURLAUTH_NTLM_WB
92# define CURLAUTH_NTLM_WB 0
93#endif
94
95/*
96 * CURL headers do:
97 *
98 * #define curl_easy_setopt(handle,opt,param) curl_easy_setopt(handle,opt,param)
99 */
113
114/** Conversion table for method config values.
115 *
116 * HTTP verb strings for http_method_t enum values. Used by libcurl in the
117 * status line of the outgoing HTTP header, by rest_response_header for decoding
118 * incoming HTTP responses, and by the configuration parser.
119 *
120 * @note must be kept in sync with http_method_t enum.
121 *
122 * @see http_method_t
123 * @see fr_table_value_by_str
124 * @see fr_table_str_by_value
125 */
127 { L("DELETE"), REST_HTTP_METHOD_DELETE },
128 { L("GET"), REST_HTTP_METHOD_GET },
129 { L("PATCH"), REST_HTTP_METHOD_PATCH },
130 { L("POST"), REST_HTTP_METHOD_POST },
131 { L("PUT"), REST_HTTP_METHOD_PUT },
132 { L("UNKNOWN"), REST_HTTP_METHOD_UNKNOWN }
133};
135
136/** Conversion table for type config values.
137 *
138 * Textual names for http_body_type_t enum values, used by the
139 * configuration parser.
140 *
141 * @see http_body_Type_t
142 * @see fr_table_value_by_str
143 * @see fr_table_str_by_value
144 */
146 { L("html"), REST_HTTP_BODY_HTML },
147 { L("invalid"), REST_HTTP_BODY_INVALID },
148 { L("json"), REST_HTTP_BODY_JSON },
149 { L("none"), REST_HTTP_BODY_NONE },
150 { L("plain"), REST_HTTP_BODY_PLAIN },
151 { L("post"), REST_HTTP_BODY_POST },
152 { L("unavailable"), REST_HTTP_BODY_UNAVAILABLE },
153 { L("unknown"), REST_HTTP_BODY_UNKNOWN },
154 { L("unsupported"), REST_HTTP_BODY_UNSUPPORTED },
155 { L("xml"), REST_HTTP_BODY_XML },
156 { L("yaml"), REST_HTTP_BODY_YAML }
157};
159
161 { L("any"), REST_HTTP_AUTH_ANY },
162 { L("basic"), REST_HTTP_AUTH_BASIC },
163 { L("digest"), REST_HTTP_AUTH_DIGEST },
164 { L("digest-ie"), REST_HTTP_AUTH_DIGEST_IE },
165 { L("gss-negotiate"), REST_HTTP_AUTH_GSSNEGOTIATE },
166 { L("none"), REST_HTTP_AUTH_NONE },
167 { L("ntlm"), REST_HTTP_AUTH_NTLM },
168 { L("ntlm-winbind"), REST_HTTP_AUTH_NTLM_WB },
169 { L("safe"), REST_HTTP_AUTH_ANY_SAFE },
170 { L("srp"), REST_HTTP_AUTH_TLS_SRP }
171};
173
174/** Conversion table for "Content-Type" header values.
175 *
176 * Used by rest_response_header for parsing incoming headers.
177 *
178 * Values we expect to see in the 'Content-Type:' header of the incoming
179 * response.
180 *
181 * Some data types (like YAML) do no have standard MIME types defined,
182 * so multiple types, are listed here.
183 *
184 * @see http_body_Type_t
185 * @see fr_table_value_by_str
186 * @see fr_table_str_by_value
187 */
189 { L("application/json"), REST_HTTP_BODY_JSON },
190 { L("application/x-www-form-urlencoded"), REST_HTTP_BODY_POST },
191 { L("application/x-yaml"), REST_HTTP_BODY_YAML },
192 { L("application/yaml"), REST_HTTP_BODY_YAML },
193 { L("text/html"), REST_HTTP_BODY_HTML },
194 { L("text/plain"), REST_HTTP_BODY_PLAIN },
195 { L("text/x-yaml"), REST_HTTP_BODY_YAML },
196 { L("text/xml"), REST_HTTP_BODY_XML },
197 { L("text/yaml"), REST_HTTP_BODY_YAML }
198};
200
201/*
202 * Encoder specific structures.
203 * @todo split encoders/decoders into submodules.
204 */
205typedef struct {
206 char const *start; //!< Start of the buffer.
207 char const *p; //!< how much text we've sent so far.
208 size_t len; //!< Length of data
210
211#ifdef HAVE_JSON
212/** Flags to control the conversion of JSON values to fr_pair_ts.
213 *
214 * These fields are set when parsing the expanded format for value pairs in
215 * JSON, and control how json_pair_alloc_leaf and json_pair_alloc convert the JSON
216 * value, and move the new fr_pair_t into an attribute list.
217 *
218 * @see json_pair_alloc
219 * @see json_pair_alloc_leaf
220 */
221typedef struct {
222 int do_xlat; //!< If true value will be expanded with xlat.
223 int is_json; //!< If true value will be inserted as raw JSON
224 // (multiple values not supported).
225 fr_token_t op; //!< The operator that determines how the new VP
226 // is processed. @see fr_tokens_table
228#endif
229
230/** Frees a libcurl handle, and any additional memory used by context data.
231 *
232 * @param[in] randle fr_curl_io_request_t to close and free.
233 * @return returns true.
234 */
236{
237 curl_easy_cleanup(randle->candle);
238
239 return 0;
240}
241
242/** Creates a new connection handle for use by the FR connection API.
243 *
244 * Matches the fr_pool_connection_create_t function prototype, is passed to
245 * fr_pool_init, and called when a new connection is required by the
246 * connection pool API.
247 *
248 * Creates an instances of fr_curl_io_request_t, and rlm_rest_curl_context_t
249 * which hold the context data required for generating requests and parsing
250 * responses.
251 *
252 * @see fr_pool_init
253 * @see fr_pool_connection_create_t
254 * @see connection.c
255 */
256void *rest_mod_conn_create(TALLOC_CTX *ctx, void *instance, UNUSED fr_time_delta_t timeout)
257{
259
260 fr_curl_io_request_t *randle = NULL;
261 rlm_rest_curl_context_t *curl_ctx = NULL;
262
263 /*
264 * Allocate memory for the connection handle abstraction.
265 */
266 randle = fr_curl_io_request_alloc(ctx);
267 if (!randle) return NULL;
268
269 MEM(curl_ctx = talloc_zero(randle, rlm_rest_curl_context_t));
270
271 curl_ctx->headers = NULL; /* CURL needs this to be NULL */
272 curl_ctx->request.instance = inst;
273 curl_ctx->response.instance = inst;
274
275 randle->uctx = curl_ctx;
276 talloc_set_destructor(randle, _mod_conn_free);
277
278 return randle;
279}
280
281/** Copies a pre-expanded xlat string to the output buffer
282 *
283 * @param[out] out Char buffer to write encoded data to.
284 * @param[in] size Multiply by nmemb to get the length of ptr.
285 * @param[in] nmemb Multiply by size to get the length of ptr.
286 * @param[in] userdata rlm_rest_request_t to keep encoding state between calls.
287 * @return
288 * - Length of data (including NULL) written to ptr.
289 * - 0 if no more data to write.
290 */
291static size_t rest_encode_custom(void *out, size_t size, size_t nmemb, void *userdata)
292{
293 rlm_rest_request_t *ctx = userdata;
295
296 size_t freespace = (size * nmemb) - 1;
297 size_t len;
298 size_t to_copy;
299
300 /*
301 * Special case for empty body
302 */
303 if (data->len == 0) return 0;
304
305 /*
306 * If len > 0 then we must have these set.
307 */
308 fr_assert(data->start);
309 fr_assert(data->p);
310
311 to_copy = data->len - (data->p - data->start);
312 len = to_copy > freespace ? freespace : to_copy;
313 if (len == 0) return 0;
314
315 memcpy(out, data->p, len);
316 data->p += len;
317
318 return len;
319}
320
321/** Encodes fr_pair_t linked list in POST format
322 *
323 * This is a stream function matching the rest_read_t prototype. Multiple
324 * successive calls will return additional encoded fr_pair_ts.
325 * Only complete attribute headers @verbatim '<name>=' @endverbatim and values
326 * will be written to the ptr buffer.
327 *
328 * POST request format is:
329 * @verbatim <attribute0>=<value0>&<attribute1>=<value1>&<attributeN>=<valueN>@endverbatim
330 *
331 * All attributes and values are url encoded. There is currently no support for
332 * nested attributes, or attribute qualifiers.
333 *
334 * Nested attributes may be added in the future using
335 * @verbatim <attribute-outer>:<attribute-inner>@endverbatim
336 * to denotate nesting.
337 *
338 * Requires libcurl for url encoding.
339 *
340 * @see rest_decode_post
341 *
342 * @param[out] out Char buffer to write encoded data to.
343 * @param[in] size Multiply by nmemb to get the length of ptr.
344 * @param[in] nmemb Multiply by size to get the length of ptr.
345 * @param[in] userdata rlm_rest_request_t to keep encoding state between calls.
346 * @return
347 * - Length of data (including NULL) written to ptr.
348 * - 0 if no more data to write.
349 */
350static size_t rest_encode_post(void *out, size_t size, size_t nmemb, void *userdata)
351{
352 rlm_rest_request_t *ctx = userdata;
353 request_t *request = ctx->request; /* Used by RDEBUG */
354 fr_pair_t *vp;
355
356 size_t len = 0;
357 ssize_t slen;
358 size_t freespace = (size * nmemb) - 1;
359
360 char *p = out; /* Position in buffer */
361 char *encoded = p; /* Position in buffer of last fully encoded attribute or value */
362 char *escaped; /* Pointer to current URL escaped data */
363
364 /* Allow manual chunking */
365 if ((ctx->chunk) && (ctx->chunk <= freespace)) freespace = (ctx->chunk - 1);
366
367 if (ctx->state == READ_STATE_END) return 0;
368
369 /* Post data requires no headers */
371
372 while (freespace > 0) {
374 if (!vp) {
375 ctx->state = READ_STATE_END;
376
377 break;
378 }
379
380 RDEBUG2("Encoding attribute \"%s\"", vp->da->name);
381
382 if (ctx->state == READ_STATE_ATTR_BEGIN) {
383 escaped = curl_escape(vp->da->name, 0);
384 if (!escaped) {
385 REDEBUG("Failed escaping string \"%s\"", vp->da->name);
386 return 0;
387 }
388
389 len = strlen(escaped);
390 if (freespace < (1 + len)) {
391 curl_free(escaped);
392 /*
393 * Cleanup for error conditions
394 */
395 no_space:
396 *encoded = '\0';
397
398 len = encoded - (char *)out;
399
400 RDEBUG3("POST Data: %pV", fr_box_strvalue_len(out, len));
401
402 /*
403 * The buffer wasn't big enough to encode a single attribute chunk.
404 */
405 if (len == 0) {
406 REDEBUG("Failed encoding attribute");
407 } else {
408 RDEBUG3("Returning %zd bytes of POST data "
409 "(buffer full or chunk exceeded)", len);
410 }
411
412 return len;
413 }
414
415 len = snprintf(p, freespace, "%s=", escaped);
416 curl_free(escaped);
417 p += len;
418 freespace -= len;
419
420 /*
421 * We wrote the attribute header, record progress.
422 */
423 encoded = p;
425 }
426
427 /*
428 * Write out single attribute string.
429 */
430 slen = fr_pair_print_value_quoted(&FR_SBUFF_OUT(p, freespace), vp, T_BARE_WORD);
431 if (slen < 0) return 0;
432
433 RINDENT();
434 RDEBUG3("Length : %zd", (size_t)slen);
435 REXDENT();
436 if (slen > 0) {
437 escaped = curl_escape(p, (size_t)slen);
438 if (!escaped) {
439 REDEBUG("Failed escaping string \"%s\"", vp->da->name);
440 return 0;
441 }
442 len = strlen(escaped);
443
444 if (freespace < len) {
445 curl_free(escaped);
446 goto no_space;
447 }
448
449 len = strlcpy(p, escaped, len + 1);
450
451 curl_free(escaped);
452
453 RINDENT();
454 RDEBUG3("Value : %s", p);
455 REXDENT();
456
457 p += len;
458 freespace -= len;
459 }
460
461 /*
462 * there are more attributes, insert a separator
463 */
464 if (fr_dcursor_next(&ctx->cursor)) {
465 if (freespace < 1) goto no_space;
466 *p++ = '&';
467 freespace--;
468 }
469
470 /*
471 * We wrote one full attribute value pair, record progress.
472 */
473 encoded = p;
474
476 }
477
478 *p = '\0';
479
480 len = p - (char *)out;
481
482 RDEBUG3("POST Data: %s", (char *)out);
483 RDEBUG3("Returning %zd bytes of POST data", len);
484
485 return len;
486}
487
488#ifdef HAVE_JSON
489/** Encodes fr_pair_t linked list in JSON format
490 *
491 * This is a stream function matching the rest_read_t prototype. Multiple
492 * successive calls will return additional encoded fr_pair_ts.
493 *
494 * Only complete attribute headers
495 * @verbatim "<name>":{"type":"<type>","value":[' @endverbatim
496 * and complete attribute values will be written to ptr.
497 *
498 * If an attribute occurs multiple times in the request the attribute values
499 * will be concatenated into a single value array.
500 *
501 * JSON request format is:
502@verbatim
503{
504 "<attribute0>":{
505 "type":"<type0>",
506 "value":[<value0>,<value1>,<valueN>]
507 },
508 "<attribute1>":{
509 "type":"<type1>",
510 "value":[...]
511 },
512 "<attributeN>":{
513 "type":"<typeN>",
514 "value":[...]
515 },
516}
517@endverbatim
518 *
519 * @param[out] out Char buffer to write encoded data to.
520 * @param[in] size Multiply by nmemb to get the length of ptr.
521 * @param[in] nmemb Multiply by size to get the length of ptr.
522 * @param[in] userdata rlm_rest_request_t to keep encoding state between calls.
523 * @return
524 * - Length of data (including NULL) written to ptr.
525 * - 0 if no more data to write.
526 */
527static size_t rest_encode_json(void *out, size_t size, size_t nmemb, void *userdata)
528{
529 rlm_rest_request_t *ctx = userdata;
530 request_t *request = ctx->request;
532
533 size_t freespace = (size * nmemb) - 1; /* account for the \0 byte here */
534 size_t len;
535 size_t to_copy;
536 const char *encoded;
537
538 fr_assert(freespace > 0);
539
540 if (ctx->state == READ_STATE_INIT) {
541 encoded = fr_json_afrom_pair_list(data, &request->request_pairs, NULL);
542 if (!encoded) return -1;
543
544 data->start = data->p = encoded;
545 data->len = strlen(encoded);
546
547 RDEBUG3("JSON Data: %s", encoded);
548 RDEBUG3("Returning %zd bytes of JSON data", data->len);
549
551 }
552
553 to_copy = data->len - (data->p - data->start);
554 len = to_copy > freespace ? freespace : to_copy;
555
556 if (len == 0) return 0;
557
558 memcpy(out, data->p, len);
559 data->p += len;
560 return len;
561}
562#endif
563
564/** Emulates successive libcurl calls to an encoding function
565 *
566 * This function is used when the request will be sent to the HTTP server as one
567 * contiguous entity. A buffer of REST_BODY_ALLOC_CHUNK bytes is allocated and passed
568 * to the stream encoding function.
569 *
570 * If the stream function does not return 0, a new buffer is allocated which is
571 * the size of the previous buffer + REST_BODY_ALLOC_CHUNK bytes, the data from the
572 * previous buffer is copied, and freed, and another call is made to the stream
573 * function, passing a pointer into the new buffer at the end of the previously
574 * written data.
575 *
576 * This process continues until the stream function signals (by returning 0)
577 * that it has no more data to write.
578 *
579 * @param[out] out where the pointer to the alloced buffer should
580 * be written.
581 * @param[in] inst of rlm_rest.
582 * @param[in] func Stream function.
583 * @param[in] limit Maximum buffer size to alloc.
584 * @param[in] userdata rlm_rest_request_t to keep encoding state between calls to
585 * stream function.
586 * @return
587 * - Length of the data written to the buffer (excluding NULL).
588 * - -1 if alloc >= limit.
589 */
591 rest_read_t func, size_t limit, void *userdata)
592{
593 char *buff = NULL;
594 size_t alloc = REST_BODY_ALLOC_CHUNK; /* Size of buffer to alloc */
595 size_t used = 0; /* Size of data written */
596 size_t len = 0;
597
598 buff = talloc_array(NULL, char, alloc);
599 for (;;) {
600 len = func(buff + used, alloc - used, 1, userdata);
601 used += len;
602 if (!len) {
603 *out = buff;
604 return used;
605 }
606
607 alloc = alloc * 2;
608 if (alloc > limit) break;
609
610 MEM(buff = talloc_realloc(NULL, buff, char, alloc));
611 }
612
614
615 return -1;
616}
617
618/** (Re-)Initialises the data in a rlm_rest_request_t.
619 *
620 * Resets the values of a rlm_rest_request_t to their defaults.
621 *
622 * @param[in] section configuration data.
623 * @param[in] request Current request.
624 * @param[in] ctx to initialise.
625 */
626static void rest_request_init(rlm_rest_section_t const *section,
627 request_t *request, rlm_rest_request_t *ctx)
628{
629 /*
630 * Setup stream read data
631 */
632 ctx->section = section;
633 ctx->request = request;
634 ctx->state = READ_STATE_INIT;
635}
636
637/** Converts plain response into a single fr_pair_t
638 *
639 * @param[in] inst configuration data.
640 * @param[in] section configuration data.
641 * @param[in] randle fr_curl_io_request_t to use.
642 * @param[in] request Current request.
643 * @param[in] raw buffer containing POST data.
644 * @param[in] rawlen Length of data in raw buffer.
645 * @return
646 * - Number of fr_pair_t processed.
647 * - -1 on unrecoverable error.
648 */
650 request_t *request, UNUSED fr_curl_io_request_t *randle, char *raw, size_t rawlen)
651{
652 fr_pair_t *vp;
653
654 /*
655 * Empty response?
656 */
657 if (*raw == '\0') return 0;
658
659 /*
660 * Use rawlen to protect against overrun, and to cope with any binary data
661 */
663 fr_pair_value_bstrndup(vp, raw, rawlen, true);
664
665 RDEBUG2("%pP", vp);
666
667 return 1;
668}
669
670/** Converts POST response into fr_pair_ts and adds them to the request
671 *
672 * Accepts fr_pair_tS in the same format as rest_encode_post, but with the
673 * addition of optional attribute list qualifiers as part of the attribute name
674 * string.
675 *
676 * If no qualifiers are specified, will default to the request list.
677 *
678 * POST response format is:
679 * @verbatim [outer.][<list>.]<attribute0>=<value0>&[outer.][<list>.]<attribute1>=<value1>&[outer.][<list>.]<attributeN>=<valueN> @endverbatim
680 *
681 * @see rest_encode_post
682 *
683 * @param[in] instance configuration data.
684 * @param[in] section configuration data.
685 * @param[in] randle fr_curl_io_request_t to use.
686 * @param[in] request Current request.
687 * @param[in] raw buffer containing POST data.
688 * @param[in] rawlen Length of data in raw buffer.
689 * @return
690 * - Number of fr_pair_ts processed.
691 * - -1 on unrecoverable error.
692 */
693static int rest_decode_post(UNUSED rlm_rest_t const *instance, UNUSED rlm_rest_section_t const *section,
694 request_t *request, fr_curl_io_request_t *randle, char *raw, size_t rawlen)
695{
696 CURL *candle = randle->candle;
697
698 char const *p = raw, *q;
699
700 int count = 0;
701 int ret;
702
703 /*
704 * Empty response?
705 */
707 if (*p == '\0') return 0;
708
709 while (((q = strchr(p, '=')) != NULL) && (count < REST_BODY_MAX_ATTRS)) {
710 tmpl_t *dst;
712 fr_pair_list_t *vps;
713 TALLOC_CTX *ctx;
714 fr_dict_attr_t const *da;
715 fr_pair_t *vp;
716
717 char *name = NULL;
718 char *value = NULL;
719
720 char *expanded = NULL;
721
722 size_t len;
723 int curl_len; /* Length from last curl_easy_unescape call */
724
725 current = request;
726
727 name = curl_easy_unescape(candle, p, (q - p), &curl_len);
728 p = (q + 1);
729
730 /*
731 * Resolve attribute name to a dictionary entry and pairlist.
732 */
733 RDEBUG2("Parsing attribute \"%pV\"", fr_box_strvalue_len(name, curl_len));
734
735 if (tmpl_afrom_attr_str(request, NULL, &dst, name,
736 &(tmpl_rules_t){
737 .attr = {
738 .dict_def = request->dict,
739 .list_def = request_attr_reply
740 }
741 }) <= 0) {
742 RPWDEBUG("Failed parsing attribute (skipping)");
743 talloc_free(dst);
744 goto skip;
745 }
746
747 if (tmpl_request_ptr(&current, tmpl_request(dst)) < 0) {
748 RWDEBUG("Attribute name refers to outer request but not in a tunnel (skipping)");
749 talloc_free(dst);
750 goto skip;
751 }
752
753 vps = tmpl_list_head(current, tmpl_list(dst));
754 if (!vps) {
755 RWDEBUG("List not valid in this context (skipping)");
756 talloc_free(dst);
757 goto skip;
758 }
759 ctx = tmpl_list_ctx(current, tmpl_list(dst));
760 da = tmpl_attr_tail_da(dst);
761
762 fr_assert(vps);
763
764 RINDENT();
765 RDEBUG3("Type : %s", fr_type_to_str(da->type));
766
767 q = strchr(p, '&');
768 len = (!q) ? (rawlen - (p - raw)) : (unsigned)(q - p);
769
770 value = curl_easy_unescape(candle, p, len, &curl_len);
771
772 /*
773 * If we found a delimiter we want to skip over it,
774 * if we didn't we do *NOT* want to skip over the end
775 * of the buffer...
776 */
777 p += (!q) ? len : (len + 1);
778
779 RDEBUG3("Length : %i", curl_len);
780 RDEBUG3("Value : \"%s\"", value);
781 REXDENT();
782
783 talloc_free(dst); /* Free our temporary tmpl */
784
785 RDEBUG2("Performing xlat expansion of response value");
786
787 if (xlat_aeval(request, &expanded, request, value, NULL, NULL) < 0) goto skip;
788
789 fr_assert(expanded);
790
791 MEM(vp = fr_pair_afrom_da(ctx, da));
792 if (!vp) {
793 REDEBUG("Failed creating valuepair");
794 talloc_free(expanded);
795
796 curl_free(name);
797 curl_free(value);
798
799 return count;
800 }
801
802 ret = fr_pair_value_from_str(vp, expanded, strlen(value), NULL, true);
803 TALLOC_FREE(expanded);
804 if (ret < 0) {
805 RWDEBUG("Incompatible value assignment, skipping");
807 goto skip;
808 }
809
810 fr_pair_append(vps, vp);
811
812 count++;
813
814 skip:
815 curl_free(name);
816 curl_free(value);
817
818 continue;
819 }
820
821 if (!count) REDEBUG("Malformed POST data \"%s\"", raw);
822
823 return count;
824
825}
826
827#ifdef HAVE_JSON
828/** Converts JSON "value" key into fr_pair_t.
829 *
830 * If leaf is not in fact a leaf node, but contains JSON data, the data will
831 * written to the attribute in JSON string format.
832 *
833 * @param[in] instance configuration data.
834 * @param[in] section configuration data.
835 * @param[in] ctx to allocate new fr_pair_ts in.
836 * @param[in] request Current request.
837 * @param[in] da Attribute to create.
838 * @param[in] flags containing the operator other flags controlling value
839 * expansion.
840 * @param[in] leaf object containing the fr_pair_t value.
841 * @return
842 * - #fr_pair_t just created.
843 * - NULL on error.
844 */
846 TALLOC_CTX *ctx, request_t *request,
847 fr_dict_attr_t const *da, json_flags_t *flags, json_object *leaf)
848{
849 char const *value;
850 char *expanded = NULL;
851 int ret;
852
853 fr_pair_t *vp;
854
856
857 if (json_object_is_type(leaf, json_type_null)) {
858 RDEBUG3("Got null value for attribute \"%s\" (skipping)", da->name);
859 return NULL;
860 }
861
862 MEM(vp = fr_pair_afrom_da(ctx, da));
863 if (!vp) {
864 RWDEBUG("Failed creating valuepair for attribute \"%s\" (skipping)", da->name);
865 return NULL;
866 }
867
869
870 switch (json_object_get_type(leaf)) {
871 case json_type_int:
872 if (flags->do_xlat) RWDEBUG("Ignoring do_xlat on 'int', attribute \"%s\"", da->name);
873 fr_value_box(&src, (int32_t)json_object_get_int(leaf), true);
874 break;
875
876 case json_type_double:
877 if (flags->do_xlat) RWDEBUG("Ignoring do_xlat on 'double', attribute \"%s\"", da->name);
878 fr_value_box(&src, (double)json_object_get_double(leaf), true);
879 break;
880
881 case json_type_string:
882 value = json_object_get_string(leaf);
883 if (flags->do_xlat && memchr(value, '%', json_object_get_string_len(leaf))) {
884 if (xlat_aeval(request, &expanded, request, value, NULL, NULL) < 0) {
886 return NULL;
887 }
888 fr_value_box_bstrndup_shallow(&src, NULL, expanded,
889 talloc_array_length(expanded) - 1, true);
890 } else {
892 json_object_get_string_len(leaf), true);
893 }
894 break;
895
896 default:
897 {
898 char const *str;
899 if (flags->do_xlat) RWDEBUG("Ignoring do_xlat on 'object', attribute \"%s\"", da->name);
900
901 /*
902 * Should encode any nested JSON structures into JSON strings.
903 *
904 * "I knew you liked JSON so I put JSON in your JSON!"
905 */
906 str = json_object_get_string(leaf);
907 if (!str) {
908 RWDEBUG("Failed getting string value for attribute \"%s\" (skipping)", da->name);
910 return NULL;
911 }
912 fr_value_box_strdup_shallow(&src, NULL, str, true);
913 }
914 }
915
916 ret = fr_value_box_cast(vp, &vp->data, da->type, da, &src);
917 talloc_free(expanded);
918 if (ret < 0) {
919 RWDEBUG("Failed parsing value for attribute \"%s\" (skipping)", da->name);
921 return NULL;
922 }
923
924 vp->op = flags->op;
925
926 return vp;
927}
928
929/** Processes JSON response and converts it into multiple fr_pair_ts
930 *
931 * Processes JSON attribute declarations in the format below. Will recurse when
932 * processing nested attributes. When processing nested attributes flags and
933 * operators from previous attributes are not inherited.
934 *
935 * JSON response format is:
936@verbatim
937{
938 "<attribute0>":{
939 "do_xlat":<bool>,
940 "is_json":<bool>,
941 "op":"<operator>",
942 "value":[<value0>,<value1>,<valueN>]
943 },
944 "<attribute1>":{
945 "value":{
946 "<nested-attribute0>":{
947 "op":"<operator>",
948 "value":<value0>
949 }
950 }
951 },
952 "<attribute2>":"<value0>",
953 "<attributeN>":[<value0>,<value1>,<valueN>]
954}
955@endverbatim
956 *
957 * JSON valuepair flags:
958 * - do_xlat (optional) Controls xlat expansion of values. Defaults to true.
959 * - is_json (optional) If true, any nested JSON data will be copied to the
960 * fr_pair_t in string form. Defaults to true.
961 * - op (optional) Controls how the attribute is inserted into
962 * the target list. Defaults to ':=' (T_OP_SET).
963 *
964 * If "op" is ':=' or '=', it will be automagically changed to '+=' for the
965 * second and subsequent values in multivalued attributes. This does not work
966 * between multiple attribute declarations.
967 *
968 * @see fr_tokens_table
969 *
970 * @param[in] instance configuration data.
971 * @param[in] section configuration data.
972 * @param[in] request Current request.
973 * @param[in] object containing root node, or parent node.
974 * @param[in] level Current nesting level.
975 * @param[in] max counter, decremented after each fr_pair_t is created,
976 * when 0 no more attributes will be processed.
977 * @return
978 * - Number of attributes created.
979 * - < 0 on error.
980 */
981static int json_pair_alloc(rlm_rest_t const *instance, rlm_rest_section_t const *section,
982 request_t *request, json_object *object, UNUSED int level, int max)
983{
984 int max_attrs = max;
985 tmpl_t *dst = NULL;
986
987 if (!json_object_is_type(object, json_type_object)) {
988#ifdef HAVE_JSON_TYPE_TO_NAME
989 REDEBUG("Can't process VP container, expected JSON object"
990 "got \"%s\" (skipping)",
991 json_type_to_name(json_object_get_type(object)));
992#else
993 REDEBUG("Can't process VP container, expected JSON object"
994 " (skipping)");
995#endif
996 return -1;
997 }
998
999 /*
1000 * Process VP container
1001 */
1002 {
1003 json_object_object_foreach(object, name, value) {
1004 int i = 0, elements;
1005 struct json_object *element, *tmp;
1006 TALLOC_CTX *ctx;
1007
1008 json_flags_t flags = {
1009 .op = T_OP_SET,
1010 .do_xlat = 1,
1011 .is_json = 0
1012 };
1013
1014 request_t *current = request;
1015 fr_pair_list_t *vps;
1016 fr_pair_t *vp = NULL;
1017
1018 TALLOC_FREE(dst);
1019
1020 /*
1021 * Resolve attribute name to a dictionary entry and pairlist.
1022 */
1023 RDEBUG2("Parsing attribute \"%s\"", name);
1024
1025 if (tmpl_afrom_attr_str(request, NULL, &dst, name,
1026 &(tmpl_rules_t){
1027 .attr = {
1028 .dict_def = request->dict,
1029 .list_def = request_attr_reply
1030 }
1031 }) <= 0) {
1032 RPWDEBUG("Failed parsing attribute (skipping)");
1033 continue;
1034 }
1035
1036 if (tmpl_request_ptr(&current, tmpl_request(dst)) < 0) {
1037 RWDEBUG("Attribute name refers to outer request but not in a tunnel (skipping)");
1038 continue;
1039 }
1040
1041 vps = tmpl_list_head(current, tmpl_list(dst));
1042 if (!vps) {
1043 RWDEBUG("List not valid in this context (skipping)");
1044 continue;
1045 }
1046 ctx = tmpl_list_ctx(current, tmpl_list(dst));
1047
1048 /*
1049 * Alternative JSON structure which allows operator,
1050 * and other flags to be specified.
1051 *
1052 * "<name>":{
1053 * "do_xlat":<bool>,
1054 * "is_json":<bool>,
1055 * "op":"<op>",
1056 * "value":<value>
1057 * }
1058 *
1059 * Where value is a:
1060 * - [] Multivalued array
1061 * - {} Nested Valuepair
1062 * - * Integer or string value
1063 */
1064 if (json_object_is_type(value, json_type_object)) {
1065 /*
1066 * Process operator if present.
1067 */
1068 if (json_object_object_get_ex(value, "op", &tmp)) {
1069 flags.op = fr_table_value_by_str(fr_tokens_table, json_object_get_string(tmp), 0);
1070 if (!flags.op) {
1071 RWDEBUG("Invalid operator value \"%s\" (skipping)",
1072 json_object_get_string(tmp));
1073 continue;
1074 }
1075 }
1076
1077 /*
1078 * Process optional do_xlat bool.
1079 */
1080 if (json_object_object_get_ex(value, "do_xlat", &tmp)) {
1081 flags.do_xlat = json_object_get_boolean(tmp);
1082 }
1083
1084 /*
1085 * Process optional is_json bool.
1086 */
1087 if (json_object_object_get_ex(value, "is_json", &tmp)) {
1088 flags.is_json = json_object_get_boolean(tmp);
1089 }
1090
1091 /*
1092 * Value key must be present if were using the expanded syntax.
1093 */
1094 if (!json_object_object_get_ex(value, "value", &tmp)) {
1095 RWDEBUG("Value key missing (skipping)");
1096 continue;
1097 }
1098
1099 /*
1100 * The value field now becomes the key we're operating on
1101 */
1102 value = tmp;
1103 }
1104
1105 /*
1106 * Setup fr_pair_afrom_da / recursion loop.
1107 */
1108 if (!flags.is_json && json_object_is_type(value, json_type_array)) {
1109 elements = json_object_array_length(value);
1110 if (!elements) {
1111 RWDEBUG("Zero length value array (skipping)");
1112 continue;
1113 }
1114 element = json_object_array_get_idx(value, 0);
1115 } else {
1116 elements = 1;
1117 element = value;
1118 }
1119
1120 /*
1121 * A JSON 'value' key, may have multiple elements, iterate
1122 * over each of them, creating a new fr_pair_t.
1123 */
1124 do {
1125 fr_pair_list_t tmp_list;
1126
1127 if (max_attrs-- <= 0) {
1128 RWDEBUG("At maximum attribute limit");
1129 talloc_free(dst);
1130 return max;
1131 }
1132
1133 /*
1134 * Automagically switch the op for multivalued attributes.
1135 */
1136 if (((flags.op == T_OP_SET) || (flags.op == T_OP_EQ)) && (i >= 1)) {
1137 flags.op = T_OP_ADD_EQ;
1138 }
1139
1140 if (json_object_is_type(element, json_type_object) && !flags.is_json) {
1141 /* TODO: Insert nested VP into VP structure...*/
1142 RWDEBUG("Found nested VP, these are not yet supported (skipping)");
1143
1144 continue;
1145
1146 /*
1147 vp = json_pair_alloc(instance, section,
1148 request, value,
1149 level + 1, max_attrs);*/
1150 } else {
1151 vp = json_pair_alloc_leaf(instance, section, ctx, request,
1152 tmpl_attr_tail_da(dst), &flags, element);
1153 if (!vp) continue;
1154 }
1155 RINDENT();
1156 RDEBUG2("%s:%pP", tmpl_list_name(tmpl_list(dst), ""), vp);
1157 REXDENT();
1158
1159 fr_pair_list_init(&tmp_list);
1160 fr_pair_append(&tmp_list, vp);
1161 radius_pairmove(current, vps, &tmp_list);
1162 /*
1163 * If we call json_object_array_get_idx on something that's not an array
1164 * the behaviour appears to be to occasionally segfault.
1165 */
1166 } while ((++i < elements) && (element = json_object_array_get_idx(value, i)));
1167 }
1168 }
1169
1170 talloc_free(dst);
1171
1172 return max - max_attrs;
1173}
1174
1175/** Converts JSON response into fr_pair_ts and adds them to the request.
1176 *
1177 * Converts the raw JSON string into a json-c object tree and passes it to
1178 * json_pair_alloc. After the tree has been parsed json_object_put is called
1179 * which decrements the reference count of the root node by one, and frees
1180 * the entire tree.
1181 *
1182 * @see rest_encode_json
1183 * @see json_pair_alloc
1184 *
1185 * @param[in] instance configuration data.
1186 * @param[in] section configuration data.
1187 * @param[in,out] request Current request.
1188 * @param[in] randle REST handle.
1189 * @param[in] raw buffer containing JSON data.
1190 * @param[in] rawlen Length of data in raw buffer.
1191 * @return
1192 * - The number of #fr_pair_t processed.
1193 * - -1 on unrecoverable error.
1194 */
1195static int rest_decode_json(rlm_rest_t const *instance, rlm_rest_section_t const *section,
1196 request_t *request, UNUSED fr_curl_io_request_t *randle, char *raw, UNUSED size_t rawlen)
1197{
1198 char const *p = raw;
1199
1200 struct json_object *json;
1201
1202 int ret;
1203
1204 /*
1205 * Empty response?
1206 */
1208 if (*p == '\0') return 0;
1209
1210 json = json_tokener_parse(p);
1211 if (!json) {
1212 REDEBUG("Malformed JSON data \"%s\"", raw);
1213 return -1;
1214 }
1215
1216 ret = json_pair_alloc(instance, section, request, json, 0, REST_BODY_MAX_ATTRS);
1217
1218 /*
1219 * Decrement reference count for root object, should free entire JSON tree.
1220 */
1221 json_object_put(json);
1222
1223 return ret;
1224}
1225#endif
1226
1227/** Processes incoming HTTP header data from libcurl.
1228 *
1229 * Processes the status line, and Content-Type headers from the incoming HTTP
1230 * response.
1231 *
1232 * Matches prototype for CURLOPT_HEADERFUNCTION, and will be called directly
1233 * by libcurl.
1234 *
1235 * @param[in] in Char buffer where inbound header data is written.
1236 * @param[in] size Multiply by nmemb to get the length of ptr.
1237 * @param[in] nmemb Multiply by size to get the length of ptr.
1238 * @param[in] userdata rlm_rest_response_t to keep parsing state between calls.
1239 * @return
1240 * - Length of data processed.
1241 * - 0 on error.
1242 */
1243static size_t rest_response_header(void *in, size_t size, size_t nmemb, void *userdata)
1244{
1245 rlm_rest_response_t *ctx = userdata;
1246 request_t *request = ctx->request; /* Used by RDEBUG */
1247
1248 char const *start = (char *)in, *p = start, *end = p + (size * nmemb);
1249 char *q;
1250 size_t len;
1251
1253
1254#ifndef NDEBUG
1255 if (ctx->instance->fail_header_decode) {
1256 REDEBUG("Forcing header decode failure");
1257 return 0;
1258 }
1259#endif
1260
1261 /*
1262 * This seems to be curl's indication there are no more header lines.
1263 */
1264 if (((end - p) == 2) && ((p[0] == '\r') && (p[1] == '\n'))) {
1265 /*
1266 * If we got a 100 Continue, we need to send additional payload data.
1267 * reset the state to WRITE_STATE_INIT, so that when were called again
1268 * we overwrite previous header data with that from the proper header.
1269 */
1270 if (ctx->code == 100) {
1271 RDEBUG2("Continuing...");
1272 ctx->state = WRITE_STATE_INIT;
1273 }
1274
1275 return (end - start);
1276 }
1277
1278 switch (ctx->state) {
1279 case WRITE_STATE_INIT:
1280 RDEBUG2("Processing response header");
1281
1282 /*
1283 * HTTP/<version> <reason_code>[ <reason_phrase>]\r\n
1284 *
1285 * "HTTP/1.1 " (9) + "100" (3) + "\r\n" (2) = 14
1286 * "HTTP/2 " (7) + "100" (3) + "\r\n" (2) = 12
1287 */
1288 if ((end - p) < 12) {
1289 REDEBUG("Malformed HTTP header: Status line too short");
1290 malformed:
1291 REDEBUG("Received %zu bytes of invalid header data: %pV",
1292 (end - start), fr_box_strvalue_len(in, (end - start)));
1293 ctx->code = 0;
1294
1295 /*
1296 * Indicate we parsed the entire line, otherwise
1297 * bad things seem to happen internally with
1298 * libcurl when we try and use it with asynchronous
1299 * I/O handlers.
1300 */
1301 return (end - start);
1302 }
1303 /*
1304 * Check start of header matches...
1305 */
1306 if (strncasecmp("HTTP/", p, 5) != 0) {
1307 REDEBUG("Malformed HTTP header: Missing HTTP version");
1308 goto malformed;
1309 }
1310 p += 5;
1311
1312 /*
1313 * Skip the version field, next space should mark start of reason_code.
1314 */
1315 q = memchr(p, ' ', (end - p));
1316 if (!q) {
1317 REDEBUG("Malformed HTTP header: Missing reason code");
1318 goto malformed;
1319 }
1320
1321 p = q;
1322
1323 /*
1324 * Process reason_code.
1325 *
1326 * " 100" (4) + "\r\n" (2) = 6
1327 */
1328 if ((end - p) < 6) {
1329 REDEBUG("Malformed HTTP header: Reason code too short");
1330 goto malformed;
1331 }
1332 p++;
1333
1334 /*
1335 * "xxx( |\r)" status code and terminator.
1336 */
1337 if (!isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2]) || !((p[3] == ' ') || (p[3] == '\r'))) {
1338 REDEBUG("Malformed HTTP header: Reason code malformed. "
1339 "Expected three digits then space or end of header, got \"%pV\"",
1340 fr_box_strvalue_len(p, 4));
1341 goto malformed;
1342 }
1343
1344 /*
1345 * Convert status code into an integer value
1346 */
1347 q = NULL;
1348 ctx->code = (int)strtoul(p, &q, 10);
1349 fr_assert(q == (p + 3)); /* We check this above */
1350 p = q;
1351
1352 /*
1353 * Process reason_phrase (if present).
1354 */
1355 RINDENT();
1356 if (*p == ' ') {
1357 p++;
1358 q = memchr(p, '\r', (end - p));
1359 if (!q) goto malformed;
1360 RDEBUG2("Status : %i (%pV)", ctx->code, fr_box_strvalue_len(p, q - p));
1361 } else {
1362 RDEBUG2("Status : %i", ctx->code);
1363 }
1364 REXDENT();
1365
1367
1368 break;
1369
1371 if (((end - p) >= 14) &&
1372 (strncasecmp("Content-Type: ", p, 14) == 0)) {
1373 p += 14;
1374
1375 /*
1376 * Check to see if there's a parameter separator.
1377 */
1378 q = memchr(p, ';', (end - p));
1379
1380 /*
1381 * If there's not, find the end of this header.
1382 */
1383 if (!q) q = memchr(p, '\r', (end - p));
1384
1385 len = (size_t)(!q ? (end - p) : (q - p));
1387
1388 RINDENT();
1389 RDEBUG2("Type : %s (%pV)", fr_table_str_by_value(http_body_type_table, type, "<INVALID>"),
1390 fr_box_strvalue_len(p, len));
1391 REXDENT();
1392
1393 /*
1394 * Assume the force_to value has already been validated.
1395 */
1396 if (ctx->force_to != REST_HTTP_BODY_UNKNOWN) {
1397 if (ctx->force_to != ctx->type) {
1398 RDEBUG3("Forcing body type to \"%s\"",
1400 ctx->type = ctx->force_to;
1401 }
1402 /*
1403 * Figure out if the type is supported by one of the decoders.
1404 */
1405 } else {
1407 switch (ctx->type) {
1409 RWDEBUG("Couldn't determine type, using the request's type \"%s\".",
1411 break;
1412
1414 REDEBUG("Type \"%s\" is currently unsupported",
1416 break;
1417
1419 REDEBUG("Type \"%s\" is unavailable, please rebuild this module with the required "
1420 "library", fr_table_str_by_value(http_body_type_table, type, "<INVALID>"));
1421 break;
1422
1424 REDEBUG("Type \"%s\" is not a valid web API data markup format",
1426 break;
1427
1428 /* supported type */
1429 default:
1430 break;
1431 }
1432 }
1433 }
1434 break;
1435
1436 default:
1437 break;
1438 }
1439
1440 return (end - start);
1441}
1442
1443/** Processes incoming HTTP body data from libcurl.
1444 *
1445 * Writes incoming body data to an intermediary buffer for later parsing by
1446 * one of the decode functions.
1447 *
1448 * @param[in] in Char buffer where inbound header data is written
1449 * @param[in] size Multiply by nmemb to get the length of ptr.
1450 * @param[in] nmemb Multiply by size to get the length of ptr.
1451 * @param[in] userdata rlm_rest_response_t to keep parsing state between calls.
1452 * @return
1453 * - Length of data processed.
1454 * - 0 on error.
1455 */
1456static size_t rest_response_body(void *in, size_t size, size_t nmemb, void *userdata)
1457{
1458 rlm_rest_response_t *ctx = userdata;
1459 request_t *request = ctx->request; /* Used by RDEBUG */
1460
1461 char const *start = in, *p = start, *end = p + (size * nmemb);
1462 char *q;
1463
1464 size_t needed;
1465
1466 if (start == end) return 0; /* Nothing to process */
1467
1468#ifndef NDEBUG
1469 if (ctx->instance->fail_body_decode) {
1470 REDEBUG("Forcing body read failure");
1471 return 0;
1472 }
1473#endif
1474
1475 /*
1476 * Any post processing of headers should go here...
1477 */
1479
1480 switch (ctx->type) {
1484 while ((q = memchr(p, '\n', (end - p)))) {
1485 REDEBUG("%pV", fr_box_strvalue_len(p, q - p));
1486 p = q + 1;
1487 }
1488
1489 if (p != end) REDEBUG("%pV", fr_box_strvalue_len(p, end - p));
1490 break;
1491
1493 while ((q = memchr(p, '\n', (end - p)))) {
1494 RDEBUG3("%pV", fr_box_strvalue_len(p, q - p));
1495 p = q + 1;
1496 }
1497
1498 if (p != end) RDEBUG3("%pV", fr_box_strvalue_len(p, end - p));
1499 break;
1500
1501 default:
1502 {
1503 char *out_p;
1504
1505 if ((ctx->section->response.max_body_in > 0) && ((ctx->used + (end - p)) > ctx->section->response.max_body_in)) {
1506 REDEBUG("Incoming data (%zu bytes) exceeds max_body_in (%zu bytes). "
1507 "Forcing body to type 'invalid'", ctx->used + (end - p), ctx->section->response.max_body_in);
1509 TALLOC_FREE(ctx->buffer);
1510 break;
1511 }
1512
1513 needed = ROUND_UP(ctx->used + (end - p), REST_BODY_ALLOC_CHUNK);
1514 if (needed > ctx->alloc) {
1515 MEM(ctx->buffer = talloc_bstr_realloc(NULL, ctx->buffer, needed));
1516 ctx->alloc = needed;
1517 }
1518
1519 out_p = ctx->buffer + ctx->used;
1520 memcpy(out_p, p, (end - p));
1521 out_p += (end - p);
1522 *out_p = '\0';
1523 ctx->used += (end - p);
1524 }
1525 break;
1526 }
1527
1528 return (end - start);
1529}
1530
1531/** Print out the response text as error lines
1532 *
1533 * @param request The Current request.
1534 * @param handle fr_curl_io_request_t used to execute the previous request.
1535 */
1537{
1538 char const *p, *end;
1539 char *q;
1540 size_t len;
1541
1542 len = rest_get_handle_data(&p, handle);
1543 if (len == 0) return;
1544
1545 end = p + len;
1546
1547 RERROR("Server returned:");
1548 while ((q = memchr(p, '\n', (end - p)))) {
1549 RERROR("%pV", fr_box_strvalue_len(p, q - p));
1550 p = q + 1;
1551 }
1552
1553 if (p != end) RERROR("%pV", fr_box_strvalue_len(p, end - p));
1554}
1555
1556/** Print out the response text
1557 *
1558 * @param request The Current request.
1559 * @param handle fr_curl_io_request_t used to execute the previous request.
1560 */
1562{
1563 char const *p, *end;
1564 char *q;
1565 size_t len;
1566
1567 len = rest_get_handle_data(&p, handle);
1568 if (len == 0) return;
1569
1570 end = p + len;
1571
1572 RDEBUG3("Server returned:");
1573 while ((q = memchr(p, '\n', (end - p)))) {
1574 RDEBUG3("%pV", fr_box_strvalue_len(p, q - p));
1575 p = q + 1;
1576 }
1577
1578 if (p != end) RDEBUG3("%pV", fr_box_strvalue_len(p, end - p));
1579}
1580
1581/** (Re-)Initialises the data in a rlm_rest_response_t.
1582 *
1583 * This resets the values of the a rlm_rest_response_t to their defaults.
1584 * Must be called between encoding sessions.
1585 *
1586 * @see rest_response_body
1587 * @see rest_response_header
1588 *
1589 * @param[in] section that created the request.
1590 * @param[in] request Current request.
1591 * @param[in] ctx data to initialise.
1592 * @param[in] type Default http_body_type to use when decoding raw data, may be
1593 * overwritten by rest_response_header.
1594 * @param[in] header Where to write out headers, may be NULL.
1595 */
1596static void rest_response_init(rlm_rest_section_t const *section,
1597 request_t *request, rlm_rest_response_t *ctx, http_body_type_t type, tmpl_t *header)
1598{
1599 ctx->section = section;
1600 ctx->request = request;
1601 ctx->type = type;
1602 ctx->state = WRITE_STATE_INIT;
1603 ctx->alloc = 0;
1604 ctx->used = 0;
1605 ctx->code = 0;
1606 ctx->header = header;
1607 TALLOC_FREE(ctx->buffer);
1608}
1609
1610/** Extracts pointer to buffer containing response data
1611 *
1612 * @param[out] out Where to write the pointer to the buffer.
1613 * @param[in] randle used for the last request.
1614 * @return
1615 * - 0 if no data i available.
1616 * - > 0 if data is available.
1617 */
1618size_t rest_get_handle_data(char const **out, fr_curl_io_request_t *randle)
1619{
1620 rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1621
1622 if (!ctx->response.buffer) return 0;
1623
1624 *out = ctx->response.buffer;
1625 return ctx->response.used;
1626}
1627
1628/** Configures body specific curlopts.
1629 *
1630 * Configures libcurl handle to use either chunked mode, where the request
1631 * data will be sent using multiple HTTP requests, or contiguous mode where
1632 * the request data will be sent in a single HTTP request.
1633 *
1634 * @param[in] mctx Call data.
1635 * @param[in] section configuration data.
1636 * @param[in] request Current request.
1637 * @param[in] randle fr_curl_io_request_t to configure.
1638 * @param[in] func to pass to libcurl for chunked.
1639 * transfers (NULL if not using chunked mode).
1640 * @return
1641 * - 0 on success.
1642 * - -1 on failure.
1643 */
1644static int rest_request_config_body(module_ctx_t const *mctx, rlm_rest_section_t const *section,
1645 request_t *request, fr_curl_io_request_t *randle, rest_read_t func)
1646{
1647 rlm_rest_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_rest_t);
1648 rlm_rest_curl_context_t *uctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1649 ssize_t len;
1650
1651 /*
1652 * We were provided with no read function, assume this means
1653 * no body should be sent.
1654 */
1655 if (!func) {
1656 FR_CURL_REQUEST_SET_OPTION(CURLOPT_POSTFIELDSIZE, 0);
1657 return 0;
1658 }
1659
1660 /*
1661 * Chunked transfer encoding means the body will be sent in
1662 * multiple parts.
1663 */
1664 if (section->request.chunk > 0) {
1665 FR_CURL_REQUEST_SET_OPTION(CURLOPT_READDATA, &uctx->request);
1666 FR_CURL_REQUEST_SET_OPTION(CURLOPT_READFUNCTION, func);
1667
1668 return 0;
1669 }
1670
1671 /*
1672 * If were not doing chunked encoding then we read the entire
1673 * body into a buffer, and send it in one go.
1674 */
1675 len = rest_request_encode_wrapper(&uctx->body, inst, func, REST_BODY_MAX_LEN, &uctx->request);
1676 if (len <= 0) {
1677 REDEBUG("Failed creating HTTP body content");
1678 return -1;
1679 }
1680 RDEBUG2("Content-Length will be %zu bytes", len);
1681
1682 fr_assert((len == 0) || (talloc_array_length(uctx->body) >= (size_t)len));
1683 FR_CURL_REQUEST_SET_OPTION(CURLOPT_POSTFIELDS, uctx->body);
1684 FR_CURL_REQUEST_SET_OPTION(CURLOPT_POSTFIELDSIZE, len);
1685
1686 return 0;
1687
1688error:
1689 return -1;
1690}
1691
1692/** Adds an additional header to a handle to use in the next reques
1693 *
1694 * @param[in] request Current request.
1695 * @param[in] randle used for the next request.
1696 * @param[in] header to add.
1697 * @param[in] validate whether to perform basic checks on the header
1698 * @return
1699 * - 0 on success.
1700 * - -1 on failure.
1701 */
1702int rest_request_config_add_header(request_t *request, fr_curl_io_request_t *randle, char const *header, bool validate)
1703{
1704 rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1705 struct curl_slist *headers;
1706
1707 if (validate && !strchr(header, ':')) {
1708 RWDEBUG("Invalid HTTP header \"%s\" must be in format '<attribute>: <value>'. Skipping...",
1709 header);
1710 return -1;
1711 }
1712
1713 RINDENT();
1714 RDEBUG3("%s", header);
1715 REXDENT();
1716
1717 headers = curl_slist_append(ctx->headers, header);
1718 if (unlikely(!headers)) {
1719 REDEBUG("Failed to add header \"%s\"", header);
1720 return -1;
1721 }
1722 ctx->headers = headers;
1723
1724 return 0;
1725}
1726
1727/** See if the list of headers already contains a header
1728 *
1729 * @note Only compares the header name, not the value.
1730 *
1731 * @param[in] randle to check headers for.
1732 * @param[in] header to find.
1733 * @return
1734 * - true if yes
1735 * - false if no
1736 */
1737static bool rest_request_contains_header(fr_curl_io_request_t *randle, char const *header)
1738{
1739 rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1740 struct curl_slist *headers = ctx->headers;
1741 char const *sep;
1742 size_t cmp_len;
1743
1744 sep = strchr(header, ':');
1745 cmp_len = sep ? (size_t)(sep - header) : strlen(header);
1746
1747 while (headers) {
1748 if (strncasecmp(headers->data, header, cmp_len) == 0) return true;
1749 headers = headers->next;
1750 }
1751
1752 return false;
1753}
1754
1755/** Configures request curlopts.
1756 *
1757 * Configures libcurl handle setting various curlopts for things like local
1758 * client time, Content-Type, and other FreeRADIUS custom headers.
1759 *
1760 * Current FreeRADIUS custom headers are:
1761 * - X-FreeRADIUS-Section The module section being processed.
1762 * - X-FreeRADIUS-Server The current virtual server the request_t is
1763 * passing through.
1764 *
1765 * Sets up callbacks for all response processing (buffers and body data).
1766 *
1767 * @param[in] mctx call data.
1768 * @param[in] section configuration data.
1769 * @param[in] randle to configure.
1770 * @param[in] request Current request.
1771 * @param[in] method to use (HTTP verbs PUT, POST, DELETE etc...).
1772 * @param[in] type Content-Type for request encoding, also sets
1773 * the default for decoding.
1774 * @param[in] uri buffer containing the expanded URI to send the request to.
1775 * @param[in] body_data (optional) custom body data. Must persist whilst we're
1776 * writing data out to the socket. Must be a talloced buffer
1777 * which is \0 terminated.
1778 * @return
1779 * - 0 on success (all opts configured).
1780 * - -1 on failure.
1781 */
1783 request_t *request, fr_curl_io_request_t *randle, http_method_t method,
1785 char const *uri, char const *body_data)
1786{
1787 rlm_rest_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_rest_t);
1788 rlm_rest_call_env_t *call_env = talloc_get_type_abort(mctx->env_data, rlm_rest_call_env_t);
1789 rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1790 CURL *candle = randle->candle;
1791 fr_time_delta_t timeout;
1792
1793 http_auth_type_t auth = section->request.auth;
1794
1795 CURLcode ret = CURLE_OK;
1796 char const *option;
1797
1798 char buffer[512];
1799
1800 fr_assert(candle);
1801
1802 buffer[(sizeof(buffer) - 1)] = '\0';
1803
1804 /*
1805 * Control which HTTP version we're going to use
1806 */
1807 if (inst->http_negotiation != CURL_HTTP_VERSION_NONE) FR_CURL_REQUEST_SET_OPTION(CURLOPT_HTTP_VERSION, inst->http_negotiation);
1808
1809 /*
1810 * Setup any header options and generic headers.
1811 */
1812 FR_CURL_REQUEST_SET_OPTION(CURLOPT_URL, uri);
1813#if CURL_AT_LEAST_VERSION(7,85,0)
1814 FR_CURL_REQUEST_SET_OPTION(CURLOPT_PROTOCOLS_STR, "http,https");
1815#else
1816 FR_CURL_REQUEST_SET_OPTION(CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
1817#endif
1818 if (section->request.proxy) {
1819 if (section->request.proxy == rest_no_proxy) {
1820 FR_CURL_REQUEST_SET_OPTION(CURLOPT_NOPROXY, "*");
1821 } else {
1822 FR_CURL_REQUEST_SET_OPTION(CURLOPT_PROXY, section->request.proxy);
1823 }
1824 }
1825 FR_CURL_REQUEST_SET_OPTION(CURLOPT_NOSIGNAL, 1L);
1826 FR_CURL_REQUEST_SET_OPTION(CURLOPT_USERAGENT, "FreeRADIUS " RADIUSD_VERSION_STRING);
1827
1828 timeout = inst->conn_config.connect_timeout;
1829 RDEBUG3("Connect timeout is %pVs, request timeout is %pVs",
1830 fr_box_time_delta(timeout), fr_box_time_delta(section->timeout));
1831 FR_CURL_REQUEST_SET_OPTION(CURLOPT_CONNECTTIMEOUT_MS, fr_time_delta_to_msec(timeout));
1832 FR_CURL_REQUEST_SET_OPTION(CURLOPT_TIMEOUT_MS, fr_time_delta_to_msec(section->timeout));
1833
1834 /*
1835 * FreeRADIUS custom headers
1836 */
1837 RDEBUG3("Adding custom headers:");
1838 snprintf(buffer, sizeof(buffer), "X-FreeRADIUS-Section: %s", section->name);
1839 if (unlikely(rest_request_config_add_header(request, randle, buffer, false) < 0)) return -1;
1840
1841 snprintf(buffer, sizeof(buffer), "X-FreeRADIUS-Server: %s", cf_section_name2(unlang_call_current(request)));
1842 if (unlikely(rest_request_config_add_header(request, randle, buffer, false) < 0)) return -1;
1843
1844 /*
1845 * Add in the section headers
1846 */
1847 if (call_env->request.header) {
1848 size_t len = talloc_array_length(call_env->request.header), i;
1849 for (i = 0; i < len; i++) {
1850 fr_value_box_list_foreach(&call_env->request.header[i], header) {
1851 if (unlikely(rest_request_config_add_header(request, randle, header->vb_strvalue, true) < 0)) return -1;
1852 }
1853 }
1854 }
1855
1856 /*
1857 * Add in dynamic headers from the request
1858 */
1859 {
1860 fr_pair_t *header;
1861 fr_dcursor_t header_cursor;
1862
1863 for (header = fr_pair_dcursor_by_da_init(&header_cursor, &request->control_pairs, attr_rest_http_header);
1864 header;
1865 header = fr_dcursor_current(&header_cursor)) {
1866 header = fr_dcursor_remove(&header_cursor);
1867 if (unlikely(rest_request_config_add_header(request, randle, header->vp_strvalue, true) < 0)) return -1;
1868
1869 talloc_free(header);
1870 }
1871 }
1872
1873 if (!rest_request_contains_header(randle, "Content-Type:")) {
1874 /*
1875 * HTTP/1.1 doesn't require a content type so only set it
1876 * if where body type requires it, and we haven't set one
1877 * already from attributes.
1878 */
1879 if (type != REST_HTTP_BODY_NONE) {
1880 char const *content_type = fr_table_str_by_value(http_content_type_table, type, section->request.body_str);
1881 snprintf(buffer, sizeof(buffer), "Content-Type: %s", content_type);
1882 if (unlikely(rest_request_config_add_header(request, randle, buffer, false) < 0)) return -1;
1883
1884 RDEBUG3("Request body content-type will be \"%s\"", content_type);
1885 }
1886 }
1887
1888 /*
1889 * Configure HTTP verb (GET, POST, PUT, PATCH, DELETE, other...)
1890 */
1891 switch (method) {
1893 FR_CURL_REQUEST_SET_OPTION(CURLOPT_HTTPGET, 1L);
1894 break;
1895
1897 FR_CURL_REQUEST_SET_OPTION(CURLOPT_POST, 1L);
1898 break;
1899
1901 /*
1902 * Do not set CURLOPT_PUT, this will cause libcurl
1903 * to ignore CURLOPT_POSTFIELDs and attempt to read
1904 * whatever was set with CURLOPT_READDATA, which by
1905 * default is stdin.
1906 *
1907 * This is many cases will cause the server to block,
1908 * indefinitely.
1909 */
1910 FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, "PUT");
1911 break;
1912
1914 FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, "PATCH");
1915 break;
1916
1918 FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, "DELETE");
1919 break;
1920
1922 FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, section->request.method_str);
1923 break;
1924
1925 default:
1926 fr_assert(0);
1927 break;
1928 }
1929
1930 /*
1931 * Set user based authentication parameters
1932 */
1933 if (auth > REST_HTTP_AUTH_NONE) {
1934
1935#define SET_AUTH_OPTION(_x, _y)\
1936do {\
1937 if ((ret = curl_easy_setopt(candle, _x, _y)) != CURLE_OK) {\
1938 option = STRINGIFY(_x);\
1939 REDEBUG("Failed setting curl option %s: %s (%i)", option, curl_easy_strerror(ret), ret); \
1940 goto error;\
1941 }\
1942} while (0)
1943 RDEBUG3("Configuring HTTP auth type %s, user \"%pV\", password \"%pV\"",
1944 fr_table_str_by_value(http_auth_table, auth, "<INVALID>"),
1945 call_env->request.username ? call_env->request.username : fr_box_strvalue("(none)"),
1946 call_env->request.password ? call_env->request.password : fr_box_strvalue("(none)"));
1947
1948 /*
1949 * FIXME - We probably want to escape \0s here...
1950 */
1951 if ((auth >= REST_HTTP_AUTH_BASIC) && (auth <= REST_HTTP_AUTH_ANY_SAFE)) {
1952 SET_AUTH_OPTION(CURLOPT_HTTPAUTH, http_curl_auth[auth]);
1953 if (call_env->request.username) SET_AUTH_OPTION(CURLOPT_USERNAME, call_env->request.username->vb_strvalue);
1954 if (call_env->request.password) SET_AUTH_OPTION(CURLOPT_PASSWORD, call_env->request.password->vb_strvalue);
1955 } else if (auth == REST_HTTP_AUTH_TLS_SRP) {
1956 SET_AUTH_OPTION(CURLOPT_TLSAUTH_TYPE, "SRP");
1957 if (call_env->request.username) SET_AUTH_OPTION(CURLOPT_TLSAUTH_USERNAME, call_env->request.username->vb_strvalue);
1958 if (call_env->request.password) SET_AUTH_OPTION(CURLOPT_TLSAUTH_PASSWORD, call_env->request.password->vb_strvalue);
1959 }
1960 }
1961
1962 /*
1963 * Set SSL/TLS authentication parameters
1964 */
1965 fr_curl_easy_tls_init(randle, &section->tls);
1966
1967 /*
1968 * Tell CURL how to get HTTP body content, and how to process incoming data.
1969 */
1970 rest_response_init(section, request, &ctx->response, type, call_env->response.header);
1971
1972 FR_CURL_REQUEST_SET_OPTION(CURLOPT_HEADERFUNCTION, rest_response_header);
1973 FR_CURL_REQUEST_SET_OPTION(CURLOPT_HEADERDATA, &ctx->response);
1974 FR_CURL_REQUEST_SET_OPTION(CURLOPT_WRITEFUNCTION, rest_response_body);
1975 FR_CURL_REQUEST_SET_OPTION(CURLOPT_WRITEDATA, &ctx->response);
1976
1977 /*
1978 * Force parsing the body text as a particular encoding.
1979 */
1980 ctx->response.force_to = section->response.force_to;
1981
1982 switch (method) {
1985 RDEBUG3("Using a HTTP method which does not require a body. Forcing request body type to \"none\"");
1986 goto finish;
1987
1992 if (section->request.chunk > 0) {
1993 ctx->request.chunk = section->request.chunk;
1994
1995 if (unlikely(rest_request_config_add_header(request, randle, "Transfer-Encoding: chunked", false) < 0)) return -1;
1996 }
1997
1998 break;
1999
2000 default:
2001 fr_assert(0);
2002 }
2003
2004 /*
2005 * Setup encoder specific options
2006 */
2007 switch (type) {
2009 if (rest_request_config_body(mctx, section, request, randle, NULL) < 0) return -1;
2010
2011 break;
2012
2014 {
2016
2017 data = talloc_zero(request, rest_custom_data_t);
2018 data->p = data->start = body_data;
2019 data->len = talloc_strlen(body_data);
2020
2021 /* Use the encoder specific pointer to store the data we need to encode */
2022 ctx->request.encoder = data;
2023 if (rest_request_config_body(mctx, section, request, randle, rest_encode_custom) < 0) {
2024 TALLOC_FREE(ctx->request.encoder);
2025 return -1;
2026 }
2027 }
2028 break;
2029
2030#ifdef HAVE_JSON
2032 {
2034
2035 data = talloc_zero(request, rest_custom_data_t);
2036 ctx->request.encoder = data;
2037
2038 rest_request_init(section, request, &ctx->request);
2039
2040 if (rest_request_config_body(mctx, section, request, randle, rest_encode_json) < 0) return -1;
2041 }
2042
2043 break;
2044#endif
2045
2047 rest_request_init(section, request, &ctx->request);
2048 fr_pair_dcursor_init(&(ctx->request.cursor), &request->request_pairs);
2049
2050 if (rest_request_config_body(mctx, section, request, randle, rest_encode_post) < 0) return -1;
2051
2052 break;
2053
2054 default:
2055 fr_assert(0);
2056 }
2057
2058
2059finish:
2060 FR_CURL_REQUEST_SET_OPTION(CURLOPT_HTTPHEADER, ctx->headers);
2061
2062 return 0;
2063
2064error:
2065 return -1;
2066}
2067
2068/** Sends the response to the correct decode function.
2069 *
2070 * Uses the Content-Type information written in rest_response_header to
2071 * determine the correct decode function to use. The decode function will
2072 * then convert the raw received data into fr_pair_ts.
2073 *
2074 * @param[in] instance configuration data.
2075 * @param[in] section configuration data.
2076 * @param[in] request Current request.
2077 * @param[in] randle to use.
2078 * @return
2079 * - 0 on success.
2080 * - -1 on failure.
2081 */
2082int rest_response_decode(rlm_rest_t const *instance, rlm_rest_section_t const *section,
2083 request_t *request, fr_curl_io_request_t *randle)
2084{
2085 rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
2086
2087 int ret = -1; /* -Wsometimes-uninitialized */
2088
2089 if (!ctx->response.buffer) {
2090 RDEBUG2("Skipping attribute processing, no valid body data received");
2091 return 0;
2092 }
2093
2094 switch (ctx->response.type) {
2096 return 0;
2097
2099 ret = rest_decode_plain(instance, section, request, randle, ctx->response.buffer, ctx->response.used);
2100 break;
2101
2103 ret = rest_decode_post(instance, section, request, randle, ctx->response.buffer, ctx->response.used);
2104 break;
2105
2106#ifdef HAVE_JSON
2108 ret = rest_decode_json(instance, section, request, randle, ctx->response.buffer, ctx->response.used);
2109 break;
2110#endif
2111
2115 return -1;
2116
2117 default:
2118 fr_assert(0);
2119 }
2120
2121 return ret;
2122}
2123
2124/** URL encodes a string.
2125 *
2126 * Encode special chars as per RFC 3986 section 4.
2127 *
2128 * @param[in] request Current request.
2129 * @param[out] out Where to write escaped string.
2130 * @param[in] outlen Size of out buffer.
2131 * @param[in] raw string to be urlencoded.
2132 * @param[in] arg pointer, gives context for escaping.
2133 * @return length of data written to out (excluding NULL).
2134 */
2135size_t rest_uri_escape(UNUSED request_t *request, char *out, size_t outlen, char const *raw, UNUSED void *arg)
2136{
2137 char *escaped;
2138
2139 escaped = curl_escape(raw, 0);
2140 strlcpy(out, escaped, outlen);
2141 curl_free(escaped);
2142
2143 return strlen(out);
2144}
2145
2146/** Unescapes the host portion of a URI string
2147 *
2148 * This is required because the xlat functions which operate on the input string
2149 * cannot distinguish between host and path components.
2150 *
2151 * @param[out] out Where to write the pointer to the new
2152 * buffer containing the escaped URI.
2153 * @param[in] inst of rlm_rest.
2154 * @param[in] request Current request
2155 * @param[in] randle to use.
2156 * @param[in] uri configuration data.
2157 * @return
2158 * - Length of data written to buffer (excluding NULL).
2159 * - < 0 if an error occurred.
2160 */
2162 fr_curl_io_request_t *randle, char const *uri)
2163{
2164 CURL *candle = randle->candle;
2165
2166 char const *p, *q;
2167
2168 char *scheme;
2169
2170 ssize_t len;
2171
2172 p = uri;
2173
2174 /*
2175 * All URLs must contain at least <scheme>://<server>/
2176 */
2177 p = strchr(p, ':');
2178 if (!p || (*++p != '/') || (*++p != '/')) {
2179 malformed:
2180 REDEBUG("URI \"%s\" is malformed, can't find start of path", uri);
2181 return -1;
2182 }
2183 p = strchr(p + 1, '/');
2184 if (!p) {
2185 goto malformed;
2186 }
2187
2188 len = (p - uri);
2189
2190 /*
2191 * Unescape any special sequences in the first part of the URI
2192 */
2193 scheme = curl_easy_unescape(candle, uri, len, NULL);
2194 if (!scheme) {
2195 REDEBUG("Error unescaping host");
2196 return -1;
2197 }
2198
2199 /*
2200 * URIs can't contain spaces, so anything after the space must
2201 * be something else.
2202 */
2203 q = strchr(p, ' ');
2204 *out = q ? talloc_typed_asprintf(request, "%s%.*s", scheme, (int)(q - p), p) :
2205 talloc_typed_asprintf(request, "%s%s", scheme, p);
2206
2207 MEM(*out);
2208 curl_free(scheme);
2209
2210 return talloc_array_length(*out) - 1; /* array_length includes \0 */
2211}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
CONF_SECTION * unlang_call_current(request_t *request)
Return the last virtual server that was called.
Definition call.c:225
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1185
#define FR_CURL_REQUEST_SET_OPTION(_x, _y)
Definition base.h:67
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:547
void * uctx
Private data for the module using the API.
Definition base.h:105
CURL * candle
Request specific handle.
Definition base.h:102
Structure representing an individual request being passed to curl for processing.
Definition base.h:101
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void * fr_dcursor_remove(fr_dcursor_t *cursor)
Remove the current item.
Definition dcursor.h:480
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define MEM(x)
Definition debug.h:36
#define RADIUSD_VERSION_STRING
Definition dependency.h:39
static fr_slen_t in
Definition dict.h:831
Test enumeration values.
Definition dict_test.h:92
char * fr_json_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
Returns a JSON string of a list of value pairs.
Definition json.c:1239
int fr_curl_easy_tls_init(fr_curl_io_request_t *randle, fr_curl_tls_t const *conf)
Definition base.c:139
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:443
#define RWDEBUG(fmt,...)
Definition log.h:361
#define RDEBUG3(fmt,...)
Definition log.h:343
#define RERROR(fmt,...)
Definition log.h:298
#define RPWDEBUG(fmt,...)
Definition log.h:366
#define RINDENT()
Indent R* messages by one level.
Definition log.h:430
talloc_free(reap)
#define ROUND_UP(_num, _mul)
Round up - Works in all cases, but is slower.
Definition math.h:148
long int ssize_t
unsigned long int size_t
static size_t used
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
Definition misc.h:59
int strncasecmp(char *s1, char *s2, int n)
Definition missing.c:36
void * env_data
Per call environment data.
Definition module_ctx.h:44
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2591
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1347
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:285
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2787
void radius_pairmove(request_t *request, fr_pair_list_t *to, fr_pair_list_t *from)
Definition pairmove.c:45
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
#define pair_update_request(_attr, _da)
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG2(fmt,...)
Definition radclient.h:54
fr_dict_attr_t const * request_attr_reply
Definition request.c:46
static int rest_decode_post(UNUSED rlm_rest_t const *instance, UNUSED rlm_rest_section_t const *section, request_t *request, fr_curl_io_request_t *randle, char *raw, size_t rawlen)
Converts POST response into fr_pair_ts and adds them to the request.
Definition rest.c:693
static int rest_decode_plain(UNUSED rlm_rest_t const *inst, UNUSED rlm_rest_section_t const *section, request_t *request, UNUSED fr_curl_io_request_t *randle, char *raw, size_t rawlen)
Converts plain response into a single fr_pair_t.
Definition rest.c:649
fr_table_num_sorted_t const http_auth_table[]
Definition rest.c:160
size_t len
Length of data.
Definition rest.c:208
char const * start
Start of the buffer.
Definition rest.c:206
#define CURLAUTH_DIGEST_IE
Definition rest.c:83
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:1782
ssize_t rest_uri_host_unescape(char **out, UNUSED rlm_rest_t const *inst, request_t *request, fr_curl_io_request_t *randle, char const *uri)
Unescapes the host portion of a URI string.
Definition rest.c:2161
int do_xlat
If true value will be expanded with xlat.
Definition rest.c:222
static size_t rest_encode_custom(void *out, size_t size, size_t nmemb, void *userdata)
Copies a pre-expanded xlat string to the output buffer.
Definition rest.c:291
fr_table_num_sorted_t const http_body_type_table[]
Conversion table for type config values.
Definition rest.c:145
static size_t rest_encode_json(void *out, size_t size, size_t nmemb, void *userdata)
Encodes fr_pair_t linked list in JSON format.
Definition rest.c:527
#define CURLOPT_TLSAUTH_SRP
Definition rest.c:74
fr_token_t op
The operator that determines how the new VP.
Definition rest.c:225
#define CURLAUTH_BASIC
Definition rest.c:77
fr_table_num_sorted_t const http_method_table[]
Conversion table for method config values.
Definition rest.c:126
void * rest_mod_conn_create(TALLOC_CTX *ctx, void *instance, UNUSED fr_time_delta_t timeout)
Creates a new connection handle for use by the FR connection API.
Definition rest.c:256
size_t rest_get_handle_data(char const **out, fr_curl_io_request_t *randle)
Extracts pointer to buffer containing response data.
Definition rest.c:1618
static bool rest_request_contains_header(fr_curl_io_request_t *randle, char const *header)
See if the list of headers already contains a header.
Definition rest.c:1737
static int json_pair_alloc(rlm_rest_t const *instance, rlm_rest_section_t const *section, request_t *request, json_object *object, UNUSED int level, int max)
Processes JSON response and converts it into multiple fr_pair_ts.
Definition rest.c:981
static void rest_request_init(rlm_rest_section_t const *section, request_t *request, rlm_rest_request_t *ctx)
(Re-)Initialises the data in a rlm_rest_request_t.
Definition rest.c:626
static ssize_t rest_request_encode_wrapper(char **out, UNUSED rlm_rest_t const *inst, rest_read_t func, size_t limit, void *userdata)
Emulates successive libcurl calls to an encoding function.
Definition rest.c:590
static int rest_decode_json(rlm_rest_t const *instance, rlm_rest_section_t const *section, request_t *request, UNUSED fr_curl_io_request_t *randle, char *raw, UNUSED size_t rawlen)
Converts JSON response into fr_pair_ts and adds them to the request.
Definition rest.c:1195
void rest_response_debug(request_t *request, fr_curl_io_request_t *handle)
Print out the response text.
Definition rest.c:1561
#define CURLAUTH_NTLM
Definition rest.c:89
static fr_pair_t * json_pair_alloc_leaf(UNUSED rlm_rest_t const *instance, UNUSED rlm_rest_section_t const *section, TALLOC_CTX *ctx, request_t *request, fr_dict_attr_t const *da, json_flags_t *flags, json_object *leaf)
Converts JSON "value" key into fr_pair_t.
Definition rest.c:845
fr_table_num_sorted_t const http_content_type_table[]
Conversion table for "Content-Type" header values.
Definition rest.c:188
#define SET_AUTH_OPTION(_x, _y)
int is_json
If true value will be inserted as raw JSON.
Definition rest.c:223
const unsigned long http_curl_auth[REST_HTTP_AUTH_NUM_ENTRIES]
Definition rest.c:100
#define CURLAUTH_NTLM_WB
Definition rest.c:92
const http_body_type_t http_body_type_supported[REST_HTTP_BODY_NUM_ENTRIES]
Table of encoder/decoder support.
Definition rest.c:51
#define CURLAUTH_GSSNEGOTIATE
Definition rest.c:86
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:2135
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:1702
void rest_response_error(request_t *request, fr_curl_io_request_t *handle)
Print out the response text as error lines.
Definition rest.c:1536
size_t http_body_type_table_len
Definition rest.c:158
static int rest_request_config_body(module_ctx_t const *mctx, rlm_rest_section_t const *section, request_t *request, fr_curl_io_request_t *randle, rest_read_t func)
Configures body specific curlopts.
Definition rest.c:1644
static int _mod_conn_free(fr_curl_io_request_t *randle)
Frees a libcurl handle, and any additional memory used by context data.
Definition rest.c:235
static void rest_response_init(rlm_rest_section_t const *section, request_t *request, rlm_rest_response_t *ctx, http_body_type_t type, tmpl_t *header)
(Re-)Initialises the data in a rlm_rest_response_t.
Definition rest.c:1596
static size_t rest_response_body(void *in, size_t size, size_t nmemb, void *userdata)
Processes incoming HTTP body data from libcurl.
Definition rest.c:1456
static size_t rest_encode_post(void *out, size_t size, size_t nmemb, void *userdata)
Encodes fr_pair_t linked list in POST format.
Definition rest.c:350
char const * p
how much text we've sent so far.
Definition rest.c:207
size_t http_auth_table_len
Definition rest.c:172
static size_t rest_response_header(void *in, size_t size, size_t nmemb, void *userdata)
Processes incoming HTTP header data from libcurl.
Definition rest.c:1243
size_t http_content_type_table_len
Definition rest.c:199
#define CURLAUTH_DIGEST
Definition rest.c:80
size_t http_method_table_len
Definition rest.c:134
int rest_response_decode(rlm_rest_t const *instance, rlm_rest_section_t const *section, request_t *request, fr_curl_io_request_t *randle)
Sends the response to the correct decode function.
Definition rest.c:2082
Flags to control the conversion of JSON values to fr_pair_ts.
Definition rest.c:221
Function prototypes and datatypes for the REST (HTTP) transport.
rlm_rest_t const * instance
This instance of rlm_rest.
Definition rest.h:229
read_state_t state
Encoder state.
Definition rest.h:215
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:119
struct curl_slist * headers
Any HTTP headers which will be sent with the request.
Definition rest.h:253
tmpl_t * header
Where to create pairs representing HTTP response headers.
Definition rest.h:243
request_t * request
Current request.
Definition rest.h:232
char * buffer
Raw incoming HTTP data.
Definition rest.h:235
int code
HTTP Status Code.
Definition rest.h:239
write_state_t state
Decoder state.
Definition rest.h:233
bool fail_header_decode
Force header decoding to fail for debugging purposes.
Definition rest.h:169
char const * proxy
Send request via this proxy.
Definition rest.h:109
size_t used
Space used in buffer.
Definition rest.h:237
http_body_type_t type
HTTP Content Type.
Definition rest.h:240
char * body
Pointer to the buffer which contains body data/ Only used when not performing chunked encoding.
Definition rest.h:256
fr_curl_tls_t tls
Definition rest.h:145
#define REST_BODY_MAX_ATTRS
Definition rest.h:41
bool fail_body_decode
Force body decoding to fail for debugging purposes.
Definition rest.h:170
http_body_type_t force_to
Override the Content-Type header in the response to force decoding as a particular type.
Definition rest.h:128
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:131
fr_dcursor_t cursor
Cursor pointing to the start of the list to encode.
Definition rest.h:217
http_body_type_t force_to
Force decoding the body type as a particular encoding.
Definition rest.h:241
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: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
char const * method_str
The string version of the HTTP method.
Definition rest.h:111
@ READ_STATE_ATTR_CONT
Definition rest.h:193
@ READ_STATE_ATTR_BEGIN
Definition rest.h:192
@ READ_STATE_END
Definition rest.h:194
@ READ_STATE_INIT
Definition rest.h:191
rlm_rest_section_t const * section
Section configuration.
Definition rest.h:230
char const * body_str
The string version of the encoding/content type.
Definition rest.h:114
#define REST_BODY_ALLOC_CHUNK
Definition rest.h:40
HIDDEN fr_dict_attr_t const * attr_rest_http_body
Definition rlm_rest.c:284
rlm_rest_section_request_t request
Request configuration.
Definition rest.h:142
struct rlm_rest_call_env_t::@179 response
rlm_rest_response_t response
Response context data.
Definition rest.h:260
size_t(* rest_read_t)(void *ptr, size_t size, size_t nmemb, void *userdata)
Definition rest.h:295
#define REST_BODY_MAX_LEN
Definition rest.h:39
rlm_rest_section_t const * section
Section configuration.
Definition rest.h:212
void * encoder
Encoder specific data.
Definition rest.h:221
rlm_rest_request_t request
Request context data.
Definition rest.h:259
rlm_rest_section_response_t response
Response configuration.
Definition rest.h:143
fr_time_delta_t timeout
Timeout timeval.
Definition rest.h:140
@ WRITE_STATE_INIT
Definition rest.h:201
@ WRITE_STATE_PARSE_HEADERS
Definition rest.h:202
@ WRITE_STATE_PARSE_CONTENT
Definition rest.h:203
char const * name
Section name.
Definition rest.h:138
http_auth_type_t
Definition rest.h:69
@ REST_HTTP_AUTH_NTLM_WB
Definition rest.h:78
@ REST_HTTP_AUTH_NUM_ENTRIES
Definition rest.h:81
@ REST_HTTP_AUTH_BASIC
Definition rest.h:73
@ REST_HTTP_AUTH_NTLM
Definition rest.h:77
@ REST_HTTP_AUTH_DIGEST
Definition rest.h:74
@ REST_HTTP_AUTH_TLS_SRP
Definition rest.h:72
@ REST_HTTP_AUTH_UNKNOWN
Definition rest.h:70
@ REST_HTTP_AUTH_GSSNEGOTIATE
Definition rest.h:76
@ REST_HTTP_AUTH_ANY
Definition rest.h:79
@ REST_HTTP_AUTH_NONE
Definition rest.h:71
@ REST_HTTP_AUTH_DIGEST_IE
Definition rest.h:75
@ REST_HTTP_AUTH_ANY_SAFE
Definition rest.h:80
size_t alloc
Space allocated for buffer.
Definition rest.h:236
struct rlm_rest_call_env_t::@178 request
request_t * request
Current request.
Definition rest.h:214
rlm_rest_t const * instance
This instance of rlm_rest.
Definition rest.h:211
char const * rest_no_proxy
Magic pointer value for determining if we should disable proxying.
Definition rlm_rest.c:95
uint32_t chunk
Max chunk-size (mainly for testing the encoders)
Definition rest.h:123
size_t chunk
Chunk size.
Definition rest.h:219
static char const * name
#define FR_SBUFF_OUT(_start, _len_or_end)
void * data
Module's instance data.
Definition module.h:271
static fr_dict_attr_t const * tmpl_list(tmpl_t const *vpt)
Definition tmpl.h:909
ssize_t tmpl_afrom_attr_str(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, char const *name, tmpl_rules_t const *rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
fr_pair_list_t * tmpl_list_head(request_t *request, fr_dict_attr_t const *list)
Resolve attribute fr_pair_list_t value to an attribute list.
Definition tmpl_eval.c:76
TALLOC_CTX * tmpl_list_ctx(request_t *request, fr_dict_attr_t const *list)
Return the correct TALLOC_CTX to alloc fr_pair_t in, for a list.
Definition tmpl_eval.c:116
int tmpl_request_ptr(request_t **request, FR_DLIST_HEAD(tmpl_request_list) const *rql)
Resolve a tmpl_request_ref_t to a request_t.
Definition tmpl_eval.c:169
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:806
static char const * tmpl_list_name(fr_dict_attr_t const *list, char const *def)
Return the name of a tmpl list or def if list not provided.
Definition tmpl.h:920
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:337
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:163
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
fr_pair_t * vp
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
#define fr_table_value_by_substr(_table, _name, _name_len, _def)
Convert a partial string to a value using an ordered or sorted table.
Definition table.h:693
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen)
Trim a bstr (char) buffer.
Definition talloc.c:628
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:492
Functions which we wish were included in the standard talloc distribution.
#define talloc_get_type_abort_const
Definition talloc.h:282
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:289
Simple time functions.
static int64_t fr_time_delta_to_msec(fr_time_delta_t delta)
Definition time.h:637
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:33
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_OP_SET
Definition token.h:84
@ T_OP_ADD_EQ
Definition token.h:69
ssize_t xlat_aeval(TALLOC_CTX *ctx, char **out, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1530
#define fr_pair_dcursor_by_da_init(_cursor, _list, _da)
Initialise a cursor that will return only attributes matching the specified fr_dict_attr_t.
Definition pair.h:628
ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_token_t quote)
Print the value of an attribute to a string.
Definition pair_print.c:53
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:591
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3370
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4066
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition value.c:4262
static fr_slen_t data
Definition value.h:1274
#define FR_VALUE_BOX_INITIALISER_NULL(_vb)
A static initialiser for stack/globally allocated boxes.
Definition value.h:499
#define fr_box_strvalue_len(_val, _len)
Definition value.h:297
#define fr_value_box_init_null(_vb)
Initialise an empty/null box that will be filled later.
Definition value.h:604
#define fr_box_strvalue(_val)
Definition value.h:296
#define fr_box_time_delta(_val)
Definition value.h:354
#define fr_value_box(_box, _var, _tainted)
Automagically fill in a box, determining the value type from the type of the C variable.
Definition value.h:886
#define fr_value_box_list_foreach(_list_head, _iter)
Definition value.h:217
static size_t char ** out
Definition value.h:1012