2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2 if the
4 * License as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * @brief Translates requests between the server an a perl interpreter.
21 * @copyright 2002,2006 The FreeRADIUS server project
22 * @copyright 2002 Boian Jordanov <bjordanov@orbitel.bg>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
37 #include <semaphore.h>
40 extern char **environ;
44 * Define a structure for our module configuration.
46 * These variables do not need to be in a structure, but it's
47 * a lot cleaner to do so, and a pointer to the structure can
48 * be used as the instance handle.
50 typedef struct rlm_perl_t {
51 /* Name of the perl module */
54 /* Name of the functions for each module method */
56 char *func_authenticate;
57 char *func_accounting;
58 char *func_start_accounting;
59 char *func_stop_accounting;
61 char *func_checksimul;
66 char *func_post_proxy;
75 PerlInterpreter *perl;
76 pthread_key_t *thread_key;
78 pthread_mutex_t clone_mutex;
81 * A mapping of configuration file names to internal variables.
83 #define RLM_PERL_CONF(_x) { "func_" Stringify(_x), PW_TYPE_STRING_PTR, \
84 offsetof(rlm_perl_t,func_##_x), NULL, Stringify(_x)}
86 static const CONF_PARSER module_config[] = {
87 { "module", PW_TYPE_FILENAME,
88 offsetof(rlm_perl_t,module), NULL, "module"},
90 RLM_PERL_CONF(authorize),
91 RLM_PERL_CONF(authenticate),
92 RLM_PERL_CONF(post_auth),
93 RLM_PERL_CONF(accounting),
94 RLM_PERL_CONF(preacct),
95 RLM_PERL_CONF(checksimul),
96 RLM_PERL_CONF(detach),
100 RLM_PERL_CONF(pre_proxy),
101 RLM_PERL_CONF(post_proxy),
104 RLM_PERL_CONF(recv_coa),
105 RLM_PERL_CONF(send_coa),
107 { "perl_flags", PW_TYPE_STRING_PTR,
108 offsetof(rlm_perl_t,perl_flags), NULL, NULL},
110 { "func_start_accounting", PW_TYPE_STRING_PTR,
111 offsetof(rlm_perl_t,func_start_accounting), NULL, NULL},
113 { "func_stop_accounting", PW_TYPE_STRING_PTR,
114 offsetof(rlm_perl_t,func_stop_accounting), NULL, NULL},
116 { NULL, -1, 0, NULL, NULL } /* end the list */
122 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
125 #define dl_librefs "DynaLoader::dl_librefs"
126 #define dl_modules "DynaLoader::dl_modules"
127 static void rlm_perl_clear_handles(pTHX)
129 AV *librefs = get_av(dl_librefs, FALSE);
135 static void **rlm_perl_get_handles(pTHX)
138 AV *librefs = get_av(dl_librefs, FALSE);
139 AV *modules = get_av(dl_modules, FALSE);
142 if (!librefs) return NULL;
144 if (!(AvFILL(librefs) >= 0)) {
148 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
150 for (i=0; i<=AvFILL(librefs); i++) {
152 SV *handle_sv = *av_fetch(librefs, i, FALSE);
155 ERROR("Could not fetch $%s[%d]!\n",
159 handle = (void *)SvIV(handle_sv);
169 handles[i] = (void *)0;
174 static void rlm_perl_close_handles(void **handles)
182 for (i=0; handles[i]; i++) {
183 DEBUG("close %p\n", handles[i]);
190 static void rlm_perl_destruct(PerlInterpreter *perl)
194 PERL_SET_CONTEXT(perl);
196 PL_perl_destruct_level = 2;
198 PL_origenviron = environ;
206 * FIXME: This shouldn't happen
209 while (PL_scopestack_ix > 1 ){
217 static void rlm_destroy_perl(PerlInterpreter *perl)
222 PERL_SET_CONTEXT(perl);
224 handles = rlm_perl_get_handles(aTHX);
225 if (handles) rlm_perl_close_handles(handles);
226 rlm_perl_destruct(perl);
230 static void rlm_perl_make_key(pthread_key_t *key)
232 pthread_key_create(key, (void*)rlm_destroy_perl);
235 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key)
239 PerlInterpreter *interp;
242 PERL_SET_CONTEXT(perl);
244 interp = pthread_getspecific(*key);
245 if (interp) return interp;
247 interp = perl_clone(perl, clone_flags);
251 #if PERL_REVISION >= 5 && PERL_VERSION <8
254 ptr_table_free(PL_ptr_table);
257 PERL_SET_CONTEXT(aTHX);
258 rlm_perl_clear_handles(aTHX);
260 ret = pthread_setspecific(*key, interp);
262 DEBUG("rlm_perl: Failed associating interpretor with thread %s", strerror(ret));
264 rlm_perl_destruct(interp);
272 static void xs_init(pTHX)
274 char const *file = __FILE__;
276 /* DynaLoader is a special case */
277 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
282 * This is wrapper for radlog
283 * Now users can call radiusd::radlog(level,msg) wich is the same
284 * calling radlog from C code.
287 static XS(XS_radiusd_radlog)
291 croak("Usage: radiusd::radlog(level, message)");
296 level = (int) SvIV(ST(0));
297 msg = (char *) SvPV(ST(1), PL_na);
300 * Because 'msg' is a 'char *', we don't want '%s', etc.
301 * in it to give us printf-style vulnerabilities.
303 radlog(level, "rlm_perl: %s", msg);
311 static size_t perl_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
314 rlm_perl_t *inst= (rlm_perl_t *) instance;
315 PerlInterpreter *perl;
322 #ifndef WITH_ITHREADS
325 perl = rlm_perl_clone(inst->perl,inst->thread_key);
330 PERL_SET_CONTEXT(perl);
338 while ((q = strchr(p, ' '))) {
339 XPUSHs(sv_2mortal(newSVpv(p, p - q)));
346 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
350 RDEBUGE("Exit %s", SvPV(ERRSV,n_a));
352 } else if (count > 0) {
354 strlcpy(out, tmp, freespace);
357 RDEBUG("Len is %zu , out is %s freespace is %zu", ret, out, freespace);
369 * Do any per-module initialization that is separate to each
370 * configured instance of the module. e.g. set up connections
371 * to external databases, read configuration files, set up
372 * dictionary entries, etc.
374 * If configuration information is given in the config section
375 * that must be referenced in later calls, store a handle to it
376 * in *instance otherwise put a null pointer there.
379 * Setup a hashes wich we will use later
380 * parse a module and give him a chance to live
383 static int mod_instantiate(CONF_SECTION *conf, void *instance)
385 rlm_perl_t *inst = instance;
390 char const *xlat_name;
391 int exitstatus = 0, argc=0;
393 MEM(embed = talloc_zero_array(inst, char *, 4));
396 * Create pthread key. This key will be stored in instance
400 pthread_mutex_init(&inst->clone_mutex, NULL);
402 inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
403 memset(inst->thread_key,0,sizeof(*inst->thread_key));
405 rlm_perl_make_key(inst->thread_key);
411 if (inst->perl_flags) {
412 embed[1] = inst->perl_flags;
413 embed[2] = inst->module;
417 embed[1] = inst->module;
422 PERL_SYS_INIT3(&argc, &embed, &envp);
424 if ((inst->perl = perl_alloc()) == NULL) {
425 ERROR("rlm_perl: No memory for allocating new perl !");
429 perl_construct(inst->perl);
432 PL_perl_destruct_level = 2;
437 PERL_SET_CONTEXT(inst->perl);
440 #if PERL_REVISION >= 5 && PERL_VERSION >=8
441 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
444 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
449 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl");
452 exitstatus = perl_run(inst->perl);
454 ERROR("rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
460 xlat_name = cf_section_name2(conf);
462 xlat_name = cf_section_name1(conf);
464 xlat_register(xlat_name, perl_xlat, NULL, inst);
471 * get the vps and put them in perl hash
472 * If one VP have multiple values it is added as array_ref
473 * Example for this is Cisco-AVPair that holds multiple values.
474 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
476 static void perl_store_vps(TALLOC_CTX *ctx, VALUE_PAIR *vps, HV *rad_hv)
478 VALUE_PAIR *head, *sublist;
488 * Copy the valuepair list so we can remove attributes
489 * we've already processed. This is a horrible hack to
490 * get around various other stupidity.
492 head = paircopy(ctx, vps);
496 * Tagged attributes are added to the hash with name
497 * <attribute>:<tag>, others just use the normal attribute
500 if (head->da->flags.has_tag && (head->tag != 0)) {
501 snprintf(namebuf, sizeof(namebuf), "%s:%d",
502 head->da->name, head->tag);
505 name = head->da->name;
509 * Create a new list with all the attributes like this one
510 * which are in the same tag group.
513 pairfilter(ctx, &sublist, &head, head->da->attr, head->da->vendor, head->tag);
516 * Attribute has multiple values
522 for (vp = sublist; vp; vp = vp->next) {
523 len = vp_prints_value(buffer, sizeof(buffer), vp, FALSE);
524 av_push(av, newSVpv(buffer, len));
526 (void)hv_store(rad_hv, name, strlen(name), newRV_noinc((SV *)av), 0);
529 * Attribute has a single value, so its value just gets
533 len = vp_prints_value(buffer, sizeof(buffer), sublist, FALSE);
534 (void)hv_store(rad_hv, name, strlen(name), newSVpv(buffer, len), 0);
545 * Verify that a Perl SV is a string and save it in FreeRadius
549 static int pairadd_sv(TALLOC_CTX *ctx, VALUE_PAIR **vps, char *key, SV *sv, FR_TOKEN op)
555 val = SvPV_nolen(sv);
556 vp = pairmake(ctx, vps, key, val, op);
558 DEBUG("rlm_perl: Added pair %s = %s", key, val);
561 DEBUG("rlm_perl: ERROR: Failed to create pair %s = %s", key, val);
569 * Gets the content from hashes
571 static int get_hv_content(TALLOC_CTX *ctx, HV *my_hv, VALUE_PAIR **vps)
576 I32 key_len, len, i, j;
580 for (i = hv_iterinit(my_hv); i > 0; i--) {
581 res_sv = hv_iternextsv(my_hv,&key,&key_len);
582 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
583 av = (AV*)SvRV(res_sv);
585 for (j = 0; j <= len; j++) {
586 av_sv = av_fetch(av, j, 0);
587 ret = pairadd_sv(ctx, vps, key, *av_sv, T_OP_ADD) + ret;
589 } else ret = pairadd_sv(ctx, vps, key, res_sv, T_OP_EQ) + ret;
596 * Call the function_name inside the module
597 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
600 static int do_perl(void *instance, REQUEST *request, char *function_name)
603 rlm_perl_t *inst = instance;
605 int exitstatus=0, count;
613 HV *rad_request_proxy_hv;
614 HV *rad_request_proxy_reply_hv;
618 * Radius has told us to call this function, but none
621 if (!function_name) return RLM_MODULE_FAIL;
624 pthread_mutex_lock(&inst->clone_mutex);
626 PerlInterpreter *interp;
628 interp = rlm_perl_clone(inst->perl,inst->thread_key);
631 PERL_SET_CONTEXT(interp);
634 pthread_mutex_unlock(&inst->clone_mutex);
636 PERL_SET_CONTEXT(inst->perl);
645 rad_reply_hv = get_hv("RAD_REPLY",1);
646 rad_check_hv = get_hv("RAD_CHECK",1);
647 rad_config_hv = get_hv("RAD_CONFIG",1);
648 rad_request_hv = get_hv("RAD_REQUEST",1);
650 perl_store_vps(request->reply, request->reply->vps, rad_reply_hv);
651 perl_store_vps(request, request->config_items, rad_check_hv);
652 perl_store_vps(request->packet, request->packet->vps, rad_request_hv);
653 perl_store_vps(request, request->config_items, rad_config_hv);
656 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
657 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
659 if (request->proxy != NULL) {
660 perl_store_vps(request->proxy, request->proxy->vps, rad_request_proxy_hv);
662 hv_undef(rad_request_proxy_hv);
665 if (request->proxy_reply !=NULL) {
666 perl_store_vps(request->proxy_reply, request->proxy_reply->vps, rad_request_proxy_reply_hv);
668 hv_undef(rad_request_proxy_reply_hv);
674 * This way %RAD_xx can be pushed onto stack as sub parameters.
675 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
676 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
677 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
681 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
686 ERROR("rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
688 function_name, SvPV(ERRSV,n_a));
694 if (exitstatus >= 100 || exitstatus < 0) {
695 exitstatus = RLM_MODULE_FAIL;
705 if ((get_hv_content(request->packet, rad_request_hv, &vp)) > 0 ) {
706 pairfree(&request->packet->vps);
707 request->packet->vps = vp;
711 * Update cached copies
713 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
714 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
715 if (!request->password)
716 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
719 if ((get_hv_content(request->reply, rad_reply_hv, &vp)) > 0 ) {
720 pairfree(&request->reply->vps);
721 request->reply->vps = vp;
725 if ((get_hv_content(request, rad_check_hv, &vp)) > 0 ) {
726 pairfree(&request->config_items);
727 request->config_items = vp;
732 if (request->proxy &&
733 (get_hv_content(request->proxy, rad_request_proxy_hv, &vp) > 0)) {
734 pairfree(&request->proxy->vps);
735 request->proxy->vps = vp;
739 if (request->proxy_reply &&
740 (get_hv_content(request->proxy_reply, rad_request_proxy_reply_hv, &vp) > 0)) {
741 pairfree(&request->proxy_reply->vps);
742 request->proxy_reply->vps = vp;
751 #define RLM_PERL_FUNC(_x) static rlm_rcode_t mod_##_x(void *instance, REQUEST *request) \
753 return do_perl(instance, request, \
754 ((rlm_perl_t *)instance)->func_##_x); \
757 RLM_PERL_FUNC(authorize)
758 RLM_PERL_FUNC(authenticate)
759 RLM_PERL_FUNC(post_auth)
761 RLM_PERL_FUNC(checksimul)
764 RLM_PERL_FUNC(pre_proxy)
765 RLM_PERL_FUNC(post_proxy)
769 RLM_PERL_FUNC(recv_coa)
770 RLM_PERL_FUNC(send_coa)
773 RLM_PERL_FUNC(preacct)
776 * Write accounting information to this modules database.
778 static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
781 int acctstatustype=0;
783 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0, TAG_ANY)) != NULL) {
784 acctstatustype = pair->vp_integer;
786 ERROR("Invalid Accounting Packet");
787 return RLM_MODULE_INVALID;
790 switch (acctstatustype) {
792 case PW_STATUS_START:
794 if (((rlm_perl_t *)instance)->func_start_accounting) {
795 return do_perl(instance, request,
796 ((rlm_perl_t *)instance)->func_start_accounting);
798 return do_perl(instance, request,
799 ((rlm_perl_t *)instance)->func_accounting);
805 if (((rlm_perl_t *)instance)->func_stop_accounting) {
806 return do_perl(instance, request,
807 ((rlm_perl_t *)instance)->func_stop_accounting);
809 return do_perl(instance, request,
810 ((rlm_perl_t *)instance)->func_accounting);
814 return do_perl(instance, request,
815 ((rlm_perl_t *)instance)->func_accounting);
822 * Detach a instance give a chance to a module to make some internal setup ...
824 static int mod_detach(void *instance)
826 rlm_perl_t *inst = (rlm_perl_t *) instance;
827 int exitstatus = 0, count = 0;
831 * FIXME: Call this in the destruct function?
834 dTHXa(handle->clone);
835 PERL_SET_CONTEXT(handle->clone);
837 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
838 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
847 if (exitstatus >= 100 || exitstatus < 0) {
848 exitstatus = RLM_MODULE_FAIL;
858 if (inst->func_detach) {
860 PERL_SET_CONTEXT(inst->perl);
862 dSP; ENTER; SAVETMPS;
865 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
870 if (exitstatus >= 100 || exitstatus < 0) {
871 exitstatus = RLM_MODULE_FAIL;
881 rlm_perl_destruct(inst->perl);
882 pthread_mutex_destroy(&inst->clone_mutex);
884 perl_destruct(inst->perl);
885 perl_free(inst->perl);
894 * The module name should be the only globally exported symbol.
895 * That is, everything else should be 'static'.
897 * If the module needs to temporarily modify it's instantiation
898 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
899 * The server will then take care of ensuring that the module
900 * is single-threaded.
902 module_t rlm_perl = {
906 RLM_TYPE_THREAD_SAFE, /* type */
908 RLM_TYPE_THREAD_UNSAFE,
912 mod_instantiate, /* instantiation */
913 mod_detach, /* detach */
915 mod_authenticate, /* authenticate */
916 mod_authorize, /* authorize */
917 mod_preacct, /* preacct */
918 mod_accounting, /* accounting */
919 mod_checksimul, /* check simul */
921 mod_pre_proxy, /* pre-proxy */
922 mod_post_proxy, /* post-proxy */
926 mod_post_auth /* post-auth */