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;
71 char *func_post_proxy;
80 PerlInterpreter *perl;
81 pthread_key_t *thread_key;
83 pthread_mutex_t clone_mutex;
86 * A mapping of configuration file names to internal variables.
88 * Note that the string is dynamically allocated, so it MUST
89 * be freed. When the configuration file parse re-reads the string,
90 * it free's the old one, and strdup's the new one, placing the pointer
91 * to the strdup'd string into 'config.string'. This gets around
94 static const CONF_PARSER module_config[] = {
95 { "module", PW_TYPE_FILENAME,
96 offsetof(PERL_INST,module), NULL, "module"},
97 { "func_authorize", PW_TYPE_STRING_PTR,
98 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
99 { "func_authenticate", PW_TYPE_STRING_PTR,
100 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
101 { "func_accounting", PW_TYPE_STRING_PTR,
102 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
103 { "func_preacct", PW_TYPE_STRING_PTR,
104 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
105 { "func_checksimul", PW_TYPE_STRING_PTR,
106 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
107 { "func_detach", PW_TYPE_STRING_PTR,
108 offsetof(PERL_INST,func_detach), NULL, "detach"},
109 { "func_xlat", PW_TYPE_STRING_PTR,
110 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
112 { "func_pre_proxy", PW_TYPE_STRING_PTR,
113 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
114 { "func_post_proxy", PW_TYPE_STRING_PTR,
115 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
117 { "func_post_auth", PW_TYPE_STRING_PTR,
118 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
120 { "func_recv_coa", PW_TYPE_STRING_PTR,
121 offsetof(PERL_INST,func_recv_coa), NULL, "recv_coa"},
122 { "func_send_coa", PW_TYPE_STRING_PTR,
123 offsetof(PERL_INST,func_send_coa), NULL, "send_coa"},
125 { "perl_flags", PW_TYPE_STRING_PTR,
126 offsetof(PERL_INST,perl_flags), NULL, NULL},
127 { "func_start_accounting", PW_TYPE_STRING_PTR,
128 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
129 { "func_stop_accounting", PW_TYPE_STRING_PTR,
130 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
132 { NULL, -1, 0, NULL, NULL } /* end the list */
138 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
141 #define dl_librefs "DynaLoader::dl_librefs"
142 #define dl_modules "DynaLoader::dl_modules"
143 static void rlm_perl_clear_handles(pTHX)
145 AV *librefs = get_av(dl_librefs, FALSE);
151 static void **rlm_perl_get_handles(pTHX)
154 AV *librefs = get_av(dl_librefs, FALSE);
155 AV *modules = get_av(dl_modules, FALSE);
160 "Could not get @%s for unloading.\n",
165 if (!(AvFILL(librefs) >= 0)) {
169 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
171 for (i=0; i<=AvFILL(librefs); i++) {
173 SV *handle_sv = *av_fetch(librefs, i, FALSE);
177 "Could not fetch $%s[%d]!\n",
181 handle = (void *)SvIV(handle_sv);
191 handles[i] = (void *)0;
196 static void rlm_perl_close_handles(void **handles)
204 for (i=0; handles[i]; i++) {
205 radlog(L_DBG, "close %p\n", handles[i]);
212 static void rlm_perl_destruct(PerlInterpreter *perl)
216 PERL_SET_CONTEXT(perl);
218 PL_perl_destruct_level = 2;
220 PL_origenviron = environ;
226 * FIXME: This shouldn't happen
229 while (PL_scopestack_ix > 1 ){
237 static void rlm_destroy_perl(PerlInterpreter *perl)
242 PERL_SET_CONTEXT(perl);
244 handles = rlm_perl_get_handles(aTHX);
245 rlm_perl_destruct(perl);
246 rlm_perl_close_handles(handles);
250 static void rlm_perl_make_key(pthread_key_t *key)
252 pthread_key_create(key, rlm_destroy_perl);
255 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key)
257 PerlInterpreter *interp;
260 PERL_SET_CONTEXT(perl);
262 interp = pthread_getspecific(*key);
263 if (interp) return interp;
265 interp = perl_clone(perl, clone_flags);
269 #if PERL_REVISION >= 5 && PERL_VERSION <8
272 ptr_table_free(PL_ptr_table);
275 PERL_SET_CONTEXT(aTHX);
276 rlm_perl_clear_handles(aTHX);
278 pthread_setspecific(*key, 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,
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, NULL, NULL)) {
338 radlog(L_ERR, "rlm_perl: xlat failed.");
342 #ifndef WITH_ITHREADS
345 perl = rlm_perl_clone(inst->perl,inst->thread_key);
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;
412 HV *rad_request_proxy_hv;
413 HV *rad_request_proxy_reply_hv;
419 const char *xlat_name;
420 int exitstatus = 0, argc=0;
422 embed = rad_malloc(4 * sizeof(char *));
423 memset(embed, 0, 4 *sizeof(char *));
425 * Set up a storage area for instance data
427 inst = rad_malloc(sizeof(PERL_INST));
428 memset(inst, 0, sizeof(PERL_INST));
431 * If the configuration parameters can't be parsed, then
434 if (cf_section_parse(conf, inst, module_config) < 0) {
441 * Create pthread key. This key will be stored in instance
445 pthread_mutex_init(&inst->clone_mutex, NULL);
447 inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
448 memset(inst->thread_key,0,sizeof(*inst->thread_key));
450 rlm_perl_make_key(inst->thread_key);
453 if (inst->perl_flags) {
454 embed[1] = inst->perl_flags;
455 embed[2] = inst->module;
459 embed[1] = inst->module;
464 PERL_SYS_INIT3(&argc, &embed, &envp);
466 if ((inst->perl = perl_alloc()) == NULL) {
467 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
473 perl_construct(inst->perl);
474 PL_perl_destruct_level = 2;
479 PERL_SET_CONTEXT(inst->perl);
481 if ((inst->perl = perl_alloc()) == NULL) {
482 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
488 perl_construct(inst->perl);
491 #if PERL_REVISION >= 5 && PERL_VERSION >=8
492 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
495 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
501 exitstatus = perl_run(inst->perl);
503 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
511 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
513 rad_reply_hv = newHV();
514 rad_check_hv = newHV();
515 rad_config_hv = newHV();
516 rad_request_hv = newHV();
518 rad_request_proxy_hv = newHV();
519 rad_request_proxy_reply_hv = newHV();
522 rad_reply_hv = get_hv("RAD_REPLY",1);
523 rad_check_hv = get_hv("RAD_CHECK",1);
524 rad_config_hv = get_hv("RAD_CONFIG",1);
525 rad_request_hv = get_hv("RAD_REQUEST",1);
527 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
528 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
531 xlat_name = cf_section_name2(conf);
532 if (xlat_name == NULL)
533 xlat_name = cf_section_name1(conf);
535 inst->xlat_name = strdup(xlat_name);
536 xlat_register(xlat_name, perl_xlat, inst);
545 * get the vps and put them in perl hash
546 * If one VP have multiple values it is added as array_ref
547 * Example for this is Cisco-AVPair that holds multiple values.
548 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
550 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
552 VALUE_PAIR *nvp, *vpa, *vpn;
554 char namebuf[256], *name;
561 while (nvp != NULL) {
562 if (nvp->flags.has_tag && (nvp->flags.tag != 0)) {
563 snprintf(namebuf, sizeof(namebuf), "%s:%d",
564 nvp->name, nvp->flags.tag);
570 vpa = paircopy2(nvp, nvp->attribute, nvp->vendor, nvp->flags.tag);
574 for (vpn = vpa; vpn; vpn = vpn->next) {
575 len = vp_prints_value(buffer, sizeof(buffer), vpn, FALSE);
576 av_push(av, newSVpv(buffer, len));
578 hv_store(rad_hv, name, strlen(name), newRV_noinc((SV *)av), 0);
580 len = vp_prints_value(buffer, sizeof(buffer), vpa, FALSE);
581 hv_store(rad_hv, name, strlen(name), newSVpv(buffer, len), 0);
585 pairdelete(&nvp, nvp->attribute, nvp->vendor, nvp->flags.tag);
591 * Verify that a Perl SV is a string and save it in FreeRadius
595 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
600 val = SvPV_nolen(sv);
601 vpp = pairmake(key, val, operator);
605 "rlm_perl: Added pair %s = %s", key, val);
609 "rlm_perl: ERROR: Failed to create pair %s = %s",
618 * Gets the content from hashes
620 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
625 I32 key_len, len, i, j;
629 for (i = hv_iterinit(my_hv); i > 0; i--) {
630 res_sv = hv_iternextsv(my_hv,&key,&key_len);
631 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
632 av = (AV*)SvRV(res_sv);
634 for (j = 0; j <= len; j++) {
635 av_sv = av_fetch(av, j, 0);
636 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
638 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
645 * Call the function_name inside the module
646 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
649 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
652 PERL_INST *inst = instance;
654 int exitstatus=0, count;
662 HV *rad_request_proxy_hv;
663 HV *rad_request_proxy_reply_hv;
667 pthread_mutex_lock(&inst->clone_mutex);
669 PerlInterpreter *interp;
671 interp = rlm_perl_clone(inst->perl,inst->thread_key);
674 PERL_SET_CONTEXT(interp);
677 pthread_mutex_unlock(&inst->clone_mutex);
679 PERL_SET_CONTEXT(inst->perl);
690 * Radius has told us to call this function, but none
693 if (!function_name) {
694 return RLM_MODULE_FAIL;
697 rad_reply_hv = get_hv("RAD_REPLY",1);
698 rad_check_hv = get_hv("RAD_CHECK",1);
699 rad_config_hv = get_hv("RAD_CONFIG",1);
700 rad_request_hv = get_hv("RAD_REQUEST",1);
702 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
703 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
706 perl_store_vps(request->reply->vps, rad_reply_hv);
707 perl_store_vps(request->config_items, rad_check_hv);
708 perl_store_vps(request->packet->vps, rad_request_hv);
709 perl_store_vps(request->config_items, rad_config_hv);
712 if (request->proxy != NULL) {
713 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
715 hv_undef(rad_request_proxy_hv);
718 if (request->proxy_reply !=NULL) {
719 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
721 hv_undef(rad_request_proxy_reply_hv);
727 * This way %RAD_xx can be pushed onto stack as sub parameters.
728 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
729 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
730 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
734 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
739 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
741 function_name, SvPV(ERRSV,n_a));
747 if (exitstatus >= 100 || exitstatus < 0) {
748 exitstatus = RLM_MODULE_FAIL;
758 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
759 pairfree(&request->packet->vps);
760 request->packet->vps = vp;
764 * Update cached copies
766 request->username = pairfind(request->packet->vps,
768 request->password = pairfind(request->packet->vps,
769 PW_USER_PASSWORD, 0);
770 if (!request->password)
771 request->password = pairfind(request->packet->vps,
772 PW_CHAP_PASSWORD, 0);
775 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
776 pairfree(&request->reply->vps);
777 request->reply->vps = vp;
781 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
782 pairfree(&request->config_items);
783 request->config_items = vp;
788 if (request->proxy &&
789 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
790 pairfree(&request->proxy->vps);
791 request->proxy->vps = vp;
795 if (request->proxy_reply &&
796 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
797 pairfree(&request->proxy_reply->vps);
798 request->proxy_reply->vps = vp;
808 * Find the named user in this modules database. Create the set
809 * of attribute-value pairs to check and reply with for this user
810 * from the database. The authentication code only needs to check
811 * the password, the rest is done here.
813 static int perl_authorize(void *instance, REQUEST *request)
815 return rlmperl_call(instance, request,
816 ((PERL_INST *)instance)->func_authorize);
820 * Authenticate the user with the given password.
822 static int perl_authenticate(void *instance, REQUEST *request)
824 return rlmperl_call(instance, request,
825 ((PERL_INST *)instance)->func_authenticate);
828 * Massage the request before recording it or proxying it
830 static int perl_preacct(void *instance, REQUEST *request)
832 return rlmperl_call(instance, request,
833 ((PERL_INST *)instance)->func_preacct);
836 * Write accounting information to this modules database.
838 static int perl_accounting(void *instance, REQUEST *request)
841 int acctstatustype=0;
843 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0)) != NULL) {
844 acctstatustype = pair->vp_integer;
846 radlog(L_ERR, "Invalid Accounting Packet");
847 return RLM_MODULE_INVALID;
850 switch (acctstatustype) {
852 case PW_STATUS_START:
854 if (((PERL_INST *)instance)->func_start_accounting) {
855 return rlmperl_call(instance, request,
856 ((PERL_INST *)instance)->func_start_accounting);
858 return rlmperl_call(instance, request,
859 ((PERL_INST *)instance)->func_accounting);
865 if (((PERL_INST *)instance)->func_stop_accounting) {
866 return rlmperl_call(instance, request,
867 ((PERL_INST *)instance)->func_stop_accounting);
869 return rlmperl_call(instance, request,
870 ((PERL_INST *)instance)->func_accounting);
874 return rlmperl_call(instance, request,
875 ((PERL_INST *)instance)->func_accounting);
880 * Check for simultaneouse-use
882 static int perl_checksimul(void *instance, REQUEST *request)
884 return rlmperl_call(instance, request,
885 ((PERL_INST *)instance)->func_checksimul);
892 static int perl_pre_proxy(void *instance, REQUEST *request)
894 return rlmperl_call(instance, request,
895 ((PERL_INST *)instance)->func_pre_proxy);
900 static int perl_post_proxy(void *instance, REQUEST *request)
902 return rlmperl_call(instance, request,
903 ((PERL_INST *)instance)->func_post_proxy);
910 static int perl_post_auth(void *instance, REQUEST *request)
912 return rlmperl_call(instance, request,
913 ((PERL_INST *)instance)->func_post_auth);
919 static int perl_recv_coa(void *instance, REQUEST *request)
921 return rlmperl_call(instance, request,
922 ((PERL_INST *)instance)->func_recv_coa);
927 static int perl_send_coa(void *instance, REQUEST *request)
929 return rlmperl_call(instance, request,
930 ((PERL_INST *)instance)->func_send_coa);
934 * Detach a instance give a chance to a module to make some internal setup ...
936 static int perl_detach(void *instance)
938 PERL_INST *inst = (PERL_INST *) instance;
939 int exitstatus = 0, count = 0;
943 * FIXME: Call this in the destruct function?
946 dTHXa(handle->clone);
947 PERL_SET_CONTEXT(handle->clone);
949 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
950 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
959 if (exitstatus >= 100 || exitstatus < 0) {
960 exitstatus = RLM_MODULE_FAIL;
970 if (inst->func_detach) {
972 PERL_SET_CONTEXT(inst->perl);
974 dSP; ENTER; SAVETMPS;
977 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
982 if (exitstatus >= 100 || exitstatus < 0) {
983 exitstatus = RLM_MODULE_FAIL;
992 xlat_unregister(inst->xlat_name, perl_xlat, instance);
993 free(inst->xlat_name);
996 rlm_perl_destruct(inst->perl);
997 pthread_mutex_destroy(&inst->clone_mutex);
999 perl_destruct(inst->perl);
1000 perl_free(inst->perl);
1010 * The module name should be the only globally exported symbol.
1011 * That is, everything else should be 'static'.
1013 * If the module needs to temporarily modify it's instantiation
1014 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1015 * The server will then take care of ensuring that the module
1016 * is single-threaded.
1018 module_t rlm_perl = {
1022 RLM_TYPE_THREAD_SAFE, /* type */
1024 RLM_TYPE_THREAD_UNSAFE,
1026 perl_instantiate, /* instantiation */
1027 perl_detach, /* detach */
1029 perl_authenticate, /* authenticate */
1030 perl_authorize, /* authorize */
1031 perl_preacct, /* preacct */
1032 perl_accounting, /* accounting */
1033 perl_checksimul, /* check simul */
1035 perl_pre_proxy, /* pre-proxy */
1036 perl_post_proxy, /* post-proxy */
1040 perl_post_auth /* post-auth */