6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2002,2006 The FreeRADIUS server project
21 * Copyright 2002 Boian Jordanov <bjordanov@orbitel.bg>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
42 #include <semaphore.h>
45 extern char **environ;
49 * Define a structure for our module configuration.
51 * These variables do not need to be in a structure, but it's
52 * a lot cleaner to do so, and a pointer to the structure can
53 * be used as the instance handle.
55 typedef struct perl_inst {
56 /* Name of the perl module */
59 /* Name of the functions for each module method */
61 char *func_authenticate;
62 char *func_accounting;
63 char *func_start_accounting;
64 char *func_stop_accounting;
66 char *func_checksimul;
70 char *func_post_proxy;
78 PerlInterpreter *perl;
81 * A mapping of configuration file names to internal variables.
83 * Note that the string is dynamically allocated, so it MUST
84 * be freed. When the configuration file parse re-reads the string,
85 * it free's the old one, and strdup's the new one, placing the pointer
86 * to the strdup'd string into 'config.string'. This gets around
89 static const CONF_PARSER module_config[] = {
90 { "module", PW_TYPE_FILENAME,
91 offsetof(PERL_INST,module), NULL, "module"},
92 { "func_authorize", PW_TYPE_STRING_PTR,
93 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
94 { "func_authenticate", PW_TYPE_STRING_PTR,
95 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
96 { "func_accounting", PW_TYPE_STRING_PTR,
97 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
98 { "func_preacct", PW_TYPE_STRING_PTR,
99 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
100 { "func_checksimul", PW_TYPE_STRING_PTR,
101 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
102 { "func_detach", PW_TYPE_STRING_PTR,
103 offsetof(PERL_INST,func_detach), NULL, "detach"},
104 { "func_xlat", PW_TYPE_STRING_PTR,
105 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
106 { "func_pre_proxy", PW_TYPE_STRING_PTR,
107 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
108 { "func_post_proxy", PW_TYPE_STRING_PTR,
109 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
110 { "func_post_auth", PW_TYPE_STRING_PTR,
111 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
113 { "func_recv_coa", PW_TYPE_STRING_PTR,
114 offsetof(PERL_INST,func_recv_coa), NULL, "recv_coa"},
115 { "func_send_coa", PW_TYPE_STRING_PTR,
116 offsetof(PERL_INST,func_send_coa), NULL, "send_coa"},
118 { "perl_flags", PW_TYPE_STRING_PTR,
119 offsetof(PERL_INST,perl_flags), NULL, NULL},
120 { "func_start_accounting", PW_TYPE_STRING_PTR,
121 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
122 { "func_stop_accounting", PW_TYPE_STRING_PTR,
123 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
125 { NULL, -1, 0, NULL, NULL } /* end the list */
131 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
134 #define dl_librefs "DynaLoader::dl_librefs"
135 #define dl_modules "DynaLoader::dl_modules"
136 static void rlm_perl_clear_handles(pTHX)
138 AV *librefs = get_av(dl_librefs, FALSE);
144 static void **rlm_perl_get_handles(pTHX)
147 AV *librefs = get_av(dl_librefs, FALSE);
148 AV *modules = get_av(dl_modules, FALSE);
153 "Could not get @%s for unloading.\n",
158 if (!(AvFILL(librefs) >= 0)) {
162 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
164 for (i=0; i<=AvFILL(librefs); i++) {
166 SV *handle_sv = *av_fetch(librefs, i, FALSE);
170 "Could not fetch $%s[%d]!\n",
174 handle = (void *)SvIV(handle_sv);
184 handles[i] = (void *)0;
189 static void rlm_perl_close_handles(void **handles)
197 for (i=0; handles[i]; i++) {
198 radlog(L_DBG, "close %p\n", handles[i]);
205 static void rlm_perl_destruct(PerlInterpreter *perl)
209 PERL_SET_CONTEXT(perl);
211 PL_perl_destruct_level = 2;
213 PL_origenviron = environ;
219 * FIXME: This shouldn't happen
222 while (PL_scopestack_ix > 1 ){
230 static void rlm_destroy_perl(PerlInterpreter *perl)
235 PERL_SET_CONTEXT(perl);
237 handles = rlm_perl_get_handles(aTHX);
238 rlm_perl_destruct(perl);
239 rlm_perl_close_handles(handles);
242 static pthread_key_t rlm_perl_key;
243 static pthread_once_t rlm_perl_once = PTHREAD_ONCE_INIT;
246 static void rlm_perl_make_key(void)
248 pthread_key_create(&rlm_perl_key, rlm_destroy_perl);
251 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
253 PerlInterpreter *interp;
256 PERL_SET_CONTEXT(perl);
258 pthread_once(&rlm_perl_once, rlm_perl_make_key);
260 interp = pthread_getspecific(rlm_perl_key);
261 if (interp) return interp;
263 interp = perl_clone(perl, clone_flags);
267 #if PERL_REVISION >= 5 && PERL_VERSION <8
270 ptr_table_free(PL_ptr_table);
273 PERL_SET_CONTEXT(aTHX);
274 rlm_perl_clear_handles(aTHX);
276 pthread_setspecific(rlm_perl_key, interp);
278 fprintf(stderr, "GOT CLONE %d %p\n", pthread_self(), interp);
284 static void xs_init(pTHX)
286 char *file = __FILE__;
288 /* DynaLoader is a special case */
289 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
294 * This is wrapper for radlog
295 * Now users can call radiusd::radlog(level,msg) wich is the same
296 * calling radlog from C code.
299 static XS(XS_radiusd_radlog)
303 croak("Usage: radiusd::radlog(level, message)");
308 level = (int) SvIV(ST(0));
309 msg = (char *) SvPV(ST(1), PL_na);
312 * Because 'msg' is a 'char *', we don't want '%s', etc.
313 * in it to give us printf-style vulnerabilities.
315 radlog(level, "rlm_perl: %s", msg);
323 static size_t perl_xlat(void *instance, REQUEST *request, char *fmt, char *out,
324 size_t freespace, RADIUS_ESCAPE_STRING func)
327 PERL_INST *inst= (PERL_INST *) instance;
328 PerlInterpreter *perl;
329 char params[1024], *ptr, *tmp;
335 * Do an xlat on the provided string (nice recursive operation).
337 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
338 radlog(L_ERR, "rlm_perl: xlat failed.");
342 #ifndef WITH_ITHREADS
345 perl = rlm_perl_clone(inst->perl);
350 PERL_SET_CONTEXT(perl);
355 ptr = strtok(params, " ");
359 while (ptr != NULL) {
360 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
361 ptr = strtok(NULL, " ");
366 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
370 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
373 } else if (count > 0) {
375 strlcpy(out, tmp, freespace);
378 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
390 * Do any per-module initialization that is separate to each
391 * configured instance of the module. e.g. set up connections
392 * to external databases, read configuration files, set up
393 * dictionary entries, etc.
395 * If configuration information is given in the config section
396 * that must be referenced in later calls, store a handle to it
397 * in *instance otherwise put a null pointer there.
400 * Setup a hashes wich we will use later
401 * parse a module and give him a chance to live
404 static int perl_instantiate(CONF_SECTION *conf, void **instance)
406 PERL_INST *inst = (PERL_INST *) instance;
411 HV *rad_request_proxy_hv;
412 HV *rad_request_proxy_reply_hv;
416 const char *xlat_name;
417 int exitstatus = 0, argc=0;
420 * Set up a storage area for instance data
422 inst = rad_malloc(sizeof(PERL_INST));
423 memset(inst, 0, sizeof(PERL_INST));
426 * If the configuration parameters can't be parsed, then
429 if (cf_section_parse(conf, inst, module_config) < 0) {
436 if (inst->perl_flags) {
437 embed[1] = inst->perl_flags;
438 embed[2] = inst->module;
442 embed[1] = inst->module;
448 if ((inst->perl = perl_alloc()) == NULL) {
449 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
453 perl_construct(inst->perl);
454 PL_perl_destruct_level = 2;
459 PERL_SET_CONTEXT(inst->perl);
461 if ((inst->perl = perl_alloc()) == NULL) {
462 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
466 perl_construct(inst->perl);
469 #if PERL_REVISION >= 5 && PERL_VERSION >=8
470 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
473 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
479 exitstatus = perl_run(inst->perl);
481 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
487 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
489 rad_reply_hv = newHV();
490 rad_check_hv = newHV();
491 rad_config_hv = newHV();
492 rad_request_hv = newHV();
493 rad_request_proxy_hv = newHV();
494 rad_request_proxy_reply_hv = newHV();
496 rad_reply_hv = get_hv("RAD_REPLY",1);
497 rad_check_hv = get_hv("RAD_CHECK",1);
498 rad_config_hv = get_hv("RAD_CONFIG",1);
499 rad_request_hv = get_hv("RAD_REQUEST",1);
500 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
501 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
503 xlat_name = cf_section_name2(conf);
504 if (xlat_name == NULL)
505 xlat_name = cf_section_name1(conf);
507 inst->xlat_name = strdup(xlat_name);
508 xlat_register(xlat_name, perl_xlat, inst);
517 * get the vps and put them in perl hash
518 * If one VP have multiple values it is added as array_ref
519 * Example for this is Cisco-AVPair that holds multiple values.
520 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
522 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
524 VALUE_PAIR *nvp, *vpa, *vpn;
526 char namebuf[256], *name;
533 while (nvp != NULL) {
535 attr = nvp->attribute;
536 vpa = paircopy2(nvp,attr);
542 len = vp_prints_value(buffer, sizeof(buffer),
544 av_push(av, newSVpv(buffer, len));
547 hv_store(rad_hv, nvp->name, strlen(nvp->name),
548 newRV_noinc((SV *) av), 0);
550 if ((vpa->flags.has_tag) &&
551 (vpa->flags.tag != 0)) {
552 snprintf(namebuf, sizeof(namebuf), "%s:%d",
553 nvp->name, nvp->flags.tag);
557 len = vp_prints_value(buffer, sizeof(buffer),
559 hv_store(rad_hv, name, strlen(name),
560 newSVpv(buffer, len), 0);
564 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
566 pairdelete(&nvp, attr);
573 * Verify that a Perl SV is a string and save it in FreeRadius
577 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
582 val = SvPV_nolen(sv);
583 vpp = pairmake(key, val, operator);
587 "rlm_perl: Added pair %s = %s", key, val);
591 "rlm_perl: ERROR: Failed to create pair %s = %s",
600 * Gets the content from hashes
602 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
607 I32 key_len, len, i, j;
611 for (i = hv_iterinit(my_hv); i > 0; i--) {
612 res_sv = hv_iternextsv(my_hv,&key,&key_len);
613 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
614 av = (AV*)SvRV(res_sv);
616 for (j = 0; j <= len; j++) {
617 av_sv = av_fetch(av, j, 0);
618 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
620 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
627 * Call the function_name inside the module
628 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
631 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
634 PERL_INST *inst = instance;
636 int exitstatus=0, count;
643 HV *rad_request_proxy_hv;
644 HV *rad_request_proxy_reply_hv;
647 PerlInterpreter *interp;
649 interp = rlm_perl_clone(inst->perl);
652 PERL_SET_CONTEXT(interp);
655 PERL_SET_CONTEXT(inst->perl);
665 * Radius has told us to call this function, but none
668 if (!function_name) {
669 return RLM_MODULE_FAIL;
672 rad_reply_hv = get_hv("RAD_REPLY",1);
673 rad_check_hv = get_hv("RAD_CHECK",1);
674 rad_config_hv = get_hv("RAD_CONFIG",1);
675 rad_request_hv = get_hv("RAD_REQUEST",1);
676 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
677 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
680 perl_store_vps(request->reply->vps, rad_reply_hv);
681 perl_store_vps(request->config_items, rad_check_hv);
682 perl_store_vps(request->packet->vps, rad_request_hv);
683 perl_store_vps(request->config_items, rad_config_hv);
685 if (request->proxy != NULL) {
686 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
688 hv_undef(rad_request_proxy_hv);
691 if (request->proxy_reply !=NULL) {
692 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
694 hv_undef(rad_request_proxy_reply_hv);
699 * This way %RAD_xx can be pushed onto stack as sub parameters.
700 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
701 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
702 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
706 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
711 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
713 function_name, SvPV(ERRSV,n_a));
719 if (exitstatus >= 100 || exitstatus < 0) {
720 exitstatus = RLM_MODULE_FAIL;
730 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
731 pairfree(&request->packet->vps);
732 request->packet->vps = vp;
736 * Update cached copies
738 request->username = pairfind(request->packet->vps,
740 request->password = pairfind(request->packet->vps,
742 if (!request->password)
743 request->password = pairfind(request->packet->vps,
747 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
748 pairfree(&request->reply->vps);
749 request->reply->vps = vp;
753 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
754 pairfree(&request->config_items);
755 request->config_items = vp;
759 if (request->proxy &&
760 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
761 pairfree(&request->proxy->vps);
762 request->proxy->vps = vp;
766 if (request->proxy_reply &&
767 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
768 pairfree(&request->proxy_reply->vps);
769 request->proxy_reply->vps = vp;
778 * Find the named user in this modules database. Create the set
779 * of attribute-value pairs to check and reply with for this user
780 * from the database. The authentication code only needs to check
781 * the password, the rest is done here.
783 static int perl_authorize(void *instance, REQUEST *request)
785 return rlmperl_call(instance, request,
786 ((PERL_INST *)instance)->func_authorize);
790 * Authenticate the user with the given password.
792 static int perl_authenticate(void *instance, REQUEST *request)
794 return rlmperl_call(instance, request,
795 ((PERL_INST *)instance)->func_authenticate);
798 * Massage the request before recording it or proxying it
800 static int perl_preacct(void *instance, REQUEST *request)
802 return rlmperl_call(instance, request,
803 ((PERL_INST *)instance)->func_preacct);
806 * Write accounting information to this modules database.
808 static int perl_accounting(void *instance, REQUEST *request)
811 int acctstatustype=0;
813 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
814 acctstatustype = pair->vp_integer;
816 radlog(L_ERR, "Invalid Accounting Packet");
817 return RLM_MODULE_INVALID;
820 switch (acctstatustype) {
822 case PW_STATUS_START:
824 if (((PERL_INST *)instance)->func_start_accounting) {
825 return rlmperl_call(instance, request,
826 ((PERL_INST *)instance)->func_start_accounting);
828 return rlmperl_call(instance, request,
829 ((PERL_INST *)instance)->func_accounting);
835 if (((PERL_INST *)instance)->func_stop_accounting) {
836 return rlmperl_call(instance, request,
837 ((PERL_INST *)instance)->func_stop_accounting);
839 return rlmperl_call(instance, request,
840 ((PERL_INST *)instance)->func_accounting);
844 return rlmperl_call(instance, request,
845 ((PERL_INST *)instance)->func_accounting);
850 * Check for simultaneouse-use
852 static int perl_checksimul(void *instance, REQUEST *request)
854 return rlmperl_call(instance, request,
855 ((PERL_INST *)instance)->func_checksimul);
860 static int perl_pre_proxy(void *instance, REQUEST *request)
862 return rlmperl_call(instance, request,
863 ((PERL_INST *)instance)->func_pre_proxy);
868 static int perl_post_proxy(void *instance, REQUEST *request)
870 return rlmperl_call(instance, request,
871 ((PERL_INST *)instance)->func_post_proxy);
876 static int perl_post_auth(void *instance, REQUEST *request)
878 return rlmperl_call(instance, request,
879 ((PERL_INST *)instance)->func_post_auth);
885 static int perl_recv_coa(void *instance, REQUEST *request)
887 return rlmperl_call(instance, request,
888 ((PERL_INST *)instance)->func_recv_coa);
893 static int perl_send_coa(void *instance, REQUEST *request)
895 return rlmperl_call(instance, request,
896 ((PERL_INST *)instance)->func_send_coa);
900 * Detach a instance give a chance to a module to make some internal setup ...
902 static int perl_detach(void *instance)
904 PERL_INST *inst = (PERL_INST *) instance;
905 int exitstatus = 0, count = 0;
909 * FIXME: Call this in the destruct function?
912 dTHXa(handle->clone);
913 PERL_SET_CONTEXT(handle->clone);
915 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
916 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
925 if (exitstatus >= 100 || exitstatus < 0) {
926 exitstatus = RLM_MODULE_FAIL;
938 PERL_SET_CONTEXT(inst->perl);
940 dSP; ENTER; SAVETMPS;
943 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
948 if (exitstatus >= 100 || exitstatus < 0) {
949 exitstatus = RLM_MODULE_FAIL;
958 xlat_unregister(inst->xlat_name, perl_xlat);
959 free(inst->xlat_name);
962 rlm_perl_destruct(inst->perl);
964 perl_destruct(inst->perl);
965 perl_free(inst->perl);
974 * The module name should be the only globally exported symbol.
975 * That is, everything else should be 'static'.
977 * If the module needs to temporarily modify it's instantiation
978 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
979 * The server will then take care of ensuring that the module
980 * is single-threaded.
982 module_t rlm_perl = {
986 RLM_TYPE_THREAD_SAFE, /* type */
988 RLM_TYPE_THREAD_UNSAFE,
990 perl_instantiate, /* instantiation */
991 perl_detach, /* detach */
993 perl_authenticate, /* authenticate */
994 perl_authorize, /* authorize */
995 perl_preacct, /* preacct */
996 perl_accounting, /* accounting */
997 perl_checksimul, /* check simul */
998 perl_pre_proxy, /* pre-proxy */
999 perl_post_proxy, /* post-proxy */
1000 perl_post_auth /* post-auth */