The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 5c9c465279d6eeb72570015af474c95d3f74a915 $
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  */
25 RCSID("$Id: 5c9c465279d6eeb72570015af474c95d3f74a915 $")
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  */
102  [REST_HTTP_AUTH_NONE] = 0,
110  [REST_HTTP_AUTH_ANY] = CURLAUTH_ANY,
111  [REST_HTTP_AUTH_ANY_SAFE] = CURLAUTH_ANYSAFE
112 };
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  */
205 typedef 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  */
221 typedef 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
227 } json_flags_t;
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  */
256 void *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  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  */
291 static 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  */
350 static 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 */
370  if (ctx->state == READ_STATE_INIT) ctx->state = READ_STATE_ATTR_BEGIN;
371 
372  while (freespace > 0) {
373  vp = fr_dcursor_current(&ctx->cursor);
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  */
527 static 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 
613  talloc_free(buff);
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  */
626 static 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  */
693 static 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  .prefix = TMPL_ATTR_REF_PREFIX_NO,
739  .dict_def = request->dict,
740  .list_def = request_attr_reply
741  }
742  }) <= 0) {
743  RPWDEBUG("Failed parsing attribute (skipping)");
744  talloc_free(dst);
745  goto skip;
746  }
747 
748  if (tmpl_request_ptr(&current, tmpl_request(dst)) < 0) {
749  RWDEBUG("Attribute name refers to outer request but not in a tunnel (skipping)");
750  talloc_free(dst);
751  goto skip;
752  }
753 
754  vps = tmpl_list_head(current, tmpl_list(dst));
755  if (!vps) {
756  RWDEBUG("List not valid in this context (skipping)");
757  talloc_free(dst);
758  goto skip;
759  }
760  ctx = tmpl_list_ctx(current, tmpl_list(dst));
761  da = tmpl_attr_tail_da(dst);
762 
763  fr_assert(vps);
764 
765  RINDENT();
766  RDEBUG3("Type : %s", fr_type_to_str(da->type));
767 
768  q = strchr(p, '&');
769  len = (!q) ? (rawlen - (p - raw)) : (unsigned)(q - p);
770 
771  value = curl_easy_unescape(candle, p, len, &curl_len);
772 
773  /*
774  * If we found a delimiter we want to skip over it,
775  * if we didn't we do *NOT* want to skip over the end
776  * of the buffer...
777  */
778  p += (!q) ? len : (len + 1);
779 
780  RDEBUG3("Length : %i", curl_len);
781  RDEBUG3("Value : \"%s\"", value);
782  REXDENT();
783 
784  talloc_free(dst); /* Free our temporary tmpl */
785 
786  RDEBUG2("Performing xlat expansion of response value");
787 
788  if (xlat_aeval(request, &expanded, request, value, NULL, NULL) < 0) goto skip;
789 
790  fr_assert(expanded);
791 
792  MEM(vp = fr_pair_afrom_da(ctx, da));
793  if (!vp) {
794  REDEBUG("Failed creating valuepair");
795  talloc_free(expanded);
796 
797  curl_free(name);
798  curl_free(value);
799 
800  return count;
801  }
802 
803  ret = fr_pair_value_from_str(vp, expanded, strlen(value), NULL, true);
804  TALLOC_FREE(expanded);
805  if (ret < 0) {
806  RWDEBUG("Incompatible value assignment, skipping");
807  talloc_free(vp);
808  goto skip;
809  }
810 
811  fr_pair_append(vps, vp);
812 
813  count++;
814 
815  skip:
816  curl_free(name);
817  curl_free(value);
818 
819  continue;
820  }
821 
822  if (!count) REDEBUG("Malformed POST data \"%s\"", raw);
823 
824  return count;
825 
826 }
827 
828 #ifdef HAVE_JSON
829 /** Converts JSON "value" key into fr_pair_t.
830  *
831  * If leaf is not in fact a leaf node, but contains JSON data, the data will
832  * written to the attribute in JSON string format.
833  *
834  * @param[in] instance configuration data.
835  * @param[in] section configuration data.
836  * @param[in] ctx to allocate new fr_pair_ts in.
837  * @param[in] request Current request.
838  * @param[in] da Attribute to create.
839  * @param[in] flags containing the operator other flags controlling value
840  * expansion.
841  * @param[in] leaf object containing the fr_pair_t value.
842  * @return
843  * - #fr_pair_t just created.
844  * - NULL on error.
845  */
847  TALLOC_CTX *ctx, request_t *request,
848  fr_dict_attr_t const *da, json_flags_t *flags, json_object *leaf)
849 {
850  char const *value;
851  char *expanded = NULL;
852  int ret;
853 
854  fr_pair_t *vp;
855 
857 
858  if (json_object_is_type(leaf, json_type_null)) {
859  RDEBUG3("Got null value for attribute \"%s\" (skipping)", da->name);
860  return NULL;
861  }
862 
863  MEM(vp = fr_pair_afrom_da(ctx, da));
864  if (!vp) {
865  RWDEBUG("Failed creating valuepair for attribute \"%s\" (skipping)", da->name);
866  return NULL;
867  }
868 
870 
871  switch (json_object_get_type(leaf)) {
872  case json_type_int:
873  if (flags->do_xlat) RWDEBUG("Ignoring do_xlat on 'int', attribute \"%s\"", da->name);
874  fr_value_box(&src, (int32_t)json_object_get_int(leaf), true);
875  break;
876 
877  case json_type_double:
878  if (flags->do_xlat) RWDEBUG("Ignoring do_xlat on 'double', attribute \"%s\"", da->name);
879  fr_value_box(&src, (double)json_object_get_double(leaf), true);
880  break;
881 
882  case json_type_string:
883  value = json_object_get_string(leaf);
884  if (flags->do_xlat && memchr(value, '%', json_object_get_string_len(leaf))) {
885  if (xlat_aeval(request, &expanded, request, value, NULL, NULL) < 0) {
886  talloc_free(vp);
887  return NULL;
888  }
889  fr_value_box_bstrndup_shallow(&src, NULL, expanded,
890  talloc_array_length(expanded) - 1, true);
891  } else {
893  json_object_get_string_len(leaf), true);
894  }
895  break;
896 
897  default:
898  {
899  char const *str;
900  if (flags->do_xlat) RWDEBUG("Ignoring do_xlat on 'object', attribute \"%s\"", da->name);
901 
902  /*
903  * Should encode any nested JSON structures into JSON strings.
904  *
905  * "I knew you liked JSON so I put JSON in your JSON!"
906  */
907  str = json_object_get_string(leaf);
908  if (!str) {
909  RWDEBUG("Failed getting string value for attribute \"%s\" (skipping)", da->name);
910  talloc_free(vp);
911  return NULL;
912  }
913  fr_value_box_strdup_shallow(&src, NULL, str, true);
914  }
915  }
916 
917  ret = fr_value_box_cast(vp, &vp->data, da->type, da, &src);
918  talloc_free(expanded);
919  if (ret < 0) {
920  RWDEBUG("Failed parsing value for attribute \"%s\" (skipping)", da->name);
921  talloc_free(vp);
922  return NULL;
923  }
924 
925  vp->op = flags->op;
926 
927  return vp;
928 }
929 
930 /** Processes JSON response and converts it into multiple fr_pair_ts
931  *
932  * Processes JSON attribute declarations in the format below. Will recurse when
933  * processing nested attributes. When processing nested attributes flags and
934  * operators from previous attributes are not inherited.
935  *
936  * JSON response format is:
937 @verbatim
938 {
939  "<attribute0>":{
940  "do_xlat":<bool>,
941  "is_json":<bool>,
942  "op":"<operator>",
943  "value":[<value0>,<value1>,<valueN>]
944  },
945  "<attribute1>":{
946  "value":{
947  "<nested-attribute0>":{
948  "op":"<operator>",
949  "value":<value0>
950  }
951  }
952  },
953  "<attribute2>":"<value0>",
954  "<attributeN>":[<value0>,<value1>,<valueN>]
955 }
956 @endverbatim
957  *
958  * JSON valuepair flags:
959  * - do_xlat (optional) Controls xlat expansion of values. Defaults to true.
960  * - is_json (optional) If true, any nested JSON data will be copied to the
961  * fr_pair_t in string form. Defaults to true.
962  * - op (optional) Controls how the attribute is inserted into
963  * the target list. Defaults to ':=' (T_OP_SET).
964  *
965  * If "op" is ':=' or '=', it will be automagically changed to '+=' for the
966  * second and subsequent values in multivalued attributes. This does not work
967  * between multiple attribute declarations.
968  *
969  * @see fr_tokens_table
970  *
971  * @param[in] instance configuration data.
972  * @param[in] section configuration data.
973  * @param[in] request Current request.
974  * @param[in] object containing root node, or parent node.
975  * @param[in] level Current nesting level.
976  * @param[in] max counter, decremented after each fr_pair_t is created,
977  * when 0 no more attributes will be processed.
978  * @return
979  * - Number of attributes created.
980  * - < 0 on error.
981  */
982 static int json_pair_alloc(rlm_rest_t const *instance, rlm_rest_section_t const *section,
983  request_t *request, json_object *object, UNUSED int level, int max)
984 {
985  int max_attrs = max;
986  tmpl_t *dst = NULL;
987 
988  if (!json_object_is_type(object, json_type_object)) {
989 #ifdef HAVE_JSON_TYPE_TO_NAME
990  REDEBUG("Can't process VP container, expected JSON object"
991  "got \"%s\" (skipping)",
992  json_type_to_name(json_object_get_type(object)));
993 #else
994  REDEBUG("Can't process VP container, expected JSON object"
995  " (skipping)");
996 #endif
997  return -1;
998  }
999 
1000  /*
1001  * Process VP container
1002  */
1003  {
1004  json_object_object_foreach(object, name, value) {
1005  int i = 0, elements;
1006  struct json_object *element, *tmp;
1007  TALLOC_CTX *ctx;
1008 
1009  json_flags_t flags = {
1010  .op = T_OP_SET,
1011  .do_xlat = 1,
1012  .is_json = 0
1013  };
1014 
1015  request_t *current = request;
1016  fr_pair_list_t *vps;
1017  fr_pair_t *vp = NULL;
1018 
1019  TALLOC_FREE(dst);
1020 
1021  /*
1022  * Resolve attribute name to a dictionary entry and pairlist.
1023  */
1024  RDEBUG2("Parsing attribute \"%s\"", name);
1025 
1026  if (tmpl_afrom_attr_str(request, NULL, &dst, name,
1027  &(tmpl_rules_t){
1028  .attr = {
1029  .prefix = TMPL_ATTR_REF_PREFIX_NO,
1030  .dict_def = request->dict,
1031  .list_def = request_attr_reply
1032  }
1033  }) <= 0) {
1034  RPWDEBUG("Failed parsing attribute (skipping)");
1035  continue;
1036  }
1037 
1038  if (tmpl_request_ptr(&current, tmpl_request(dst)) < 0) {
1039  RWDEBUG("Attribute name refers to outer request but not in a tunnel (skipping)");
1040  continue;
1041  }
1042 
1043  vps = tmpl_list_head(current, tmpl_list(dst));
1044  if (!vps) {
1045  RWDEBUG("List not valid in this context (skipping)");
1046  continue;
1047  }
1048  ctx = tmpl_list_ctx(current, tmpl_list(dst));
1049 
1050  /*
1051  * Alternative JSON structure which allows operator,
1052  * and other flags to be specified.
1053  *
1054  * "<name>":{
1055  * "do_xlat":<bool>,
1056  * "is_json":<bool>,
1057  * "op":"<op>",
1058  * "value":<value>
1059  * }
1060  *
1061  * Where value is a:
1062  * - [] Multivalued array
1063  * - {} Nested Valuepair
1064  * - * Integer or string value
1065  */
1066  if (json_object_is_type(value, json_type_object)) {
1067  /*
1068  * Process operator if present.
1069  */
1070  if (json_object_object_get_ex(value, "op", &tmp)) {
1071  flags.op = fr_table_value_by_str(fr_tokens_table, json_object_get_string(tmp), 0);
1072  if (!flags.op) {
1073  RWDEBUG("Invalid operator value \"%s\" (skipping)",
1074  json_object_get_string(tmp));
1075  continue;
1076  }
1077  }
1078 
1079  /*
1080  * Process optional do_xlat bool.
1081  */
1082  if (json_object_object_get_ex(value, "do_xlat", &tmp)) {
1083  flags.do_xlat = json_object_get_boolean(tmp);
1084  }
1085 
1086  /*
1087  * Process optional is_json bool.
1088  */
1089  if (json_object_object_get_ex(value, "is_json", &tmp)) {
1090  flags.is_json = json_object_get_boolean(tmp);
1091  }
1092 
1093  /*
1094  * Value key must be present if were using the expanded syntax.
1095  */
1096  if (!json_object_object_get_ex(value, "value", &tmp)) {
1097  RWDEBUG("Value key missing (skipping)");
1098  continue;
1099  }
1100 
1101  /*
1102  * The value field now becomes the key we're operating on
1103  */
1104  value = tmp;
1105  }
1106 
1107  /*
1108  * Setup fr_pair_afrom_da / recursion loop.
1109  */
1110  if (!flags.is_json && json_object_is_type(value, json_type_array)) {
1111  elements = json_object_array_length(value);
1112  if (!elements) {
1113  RWDEBUG("Zero length value array (skipping)");
1114  continue;
1115  }
1116  element = json_object_array_get_idx(value, 0);
1117  } else {
1118  elements = 1;
1119  element = value;
1120  }
1121 
1122  /*
1123  * A JSON 'value' key, may have multiple elements, iterate
1124  * over each of them, creating a new fr_pair_t.
1125  */
1126  do {
1127  fr_pair_list_t tmp_list;
1128 
1129  if (max_attrs-- <= 0) {
1130  RWDEBUG("At maximum attribute limit");
1131  talloc_free(dst);
1132  return max;
1133  }
1134 
1135  /*
1136  * Automagically switch the op for multivalued attributes.
1137  */
1138  if (((flags.op == T_OP_SET) || (flags.op == T_OP_EQ)) && (i >= 1)) {
1139  flags.op = T_OP_ADD_EQ;
1140  }
1141 
1142  if (json_object_is_type(element, json_type_object) && !flags.is_json) {
1143  /* TODO: Insert nested VP into VP structure...*/
1144  RWDEBUG("Found nested VP, these are not yet supported (skipping)");
1145 
1146  continue;
1147 
1148  /*
1149  vp = json_pair_alloc(instance, section,
1150  request, value,
1151  level + 1, max_attrs);*/
1152  } else {
1153  vp = json_pair_alloc_leaf(instance, section, ctx, request,
1154  tmpl_attr_tail_da(dst), &flags, element);
1155  if (!vp) continue;
1156  }
1157  RINDENT();
1158  RDEBUG2("&%s:%pP", tmpl_list_name(tmpl_list(dst), ""), vp);
1159  REXDENT();
1160 
1161  fr_pair_list_init(&tmp_list);
1162  fr_pair_append(&tmp_list, vp);
1163  radius_pairmove(current, vps, &tmp_list);
1164  /*
1165  * If we call json_object_array_get_idx on something that's not an array
1166  * the behaviour appears to be to occasionally segfault.
1167  */
1168  } while ((++i < elements) && (element = json_object_array_get_idx(value, i)));
1169  }
1170  }
1171 
1172  talloc_free(dst);
1173 
1174  return max - max_attrs;
1175 }
1176 
1177 /** Converts JSON response into fr_pair_ts and adds them to the request.
1178  *
1179  * Converts the raw JSON string into a json-c object tree and passes it to
1180  * json_pair_alloc. After the tree has been parsed json_object_put is called
1181  * which decrements the reference count of the root node by one, and frees
1182  * the entire tree.
1183  *
1184  * @see rest_encode_json
1185  * @see json_pair_alloc
1186  *
1187  * @param[in] instance configuration data.
1188  * @param[in] section configuration data.
1189  * @param[in,out] request Current request.
1190  * @param[in] randle REST handle.
1191  * @param[in] raw buffer containing JSON data.
1192  * @param[in] rawlen Length of data in raw buffer.
1193  * @return
1194  * - The number of #fr_pair_t processed.
1195  * - -1 on unrecoverable error.
1196  */
1197 static int rest_decode_json(rlm_rest_t const *instance, rlm_rest_section_t const *section,
1198  request_t *request, UNUSED fr_curl_io_request_t *randle, char *raw, UNUSED size_t rawlen)
1199 {
1200  char const *p = raw;
1201 
1202  struct json_object *json;
1203 
1204  int ret;
1205 
1206  /*
1207  * Empty response?
1208  */
1209  fr_skip_whitespace(p);
1210  if (*p == '\0') return 0;
1211 
1212  json = json_tokener_parse(p);
1213  if (!json) {
1214  REDEBUG("Malformed JSON data \"%s\"", raw);
1215  return -1;
1216  }
1217 
1218  ret = json_pair_alloc(instance, section, request, json, 0, REST_BODY_MAX_ATTRS);
1219 
1220  /*
1221  * Decrement reference count for root object, should free entire JSON tree.
1222  */
1223  json_object_put(json);
1224 
1225  return ret;
1226 }
1227 #endif
1228 
1229 /** Processes incoming HTTP header data from libcurl.
1230  *
1231  * Processes the status line, and Content-Type headers from the incoming HTTP
1232  * response.
1233  *
1234  * Matches prototype for CURLOPT_HEADERFUNCTION, and will be called directly
1235  * by libcurl.
1236  *
1237  * @param[in] in Char buffer where inbound header data is written.
1238  * @param[in] size Multiply by nmemb to get the length of ptr.
1239  * @param[in] nmemb Multiply by size to get the length of ptr.
1240  * @param[in] userdata rlm_rest_response_t to keep parsing state between calls.
1241  * @return
1242  * - Length of data processed.
1243  * - 0 on error.
1244  */
1245 static size_t rest_response_header(void *in, size_t size, size_t nmemb, void *userdata)
1246 {
1247  rlm_rest_response_t *ctx = userdata;
1248  request_t *request = ctx->request; /* Used by RDEBUG */
1249 
1250  char const *start = (char *)in, *p = start, *end = p + (size * nmemb);
1251  char *q;
1252  size_t len;
1253 
1255 
1256 #ifndef NDEBUG
1257  if (ctx->instance->fail_header_decode) {
1258  REDEBUG("Forcing header decode failure");
1259  return 0;
1260  }
1261 #endif
1262 
1263  /*
1264  * This seems to be curl's indication there are no more header lines.
1265  */
1266  if (((end - p) == 2) && ((p[0] == '\r') && (p[1] == '\n'))) {
1267  /*
1268  * If we got a 100 Continue, we need to send additional payload data.
1269  * reset the state to WRITE_STATE_INIT, so that when were called again
1270  * we overwrite previous header data with that from the proper header.
1271  */
1272  if (ctx->code == 100) {
1273  RDEBUG2("Continuing...");
1274  ctx->state = WRITE_STATE_INIT;
1275  }
1276 
1277  return (end - start);
1278  }
1279 
1280  switch (ctx->state) {
1281  case WRITE_STATE_INIT:
1282  RDEBUG2("Processing response header");
1283 
1284  /*
1285  * HTTP/<version> <reason_code>[ <reason_phrase>]\r\n
1286  *
1287  * "HTTP/1.1 " (9) + "100" (3) + "\r\n" (2) = 14
1288  * "HTTP/2 " (7) + "100" (3) + "\r\n" (2) = 12
1289  */
1290  if ((end - p) < 12) {
1291  REDEBUG("Malformed HTTP header: Status line too short");
1292  malformed:
1293  REDEBUG("Received %zu bytes of invalid header data: %pV",
1294  (end - start), fr_box_strvalue_len(in, (end - start)));
1295  ctx->code = 0;
1296 
1297  /*
1298  * Indicate we parsed the entire line, otherwise
1299  * bad things seem to happen internally with
1300  * libcurl when we try and use it with asynchronous
1301  * I/O handlers.
1302  */
1303  return (end - start);
1304  }
1305  /*
1306  * Check start of header matches...
1307  */
1308  if (strncasecmp("HTTP/", p, 5) != 0) {
1309  REDEBUG("Malformed HTTP header: Missing HTTP version");
1310  goto malformed;
1311  }
1312  p += 5;
1313 
1314  /*
1315  * Skip the version field, next space should mark start of reason_code.
1316  */
1317  q = memchr(p, ' ', (end - p));
1318  if (!q) {
1319  REDEBUG("Malformed HTTP header: Missing reason code");
1320  goto malformed;
1321  }
1322 
1323  p = q;
1324 
1325  /*
1326  * Process reason_code.
1327  *
1328  * " 100" (4) + "\r\n" (2) = 6
1329  */
1330  if ((end - p) < 6) {
1331  REDEBUG("Malformed HTTP header: Reason code too short");
1332  goto malformed;
1333  }
1334  p++;
1335 
1336  /*
1337  * "xxx( |\r)" status code and terminator.
1338  */
1339  if (!isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2]) || !((p[3] == ' ') || (p[3] == '\r'))) {
1340  REDEBUG("Malformed HTTP header: Reason code malformed. "
1341  "Expected three digits then space or end of header, got \"%pV\"",
1342  fr_box_strvalue_len(p, 4));
1343  goto malformed;
1344  }
1345 
1346  /*
1347  * Convert status code into an integer value
1348  */
1349  q = NULL;
1350  ctx->code = (int)strtoul(p, &q, 10);
1351  fr_assert(q == (p + 3)); /* We check this above */
1352  p = q;
1353 
1354  /*
1355  * Process reason_phrase (if present).
1356  */
1357  RINDENT();
1358  if (*p == ' ') {
1359  p++;
1360  q = memchr(p, '\r', (end - p));
1361  if (!q) goto malformed;
1362  RDEBUG2("Status : %i (%pV)", ctx->code, fr_box_strvalue_len(p, q - p));
1363  } else {
1364  RDEBUG2("Status : %i", ctx->code);
1365  }
1366  REXDENT();
1367 
1369 
1370  break;
1371 
1373  if (((end - p) >= 14) &&
1374  (strncasecmp("Content-Type: ", p, 14) == 0)) {
1375  p += 14;
1376 
1377  /*
1378  * Check to see if there's a parameter separator.
1379  */
1380  q = memchr(p, ';', (end - p));
1381 
1382  /*
1383  * If there's not, find the end of this header.
1384  */
1385  if (!q) q = memchr(p, '\r', (end - p));
1386 
1387  len = (size_t)(!q ? (end - p) : (q - p));
1389 
1390  RINDENT();
1391  RDEBUG2("Type : %s (%pV)", fr_table_str_by_value(http_body_type_table, type, "<INVALID>"),
1392  fr_box_strvalue_len(p, len));
1393  REXDENT();
1394 
1395  /*
1396  * Assume the force_to value has already been validated.
1397  */
1398  if (ctx->force_to != REST_HTTP_BODY_UNKNOWN) {
1399  if (ctx->force_to != ctx->type) {
1400  RDEBUG3("Forcing body type to \"%s\"",
1402  ctx->type = ctx->force_to;
1403  }
1404  /*
1405  * Figure out if the type is supported by one of the decoders.
1406  */
1407  } else {
1409  switch (ctx->type) {
1411  RWDEBUG("Couldn't determine type, using the request's type \"%s\".",
1413  break;
1414 
1416  REDEBUG("Type \"%s\" is currently unsupported",
1418  break;
1419 
1421  REDEBUG("Type \"%s\" is unavailable, please rebuild this module with the required "
1422  "library", fr_table_str_by_value(http_body_type_table, type, "<INVALID>"));
1423  break;
1424 
1426  REDEBUG("Type \"%s\" is not a valid web API data markup format",
1428  break;
1429 
1430  /* supported type */
1431  default:
1432  break;
1433  }
1434  }
1435  }
1436  break;
1437 
1438  default:
1439  break;
1440  }
1441 
1442  return (end - start);
1443 }
1444 
1445 /** Processes incoming HTTP body data from libcurl.
1446  *
1447  * Writes incoming body data to an intermediary buffer for later parsing by
1448  * one of the decode functions.
1449  *
1450  * @param[in] in Char buffer where inbound header data is written
1451  * @param[in] size Multiply by nmemb to get the length of ptr.
1452  * @param[in] nmemb Multiply by size to get the length of ptr.
1453  * @param[in] userdata rlm_rest_response_t to keep parsing state between calls.
1454  * @return
1455  * - Length of data processed.
1456  * - 0 on error.
1457  */
1458 static size_t rest_response_body(void *in, size_t size, size_t nmemb, void *userdata)
1459 {
1460  rlm_rest_response_t *ctx = userdata;
1461  request_t *request = ctx->request; /* Used by RDEBUG */
1462 
1463  char const *start = in, *p = start, *end = p + (size * nmemb);
1464  char *q;
1465 
1466  size_t needed;
1467 
1468  if (start == end) return 0; /* Nothing to process */
1469 
1470 #ifndef NDEBUG
1471  if (ctx->instance->fail_body_decode) {
1472  REDEBUG("Forcing body read failure");
1473  return 0;
1474  }
1475 #endif
1476 
1477  /*
1478  * Any post processing of headers should go here...
1479  */
1481 
1482  switch (ctx->type) {
1486  while ((q = memchr(p, '\n', (end - p)))) {
1487  REDEBUG("%pV", fr_box_strvalue_len(p, q - p));
1488  p = q + 1;
1489  }
1490 
1491  if (p != end) REDEBUG("%pV", fr_box_strvalue_len(p, end - p));
1492  break;
1493 
1494  case REST_HTTP_BODY_NONE:
1495  while ((q = memchr(p, '\n', (end - p)))) {
1496  RDEBUG3("%pV", fr_box_strvalue_len(p, q - p));
1497  p = q + 1;
1498  }
1499 
1500  if (p != end) RDEBUG3("%pV", fr_box_strvalue_len(p, end - p));
1501  break;
1502 
1503  default:
1504  {
1505  char *out_p;
1506 
1507  if ((ctx->section->response.max_body_in > 0) && ((ctx->used + (end - p)) > ctx->section->response.max_body_in)) {
1508  REDEBUG("Incoming data (%zu bytes) exceeds max_body_in (%zu bytes). "
1509  "Forcing body to type 'invalid'", ctx->used + (end - p), ctx->section->response.max_body_in);
1511  TALLOC_FREE(ctx->buffer);
1512  break;
1513  }
1514 
1515  needed = ROUND_UP(ctx->used + (end - p), REST_BODY_ALLOC_CHUNK);
1516  if (needed > ctx->alloc) {
1517  MEM(ctx->buffer = talloc_bstr_realloc(NULL, ctx->buffer, needed));
1518  ctx->alloc = needed;
1519  }
1520 
1521  out_p = ctx->buffer + ctx->used;
1522  memcpy(out_p, p, (end - p));
1523  out_p += (end - p);
1524  *out_p = '\0';
1525  ctx->used += (end - p);
1526  }
1527  break;
1528  }
1529 
1530  return (end - start);
1531 }
1532 
1533 /** Print out the response text as error lines
1534  *
1535  * @param request The Current request.
1536  * @param handle fr_curl_io_request_t used to execute the previous request.
1537  */
1539 {
1540  char const *p, *end;
1541  char *q;
1542  size_t len;
1543 
1544  len = rest_get_handle_data(&p, handle);
1545  if (len == 0) return;
1546 
1547  end = p + len;
1548 
1549  RERROR("Server returned:");
1550  while ((q = memchr(p, '\n', (end - p)))) {
1551  RERROR("%pV", fr_box_strvalue_len(p, q - p));
1552  p = q + 1;
1553  }
1554 
1555  if (p != end) RERROR("%pV", fr_box_strvalue_len(p, end - p));
1556 }
1557 
1558 /** Print out the response text
1559  *
1560  * @param request The Current request.
1561  * @param handle fr_curl_io_request_t used to execute the previous request.
1562  */
1564 {
1565  char const *p, *end;
1566  char *q;
1567  size_t len;
1568 
1569  len = rest_get_handle_data(&p, handle);
1570  if (len == 0) return;
1571 
1572  end = p + len;
1573 
1574  RDEBUG3("Server returned:");
1575  while ((q = memchr(p, '\n', (end - p)))) {
1576  RDEBUG3("%pV", fr_box_strvalue_len(p, q - p));
1577  p = q + 1;
1578  }
1579 
1580  if (p != end) RDEBUG3("%pV", fr_box_strvalue_len(p, end - p));
1581 }
1582 
1583 /** (Re-)Initialises the data in a rlm_rest_response_t.
1584  *
1585  * This resets the values of the a rlm_rest_response_t to their defaults.
1586  * Must be called between encoding sessions.
1587  *
1588  * @see rest_response_body
1589  * @see rest_response_header
1590  *
1591  * @param[in] section that created the request.
1592  * @param[in] request Current request.
1593  * @param[in] ctx data to initialise.
1594  * @param[in] type Default http_body_type to use when decoding raw data, may be
1595  * overwritten by rest_response_header.
1596  * @param[in] header Where to write out headers, may be NULL.
1597  */
1598 static void rest_response_init(rlm_rest_section_t const *section,
1599  request_t *request, rlm_rest_response_t *ctx, http_body_type_t type, tmpl_t *header)
1600 {
1601  ctx->section = section;
1602  ctx->request = request;
1603  ctx->type = type;
1604  ctx->state = WRITE_STATE_INIT;
1605  ctx->alloc = 0;
1606  ctx->used = 0;
1607  ctx->code = 0;
1608  ctx->header = header;
1609  TALLOC_FREE(ctx->buffer);
1610 }
1611 
1612 /** Extracts pointer to buffer containing response data
1613  *
1614  * @param[out] out Where to write the pointer to the buffer.
1615  * @param[in] randle used for the last request.
1616  * @return
1617  * - 0 if no data i available.
1618  * - > 0 if data is available.
1619  */
1620 size_t rest_get_handle_data(char const **out, fr_curl_io_request_t *randle)
1621 {
1622  rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1623 
1624  if (!ctx->response.buffer) return 0;
1625 
1626  *out = ctx->response.buffer;
1627  return ctx->response.used;
1628 }
1629 
1630 /** Configures body specific curlopts.
1631  *
1632  * Configures libcurl handle to use either chunked mode, where the request
1633  * data will be sent using multiple HTTP requests, or contiguous mode where
1634  * the request data will be sent in a single HTTP request.
1635  *
1636  * @param[in] mctx Call data.
1637  * @param[in] section configuration data.
1638  * @param[in] request Current request.
1639  * @param[in] randle fr_curl_io_request_t to configure.
1640  * @param[in] func to pass to libcurl for chunked.
1641  * transfers (NULL if not using chunked mode).
1642  * @return
1643  * - 0 on success.
1644  * - -1 on failure.
1645  */
1646 static int rest_request_config_body(module_ctx_t const *mctx, rlm_rest_section_t const *section,
1647  request_t *request, fr_curl_io_request_t *randle, rest_read_t func)
1648 {
1649  rlm_rest_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_rest_t);
1650  rlm_rest_curl_context_t *uctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1651  ssize_t len;
1652 
1653  /*
1654  * We were provided with no read function, assume this means
1655  * no body should be sent.
1656  */
1657  if (!func) {
1658  FR_CURL_REQUEST_SET_OPTION(CURLOPT_POSTFIELDSIZE, 0);
1659  return 0;
1660  }
1661 
1662  /*
1663  * Chunked transfer encoding means the body will be sent in
1664  * multiple parts.
1665  */
1666  if (section->request.chunk > 0) {
1667  FR_CURL_REQUEST_SET_OPTION(CURLOPT_READDATA, &uctx->request);
1668  FR_CURL_REQUEST_SET_OPTION(CURLOPT_READFUNCTION, func);
1669 
1670  return 0;
1671  }
1672 
1673  /*
1674  * If were not doing chunked encoding then we read the entire
1675  * body into a buffer, and send it in one go.
1676  */
1677  len = rest_request_encode_wrapper(&uctx->body, inst, func, REST_BODY_MAX_LEN, &uctx->request);
1678  if (len <= 0) {
1679  REDEBUG("Failed creating HTTP body content");
1680  return -1;
1681  }
1682  RDEBUG2("Content-Length will be %zu bytes", len);
1683 
1684  fr_assert((len == 0) || (talloc_array_length(uctx->body) >= (size_t)len));
1685  FR_CURL_REQUEST_SET_OPTION(CURLOPT_POSTFIELDS, uctx->body);
1686  FR_CURL_REQUEST_SET_OPTION(CURLOPT_POSTFIELDSIZE, len);
1687 
1688  return 0;
1689 
1690 error:
1691  return -1;
1692 }
1693 
1694 /** Configures request curlopts.
1695  *
1696  * Configures libcurl handle setting various curlopts for things like local
1697  * client time, Content-Type, and other FreeRADIUS custom headers.
1698  *
1699  * Current FreeRADIUS custom headers are:
1700  * - X-FreeRADIUS-Section The module section being processed.
1701  * - X-FreeRADIUS-Server The current virtual server the request_t is
1702  * passing through.
1703  *
1704  * Sets up callbacks for all response processing (buffers and body data).
1705  *
1706  * @param[in] mctx call data.
1707  * @param[in] section configuration data.
1708  * @param[in] randle to configure.
1709  * @param[in] request Current request.
1710  * @param[in] method to use (HTTP verbs PUT, POST, DELETE etc...).
1711  * @param[in] type Content-Type for request encoding, also sets
1712  * the default for decoding.
1713  * @param[in] uri buffer containing the expanded URI to send the request to.
1714  * @param[in] body_data (optional) custom body data. Must persist whilst we're
1715  * writing data out to the socket. Must be a talloced buffer
1716  * which is \0 terminated.
1717  * @return
1718  * - 0 on success (all opts configured).
1719  * - -1 on failure.
1720  */
1721 int rest_request_config(module_ctx_t const *mctx, rlm_rest_section_t const *section,
1722  request_t *request, fr_curl_io_request_t *randle, http_method_t method,
1724  char const *uri, char const *body_data)
1725 {
1726  rlm_rest_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_rest_t);
1727  rlm_rest_call_env_t *call_env = talloc_get_type_abort(mctx->env_data, rlm_rest_call_env_t);
1728  rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
1729  CURL *candle = randle->candle;
1731 
1732  http_auth_type_t auth = section->request.auth;
1733 
1734  CURLcode ret = CURLE_OK;
1735  char const *option;
1736 
1737  char buffer[512];
1738  bool content_type_set = false;
1739 
1740  fr_assert(candle);
1741 
1742  buffer[(sizeof(buffer) - 1)] = '\0';
1743 
1744  /*
1745  * Control which HTTP version we're going to use
1746  */
1747  if (inst->http_negotiation != CURL_HTTP_VERSION_NONE) FR_CURL_REQUEST_SET_OPTION(CURLOPT_HTTP_VERSION, inst->http_negotiation);
1748 
1749  /*
1750  * Setup any header options and generic headers.
1751  */
1752  FR_CURL_REQUEST_SET_OPTION(CURLOPT_URL, uri);
1753 #if CURL_AT_LEAST_VERSION(7,85,0)
1754  FR_CURL_REQUEST_SET_OPTION(CURLOPT_PROTOCOLS_STR, "http,https");
1755 #else
1756  FR_CURL_REQUEST_SET_OPTION(CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
1757 #endif
1758  if (section->request.proxy) {
1759  if (section->request.proxy == rest_no_proxy) {
1760  FR_CURL_REQUEST_SET_OPTION(CURLOPT_NOPROXY, "*");
1761  } else {
1762  FR_CURL_REQUEST_SET_OPTION(CURLOPT_PROXY, section->request.proxy);
1763  }
1764  }
1765  FR_CURL_REQUEST_SET_OPTION(CURLOPT_NOSIGNAL, 1L);
1766  FR_CURL_REQUEST_SET_OPTION(CURLOPT_USERAGENT, "FreeRADIUS " RADIUSD_VERSION_STRING);
1767 
1768  timeout = inst->conn_config.connect_timeout;
1769  RDEBUG3("Connect timeout is %pVs, request timeout is %pVs",
1771  FR_CURL_REQUEST_SET_OPTION(CURLOPT_CONNECTTIMEOUT_MS, fr_time_delta_to_msec(timeout));
1772  FR_CURL_REQUEST_SET_OPTION(CURLOPT_TIMEOUT_MS, fr_time_delta_to_msec(section->timeout));
1773 
1774  /*
1775  * FreeRADIUS custom headers
1776  */
1777  RDEBUG3("Adding custom headers:");
1778  snprintf(buffer, sizeof(buffer), "X-FreeRADIUS-Section: %s", section->name);
1779 
1780  RINDENT();
1781  RDEBUG3("%s", buffer);
1782  REXDENT();
1783  ctx->headers = curl_slist_append(ctx->headers, buffer);
1784  if (!ctx->headers) goto error_header;
1785 
1786  snprintf(buffer, sizeof(buffer), "X-FreeRADIUS-Server: %s", cf_section_name2(unlang_call_current(request)));
1787  RINDENT();
1788  RDEBUG3("%s", buffer);
1789  REXDENT();
1790  ctx->headers = curl_slist_append(ctx->headers, buffer);
1791  if (!ctx->headers) goto error_header;
1792 
1793  /*
1794  * Add in the section headers
1795  */
1796  if (call_env->request.header) {
1797  size_t len = talloc_array_length(call_env->request.header), i;
1798  for (i = 0; i < len; i++) {
1799  fr_value_box_list_foreach(&call_env->request.header[i], header) {
1800  RINDENT();
1801  RDEBUG3("%pV", header);
1802  REXDENT();
1803 
1804  ctx->headers = curl_slist_append(ctx->headers, header->vb_strvalue);
1805  }
1806  }
1807  }
1808 
1809  /*
1810  * Add in dynamic headers from the request
1811  */
1812  {
1813  fr_pair_t *header;
1814  fr_dcursor_t headers;
1815 
1816  for (header = fr_pair_dcursor_by_da_init(&headers, &request->control_pairs, attr_rest_http_header);
1817  header;
1818  header = fr_dcursor_current(&headers)) {
1819  header = fr_dcursor_remove(&headers);
1820  if (!strchr(header->vp_strvalue, ':')) {
1821  RWDEBUG("Invalid HTTP header \"%s\" must be in format '<attribute>: <value>'. Skipping...",
1822  header->vp_strvalue);
1823  talloc_free(header);
1824  continue;
1825  }
1826  RINDENT();
1827  RDEBUG3("%pV", &header->data);
1828  REXDENT();
1829 
1830  ctx->headers = curl_slist_append(ctx->headers, header->vp_strvalue);
1831 
1832  /*
1833  * Set content-type based on a corresponding REST-HTTP-Header attribute, if provided.
1834  */
1835  if (!content_type_set && (strncasecmp(header->vp_strvalue, "content-type:", sizeof("content-type:") - 1) == 0)) {
1836  char const *content_type = header->vp_strvalue + (sizeof("content-type:") - 1);
1837 
1838  while (isspace((uint8_t)*content_type)) content_type++;
1839 
1840  RDEBUG3("Request body content-type provided as \"%s\"", content_type);
1841 
1842  content_type_set = true;
1843  }
1844 
1845  talloc_free(header);
1846  }
1847  }
1848 
1849  if (!content_type_set) {
1850  /*
1851  * HTTP/1.1 doesn't require a content type so only set it
1852  * if where body type requires it, and we haven't set one
1853  * already from attributes.
1854  */
1855  if (type != REST_HTTP_BODY_NONE) {
1856  char const *content_type = fr_table_str_by_value(http_content_type_table, type, section->request.body_str);
1857  snprintf(buffer, sizeof(buffer), "Content-Type: %s", content_type);
1858  ctx->headers = curl_slist_append(ctx->headers, buffer);
1859  if (!ctx->headers) {
1860  error_header:
1861  REDEBUG("Failed creating header");
1862  return -1;
1863  }
1864 
1865  RDEBUG3("Request body content-type will be \"%s\"", content_type);
1866  }
1867  }
1868 
1869  /*
1870  * Configure HTTP verb (GET, POST, PUT, PATCH, DELETE, other...)
1871  */
1872  switch (method) {
1873  case REST_HTTP_METHOD_GET:
1874  FR_CURL_REQUEST_SET_OPTION(CURLOPT_HTTPGET, 1L);
1875  break;
1876 
1877  case REST_HTTP_METHOD_POST:
1878  FR_CURL_REQUEST_SET_OPTION(CURLOPT_POST, 1L);
1879  break;
1880 
1881  case REST_HTTP_METHOD_PUT:
1882  /*
1883  * Do not set CURLOPT_PUT, this will cause libcurl
1884  * to ignore CURLOPT_POSTFIELDs and attempt to read
1885  * whatever was set with CURLOPT_READDATA, which by
1886  * default is stdin.
1887  *
1888  * This is many cases will cause the server to block,
1889  * indefinitely.
1890  */
1891  FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, "PUT");
1892  break;
1893 
1895  FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, "PATCH");
1896  break;
1897 
1899  FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, "DELETE");
1900  break;
1901 
1903  FR_CURL_REQUEST_SET_OPTION(CURLOPT_CUSTOMREQUEST, section->request.method_str);
1904  break;
1905 
1906  default:
1907  fr_assert(0);
1908  break;
1909  }
1910 
1911  /*
1912  * Set user based authentication parameters
1913  */
1914  if (auth > REST_HTTP_AUTH_NONE) {
1915 
1916 #define SET_AUTH_OPTION(_x, _y)\
1917 do {\
1918  if ((ret = curl_easy_setopt(candle, _x, _y)) != CURLE_OK) {\
1919  option = STRINGIFY(_x);\
1920  REDEBUG("Failed setting curl option %s: %s (%i)", option, curl_easy_strerror(ret), ret); \
1921  goto error;\
1922  }\
1923 } while (0)
1924  RDEBUG3("Configuring HTTP auth type %s, user \"%pV\", password \"%pV\"",
1925  fr_table_str_by_value(http_auth_table, auth, "<INVALID>"),
1926  call_env->request.username ? call_env->request.username : fr_box_strvalue("(none)"),
1927  call_env->request.password ? call_env->request.password : fr_box_strvalue("(none)"));
1928 
1929  /*
1930  * FIXME - We probably want to escape \0s here...
1931  */
1932  if ((auth >= REST_HTTP_AUTH_BASIC) && (auth <= REST_HTTP_AUTH_ANY_SAFE)) {
1933  SET_AUTH_OPTION(CURLOPT_HTTPAUTH, http_curl_auth[auth]);
1934  if (call_env->request.username) SET_AUTH_OPTION(CURLOPT_USERNAME, call_env->request.username->vb_strvalue);
1935  if (call_env->request.password) SET_AUTH_OPTION(CURLOPT_PASSWORD, call_env->request.password->vb_strvalue);
1936  } else if (auth == REST_HTTP_AUTH_TLS_SRP) {
1937  SET_AUTH_OPTION(CURLOPT_TLSAUTH_TYPE, http_curl_auth[auth]);
1938  if (call_env->request.username) SET_AUTH_OPTION(CURLOPT_TLSAUTH_USERNAME, call_env->request.username->vb_strvalue);
1939  if (call_env->request.password) SET_AUTH_OPTION(CURLOPT_TLSAUTH_PASSWORD, call_env->request.password->vb_strvalue);
1940  }
1941  }
1942 
1943  /*
1944  * Set SSL/TLS authentication parameters
1945  */
1946  fr_curl_easy_tls_init(randle, &section->tls);
1947 
1948  /*
1949  * Tell CURL how to get HTTP body content, and how to process incoming data.
1950  */
1951  rest_response_init(section, request, &ctx->response, type, call_env->response.header);
1952 
1953  FR_CURL_REQUEST_SET_OPTION(CURLOPT_HEADERFUNCTION, rest_response_header);
1954  FR_CURL_REQUEST_SET_OPTION(CURLOPT_HEADERDATA, &ctx->response);
1955  FR_CURL_REQUEST_SET_OPTION(CURLOPT_WRITEFUNCTION, rest_response_body);
1956  FR_CURL_REQUEST_SET_OPTION(CURLOPT_WRITEDATA, &ctx->response);
1957 
1958  /*
1959  * Force parsing the body text as a particular encoding.
1960  */
1961  ctx->response.force_to = section->response.force_to;
1962 
1963  switch (method) {
1964  case REST_HTTP_METHOD_GET:
1966  RDEBUG3("Using a HTTP method which does not require a body. Forcing request body type to \"none\"");
1967  goto finish;
1968 
1969  case REST_HTTP_METHOD_POST:
1970  case REST_HTTP_METHOD_PUT:
1973  if (section->request.chunk > 0) {
1974  ctx->request.chunk = section->request.chunk;
1975 
1976  ctx->headers = curl_slist_append(ctx->headers, "Expect:");
1977  if (!ctx->headers) goto error_header;
1978 
1979  ctx->headers = curl_slist_append(ctx->headers, "Transfer-Encoding: chunked");
1980  if (!ctx->headers) goto error_header;
1981  }
1982 
1983  break;
1984 
1985  default:
1986  fr_assert(0);
1987  }
1988 
1989  /*
1990  * Setup encoder specific options
1991  */
1992  switch (type) {
1993  case REST_HTTP_BODY_NONE:
1994  if (rest_request_config_body(mctx, section, request, randle, NULL) < 0) return -1;
1995 
1996  break;
1997 
1998  case REST_HTTP_BODY_CUSTOM:
1999  {
2001 
2002  data = talloc_zero(request, rest_custom_data_t);
2003  data->p = data->start = body_data;
2004  data->len = talloc_strlen(body_data);
2005 
2006  /* Use the encoder specific pointer to store the data we need to encode */
2007  ctx->request.encoder = data;
2008  if (rest_request_config_body(mctx, section, request, randle, rest_encode_custom) < 0) {
2009  TALLOC_FREE(ctx->request.encoder);
2010  return -1;
2011  }
2012  }
2013  break;
2014 
2015 #ifdef HAVE_JSON
2016  case REST_HTTP_BODY_JSON:
2017  {
2019 
2020  data = talloc_zero(request, rest_custom_data_t);
2021  ctx->request.encoder = data;
2022 
2023  rest_request_init(section, request, &ctx->request);
2024 
2025  if (rest_request_config_body(mctx, section, request, randle, rest_encode_json) < 0) return -1;
2026  }
2027 
2028  break;
2029 #endif
2030 
2031  case REST_HTTP_BODY_POST:
2032  rest_request_init(section, request, &ctx->request);
2033  fr_pair_dcursor_init(&(ctx->request.cursor), &request->request_pairs);
2034 
2035  if (rest_request_config_body(mctx, section, request, randle, rest_encode_post) < 0) return -1;
2036 
2037  break;
2038 
2039  default:
2040  fr_assert(0);
2041  }
2042 
2043 
2044 finish:
2045  FR_CURL_REQUEST_SET_OPTION(CURLOPT_HTTPHEADER, ctx->headers);
2046 
2047  return 0;
2048 
2049 error:
2050  return -1;
2051 }
2052 
2053 /** Sends the response to the correct decode function.
2054  *
2055  * Uses the Content-Type information written in rest_response_header to
2056  * determine the correct decode function to use. The decode function will
2057  * then convert the raw received data into fr_pair_ts.
2058  *
2059  * @param[in] instance configuration data.
2060  * @param[in] section configuration data.
2061  * @param[in] request Current request.
2062  * @param[in] randle to use.
2063  * @return
2064  * - 0 on success.
2065  * - -1 on failure.
2066  */
2067 int rest_response_decode(rlm_rest_t const *instance, rlm_rest_section_t const *section,
2068  request_t *request, fr_curl_io_request_t *randle)
2069 {
2070  rlm_rest_curl_context_t *ctx = talloc_get_type_abort(randle->uctx, rlm_rest_curl_context_t);
2071 
2072  int ret = -1; /* -Wsometimes-uninitialized */
2073 
2074  if (!ctx->response.buffer) {
2075  RDEBUG2("Skipping attribute processing, no valid body data received");
2076  return 0;
2077  }
2078 
2079  switch (ctx->response.type) {
2080  case REST_HTTP_BODY_NONE:
2081  return 0;
2082 
2083  case REST_HTTP_BODY_PLAIN:
2084  ret = rest_decode_plain(instance, section, request, randle, ctx->response.buffer, ctx->response.used);
2085  break;
2086 
2087  case REST_HTTP_BODY_POST:
2088  ret = rest_decode_post(instance, section, request, randle, ctx->response.buffer, ctx->response.used);
2089  break;
2090 
2091 #ifdef HAVE_JSON
2092  case REST_HTTP_BODY_JSON:
2093  ret = rest_decode_json(instance, section, request, randle, ctx->response.buffer, ctx->response.used);
2094  break;
2095 #endif
2096 
2100  return -1;
2101 
2102  default:
2103  fr_assert(0);
2104  }
2105 
2106  return ret;
2107 }
2108 
2109 /** URL encodes a string.
2110  *
2111  * Encode special chars as per RFC 3986 section 4.
2112  *
2113  * @param[in] request Current request.
2114  * @param[out] out Where to write escaped string.
2115  * @param[in] outlen Size of out buffer.
2116  * @param[in] raw string to be urlencoded.
2117  * @param[in] arg pointer, gives context for escaping.
2118  * @return length of data written to out (excluding NULL).
2119  */
2120 size_t rest_uri_escape(UNUSED request_t *request, char *out, size_t outlen, char const *raw, UNUSED void *arg)
2121 {
2122  char *escaped;
2123 
2124  escaped = curl_escape(raw, 0);
2125  strlcpy(out, escaped, outlen);
2126  curl_free(escaped);
2127 
2128  return strlen(out);
2129 }
2130 
2131 /** Unescapes the host portion of a URI string
2132  *
2133  * This is required because the xlat functions which operate on the input string
2134  * cannot distinguish between host and path components.
2135  *
2136  * @param[out] out Where to write the pointer to the new
2137  * buffer containing the escaped URI.
2138  * @param[in] inst of rlm_rest.
2139  * @param[in] request Current request
2140  * @param[in] randle to use.
2141  * @param[in] uri configuration data.
2142  * @return
2143  * - Length of data written to buffer (excluding NULL).
2144  * - < 0 if an error occurred.
2145  */
2147  fr_curl_io_request_t *randle, char const *uri)
2148 {
2149  CURL *candle = randle->candle;
2150 
2151  char const *p, *q;
2152 
2153  char *scheme;
2154 
2155  ssize_t len;
2156 
2157  p = uri;
2158 
2159  /*
2160  * All URLs must contain at least <scheme>://<server>/
2161  */
2162  p = strchr(p, ':');
2163  if (!p || (*++p != '/') || (*++p != '/')) {
2164  malformed:
2165  REDEBUG("URI \"%s\" is malformed, can't find start of path", uri);
2166  return -1;
2167  }
2168  p = strchr(p + 1, '/');
2169  if (!p) {
2170  goto malformed;
2171  }
2172 
2173  len = (p - uri);
2174 
2175  /*
2176  * Unescape any special sequences in the first part of the URI
2177  */
2178  scheme = curl_easy_unescape(candle, uri, len, NULL);
2179  if (!scheme) {
2180  REDEBUG("Error unescaping host");
2181  return -1;
2182  }
2183 
2184  /*
2185  * URIs can't contain spaces, so anything after the space must
2186  * be something else.
2187  */
2188  q = strchr(p, ' ');
2189  *out = q ? talloc_typed_asprintf(request, "%s%.*s", scheme, (int)(q - p), p) :
2190  talloc_typed_asprintf(request, "%s%s", scheme, p);
2191 
2192  MEM(*out);
2193  curl_free(scheme);
2194 
2195  return talloc_array_length(*out) - 1; /* array_length includes \0 */
2196 }
static int const char char buffer[256]
Definition: acutest.h:574
#define RCSID(id)
Definition: build.h:481
#define L(_str)
Helper for initialising arrays of string literals.
Definition: build.h:207
#define UNUSED
Definition: build.h:313
#define NUM_ELEMENTS(_t)
Definition: build.h:335
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:546
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_remove(fr_dcursor_t *cursor)
Remove the current item.
Definition: dcursor.h:480
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
fr_dcursor_iter_t void * current
Definition: dcursor.h:148
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition: dcursor.h:288
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition: dcursor.h:337
#define RADIUSD_VERSION_STRING
Definition: dependency.h:39
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
static fr_slen_t in
Definition: dict.h:821
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:141
#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
@ TMPL_ATTR_REF_PREFIX_NO
Attribute refs have no '&' prefix.
Definition: merged_model.c:230
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
unsigned long int size_t
Definition: merged_model.c:25
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
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:283
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:1345
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:2784
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, bool tainted)
Convert string value to native attribute value.
Definition: pair.c:2589
void radius_pairmove(request_t *request, fr_pair_list_t *to, fr_pair_list_t *from)
Definition: pairmove.c:45
#define pair_update_request(_attr, _da)
Definition: radclient-ng.c:60
#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:1721
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:2146
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
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:846
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:1620
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:982
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:1197
void rest_response_debug(request_t *request, fr_curl_io_request_t *handle)
Print out the response text.
Definition: rest.c:1563
#define CURLAUTH_NTLM
Definition: rest.c:89
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:2120
void rest_response_error(request_t *request, fr_curl_io_request_t *handle)
Print out the response text as error lines.
Definition: rest.c:1538
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:1646
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:1598
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:1458
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:1245
size_t http_content_type_table_len
Definition: rest.c:199
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
#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:2067
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:282
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
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:281
rlm_rest_section_request_t request
Request configuration.
Definition: rest.h:142
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::@171 response
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
struct rlm_rest_call_env_t::@170 request
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
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
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:926
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition: tmpl.h:812
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
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_list(tmpl_t const *vpt)
Definition: tmpl.h:915
Optional arguments passed to vp_tmpl functions.
Definition: tmpl.h:341
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
fr_assert(0)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
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:1545
#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:40
#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:3352
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:4036
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:4232
static fr_slen_t data
Definition: value.h:1265
#define FR_VALUE_BOX_INITIALISER_NULL(_vb)
A static initialiser for stack/globally allocated boxes.
Definition: value.h:488
#define fr_box_strvalue_len(_val, _len)
Definition: value.h:286
#define fr_value_box_init_null(_vb)
Initialise an empty/null box that will be filled later.
Definition: value.h:593
#define fr_box_strvalue(_val)
Definition: value.h:285
#define fr_box_time_delta(_val)
Definition: value.h:343
#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:871
#define fr_value_box_list_foreach(_list_head, _iter)
Definition: value.h:206
static size_t char ** out
Definition: value.h:997