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;
79 pthread_key_t *thread_key;
82 * A mapping of configuration file names to internal variables.
84 * Note that the string is dynamically allocated, so it MUST
85 * be freed. When the configuration file parse re-reads the string,
86 * it free's the old one, and strdup's the new one, placing the pointer
87 * to the strdup'd string into 'config.string'. This gets around
90 static const CONF_PARSER module_config[] = {
91 { "module", PW_TYPE_FILENAME,
92 offsetof(PERL_INST,module), NULL, "module"},
93 { "func_authorize", PW_TYPE_STRING_PTR,
94 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
95 { "func_authenticate", PW_TYPE_STRING_PTR,
96 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
97 { "func_accounting", PW_TYPE_STRING_PTR,
98 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
99 { "func_preacct", PW_TYPE_STRING_PTR,
100 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
101 { "func_checksimul", PW_TYPE_STRING_PTR,
102 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
103 { "func_detach", PW_TYPE_STRING_PTR,
104 offsetof(PERL_INST,func_detach), NULL, "detach"},
105 { "func_xlat", PW_TYPE_STRING_PTR,
106 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
107 { "func_pre_proxy", PW_TYPE_STRING_PTR,
108 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
109 { "func_post_proxy", PW_TYPE_STRING_PTR,
110 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
111 { "func_post_auth", PW_TYPE_STRING_PTR,
112 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
114 { "func_recv_coa", PW_TYPE_STRING_PTR,
115 offsetof(PERL_INST,func_recv_coa), NULL, "recv_coa"},
116 { "func_send_coa", PW_TYPE_STRING_PTR,
117 offsetof(PERL_INST,func_send_coa), NULL, "send_coa"},
119 { "perl_flags", PW_TYPE_STRING_PTR,
120 offsetof(PERL_INST,perl_flags), NULL, NULL},
121 { "func_start_accounting", PW_TYPE_STRING_PTR,
122 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
123 { "func_stop_accounting", PW_TYPE_STRING_PTR,
124 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
126 { NULL, -1, 0, NULL, NULL } /* end the list */
132 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
135 #define dl_librefs "DynaLoader::dl_librefs"
136 #define dl_modules "DynaLoader::dl_modules"
137 static void rlm_perl_clear_handles(pTHX)
139 AV *librefs = get_av(dl_librefs, FALSE);
145 static void **rlm_perl_get_handles(pTHX)
148 AV *librefs = get_av(dl_librefs, FALSE);
149 AV *modules = get_av(dl_modules, FALSE);
154 "Could not get @%s for unloading.\n",
159 if (!(AvFILL(librefs) >= 0)) {
163 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
165 for (i=0; i<=AvFILL(librefs); i++) {
167 SV *handle_sv = *av_fetch(librefs, i, FALSE);
171 "Could not fetch $%s[%d]!\n",
175 handle = (void *)SvIV(handle_sv);
185 handles[i] = (void *)0;
190 static void rlm_perl_close_handles(void **handles)
198 for (i=0; handles[i]; i++) {
199 radlog(L_DBG, "close %p\n", handles[i]);
206 static void rlm_perl_destruct(PerlInterpreter *perl)
210 PERL_SET_CONTEXT(perl);
212 PL_perl_destruct_level = 2;
214 PL_origenviron = environ;
220 * FIXME: This shouldn't happen
223 while (PL_scopestack_ix > 1 ){
231 static void rlm_destroy_perl(PerlInterpreter *perl)
236 PERL_SET_CONTEXT(perl);
238 handles = rlm_perl_get_handles(aTHX);
239 rlm_perl_destruct(perl);
240 rlm_perl_close_handles(handles);
244 static void rlm_perl_make_key(pthread_key_t *key)
246 pthread_key_create(key, rlm_destroy_perl);
249 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key)
251 PerlInterpreter *interp;
254 PERL_SET_CONTEXT(perl);
256 interp = pthread_getspecific(*key);
257 if (interp) return interp;
259 interp = perl_clone(perl, clone_flags);
263 #if PERL_REVISION >= 5 && PERL_VERSION <8
266 ptr_table_free(PL_ptr_table);
269 PERL_SET_CONTEXT(aTHX);
270 rlm_perl_clear_handles(aTHX);
272 pthread_setspecific(*key, interp);
278 static void xs_init(pTHX)
280 char *file = __FILE__;
282 /* DynaLoader is a special case */
283 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
288 * This is wrapper for radlog
289 * Now users can call radiusd::radlog(level,msg) wich is the same
290 * calling radlog from C code.
293 static XS(XS_radiusd_radlog)
297 croak("Usage: radiusd::radlog(level, message)");
302 level = (int) SvIV(ST(0));
303 msg = (char *) SvPV(ST(1), PL_na);
306 * Because 'msg' is a 'char *', we don't want '%s', etc.
307 * in it to give us printf-style vulnerabilities.
309 radlog(level, "rlm_perl: %s", msg);
317 static size_t perl_xlat(void *instance, REQUEST *request, char *fmt, char *out,
318 size_t freespace, RADIUS_ESCAPE_STRING func)
321 PERL_INST *inst= (PERL_INST *) instance;
322 PerlInterpreter *perl;
323 char params[1024], *ptr, *tmp;
329 * Do an xlat on the provided string (nice recursive operation).
331 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
332 radlog(L_ERR, "rlm_perl: xlat failed.");
336 #ifndef WITH_ITHREADS
339 perl = rlm_perl_clone(inst->perl,inst->thread_key);
344 PERL_SET_CONTEXT(perl);
349 ptr = strtok(params, " ");
353 while (ptr != NULL) {
354 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
355 ptr = strtok(NULL, " ");
360 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
364 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
367 } else if (count > 0) {
369 strlcpy(out, tmp, freespace);
372 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
384 * Do any per-module initialization that is separate to each
385 * configured instance of the module. e.g. set up connections
386 * to external databases, read configuration files, set up
387 * dictionary entries, etc.
389 * If configuration information is given in the config section
390 * that must be referenced in later calls, store a handle to it
391 * in *instance otherwise put a null pointer there.
394 * Setup a hashes wich we will use later
395 * parse a module and give him a chance to live
398 static int perl_instantiate(CONF_SECTION *conf, void **instance)
400 PERL_INST *inst = (PERL_INST *) instance;
405 HV *rad_request_proxy_hv;
406 HV *rad_request_proxy_reply_hv;
411 const char *xlat_name;
412 int exitstatus = 0, argc=0;
414 embed = rad_malloc(4*(sizeof(char *)));
415 memset(embed, 0, sizeof(4*(sizeof(char *))));
417 * Set up a storage area for instance data
419 inst = rad_malloc(sizeof(PERL_INST));
420 memset(inst, 0, sizeof(PERL_INST));
423 * If the configuration parameters can't be parsed, then
426 if (cf_section_parse(conf, inst, module_config) < 0) {
432 * Create pthread key. This key will be stored in instance
436 inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
437 memset(inst->thread_key,0,sizeof(*inst->thread_key));
439 rlm_perl_make_key(inst->thread_key);
442 if (inst->perl_flags) {
443 embed[1] = inst->perl_flags;
444 embed[2] = inst->module;
448 embed[1] = inst->module;
453 PERL_SYS_INIT3(&argc, &embed, &envp);
455 if ((inst->perl = perl_alloc()) == NULL) {
456 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
460 perl_construct(inst->perl);
461 PL_perl_destruct_level = 2;
466 PERL_SET_CONTEXT(inst->perl);
468 if ((inst->perl = perl_alloc()) == NULL) {
469 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
473 perl_construct(inst->perl);
476 #if PERL_REVISION >= 5 && PERL_VERSION >=8
477 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
480 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
486 exitstatus = perl_run(inst->perl);
488 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
494 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
496 rad_reply_hv = newHV();
497 rad_check_hv = newHV();
498 rad_config_hv = newHV();
499 rad_request_hv = newHV();
500 rad_request_proxy_hv = newHV();
501 rad_request_proxy_reply_hv = newHV();
503 rad_reply_hv = get_hv("RAD_REPLY",1);
504 rad_check_hv = get_hv("RAD_CHECK",1);
505 rad_config_hv = get_hv("RAD_CONFIG",1);
506 rad_request_hv = get_hv("RAD_REQUEST",1);
507 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
508 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
510 xlat_name = cf_section_name2(conf);
511 if (xlat_name == NULL)
512 xlat_name = cf_section_name1(conf);
514 inst->xlat_name = strdup(xlat_name);
515 xlat_register(xlat_name, perl_xlat, inst);
524 * get the vps and put them in perl hash
525 * If one VP have multiple values it is added as array_ref
526 * Example for this is Cisco-AVPair that holds multiple values.
527 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
529 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
531 VALUE_PAIR *nvp, *vpa, *vpn;
533 char namebuf[256], *name;
535 int attr, vendor, len;
540 while (nvp != NULL) {
542 attr = nvp->attribute;
543 vendor = nvp->vendor;
544 vpa = paircopy2(nvp, attr, vendor);
550 len = vp_prints_value(buffer, sizeof(buffer),
552 av_push(av, newSVpv(buffer, len));
555 hv_store(rad_hv, nvp->name, strlen(nvp->name),
556 newRV_noinc((SV *) av), 0);
558 if ((vpa->flags.has_tag) &&
559 (vpa->flags.tag != 0)) {
560 snprintf(namebuf, sizeof(namebuf), "%s:%d",
561 nvp->name, nvp->flags.tag);
565 len = vp_prints_value(buffer, sizeof(buffer),
567 hv_store(rad_hv, name, strlen(name),
568 newSVpv(buffer, len), 0);
572 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr) && (vpa->vendor == vendor))
574 pairdelete(&nvp, attr, vendor);
581 * Verify that a Perl SV is a string and save it in FreeRadius
585 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
590 val = SvPV_nolen(sv);
591 vpp = pairmake(key, val, operator);
595 "rlm_perl: Added pair %s = %s", key, val);
599 "rlm_perl: ERROR: Failed to create pair %s = %s",
608 * Gets the content from hashes
610 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
615 I32 key_len, len, i, j;
619 for (i = hv_iterinit(my_hv); i > 0; i--) {
620 res_sv = hv_iternextsv(my_hv,&key,&key_len);
621 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
622 av = (AV*)SvRV(res_sv);
624 for (j = 0; j <= len; j++) {
625 av_sv = av_fetch(av, j, 0);
626 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
628 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
635 * Call the function_name inside the module
636 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
639 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
642 PERL_INST *inst = instance;
644 int exitstatus=0, count;
651 HV *rad_request_proxy_hv;
652 HV *rad_request_proxy_reply_hv;
655 PerlInterpreter *interp;
657 interp = rlm_perl_clone(inst->perl,inst->thread_key);
660 PERL_SET_CONTEXT(interp);
663 PERL_SET_CONTEXT(inst->perl);
673 * Radius has told us to call this function, but none
676 if (!function_name) {
677 return RLM_MODULE_FAIL;
680 rad_reply_hv = get_hv("RAD_REPLY",1);
681 rad_check_hv = get_hv("RAD_CHECK",1);
682 rad_config_hv = get_hv("RAD_CONFIG",1);
683 rad_request_hv = get_hv("RAD_REQUEST",1);
684 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
685 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
688 perl_store_vps(request->reply->vps, rad_reply_hv);
689 perl_store_vps(request->config_items, rad_check_hv);
690 perl_store_vps(request->packet->vps, rad_request_hv);
691 perl_store_vps(request->config_items, rad_config_hv);
693 if (request->proxy != NULL) {
694 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
696 hv_undef(rad_request_proxy_hv);
699 if (request->proxy_reply !=NULL) {
700 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
702 hv_undef(rad_request_proxy_reply_hv);
707 * This way %RAD_xx can be pushed onto stack as sub parameters.
708 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
709 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
710 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
714 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
719 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
721 function_name, SvPV(ERRSV,n_a));
727 if (exitstatus >= 100 || exitstatus < 0) {
728 exitstatus = RLM_MODULE_FAIL;
738 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
739 pairfree(&request->packet->vps);
740 request->packet->vps = vp;
744 * Update cached copies
746 request->username = pairfind(request->packet->vps,
748 request->password = pairfind(request->packet->vps,
749 PW_USER_PASSWORD, 0);
750 if (!request->password)
751 request->password = pairfind(request->packet->vps,
752 PW_CHAP_PASSWORD, 0);
755 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
756 pairfree(&request->reply->vps);
757 request->reply->vps = vp;
761 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
762 pairfree(&request->config_items);
763 request->config_items = vp;
767 if (request->proxy &&
768 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
769 pairfree(&request->proxy->vps);
770 request->proxy->vps = vp;
774 if (request->proxy_reply &&
775 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
776 pairfree(&request->proxy_reply->vps);
777 request->proxy_reply->vps = vp;
786 * Find the named user in this modules database. Create the set
787 * of attribute-value pairs to check and reply with for this user
788 * from the database. The authentication code only needs to check
789 * the password, the rest is done here.
791 static int perl_authorize(void *instance, REQUEST *request)
793 return rlmperl_call(instance, request,
794 ((PERL_INST *)instance)->func_authorize);
798 * Authenticate the user with the given password.
800 static int perl_authenticate(void *instance, REQUEST *request)
802 return rlmperl_call(instance, request,
803 ((PERL_INST *)instance)->func_authenticate);
806 * Massage the request before recording it or proxying it
808 static int perl_preacct(void *instance, REQUEST *request)
810 return rlmperl_call(instance, request,
811 ((PERL_INST *)instance)->func_preacct);
814 * Write accounting information to this modules database.
816 static int perl_accounting(void *instance, REQUEST *request)
819 int acctstatustype=0;
821 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0)) != NULL) {
822 acctstatustype = pair->vp_integer;
824 radlog(L_ERR, "Invalid Accounting Packet");
825 return RLM_MODULE_INVALID;
828 switch (acctstatustype) {
830 case PW_STATUS_START:
832 if (((PERL_INST *)instance)->func_start_accounting) {
833 return rlmperl_call(instance, request,
834 ((PERL_INST *)instance)->func_start_accounting);
836 return rlmperl_call(instance, request,
837 ((PERL_INST *)instance)->func_accounting);
843 if (((PERL_INST *)instance)->func_stop_accounting) {
844 return rlmperl_call(instance, request,
845 ((PERL_INST *)instance)->func_stop_accounting);
847 return rlmperl_call(instance, request,
848 ((PERL_INST *)instance)->func_accounting);
852 return rlmperl_call(instance, request,
853 ((PERL_INST *)instance)->func_accounting);
858 * Check for simultaneouse-use
860 static int perl_checksimul(void *instance, REQUEST *request)
862 return rlmperl_call(instance, request,
863 ((PERL_INST *)instance)->func_checksimul);
868 static int perl_pre_proxy(void *instance, REQUEST *request)
870 return rlmperl_call(instance, request,
871 ((PERL_INST *)instance)->func_pre_proxy);
876 static int perl_post_proxy(void *instance, REQUEST *request)
878 return rlmperl_call(instance, request,
879 ((PERL_INST *)instance)->func_post_proxy);
884 static int perl_post_auth(void *instance, REQUEST *request)
886 return rlmperl_call(instance, request,
887 ((PERL_INST *)instance)->func_post_auth);
893 static int perl_recv_coa(void *instance, REQUEST *request)
895 return rlmperl_call(instance, request,
896 ((PERL_INST *)instance)->func_recv_coa);
901 static int perl_send_coa(void *instance, REQUEST *request)
903 return rlmperl_call(instance, request,
904 ((PERL_INST *)instance)->func_send_coa);
908 * Detach a instance give a chance to a module to make some internal setup ...
910 static int perl_detach(void *instance)
912 PERL_INST *inst = (PERL_INST *) instance;
913 int exitstatus = 0, count = 0;
917 * FIXME: Call this in the destruct function?
920 dTHXa(handle->clone);
921 PERL_SET_CONTEXT(handle->clone);
923 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
924 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
933 if (exitstatus >= 100 || exitstatus < 0) {
934 exitstatus = RLM_MODULE_FAIL;
944 if (inst->func_detach) {
946 PERL_SET_CONTEXT(inst->perl);
948 dSP; ENTER; SAVETMPS;
951 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
956 if (exitstatus >= 100 || exitstatus < 0) {
957 exitstatus = RLM_MODULE_FAIL;
966 xlat_unregister(inst->xlat_name, perl_xlat);
967 free(inst->xlat_name);
970 rlm_perl_destruct(inst->perl);
972 perl_destruct(inst->perl);
973 perl_free(inst->perl);
983 * The module name should be the only globally exported symbol.
984 * That is, everything else should be 'static'.
986 * If the module needs to temporarily modify it's instantiation
987 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
988 * The server will then take care of ensuring that the module
989 * is single-threaded.
991 module_t rlm_perl = {
995 RLM_TYPE_THREAD_SAFE, /* type */
997 RLM_TYPE_THREAD_UNSAFE,
999 perl_instantiate, /* instantiation */
1000 perl_detach, /* detach */
1002 perl_authenticate, /* authenticate */
1003 perl_authorize, /* authorize */
1004 perl_preacct, /* preacct */
1005 perl_accounting, /* accounting */
1006 perl_checksimul, /* check simul */
1007 perl_pre_proxy, /* pre-proxy */
1008 perl_post_proxy, /* post-proxy */
1009 perl_post_auth /* post-auth */