The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_perl.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 (at
5 * 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: ad63c797bf915064a4124fc6be8be2466b9c2bf0 $
19 * @file rlm_perl.c
20 * @brief Translates requests between the server an a perl interpreter.
21 *
22 * @copyright 2002,2006 The FreeRADIUS server project
23 * @copyright 2002 Boian Jordanov (bjordanov@orbitel.bg)
24 */
25RCSID("$Id: ad63c797bf915064a4124fc6be8be2466b9c2bf0 $")
26
27#define LOG_PREFIX "perl"
28
29#include <freeradius-devel/server/base.h>
30#include <freeradius-devel/server/module_rlm.h>
31#include <freeradius-devel/util/debug.h>
32#include <freeradius-devel/unlang/xlat_func.h>
33
35DIAG_OFF(compound-token-split-by-macro) /* Perl does horrible things with macros */
36DIAG_OFF(unreachable-code-return)
37DIAG_OFF(unreachable-code-break)
39
40#ifdef INADDR_ANY
41# undef INADDR_ANY
42#endif
43#include <EXTERN.h>
44#include <perl.h>
45#include <XSUB.h>
46#include <dlfcn.h>
47#include <semaphore.h>
48
49#if defined(__APPLE__) || defined(__FreeBSD__)
50extern char **environ;
51#endif
52
53#ifndef USE_ITHREADS
54# error perl must be compiled with USE_ITHREADS
55#endif
56
57typedef struct {
58 char const *function_name; //!< Name of the function being called
59 char *name1; //!< Section name1 where this is called
60 char *name2; //!< Section name2 where this is called
61 fr_rb_node_t node; //!< Node in tree of function calls.
63
64typedef struct {
67
71
72/*
73 * Define a structure for our module configuration.
74 *
75 * These variables do not need to be in a structure, but it's
76 * a lot cleaner to do so, and a pointer to the structure can
77 * be used as the instance handle.
78 */
79typedef struct {
80 /* Name of the perl module */
81 char const *module;
82
83 fr_rb_tree_t funcs; //!< Tree of function calls found by call_env parser.
84 bool funcs_init; //!< Has the tree been initialised.
85 char const *func_detach; //!< Function to run when mod_detach is run.
86 char const *perl_flags;
87 PerlInterpreter *perl;
89 HV *rad_perlconf_hv; //!< holds "config" items (perl %RAD_PERLCONF hash).
91
93
94typedef struct {
95 PerlInterpreter *perl; //!< Thread specific perl interpreter.
97
98/*
99 * C structure associated with tied hashes and arrays
100 */
103 fr_dict_attr_t const *da; //!< Dictionary attribute associated with hash / array.
104 fr_pair_t *vp; //!< Real pair associated with the hash / array, if it exists.
105 unsigned int idx; //!< Instance number.
106 fr_perl_pair_t *parent; //!< Parent attribute data.
107 fr_dcursor_t cursor; //!< Cursor used for iterating over the keys of a tied hash.
108};
109
110/*
111 * Dummy Magic Virtual Table used to ensure we retrieve the correct magic data
112 */
113static MGVTBL rlm_perl_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 };
114
115static void *perl_dlhandle; //!< To allow us to load perl's symbols into the global symbol table.
116
117/*
118 * A mapping of configuration file names to internal variables.
119 */
120static const conf_parser_t module_config[] = {
122
123 { FR_CONF_OFFSET("func_detach", rlm_perl_t, func_detach), .data = NULL, .dflt = "detach", .quote = T_INVALID },
124
125 { FR_CONF_OFFSET("perl_flags", rlm_perl_t, perl_flags) },
126
128};
129
130/** How to compare two Perl function calls
131 *
132 */
133static int8_t perl_func_def_cmp(void const *one, void const *two)
134{
135 perl_func_def_t const *a = one, *b = two;
136 int ret;
137
138 ret = strcmp(a->name1, b->name1);
139 if (ret != 0) return CMP(ret, 0);
140 if (!a->name2 && !b->name2) return 0;
141 if (!a->name2 || !b->name2) return a->name2 ? 1 : -1;
142 ret = strcmp(a->name2, b->name2);
143 return CMP(ret, 0);
144}
145
146/*
147 * man perlembed
148 */
149EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
150
151static _Thread_local request_t *rlm_perl_request;
152
153# define dl_librefs "DynaLoader::dl_librefs"
154# define dl_modules "DynaLoader::dl_modules"
155static void rlm_perl_clear_handles(pTHX)
156{
157 AV *librefs = get_av(dl_librefs, false);
158 if (librefs) {
159 av_clear(librefs);
160 }
161}
162
163static void **rlm_perl_get_handles(pTHX)
164{
165 I32 i;
166 AV *librefs = get_av(dl_librefs, false);
167 AV *modules = get_av(dl_modules, false);
168 void **handles;
169
170 if (!librefs) return NULL;
171
172 if (!(AvFILL(librefs) >= 0)) {
173 return NULL;
174 }
175
176 MEM(handles = talloc_array(NULL, void *, AvFILL(librefs) + 2));
177 for (i = 0; i <= AvFILL(librefs); i++) {
178 void *handle;
179 SV *handle_sv = *av_fetch(librefs, i, false);
180 if (!handle_sv) {
181 ERROR("Could not fetch $%s[%d]!", dl_librefs, (int)i);
182 continue;
183 }
184 handle = (void *)SvIV(handle_sv);
185
186 if (handle) handles[i] = handle;
187 }
188
189 av_clear(modules);
190 av_clear(librefs);
191
192 handles[i] = (void *)0;
193
194 return handles;
195}
196
197static void rlm_perl_close_handles(void **handles)
198{
199 int i;
200
201 if (!handles) {
202 return;
203 }
204
205 for (i = 0; handles[i]; i++) {
206 DEBUG("Close %p", handles[i]);
207 dlclose(handles[i]);
208 }
209
210 talloc_free(handles);
211}
212
213/*
214 * This is wrapper for fr_log
215 * Now users can call freeradius::log(level,msg) which is the same
216 * as calling fr_log from C code.
217 */
218static XS(XS_freeradius_log)
219{
220 dXSARGS;
221 if (items !=2)
222 croak("Usage: radiusd::log(level, message)");
223 {
224 int level;
225 char *msg;
226
227 level = (int) SvIV(ST(0));
228 msg = (char *) SvPV(ST(1), PL_na);
229
230 /*
231 * Because 'msg' is a 'char *', we don't want '%s', etc.
232 * in it to give us printf-style vulnerabilities.
233 */
234 fr_log(&default_log, level, __FILE__, __LINE__, "rlm_perl: %s", msg);
235 }
236 XSRETURN_NO;
237}
238
239/*
240 * This is a wrapper for xlat_aeval
241 * Now users are able to get data that is accessible only via xlat
242 * e.g. %request.client(...)
243 * Call syntax is freeradius::xlat(string), string will be handled as
244 * a double-quoted string in the configuration files.
245 */
246static XS(XS_freeradius_xlat)
247{
248 dXSARGS;
249 char *in_str;
250 char *expanded;
251 ssize_t slen;
252 request_t *request;
253
254 if (items != 1) croak("Usage: radiusd::xlat(string)");
255
256 request = rlm_perl_request;
257
258 in_str = (char *) SvPV(ST(0), PL_na);
259
260 slen = xlat_aeval(request, &expanded, request, in_str, NULL, NULL);
261 if (slen < 0) {
262 REDEBUG("Error parsing xlat '%s'", in_str);
263 XSRETURN_UNDEF;
264 }
265
266 XST_mPV(0, expanded);
267 talloc_free(expanded);
268 XSRETURN(1);
269}
270
271/** Helper function for turning hash keys into dictionary attributes
272 *
273 */
274static inline fr_dict_attr_t const *perl_attr_lookup(fr_perl_pair_t *pair_data, char const *attr)
275{
276 fr_dict_attr_t const *da = fr_dict_attr_by_name(NULL, pair_data->da, attr);
277
278 /*
279 * Allow fallback to internal attributes if the parent is a group or dictionary root.
280 */
281 if (!da && (fr_type_is_group(pair_data->da->type) || pair_data->da->flags.is_root)) {
283 }
284
285 if (!da) croak("Unknown or invalid attribute name \"%s\"", attr);
286
287 return da;
288}
289
290/** Convenience macro for fetching C data associated with tied hash / array and validating stack size
291 *
292 */
293#define GET_PAIR_MAGIC(count) MAGIC *mg = mg_findext(ST(0), PERL_MAGIC_ext, &rlm_perl_vtbl); \
294 fr_perl_pair_t *pair_data; \
295 if (unlikely(items < count)) { \
296 croak("Expected %d stack entries, got %d", count, items); \
297 XSRETURN_UNDEF; \
298 } \
299 if (!mg) { \
300 croak("Failed to find Perl magic value"); \
301 XSRETURN_UNDEF; \
302 } \
303 pair_data = (fr_perl_pair_t *)mg->mg_ptr;
304
305/** Functions to implement subroutines required for a tied hash
306 *
307 * All structural components of attributes are represented by tied hashes
308 */
309
310/** Called when fetching hash values
311 *
312 * The stack contains
313 * - the tied SV
314 * - the hash key being requested
315 *
316 * When a numeric key is requested, we treat that as in instruction to find
317 * a specific instance of the key of the parent.
318 *
319 * Whilst this is a bit odd, the alternative would be for every attribute to
320 * be returned as an array so you would end up with crazy syntax like
321 * p{'request'}{'Vendor-Specific'}[0]{'Cisco'}[0]{'AVPair}[0]
322 */
323static XS(XS_pairlist_FETCH)
324{
325 dXSARGS;
326 char *attr;
327 fr_dict_attr_t const *da;
328 fr_pair_t *vp = NULL;
329 STRLEN len, i = 0;
330 int idx = 0;
331
333
334 attr = (char *) SvPV(ST(1), len);
335
336 /*
337 * Check if our key is entirely numeric.
338 */
339 while (i < len) {
340 if (!isdigit(attr[i])) break;
341 i++;
342 }
343 if (i == len) {
344 idx = SvIV(ST(1));
345 da = pair_data->da;
346 if (pair_data->parent->vp) vp = fr_pair_find_by_da_idx(&pair_data->parent->vp->vp_group, da, idx);
347 } else {
348 da = perl_attr_lookup(pair_data, attr);
349 if (!da) XSRETURN_UNDEF;
350 if (pair_data->vp) vp = fr_pair_find_by_da(&pair_data->vp->vp_group, NULL, da);
351 }
352
353 switch(da->type) {
354 /*
355 * Leaf attributes are returned as an array with magic
356 */
357 case FR_TYPE_LEAF:
358 {
359 AV *pair_av;
360 SV *pair_tie;
361 HV *frpair_stash;
362 fr_perl_pair_t child_pair_data;
363
364 frpair_stash = gv_stashpv("freeradiuspairs", GV_ADD);
365 pair_av = newAV();
366 pair_tie = newRV_noinc((SV *)newAV());
367 sv_bless(pair_tie, frpair_stash);
368 sv_magic(MUTABLE_SV(pair_av), MUTABLE_SV((GV *)pair_tie), PERL_MAGIC_tied, NULL, 0);
369 SvREFCNT_dec(pair_tie);
370
371 child_pair_data = (fr_perl_pair_t) {
372 .vp = vp,
373 .da = da,
374 .parent = pair_data
375 };
376 sv_magicext((SV *)pair_tie, 0, PERL_MAGIC_ext, &rlm_perl_vtbl, (char *)&child_pair_data, sizeof(child_pair_data));
377 ST(0) = sv_2mortal(newRV((SV *)pair_av));
378 }
379 break;
380
381 /*
382 * Structural attributes are returned as a hash with magic
383 */
385 {
386 HV *struct_hv;
387 SV *struct_tie;
388 HV *frpair_stash;
389 fr_perl_pair_t child_pair_data;
390
391 frpair_stash = gv_stashpv("freeradiuspairlist", GV_ADD);
392 struct_hv = newHV();
393 struct_tie = newRV_noinc((SV *)newHV());
394 sv_bless(struct_tie, frpair_stash);
395 hv_magic(struct_hv, (GV *)struct_tie, PERL_MAGIC_tied);
396 SvREFCNT_dec(struct_tie);
397
398 child_pair_data = (fr_perl_pair_t) {
399 .vp = vp,
400 .da = da,
401 .parent = pair_data,
402 .idx = idx
403 };
404 sv_magicext((SV *)struct_tie, 0, PERL_MAGIC_ext, &rlm_perl_vtbl, (char *)&child_pair_data, sizeof(child_pair_data));
405 ST(0) = sv_2mortal(newRV((SV *)struct_hv));
406 }
407 break;
408
409 default:
410 fr_assert(0);
411 }
412
413 XSRETURN(1);
414}
415
416/** Called when a hash value is set / updated
417 *
418 * This is not allowed - only leaf node arrays can have values set
419 */
420static XS(XS_pairlist_STORE)
421{
422 dXSARGS;
423 char *attr;
424 fr_dict_attr_t const *da;
425
427
428 attr = (char *) SvPV(ST(1), PL_na);
429 da = perl_attr_lookup(pair_data, attr);
430 if (!da) XSRETURN(0);
431
432 if (fr_type_is_leaf(da->type)) {
433 croak("Cannot set value of array of \"%s\" values. Use array index to set a specific instance.", da->name);
434 } else {
435 croak("Cannot set values of structural object %s", da->name);
436 }
437 XSRETURN(0);
438}
439
440/** Called to test the existence of a key in a tied hash
441 *
442 * The stack contains
443 * - the tied SV
444 * - the key to check for
445 */
446static XS(XS_pairlist_EXISTS)
447{
448 dXSARGS;
449 char *attr;
450 fr_dict_attr_t const *da;
451 STRLEN len, i = 0;
452
454
455 attr = (char *) SvPV(ST(1), len);
456 while (i < len) {
457 if (!isdigit(attr[i])) break;
458 i++;
459 }
460
461 /*
462 * Numeric key - check for an instance of the attribute
463 */
464 if (i == len) {
465 unsigned int idx = SvIV(ST(1));
466 if (pair_data->parent->vp) {
467 if (fr_pair_find_by_da_idx(&pair_data->parent->vp->vp_group, pair_data->da, idx)) XSRETURN_YES;
468 }
469 XSRETURN_NO;
470 }
471
472 if (!pair_data->vp) XSRETURN_NO;
473
474 da = perl_attr_lookup(pair_data, attr);
475 if (!da) XSRETURN_NO;
476
477 if(fr_pair_find_by_da(&pair_data->vp->vp_group, NULL, da)) XSRETURN_YES;
478
479 XSRETURN_NO;
480}
481
482/** Called when functions like keys() want the first key in a tied hash
483 *
484 * The stack contains just the tied SV
485 */
486static XS(XS_pairlist_FIRSTKEY)
487{
488 dXSARGS;
489 fr_pair_t *vp;
490
492 if (!pair_data->vp) XSRETURN_EMPTY;
493
494 vp = fr_pair_dcursor_init(&pair_data->cursor, &pair_data->vp->vp_group);
495 if (!vp) XSRETURN_EMPTY;
496 ST(0) = sv_2mortal(newSVpv(vp->da->name, vp->da->name_len));
497 XSRETURN(1);
498}
499
500/** Called by functions like keys() which iterate over the keys in a tied hash
501 *
502 * The stack contains
503 * - the tied SV
504 * - the previous key
505 */
506static XS(XS_pairlist_NEXTKEY)
507{
508 dXSARGS;
509 fr_pair_t *vp;
510
512 if (!pair_data->vp) XSRETURN_EMPTY;
513
514 vp = fr_dcursor_next(&pair_data->cursor);
515 if (!vp) XSRETURN_EMPTY;
516
517 ST(0) = sv_2mortal(newSVpv(vp->da->name, vp->da->name_len));
518 XSRETURN(1);
519}
520
521/** Called to delete a key from a tied hash
522 *
523 * The stack contains
524 * - the tied SV
525 * - the key being deleted
526 */
527static XS(XS_pairlist_DELETE)
528{
529 dXSARGS;
530 char *attr;
531 fr_dict_attr_t const *da;
532 fr_pair_t *vp;
533
535 attr = SvPV(ST(1), PL_na);
536
537 da = perl_attr_lookup(pair_data, attr);
538 if (!da) XSRETURN(0);
539 if (!pair_data->vp) XSRETURN(0);
540
541 vp = fr_pair_find_by_da(&pair_data->vp->vp_group, NULL, da);
542
543 if (vp) fr_pair_delete(&pair_data->vp->vp_group, vp);
544
545 XSRETURN(0);
546}
547
548/** Functions to implement subroutines required for a tied array
549 *
550 * Leaf attributes are represented by tied arrays to allow multiple instances.
551 */
552
554{
555 switch(vp->vp_type) {
556 case FR_TYPE_STRING:
557 *value = sv_2mortal(newSVpvn(vp->vp_strvalue, vp->vp_length));
558 break;
559
560 case FR_TYPE_OCTETS:
561 *value = sv_2mortal(newSVpvn((char const *)vp->vp_octets, vp->vp_length));
562 break;
563
564#define PERLUINT(_size) case FR_TYPE_UINT ## _size: \
565 *value = sv_2mortal(newSVuv(vp->vp_uint ## _size)); \
566 break;
567 PERLUINT(8)
568 PERLUINT(16)
569 PERLUINT(32)
570 PERLUINT(64)
571
572#define PERLINT(_size) case FR_TYPE_INT ## _size: \
573 *value = sv_2mortal(newSViv(vp->vp_int ## _size)); \
574 break;
575 PERLINT(8)
576 PERLINT(16)
577 PERLINT(32)
578 PERLINT(64)
579
580
581 case FR_TYPE_SIZE:
582 *value = sv_2mortal(newSVuv(vp->vp_size));
583 break;
584
585 case FR_TYPE_BOOL:
586 *value = sv_2mortal(newSVuv(vp->vp_bool));
587 break;
588
589 case FR_TYPE_FLOAT32:
590 *value = sv_2mortal(newSVnv(vp->vp_float32));
591 break;
592
593 case FR_TYPE_FLOAT64:
594 *value = sv_2mortal(newSVnv(vp->vp_float64));
595 break;
596
597 case FR_TYPE_ETHERNET:
604 case FR_TYPE_IFID:
605 case FR_TYPE_DATE:
607 case FR_TYPE_ATTR:
608 {
609 char buff[128];
610 ssize_t slen;
611
613 if (slen < 0) {
614 croak("Cannot convert %s to Perl type, insufficient buffer space",
615 fr_type_to_str(vp->vp_type));
616 return -1;
617 }
618
619 *value = sv_2mortal(newSVpv(buff, slen));
620 }
621 break;
622
623 /* Only leaf nodes should be able to call this */
624 case FR_TYPE_NON_LEAF:
625 croak("Cannot convert %s to Perl type", fr_type_to_str(vp->vp_type));
626 return -1;
627 }
628
629 return 0;
630}
631
632/** Called to retrieve the value of an array entry
633 *
634 * In our case, retrieve the value of a specific instance of a leaf attribute
635 *
636 * The stack contains
637 * - the tied SV
638 * - the index to retrieve
639 *
640 * The magic data will hold the DA of the attribute.
641 */
642static XS(XS_pairs_FETCH)
643{
644 dXSARGS;
645 unsigned int idx = SvUV(ST(1));
646 fr_pair_t *vp = NULL;
648
650
651 parent = pair_data->parent;
652 if (!parent->vp) XSRETURN_UNDEF;
653
654 if (idx == 0) vp = pair_data->vp;
655 if (!vp) vp = fr_pair_find_by_da_idx(&parent->vp->vp_group, pair_data->da, idx);
656 if (!vp) XSRETURN_UNDEF;
657
658 if (perl_value_marshal(vp, &ST(0)) < 0) XSRETURN(0);
659 XSRETURN(1);
660}
661
662/** Build parent structural pairs needed when a leaf node is set
663 *
664 */
666{
667 fr_perl_pair_t *parent = pair_data->parent;
668 if (!parent->vp) {
669 /*
670 * When building parent with idx > 0, it's "parent" is the
671 * first instance of the attribute - so if that's not there
672 * we don't have any.
673 */
674 if (pair_data->idx > 0) {
675 none_exist:
676 croak("Attempt to set instance %d when none exist", pair_data->idx);
677 return -1;
678 }
679 if (fr_perl_pair_parent_build(parent) < 0) return -1;
680 }
681
682 if (pair_data->idx > 0) {
683 unsigned int count;
684
685 if (!parent->parent->vp) goto none_exist;
686 count = fr_pair_count_by_da(&parent->parent->vp->vp_group, pair_data->da);
687 if (count < pair_data->idx) {
688 croak("Attempt to set instance %d when only %d exist", pair_data->idx, count);
689 return -1;
690 }
691 parent = parent->parent;
692 }
693
694 if (fr_pair_append_by_da(parent->vp, &pair_data->vp, &parent->vp->vp_group, pair_data->da) < 0) return -1;
695 return 0;
696}
697
698/** Convert a Perl SV to a pair value.
699 *
700 */
702{
704
705 switch (SvTYPE(value)) {
706 case SVt_IV:
707 fr_value_box_init(&vb, FR_TYPE_INT64, NULL, true);
708 vb.vb_int64 = SvIV(value);
709 break;
710
711 case SVt_NV:
712 fr_value_box_init(&vb, FR_TYPE_FLOAT64, NULL, true);
713 vb.vb_float64 = SvNV(value);
714 break;
715
716 case SVt_PV:
717 case SVt_PVLV:
718 {
719 char *val;
720 STRLEN len;
721 fr_value_box_init(&vb, FR_TYPE_STRING, NULL, true);
722 val = SvPV(value, len);
723 fr_value_box_bstrndup_shallow(&vb, NULL, val, len, true);
724 }
725 break;
726
727 default:
728 croak("Unsupported Perl data type");
729 return -1;
730 }
731
733 if (fr_value_box_cast(vp, &vp->data, vp->vp_type, vp->da, &vb) < 0) {
734 croak("Failed casting Perl value to %s", fr_type_to_str(vp->vp_type));
735 return -1;
736 }
737
738 return 0;
739}
740
741/** Called when an array value is set / updated
742 *
743 * The stack contains
744 * - the tied SV
745 * - the index being updated
746 * - the value being assigned
747 */
748static XS(XS_pairs_STORE)
749{
750 dXSARGS;
751 unsigned int idx = SvUV(ST(1));
752 fr_pair_t *vp;
754
756
757 fr_assert(fr_type_is_leaf(pair_data->da->type));
758
759 parent = pair_data->parent;
760
761 if (!parent->vp) {
762 /*
763 * Trying to set something other than the first instance when
764 * the parent doesn't exist is invalid.
765 */
766 if (idx > 0) {
767 croak("Attempting to set instance %d when none exist", idx);
768 XSRETURN(0);
769 }
770
771 if(fr_perl_pair_parent_build(parent) < 0) XSRETURN(0);
772 }
773
774 vp = fr_pair_find_by_da_idx(&parent->vp->vp_group, pair_data->da, idx);
775 if (!vp) {
776 if (idx > 0) {
777 unsigned int count = fr_pair_count_by_da(&pair_data->parent->vp->vp_group, pair_data->da);
778 if (count < idx) {
779 croak("Attempt to set instance %d when only %d exist", idx, count);
780 XSRETURN(0);
781 }
782 }
783 fr_pair_append_by_da(parent->vp, &vp, &parent->vp->vp_group, pair_data->da);
784 }
785
786 perl_value_unmarshal(vp, ST(2));
787
788 XSRETURN(0);
789}
790
791/** Called when an array entry's existence is tested
792 *
793 */
794static XS(XS_pairs_EXISTS)
795{
796 dXSARGS;
797 unsigned int idx = SvUV(ST(1));
798 fr_pair_t *vp;
800
802
803 parent = pair_data->parent;
804 if (!parent->vp) XSRETURN_NO;
805
806 vp = fr_pair_find_by_da_idx(&parent->vp->vp_group, pair_data->da, idx);
807 if (vp) XSRETURN_YES;
808 XSRETURN_NO;
809}
810
811/** Called when an array entry is deleted
812 *
813 */
814static XS(XS_pairs_DELETE)
815{
816 dXSARGS;
817 unsigned int idx = SvUV(ST(1));
818 fr_pair_t *vp;
820
822
823 parent = pair_data->parent;
824 if (!parent->vp) XSRETURN(0);
825
826 vp = fr_pair_find_by_da_idx(&parent->vp->vp_group, pair_data->da, idx);
827 if (vp) fr_pair_delete(&parent->vp->vp_group, vp);
828 XSRETURN(0);
829}
830
831/** Called when Perl wants the size of a tied array
832 *
833 * The stack contains just the tied SV
834 */
835static XS(XS_pairs_FETCHSIZE)
836{
837 dXSARGS;
839
840 if (!pair_data->parent->vp) XSRETURN_UV(0);
841 XSRETURN_UV(fr_pair_count_by_da(&pair_data->parent->vp->vp_group, pair_data->da));
842}
843
844/** Called when attempting to set the size of an array
845 *
846 * We don't allow expanding the array this way, but will allow deleting pairs
847 *
848 * The stack contains
849 * - the tied SV
850 * - the requested size of the array
851 */
852static XS(XS_pairs_STORESIZE)
853{
854 dXSARGS;
855 unsigned int count, req_size = SvUV(ST(1));
856 fr_pair_t *vp, *prev;
859
860 parent = pair_data->parent;
861 if (!parent->vp) {
862 if (req_size > 0) {
863 croak("Unable to set attribute instance count");
864 }
865 XSRETURN(0);
866 }
867
868 count = fr_pair_count_by_da(&parent->vp->vp_group, pair_data->da);
869 if (req_size > count) {
870 croak("Increasing attribute instance count not supported");
871 XSRETURN(0);
872 }
873
874 /*
875 * As req_size is 1 based and the attribute instance count is
876 * 0 based, searching for instance `req_size` will give the first
877 * pair to delete.
878 */
879 vp = fr_pair_find_by_da_idx(&parent->vp->vp_group, pair_data->da, req_size);
880 while (vp) {
881 prev = fr_pair_list_prev(&parent->vp->vp_group, vp);
882 fr_pair_delete(&parent->vp->vp_group, vp);
883 vp = fr_pair_find_by_da(&parent->vp->vp_group, prev, pair_data->da);
884 }
885 XSRETURN(0);
886}
887
888/** Called when values are pushed on a tied array
889 *
890 * The stack contains
891 * - the tied SV
892 * - one or more values being pushed onto the array
893 */
894static XS(XS_pairs_PUSH)
895{
896 dXSARGS;
897 int i = 1;
898 fr_pair_t *vp;
900
902
903 fr_assert(fr_type_is_leaf(pair_data->da->type));
904
905 parent = pair_data->parent;
906 if (!parent->vp) {
907 if (fr_perl_pair_parent_build(parent) < 0) XSRETURN(0);
908 }
909
910 while (i < items) {
911 fr_pair_append_by_da(parent->vp, &vp, &parent->vp->vp_group, pair_data->da);
912 if (perl_value_unmarshal(vp, ST(i++)) < 0) break;
913 }
914
915 XSRETURN(0);
916}
917
918/** Called when values are popped off a tied array
919 *
920 * The stack contains just the tied SV
921 */
922static XS(XS_pairs_POP)
923{
924 dXSARGS;
925 fr_pair_t *vp;
927
929
930 fr_assert(fr_type_is_leaf(pair_data->da->type));
931
932 parent = pair_data->parent;
933 if (!parent->vp) XSRETURN(0);
934
935 vp = fr_pair_find_last_by_da(&parent->vp->vp_group, NULL, pair_data->da);
936 if (!vp) XSRETURN(0);
937
938 if (perl_value_marshal(vp, &ST(0)) < 0) XSRETURN(0);
939
940 fr_pair_remove(&parent->vp->vp_group, vp);
941 XSRETURN(1);
942}
943
944/** Called when values are "shifted" off a tied array
945 *
946 * The stack contains just the tied SV
947 */
948static XS(XS_pairs_SHIFT)
949{
950 dXSARGS;
951 fr_pair_t *vp;
953
955
956 fr_assert(fr_type_is_leaf(pair_data->da->type));
957
958 parent = pair_data->parent;
959 if (!parent->vp) XSRETURN(0);
960
961 vp = fr_pair_find_by_da(&parent->vp->vp_group, NULL, pair_data->da);
962 if (!vp) XSRETURN(0);
963
964 if (perl_value_marshal(vp, &ST(0)) < 0) XSRETURN(0);
965
966 fr_pair_remove(&parent->vp->vp_group, vp);
967 XSRETURN(1);
968}
969
970/** Called when values are "unshifted" onto a tied array
971 *
972 * The stack contains
973 * - the tied SV
974 * - one or more values being shifted onto the array
975 */
976static XS(XS_pairs_UNSHIFT)
977{
978 dXSARGS;
979 int i = 1;
980 fr_pair_t *vp;
982
984
985 fr_assert(fr_type_is_leaf(pair_data->da->type));
986
987 parent = pair_data->parent;
988 if (!parent->vp) {
989 if (fr_perl_pair_parent_build(parent) < 0) XSRETURN(0);
990 }
991
992 while (i < items) {
993 if (unlikely(fr_pair_prepend_by_da(parent->vp, &vp, &parent->vp->vp_group, pair_data->da) < 0)) {
994 croak("Failed adding attribute %s", pair_data->da->name);
995 break;
996 }
997 if (perl_value_unmarshal(vp, ST(i++)) < 0) break;
998 }
999
1000 XSRETURN(0);
1001}
1002
1003static void xs_init(pTHX)
1004{
1005 char const *file = __FILE__;
1006
1007 /* DynaLoader is a special case */
1008 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1009
1010 newXS("freeradius::log",XS_freeradius_log, "rlm_perl");
1011 newXS("freeradius::xlat",XS_freeradius_xlat, "rlm_perl");
1012
1013 /*
1014 * The freeradiuspairlist package implements functions required
1015 * for a tied hash handling structural attributes.
1016 */
1017 newXS("freeradiuspairlist::FETCH", XS_pairlist_FETCH, "rlm_perl");
1018 newXS("freeradiuspairlist::STORE", XS_pairlist_STORE, "rlm_perl");
1019 newXS("freeradiuspairlist::EXISTS", XS_pairlist_EXISTS, "rlm_perl");
1020 newXS("freeradiuspairlist::FIRSTKEY", XS_pairlist_FIRSTKEY, "rlm_perl");
1021 newXS("freeradiuspairlist::NEXTKEY", XS_pairlist_NEXTKEY, "rlm_perl");
1022 newXS("freeradiuspairlist::DELETE", XS_pairlist_DELETE, "rlm_perl");
1023
1024 /*
1025 * The freeradiuspairs package implements functions required
1026 * for a tied array handling leaf attributes.
1027 */
1028 newXS("freeradiuspairs::FETCH", XS_pairs_FETCH, "rlm_perl");
1029 newXS("freeradiuspairs::STORE", XS_pairs_STORE, "rlm_perl");
1030 newXS("freeradiuspairs::EXISTS", XS_pairs_EXISTS, "rlm_perl");
1031 newXS("freeradiuspairs::DELETE", XS_pairs_DELETE, "rlm_perl");
1032 newXS("freeradiuspairs::FETCHSIZE", XS_pairs_FETCHSIZE, "rlm_perl");
1033 newXS("freeradiuspairs::STORESIZE", XS_pairs_STORESIZE, "rlm_perl");
1034 newXS("freeradiuspairs::PUSH", XS_pairs_PUSH, "rlm_perl");
1035 newXS("freeradiuspairs::POP", XS_pairs_POP, "rlm_perl");
1036 newXS("freeradiuspairs::SHIFT", XS_pairs_SHIFT, "rlm_perl");
1037 newXS("freeradiuspairs::UNSHIFT", XS_pairs_UNSHIFT, "rlm_perl");
1038}
1039
1040/** Convert a list of value boxes to a Perl array for passing to subroutines
1041 *
1042 * The Perl array object should be created before calling this
1043 * to populate it.
1044 *
1045 * @param[in,out] av Perl array object to append values to.
1046 * @param[in] head of VB list.
1047 * @return
1048 * - 0 on success
1049 * - -1 on failure
1050 */
1051static int perl_vblist_to_av(AV *av, fr_value_box_list_t *head) {
1052 fr_value_box_t *vb = NULL;
1053 SV *sv;
1054
1055 while ((vb = fr_value_box_list_next(head, vb))) {
1056 switch (vb->type) {
1057 case FR_TYPE_STRING:
1058 sv = newSVpvn(vb->vb_strvalue, vb->vb_length);
1059 break;
1060
1061 case FR_TYPE_OCTETS:
1062 sv = newSVpvn((char const *)vb->vb_octets, vb->vb_length);
1063 break;
1064
1065 case FR_TYPE_GROUP:
1066 {
1067 AV *sub_av;
1068 sub_av = newAV();
1069 perl_vblist_to_av(sub_av, &vb->vb_group);
1070 sv = newRV_inc((SV *)sub_av);
1071 }
1072 break;
1073 default:
1074 {
1075 char buffer[1024];
1076 ssize_t slen;
1077
1078 slen = fr_value_box_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), vb, NULL);
1079 if (slen < 0) return -1;
1080 sv = newSVpvn(buffer, (size_t)slen);
1081 }
1082 break;
1083 }
1084 if (!sv) return -1;
1085 if (vb->tainted) SvTAINT(sv);
1086 av_push(av, sv);
1087 }
1088 return 0;
1089}
1090
1091/** Parse a Perl SV and create value boxes, appending to a list
1092 *
1093 * For parsing values passed back from a Perl subroutine
1094 *
1095 * When hashes are returned, first the key is added as a value box then the value
1096 *
1097 * @param[in] ctx to allocate boxes in.
1098 * @param[out] list to append value boxes to.
1099 * @param[in] request being handled - only used for debug messages
1100 * @param[in] sv to parse
1101 * @return
1102 * - 0 on success
1103 * - -1 on failure
1104 */
1105static int perl_sv_to_vblist(TALLOC_CTX *ctx, fr_value_box_list_t *list, request_t *request, SV *sv) {
1106 fr_value_box_t *vb = NULL;
1107 char *tmp;
1108 STRLEN len;
1109 AV *av;
1110 HV *hv;
1111 I32 sv_len, i;
1112 int type;
1113
1114 type = SvTYPE(sv);
1115
1116 switch (type) {
1117 case SVt_IV:
1118 /* Integer or Reference */
1119 if (SvROK(sv)) {
1120 RDEBUG3("Reference returned");
1121 if (perl_sv_to_vblist(ctx, list, request, SvRV(sv)) < 0) return -1;
1122 break;
1123 }
1124 RDEBUG3("Integer returned");
1125 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_INT32, NULL));
1126 vb->vb_int32 = SvIV(sv);
1127 break;
1128
1129 case SVt_NV:
1130 /* Float */
1131 RDEBUG3("Float returned");
1132 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_FLOAT64, NULL));
1133 vb->vb_float64 = SvNV(sv);
1134 break;
1135
1136 case SVt_PV:
1137 /* String */
1138 RDEBUG3("String returned");
1139 tmp = SvPVutf8(sv, len);
1140 MEM(vb = fr_value_box_alloc_null(ctx));
1141 if (fr_value_box_bstrndup(vb, vb, NULL, tmp, len, SvTAINTED(sv)) < 0) {
1142 talloc_free(vb);
1143 RPEDEBUG("Failed to allocate %ld for output", len);
1144 return -1;
1145 }
1146 break;
1147
1148 case SVt_PVAV:
1149 /* Array */
1150 {
1151 SV **av_sv;
1152 RDEBUG3("Array returned");
1153 av = (AV*)sv;
1154 sv_len = av_len(av);
1155 for (i = 0; i <= sv_len; i++) {
1156 av_sv = av_fetch(av, i, 0);
1157 if (SvOK(*av_sv)) {
1158 if (perl_sv_to_vblist(ctx, list, request, *av_sv) < 0) return -1;
1159 }
1160 }
1161 }
1162 break;
1163
1164 case SVt_PVHV:
1165 /* Hash */
1166 {
1167 SV *hv_sv;
1168 RDEBUG3("Hash returned");
1169 hv = (HV*)sv;
1170 for (i = hv_iterinit(hv); i > 0; i--) {
1171 hv_sv = hv_iternextsv(hv, &tmp, &sv_len);
1172 /*
1173 * Add key first
1174 */
1175 MEM(vb = fr_value_box_alloc_null(ctx));
1176 if (fr_value_box_bstrndup(vb, vb, NULL, tmp, sv_len, SvTAINTED(hv_sv)) < 0) {
1177 talloc_free(vb);
1178 RPEDEBUG("Failed to allocate %d for output", sv_len);
1179 return -1;
1180 }
1181 fr_value_box_list_insert_tail(list, vb);
1182
1183 /*
1184 * Now process value
1185 */
1186 if (perl_sv_to_vblist(ctx, list, request, hv_sv) < 0) return -1;
1187
1188 }
1189 /*
1190 * Box has already been added to list - return
1191 */
1192 return 0;
1193 }
1194
1195 case SVt_NULL:
1196 break;
1197
1198 default:
1199 RPEDEBUG("Perl returned unsupported data type %d", type);
1200 return -1;
1201
1202 }
1203
1204 if (vb) {
1205 vb->tainted = SvTAINTED(sv);
1206 fr_value_box_list_insert_tail(list, vb);
1207 }
1208
1209 return 0;
1210}
1211
1213 { .required = true, .single = true, .type = FR_TYPE_STRING },
1214 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .type = FR_TYPE_VOID },
1216};
1217
1218/** Call perl code using an xlat
1219 *
1220 * @ingroup xlat_functions
1221 */
1222static xlat_action_t perl_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
1223 xlat_ctx_t const *xctx,
1224 request_t *request, fr_value_box_list_t *in)
1225{
1227 int count, i;
1229 STRLEN n_a;
1230 fr_value_box_t *func = fr_value_box_list_pop_head(in);
1231 fr_value_box_t *child;
1232 SV *sv;
1233 AV *av;
1234 fr_value_box_list_t list, sub_list;
1235 fr_value_box_t *vb = NULL;
1236
1237 fr_assert(func);
1238
1239 fr_value_box_list_init(&list);
1240 fr_value_box_list_init(&sub_list);
1241
1242 {
1243 dTHXa(t->perl);
1244 PERL_SET_CONTEXT(t->perl);
1245 }
1246
1247 {
1248 ssize_t slen;
1249 fr_sbuff_t *sbuff;
1250
1251 dSP;
1252 ENTER;SAVETMPS;
1253
1254 PUSHMARK(SP);
1255
1256 FR_SBUFF_TALLOC_THREAD_LOCAL(&sbuff, 256, 16384);
1257
1259
1260 fr_assert(arg->type == FR_TYPE_GROUP);
1261 if (fr_value_box_list_empty(&arg->vb_group)) continue;
1262
1263 if (fr_value_box_list_num_elements(&arg->vb_group) == 1) {
1264 child = fr_value_box_list_head(&arg->vb_group);
1265
1266 switch (child->type) {
1267 case FR_TYPE_STRING:
1268 if (child->vb_length == 0) continue;
1269
1270 RDEBUG3("Passing single value %pV", child);
1271 sv = newSVpvn(child->vb_strvalue, child->vb_length);
1272 break;
1273
1274 case FR_TYPE_GROUP:
1275 RDEBUG3("Ignoring nested group");
1276 continue;
1277
1278 default:
1279 /*
1280 * @todo - turn over integers as strings.
1281 */
1282 slen = fr_value_box_print(sbuff, child, NULL);
1283 if (slen <= 0) {
1284 RPEDEBUG("Failed printing sbuff");
1285 continue;
1286 }
1287
1288 RDEBUG3("Passing single value %pV", child);
1289 sv = newSVpvn(fr_sbuff_start(sbuff), fr_sbuff_used(sbuff));
1290 fr_sbuff_set_to_start(sbuff);
1291 break;
1292 }
1293
1294 if (child->tainted) SvTAINT(sv);
1295 XPUSHs(sv_2mortal(sv));
1296 continue;
1297 }
1298
1299 /*
1300 * Multiple child values - create array and pass reference
1301 */
1302 av = newAV();
1303 perl_vblist_to_av(av, &arg->vb_group);
1304 RDEBUG3("Passing list as array %pM", &arg->vb_group);
1305 sv = newRV_inc((SV *)av);
1306 XPUSHs(sv_2mortal(sv));
1307 }
1308
1309 PUTBACK;
1310
1311 count = call_pv(func->vb_strvalue, G_ARRAY | G_EVAL);
1312
1313 SPAGAIN;
1314 if (SvTRUE(ERRSV)) {
1315 REDEBUG("Exit %s", SvPV(ERRSV,n_a));
1316 (void)POPs;
1317 goto cleanup;
1318 }
1319
1320 /*
1321 * As results are popped from a stack, they are in reverse
1322 * sequence. Add to a temporary list and then prepend to
1323 * main list.
1324 */
1325 for (i = 0; i < count; i++) {
1326 sv = POPs;
1327 if (perl_sv_to_vblist(ctx, &sub_list, request, sv) < 0) goto cleanup;
1328 fr_value_box_list_move_head(&list, &sub_list);
1329 }
1330 ret = XLAT_ACTION_DONE;
1331
1332 /*
1333 * Move the assembled list of boxes to the output
1334 */
1335 while ((vb = fr_value_box_list_pop_head(&list))) fr_dcursor_append(out, vb);
1336
1337 cleanup:
1338 PUTBACK;
1339 FREETMPS;
1340 LEAVE;
1341
1342 }
1343
1344 return ret;
1345}
1346
1347/*
1348 * Parse a configuration section, and populate a HV.
1349 * This function is recursively called (allows to have nested hashes.)
1350 */
1351static void perl_parse_config(CONF_SECTION *cs, int lvl, HV *rad_hv)
1352{
1353 int indent_section = (lvl + 1) * 4;
1354 int indent_item = (lvl + 2) * 4;
1355
1356 if (!cs || !rad_hv) return;
1357
1358 DEBUG("%*s%s {", indent_section, " ", cf_section_name1(cs));
1359
1360 for (CONF_ITEM *ci = NULL; (ci = cf_item_next(cs, ci)); ) {
1361 /*
1362 * This is a section.
1363 * Create a new HV, store it as a reference in current HV,
1364 * Then recursively call perl_parse_config with this section and the new HV.
1365 */
1366 if (cf_item_is_section(ci)) {
1367 CONF_SECTION *sub_cs = cf_item_to_section(ci);
1368 char const *key = cf_section_name1(sub_cs); /* hash key */
1369 HV *sub_hv;
1370 SV *ref;
1371
1372 if (!key) continue;
1373
1374 if (hv_exists(rad_hv, key, strlen(key))) {
1375 WARN("Ignoring duplicate config section '%s'", key);
1376 continue;
1377 }
1378
1379 sub_hv = newHV();
1380 ref = newRV_inc((SV*) sub_hv);
1381
1382 (void)hv_store(rad_hv, key, strlen(key), ref, 0);
1383
1384 perl_parse_config(sub_cs, lvl + 1, sub_hv);
1385 } else if (cf_item_is_pair(ci)){
1386 CONF_PAIR *cp = cf_item_to_pair(ci);
1387 char const *key = cf_pair_attr(cp); /* hash key */
1388 char const *value = cf_pair_value(cp); /* hash value */
1389
1390 if (!key || !value) continue;
1391
1392 /*
1393 * This is an item.
1394 * Store item attr / value in current HV.
1395 */
1396 if (hv_exists(rad_hv, key, strlen(key))) {
1397 WARN("Ignoring duplicate config item '%s'", key);
1398 continue;
1399 }
1400
1401 (void)hv_store(rad_hv, key, strlen(key), newSVpvn(value, strlen(value)), 0);
1402
1403 DEBUG("%*s%s = %s", indent_item, " ", key, value);
1404 }
1405 }
1406
1407 DEBUG("%*s}", indent_section, " ");
1408}
1409
1410/** Create a Perl tied hash representing a pair list
1411 *
1412 */
1413static void perl_pair_list_tie(HV *parent, HV *frpair_stash, char const *name, fr_pair_t *vp, fr_dict_attr_t const *da)
1414{
1415 HV *list_hv;
1416 SV *list_tie;
1417 fr_perl_pair_t pair_data;
1418
1419 list_hv = newHV();
1420 list_tie = newRV_noinc((SV *)newHV());
1421 sv_bless(list_tie, frpair_stash);
1422 hv_magic(list_hv, (GV *)list_tie, PERL_MAGIC_tied);
1423 SvREFCNT_dec(list_tie);
1424
1425 pair_data = (fr_perl_pair_t) {
1426 .vp = vp,
1427 .da = da
1428 };
1429
1430 sv_magicext((SV *)list_tie, 0, PERL_MAGIC_ext, &rlm_perl_vtbl, (char *)&pair_data, sizeof(pair_data));
1431
1432 (void)hv_store(parent, name, strlen(name), newRV_inc((SV *)list_hv), 0);
1433}
1434
1435/*
1436 * Call the function_name inside the module
1437 * Store all vps in hashes %RAD_CONFIG %RAD_REPLY %RAD_REQUEST
1438 *
1439 */
1440static unlang_action_t CC_HINT(nonnull) mod_perl(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
1441{
1442 rlm_perl_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_perl_t);
1443 perl_call_env_t *func = talloc_get_type_abort(mctx->env_data, perl_call_env_t);
1444 PerlInterpreter *interp = ((rlm_perl_thread_t *)talloc_get_type_abort(mctx->thread, rlm_perl_thread_t))->perl;
1445 int ret=0, count;
1446 STRLEN n_a;
1447
1448 HV *frpair_stash;
1449 HV *fr_packet;
1450
1451 /*
1452 * call_env parsing will have established the function name to call.
1453 */
1455
1456 {
1457 dTHXa(interp);
1458 PERL_SET_CONTEXT(interp);
1459 }
1460
1461 {
1462 dSP;
1463
1464 ENTER;
1465 SAVETMPS;
1466
1467 /* Get the stash for the freeradiuspairlist package */
1468 frpair_stash = gv_stashpv("freeradiuspairlist", GV_ADD);
1469
1470 /* New hash to hold the pair list roots and pass to the Perl subroutine */
1471 fr_packet = newHV();
1472
1473 perl_pair_list_tie(fr_packet, frpair_stash, "request",
1474 fr_pair_list_parent(&request->request_pairs), fr_dict_root(request->proto_dict));
1475 perl_pair_list_tie(fr_packet, frpair_stash, "reply",
1476 fr_pair_list_parent(&request->reply_pairs), fr_dict_root(request->proto_dict));
1477 perl_pair_list_tie(fr_packet, frpair_stash, "control",
1478 fr_pair_list_parent(&request->control_pairs), fr_dict_root(request->proto_dict));
1479 perl_pair_list_tie(fr_packet, frpair_stash, "session-state",
1480 fr_pair_list_parent(&request->session_state_pairs), fr_dict_root(request->proto_dict));
1481
1482 /*
1483 * Store pointer to request structure globally so radiusd::xlat works
1484 */
1485 rlm_perl_request = request;
1486
1487 PUSHMARK(SP);
1488 XPUSHs( sv_2mortal(newRV((SV *)fr_packet)) );
1489 PUTBACK;
1490
1491 count = call_pv(func->func->function_name, G_SCALAR | G_EVAL );
1492
1493 rlm_perl_request = NULL;
1494
1495 SPAGAIN;
1496
1497 if (SvTRUE(ERRSV)) {
1498 REDEBUG("perl_embed:: module = %s , func = %s exit status= %s\n",
1499 inst->module, func->func->function_name, SvPV(ERRSV,n_a));
1500 (void)POPs;
1501 ret = RLM_MODULE_FAIL;
1502 } else if (count == 1) {
1503 ret = POPi;
1504 if (ret >= 100 || ret < 0) {
1505 ret = RLM_MODULE_FAIL;
1506 }
1507 }
1508
1509 PUTBACK;
1510 FREETMPS;
1511 LEAVE;
1512 }
1513
1515}
1516
1518DIAG_OFF(shadow)
1519static void rlm_perl_interp_free(PerlInterpreter *perl)
1520{
1521 void **handles;
1522
1523 {
1524 dTHXa(perl);
1525 PERL_SET_CONTEXT(perl);
1526 }
1527
1528 handles = rlm_perl_get_handles(aTHX);
1529 if (handles) rlm_perl_close_handles(handles);
1530
1531 PL_perl_destruct_level = 2;
1532
1533 PL_origenviron = environ;
1534
1535 /*
1536 * FIXME: This shouldn't happen
1537 *
1538 */
1539 while (PL_scopestack_ix > 1) LEAVE;
1540
1541 perl_destruct(perl);
1542 perl_free(perl);
1543}
1544DIAG_ON(shadow)
1546
1548{
1549 rlm_perl_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_perl_t);
1550 rlm_perl_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_perl_thread_t);
1551 PerlInterpreter *interp;
1552 UV clone_flags = 0;
1553
1554 PERL_SET_CONTEXT(inst->perl);
1555
1556 /*
1557 * Ensure only one thread is cloning an interpreter at a time
1558 * Whilst the documentation of perl_clone() does not say anything
1559 * about this, seg faults have been seen if multiple threads clone
1560 * the same inst->perl at the same time.
1561 */
1562 pthread_mutex_lock(&inst->mutable->mutex);
1563 interp = perl_clone(inst->perl, clone_flags);
1564 pthread_mutex_unlock(&inst->mutable->mutex);
1565 {
1566 dTHXa(interp); /* Sets the current thread's interpreter */
1567 }
1568# if PERL_REVISION >= 5 && PERL_VERSION <8
1569 call_pv("CLONE", 0);
1570# endif
1571 ptr_table_free(PL_ptr_table);
1572 PL_ptr_table = NULL;
1573
1574 PERL_SET_CONTEXT(aTHX);
1576
1577 t->perl = interp; /* Store perl interp for easy freeing later */
1578
1579 return 0;
1580}
1581
1583{
1584 rlm_perl_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_perl_thread_t);
1585
1587
1588 return 0;
1589}
1590
1591/** Check if a given Perl subroutine exists
1592 *
1593 */
1594static bool perl_func_exists(char const *func)
1595{
1596 char *eval_str;
1597 SV *val;
1598
1599 /*
1600 * Perl's "can" method checks if the object contains a subroutine of the given name.
1601 * We expect referenced subroutines to be in the "main" namespace.
1602 */
1603 eval_str = talloc_asprintf(NULL, "(main->can('%s') ? 1 : 0)", func);
1604 val = eval_pv(eval_str, TRUE);
1605 talloc_free(eval_str);
1606 return SvIV(val) ? true : false;
1607}
1608
1609/*
1610 * Do any per-module initialization that is separate to each
1611 * configured instance of the module. e.g. set up connections
1612 * to external databases, read configuration files, set up
1613 * dictionary entries, etc.
1614 *
1615 * If configuration information is given in the config section
1616 * that must be referenced in later calls, store a handle to it
1617 * in *instance otherwise put a null pointer there.
1618 *
1619 * Setup a hashes which we will use later
1620 * parse a module and give it a chance to live
1621 *
1622 */
1623static int mod_instantiate(module_inst_ctx_t const *mctx)
1624{
1625 rlm_perl_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_perl_t);
1626 perl_func_def_t *func = NULL;
1628 CONF_PAIR *cp;
1629 char *pair_name;
1630
1631 CONF_SECTION *conf = mctx->mi->conf;
1632 AV *end_AV;
1633
1634 char const **embed_c; /* Stupid Perl and lack of const consistency */
1635 char **embed;
1636 int ret = 0, argc = 0;
1637 char arg[] = "0";
1638
1639 CONF_SECTION *cs;
1640
1641 /*
1642 * Setup the argument array we pass to the perl interpreter
1643 */
1644 MEM(embed_c = talloc_zero_array(inst, char const *, 4));
1645 memcpy(&embed, &embed_c, sizeof(embed));
1646 embed_c[0] = NULL;
1647 if (inst->perl_flags) {
1648 embed_c[1] = inst->perl_flags;
1649 embed_c[2] = inst->module;
1650 embed_c[3] = arg;
1651 argc = 4;
1652 } else {
1653 embed_c[1] = inst->module;
1654 embed_c[2] = arg;
1655 argc = 3;
1656 }
1657
1658 /*
1659 * Allocate a new perl interpreter to do the parsing
1660 */
1661 if ((inst->perl = perl_alloc()) == NULL) {
1662 ERROR("No memory for allocating new perl interpreter!");
1663 return -1;
1664 }
1665 perl_construct(inst->perl); /* ...and initialise it */
1666
1667 PL_perl_destruct_level = 2;
1668 {
1669 dTHXa(inst->perl);
1670 }
1671 PERL_SET_CONTEXT(inst->perl);
1672
1673#if PERL_REVISION >= 5 && PERL_VERSION >=8
1674 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1675#endif
1676
1677 ret = perl_parse(inst->perl, xs_init, argc, embed, NULL);
1678
1679 end_AV = PL_endav;
1680 PL_endav = (AV *)NULL;
1681
1682 if (ret) {
1683 ERROR("Perl_parse failed: %s not found or has syntax errors", inst->module);
1684 return -1;
1685 }
1686
1687 /* parse perl configuration sub-section */
1688 cs = cf_section_find(conf, "config", NULL);
1689 if (cs) {
1690 inst->rad_perlconf_hv = get_hv("RAD_PERLCONF", 1);
1691 perl_parse_config(cs, 0, inst->rad_perlconf_hv);
1692 }
1693
1694 inst->perl_parsed = true;
1695 perl_run(inst->perl);
1696
1697 /*
1698 * The call_env parser has found all the places the module is called
1699 * Check for config options which set the subroutine name, falling back to
1700 * automatic subroutine names based on section name.
1701 */
1702 if (!inst->funcs_init) fr_rb_inline_init(&inst->funcs, perl_func_def_t, node, perl_func_def_cmp, NULL);
1703
1704 for (func = fr_rb_iter_init_inorder(&inst->funcs, &iter);
1705 func != NULL;
1706 func = fr_rb_iter_next_inorder(&inst->funcs, &iter)) {
1707 /*
1708 * Check for func_<name1>_<name2> or func_<name1> config pairs.
1709 */
1710 if (func->name2) {
1711 pair_name = talloc_asprintf(func, "func_%s_%s", func->name1, func->name2);
1712 cp = cf_pair_find(mctx->mi->conf, pair_name);
1713 talloc_free(pair_name);
1714 if (cp) goto found_func;
1715 }
1716 pair_name = talloc_asprintf(func, "func_%s", func->name1);
1717 cp = cf_pair_find(conf, pair_name);
1718 talloc_free(pair_name);
1719 found_func:
1720 if (cp){
1721 func->function_name = cf_pair_value(cp);
1722 if (!perl_func_exists(func->function_name)) {
1723 cf_log_err(cp, "Perl subroutine %s does not exist", func->function_name);
1724 return -1;
1725 }
1726 /*
1727 * If no pair was found, then use <name1>_<name2> or <name1> as the function to call.
1728 */
1729 } else if (func->name2) {
1730 func->function_name = talloc_asprintf(func, "%s_%s", func->name1, func->name2);
1731 if (!perl_func_exists(func->function_name)) {
1733 goto name1_only;
1734 }
1735 } else {
1736 name1_only:
1737 func->function_name = func->name1;
1738 if (!perl_func_exists(func->function_name)) {
1739 cf_log_err(cp, "Perl subroutine %s does not exist", func->function_name);
1740 return -1;
1741 }
1742 }
1743 }
1744
1745 PL_endav = end_AV;
1746
1747 inst->mutable = talloc(NULL, rlm_perl_mutable_t);
1748 pthread_mutex_init(&inst->mutable->mutex, NULL);
1749
1750 return 0;
1751}
1752
1753/*
1754 * Detach a instance give a chance to a module to make some internal setup ...
1755 */
1756DIAG_OFF(nested-externs)
1757static int mod_detach(module_detach_ctx_t const *mctx)
1758{
1759 rlm_perl_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_perl_t);
1760 int ret = 0, count = 0;
1761
1762
1763 if (inst->perl_parsed) {
1764 dTHXa(inst->perl);
1765 PERL_SET_CONTEXT(inst->perl);
1766 if (inst->rad_perlconf_hv != NULL) hv_undef(inst->rad_perlconf_hv);
1767
1768 if (inst->func_detach) {
1769 dSP; ENTER; SAVETMPS;
1770 PUSHMARK(SP);
1771
1772 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1773 SPAGAIN;
1774
1775 if (count == 1) {
1776 ret = POPi;
1777 if (ret >= 100 || ret < 0) {
1778 ret = RLM_MODULE_FAIL;
1779 }
1780 }
1781 PUTBACK;
1782 FREETMPS;
1783 LEAVE;
1784 }
1785 }
1786
1788 talloc_free(inst->mutable);
1789
1790 return ret;
1791}
1792DIAG_ON(nested-externs)
1793
1794static int mod_bootstrap(module_inst_ctx_t const *mctx)
1795{
1796 xlat_t *xlat;
1797
1798 xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, NULL, perl_xlat, FR_TYPE_VOID);
1800
1801 return 0;
1802}
1803
1804static int mod_load(void)
1805{
1806 char const **embed_c; /* Stupid Perl and lack of const consistency */
1807 char **embed;
1808 char **envp = NULL;
1809 int argc = 0;
1810
1811#define LOAD_INFO(_fmt, ...) fr_log(LOG_DST, L_INFO, __FILE__, __LINE__, "rlm_perl - " _fmt, ## __VA_ARGS__)
1812#define LOAD_WARN(_fmt, ...) fr_log_perror(LOG_DST, L_WARN, __FILE__, __LINE__, \
1813 &(fr_log_perror_format_t){ \
1814 .first_prefix = "rlm_perl - ", \
1815 .subsq_prefix = "rlm_perl - ", \
1816 }, \
1817 _fmt, ## __VA_ARGS__)
1818
1819 LOAD_INFO("Perl version: %s", PERL_API_VERSION_STRING);
1820 dependency_version_number_add(NULL, "perl", PERL_API_VERSION_STRING);
1821
1822 /*
1823 * Load perl using RTLD_GLOBAL and dlopen.
1824 * This fixes issues where Perl C extensions
1825 * can't find the symbols they need.
1826 */
1827 perl_dlhandle = dl_open_by_sym("perl_construct", RTLD_NOW | RTLD_GLOBAL);
1828 if (!perl_dlhandle) LOAD_WARN("Failed loading libperl symbols into global symbol table");
1829
1830 /*
1831 * Setup the argument array we pass to the perl interpreter
1832 */
1833 MEM(embed_c = talloc_zero_array(NULL, char const *, 1));
1834 memcpy(&embed, &embed_c, sizeof(embed));
1835 embed_c[0] = NULL;
1836 argc = 1;
1837
1838 PERL_SYS_INIT3(&argc, &embed, &envp);
1839
1840 talloc_free(embed_c);
1841
1842 return 0;
1843}
1844
1845static void mod_unload(void)
1846{
1847 if (perl_dlhandle) dlclose(perl_dlhandle);
1848 PERL_SYS_TERM();
1849}
1850
1851/*
1852 * Restrict automatic Perl function names to lowercase characters, numbers and underscore
1853 * meaning that a module call in `recv Access-Request` will look for `recv_access_request`
1854 */
1855static void perl_func_name_safe(char *name) {
1856 char *p;
1857 size_t i;
1858
1859 p = name;
1860 for (i = 0; i < talloc_array_length(name); i++) {
1861 *p = tolower(*p);
1862 if (!strchr("abcdefghijklmnopqrstuvwxyz1234567890", *p)) *p = '_';
1863 p++;
1864 }
1865}
1866
1867static int perl_func_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, UNUSED tmpl_rules_t const *t_rules,
1868 UNUSED CONF_ITEM *ci, call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
1869{
1870 rlm_perl_t *inst = talloc_get_type_abort(cec->mi->data, rlm_perl_t);
1871 call_env_parsed_t *parsed;
1872 perl_func_def_t *func;
1873 void *found;
1874
1875 if (!inst->funcs_init) {
1877 inst->funcs_init = true;
1878 }
1879
1880 MEM(parsed = call_env_parsed_add(ctx, out,
1882 .name = "func",
1883 .flags = CALL_ENV_FLAG_PARSE_ONLY,
1884 .pair = {
1885 .parsed = {
1886 .offset = rule->pair.offset,
1888 }
1889 }
1890 }));
1891
1892 MEM(func = talloc_zero(inst, perl_func_def_t));
1893 func->name1 = talloc_strdup(func, cec->asked->name1);
1895 if (cec->asked->name2) {
1896 func->name2 = talloc_strdup(func, cec->asked->name2);
1898 }
1899 if (fr_rb_find_or_insert(&found, &inst->funcs, func) < 0) {
1900 talloc_free(func);
1901 return -1;
1902 }
1903
1904 /*
1905 * If the function call is already in the tree, use that entry.
1906 */
1907 if (found) {
1908 talloc_free(func);
1909 call_env_parsed_set_data(parsed, found);
1910 } else {
1911 call_env_parsed_set_data(parsed, func);
1912 }
1913 return 0;
1914}
1915
1923
1924/*
1925 * The module name should be the only globally exported symbol.
1926 * That is, everything else should be 'static'.
1927 *
1928 * If the module needs to temporarily modify it's instantiation
1929 * data, the type should be changed to MODULE_TYPE_THREAD_UNSAFE.
1930 * The server will then take care of ensuring that the module
1931 * is single-threaded.
1932 */
1933extern module_rlm_t rlm_perl;
1935 .common = {
1936 .magic = MODULE_MAGIC_INIT,
1937 .name = "perl",
1938 .inst_size = sizeof(rlm_perl_t),
1939
1941 .onload = mod_load,
1942 .unload = mod_unload,
1943 .bootstrap = mod_bootstrap,
1944 .instantiate = mod_instantiate,
1945 .detach = mod_detach,
1946
1947 .thread_inst_size = sizeof(rlm_perl_thread_t),
1948 .thread_instantiate = mod_thread_instantiate,
1949 .thread_detach = mod_thread_detach,
1950 },
1951 .method_group = {
1952 .bindings = (module_method_binding_t[]){
1953 { .section = SECTION_NAME(CF_IDENT_ANY, CF_IDENT_ANY), .method = mod_perl, .method_env = &perl_method_env },
1955 }
1956 }
1957};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
log_entry msg
Definition acutest.h:794
#define RCSID(id)
Definition build.h:512
#define DIAG_UNKNOWN_PRAGMAS
Definition build.h:485
#define DIAG_ON(_x)
Definition build.h:487
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:113
#define unlikely(_x)
Definition build.h:407
#define UNUSED
Definition build.h:336
#define DIAG_OFF(_x)
Definition build.h:486
call_env_parsed_t * call_env_parsed_add(TALLOC_CTX *ctx, call_env_parsed_head_t *head, call_env_parser_t const *rule)
Allocate a new call_env_parsed_t structure and add it to the list of parsed call envs.
Definition call_env.c:690
void call_env_parsed_set_data(call_env_parsed_t *parsed, void const *data)
Assign data to a call_env_parsed_t.
Definition call_env.c:747
#define CALL_ENV_TERMINATOR
Definition call_env.h:236
#define FR_CALL_ENV_METHOD_OUT(_inst)
Helper macro for populating the size/type fields of a call_env_method_t from the output structure typ...
Definition call_env.h:240
call_env_parser_t const * env
Parsing rules for call method env.
Definition call_env.h:247
section_name_t const * asked
The actual name1/name2 that resolved to a module_method_binding_t.
Definition call_env.h:232
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
Definition call_env.h:85
@ CALL_ENV_FLAG_PARSE_MISSING
If this subsection is missing, still parse it.
Definition call_env.h:88
@ CALL_ENV_PARSE_TYPE_VOID
Output of the parsing phase is undefined (a custom structure).
Definition call_env.h:62
module_instance_t const * mi
Module instance that the callenv is registered to.
Definition call_env.h:229
#define FR_CALL_ENV_SUBSECTION_FUNC(_name, _name2, _flags, _func)
Specify a call_env_parser_t which parses a subsection using a callback function.
Definition call_env.h:412
Per method call config.
Definition call_env.h:180
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:669
void * data
Pointer to a static variable to write the parsed value to.
Definition cf_parse.h:620
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:429
@ CONF_FLAG_FILE_READABLE
File matching value must exist, and must be readable.
Definition cf_parse.h:435
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:606
Common header for all CONF_* types.
Definition cf_priv.h:54
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:77
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition cf_util.c:640
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
Definition cf_util.c:1338
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1194
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:692
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition cf_util.c:1587
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:626
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:672
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1746
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1730
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:345
#define cf_item_next(_parent, _curr)
Definition cf_util.h:94
#define CF_IDENT_ANY
Definition cf_util.h:80
static int split(char **input, char **output, bool syntax_string)
Definition command.c:393
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
#define MEM(x)
Definition debug.h:36
int dependency_version_number_add(CONF_SECTION *cs, char const *name, char const *version)
Add a library/server version pair to the main configuration.
Definition dependency.c:152
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3505
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2639
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4905
static fr_slen_t in
Definition dict.h:882
Test enumeration values.
Definition dict_test.h:92
void * dl_open_by_sym(char const *sym_name, int flags)
Utility function to dlopen the library containing a particular symbol.
Definition dl.c:186
#define RTLD_NOW
Definition dl.c:44
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
static xlat_action_t perl_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Call perl code using an xlat.
Definition rlm_perl.c:1222
talloc_free(hp)
#define RDEBUG3(fmt,...)
Definition log.h:355
#define RPEDEBUG(fmt,...)
Definition log.h:388
fr_log_t default_log
Definition log.c:306
void fr_log(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination.
Definition log.c:616
#define ENTER(_x)
Definition machine.h:93
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_FLOAT32
Single precision floating point.
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_SIZE
Unsigned integer capable of representing any memory address on the local system.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_IFID
Interface ID.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
@ FR_TYPE_FLOAT64
Double precision floating point.
long int ssize_t
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
void * thread
Thread specific instance data.
Definition module_ctx.h:43
void * thread
Thread instance data.
Definition module_ctx.h:67
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
Temporary structure to hold arguments for detach calls.
Definition module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
xlat_t * module_rlm_xlat_register(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type)
Definition module_rlm.c:232
module_t common
Common fields presented by all modules.
Definition module_rlm.h:39
fr_pair_t * fr_pair_list_parent(fr_pair_list_t const *list)
Return a pointer to the parent pair which contains this list.
Definition pair.c:970
unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Return the number of instances of a given da in the specified list.
Definition pair.c:684
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1471
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:707
fr_pair_t * fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
Find a pair with a matching da at a given index.
Definition pair.c:755
void fr_pair_value_clear(fr_pair_t *vp)
Free/zero out value (or children) of a given VP.
Definition pair.c:2561
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1833
int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and prepend)
Definition pair.c:1498
fr_pair_t * fr_pair_find_last_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the last pair with a matching da.
Definition pair.c:731
static const conf_parser_t config[]
Definition base.c:163
#define fr_assert(_expr)
Definition rad_assert.h:37
#define REDEBUG(fmt,...)
#define WARN(fmt,...)
static bool cleanup
Definition radsniff.c:59
static rs_t * conf
Definition radsniff.c:52
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:824
int fr_rb_find_or_insert(void **found, fr_rb_tree_t *tree, void const *data)
Attempt to find current data in the tree, if it does not exist insert it.
Definition rb.c:598
void * fr_rb_iter_next_inorder(UNUSED fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:850
#define fr_rb_inline_init(_tree, _type, _field, _data_cmp, _data_free)
Initialises a red black tree.
Definition rb.h:178
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:319
The main red black tree structure.
Definition rb.h:71
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:61
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
static int perl_sv_to_vblist(TALLOC_CTX *ctx, fr_value_box_list_t *list, request_t *request, SV *sv)
Parse a Perl SV and create value boxes, appending to a list.
Definition rlm_perl.c:1105
static fr_dict_attr_t const * perl_attr_lookup(fr_perl_pair_t *pair_data, char const *attr)
Helper function for turning hash keys into dictionary attributes.
Definition rlm_perl.c:274
fr_rb_node_t node
Node in tree of function calls.
Definition rlm_perl.c:61
static int mod_detach(module_detach_ctx_t const *mctx)
Definition rlm_perl.c:1757
static int mod_load(void)
Definition rlm_perl.c:1804
PerlInterpreter * perl
Thread specific perl interpreter.
Definition rlm_perl.c:95
static bool perl_func_exists(char const *func)
Check if a given Perl subroutine exists.
Definition rlm_perl.c:1594
HV * rad_perlconf_hv
holds "config" items (perl RAD_PERLCONF hash).
Definition rlm_perl.c:89
static XS(XS_freeradius_log)
Definition rlm_perl.c:218
static void perl_func_name_safe(char *name)
Definition rlm_perl.c:1855
static xlat_arg_parser_t const perl_xlat_args[]
Definition rlm_perl.c:1212
char const * func_detach
Function to run when mod_detach is run.
Definition rlm_perl.c:85
char const * function_name
Name of the function being called.
Definition rlm_perl.c:58
#define PERLINT(_size)
#define PERLUINT(_size)
static void ** rlm_perl_get_handles(pTHX)
Definition rlm_perl.c:163
bool perl_parsed
Definition rlm_perl.c:88
fr_perl_pair_t * parent
Parent attribute data.
Definition rlm_perl.c:106
static int perl_value_marshal(fr_pair_t *vp, SV **value)
Functions to implement subroutines required for a tied array.
Definition rlm_perl.c:553
EXTERN_C void boot_DynaLoader(pTHX_ CV *cv)
static void perl_parse_config(CONF_SECTION *cs, int lvl, HV *rad_hv)
Definition rlm_perl.c:1351
char const * perl_flags
Definition rlm_perl.c:86
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Definition rlm_perl.c:1794
char * name1
Section name1 where this is called.
Definition rlm_perl.c:59
#define dl_modules
Definition rlm_perl.c:154
module_rlm_t rlm_perl
Definition rlm_perl.c:1934
static void rlm_perl_interp_free(PerlInterpreter *perl)
Definition rlm_perl.c:1519
static int perl_value_unmarshal(fr_pair_t *vp, SV *value)
Convert a Perl SV to a pair value.
Definition rlm_perl.c:701
char const *fr_rb_tree_t funcs
Tree of function calls found by call_env parser.
Definition rlm_perl.c:83
static MGVTBL rlm_perl_vtbl
Definition rlm_perl.c:113
static void mod_unload(void)
Definition rlm_perl.c:1845
static void xs_init(pTHX)
Definition rlm_perl.c:1003
fr_dict_attr_t const * da
Dictionary attribute associated with hash / array.
Definition rlm_perl.c:103
struct fr_perl_pair_s fr_perl_pair_t
Definition rlm_perl.c:101
static int8_t perl_func_def_cmp(void const *one, void const *two)
How to compare two Perl function calls.
Definition rlm_perl.c:133
#define LOAD_WARN(_fmt,...)
static void rlm_perl_close_handles(void **handles)
Definition rlm_perl.c:197
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
Definition rlm_perl.c:1547
fr_dcursor_t cursor
Cursor used for iterating over the keys of a tied hash.
Definition rlm_perl.c:107
static void perl_pair_list_tie(HV *parent, HV *frpair_stash, char const *name, fr_pair_t *vp, fr_dict_attr_t const *da)
Create a Perl tied hash representing a pair list.
Definition rlm_perl.c:1413
#define GET_PAIR_MAGIC(count)
Convenience macro for fetching C data associated with tied hash / array and validating stack size.
Definition rlm_perl.c:293
unsigned int idx
Instance number.
Definition rlm_perl.c:105
PerlInterpreter * perl
Definition rlm_perl.c:87
static unlang_action_t mod_perl(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition rlm_perl.c:1440
char * name2
Section name2 where this is called.
Definition rlm_perl.c:60
#define dl_librefs
Definition rlm_perl.c:153
static int perl_vblist_to_av(AV *av, fr_value_box_list_t *head)
Convert a list of value boxes to a Perl array for passing to subroutines.
Definition rlm_perl.c:1051
static void * perl_dlhandle
To allow us to load perl's symbols into the global symbol table.
Definition rlm_perl.c:115
#define LOAD_INFO(_fmt,...)
static const conf_parser_t module_config[]
Definition rlm_perl.c:120
static _Thread_local request_t * rlm_perl_request
Definition rlm_perl.c:151
static const call_env_method_t perl_method_env
Definition rlm_perl.c:1916
bool funcs_init
Has the tree been initialised.
Definition rlm_perl.c:84
pthread_mutex_t mutex
Definition rlm_perl.c:69
static int mod_thread_detach(module_thread_inst_ctx_t const *mctx)
Definition rlm_perl.c:1582
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition rlm_perl.c:1623
fr_pair_t * vp
Real pair associated with the hash / array, if it exists.
Definition rlm_perl.c:104
perl_func_def_t * func
Definition rlm_perl.c:65
static void rlm_perl_clear_handles(pTHX)
Definition rlm_perl.c:155
static int fr_perl_pair_parent_build(fr_perl_pair_t *pair_data)
Build parent structural pairs needed when a leaf node is set.
Definition rlm_perl.c:665
static int perl_func_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, UNUSED tmpl_rules_t const *t_rules, UNUSED CONF_ITEM *ci, call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
Definition rlm_perl.c:1867
static char const * name
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
#define SECTION_NAME(_name1, _name2)
Define a section name consisting of a verb and a noun.
Definition section.h:39
char const * name2
Second section name. Usually a packet type like 'access-request', 'access-accept',...
Definition section.h:45
char const * name1
First section name. Usually a verb like 'recv', 'send', etc...
Definition section.h:44
API for POSIX semaphores in mmapped memory.
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:351
size_t inst_size
Size of the module's instance data.
Definition module.h:212
void * data
Module's instance data.
Definition module.h:293
#define MODULE_BINDING_TERMINATOR
Terminate a module binding list.
Definition module.h:152
Named methods exported by a module.
Definition module.h:174
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:336
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
return count
Definition module.c:155
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
fr_pair_t * vp
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 talloc_get_type_abort_const
Definition talloc.h:117
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:254
#define talloc_asprintf
Definition talloc.h:151
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
@ T_INVALID
Definition token.h:37
@ T_BARE_WORD
Definition token.h:118
@ XLAT_ARG_VARIADIC_EMPTY_KEEP
Empty argument groups are left alone, and either passed through as empty groups or null boxes.
Definition xlat.h:137
static fr_slen_t head
Definition xlat.h:420
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
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:1893
Definition for a single argument consumed by an xlat function.
Definition xlat.h:145
fr_pair_t * fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list without freeing.
Definition pair_inline.c:93
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:59
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:604
fr_pair_t * fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item))
Get the previous item in a valuepair list before a specific entry.
Definition pair_inline.c:82
static fr_slen_t parent
Definition pair.h:858
#define fr_type_is_group(_x)
Definition types.h:376
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
#define FR_TYPE_NON_LEAF
Definition types.h:318
#define FR_TYPE_STRUCTURAL
Definition types.h:316
#define fr_type_is_leaf(_x)
Definition types.h:393
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define FR_TYPE_LEAF
Definition types.h:317
ssize_t fr_value_box_print(fr_sbuff_t *out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules)
Print one boxed value to a string.
Definition value.c:6105
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:3946
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4838
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:4925
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:655
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610
#define fr_value_box_list_foreach(_list_head, _iter)
Definition value.h:224
static size_t char ** out
Definition value.h:1030
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition xlat_ctx.h:52
An xlat calling ctx.
Definition xlat_ctx.h:49
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:363