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>
41 #include <semaphore.h>
44 extern char **environ;
48 * Define a structure for our module configuration.
50 * These variables do not need to be in a structure, but it's
51 * a lot cleaner to do so, and a pointer to the structure can
52 * be used as the instance handle.
54 typedef struct perl_inst {
55 /* Name of the perl module */
58 /* Name of the functions for each module method */
60 char *func_authenticate;
61 char *func_accounting;
62 char *func_start_accounting;
63 char *func_stop_accounting;
65 char *func_checksimul;
70 char *func_post_proxy;
79 PerlInterpreter *perl;
80 pthread_key_t *thread_key;
82 pthread_mutex_t clone_mutex;
85 * A mapping of configuration file names to internal variables.
87 * Note that the string is dynamically allocated, so it MUST
88 * be freed. When the configuration file parse re-reads the string,
89 * it free's the old one, and strdup's the new one, placing the pointer
90 * to the strdup'd string into 'config.string'. This gets around
93 static const CONF_PARSER module_config[] = {
94 { "module", PW_TYPE_FILENAME,
95 offsetof(PERL_INST,module), NULL, "module"},
96 { "func_authorize", PW_TYPE_STRING_PTR,
97 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
98 { "func_authenticate", PW_TYPE_STRING_PTR,
99 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
100 { "func_accounting", PW_TYPE_STRING_PTR,
101 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
102 { "func_preacct", PW_TYPE_STRING_PTR,
103 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
104 { "func_checksimul", PW_TYPE_STRING_PTR,
105 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
106 { "func_detach", PW_TYPE_STRING_PTR,
107 offsetof(PERL_INST,func_detach), NULL, "detach"},
108 { "func_xlat", PW_TYPE_STRING_PTR,
109 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
111 { "func_pre_proxy", PW_TYPE_STRING_PTR,
112 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
113 { "func_post_proxy", PW_TYPE_STRING_PTR,
114 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
116 { "func_post_auth", PW_TYPE_STRING_PTR,
117 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
119 { "func_recv_coa", PW_TYPE_STRING_PTR,
120 offsetof(PERL_INST,func_recv_coa), NULL, "recv_coa"},
121 { "func_send_coa", PW_TYPE_STRING_PTR,
122 offsetof(PERL_INST,func_send_coa), NULL, "send_coa"},
124 { "perl_flags", PW_TYPE_STRING_PTR,
125 offsetof(PERL_INST,perl_flags), NULL, NULL},
126 { "func_start_accounting", PW_TYPE_STRING_PTR,
127 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
128 { "func_stop_accounting", PW_TYPE_STRING_PTR,
129 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
131 { NULL, -1, 0, NULL, NULL } /* end the list */
137 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
140 #define dl_librefs "DynaLoader::dl_librefs"
141 #define dl_modules "DynaLoader::dl_modules"
142 static void rlm_perl_clear_handles(pTHX)
144 AV *librefs = get_av(dl_librefs, FALSE);
150 static void **rlm_perl_get_handles(pTHX)
153 AV *librefs = get_av(dl_librefs, FALSE);
154 AV *modules = get_av(dl_modules, FALSE);
157 if (!librefs) return NULL;
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 if (handles) rlm_perl_close_handles(handles);
240 rlm_perl_destruct(perl);
244 static void rlm_perl_make_key(pthread_key_t *key)
246 pthread_key_create(key, (void*)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 const 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, const char *fmt,
318 char *out, size_t freespace)
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, NULL, NULL)) {
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 %zu , out is %s freespace is %zu",
373 ret, out, freespace);
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;
406 HV *rad_request_proxy_hv;
407 HV *rad_request_proxy_reply_hv;
413 const char *xlat_name;
414 int exitstatus = 0, argc=0;
416 embed = rad_malloc(4 * sizeof(char *));
417 memset(embed, 0, 4 *sizeof(char *));
419 * Set up a storage area for instance data
421 inst = rad_malloc(sizeof(PERL_INST));
422 memset(inst, 0, sizeof(PERL_INST));
425 * If the configuration parameters can't be parsed, then
428 if (cf_section_parse(conf, inst, module_config) < 0) {
435 * Create pthread key. This key will be stored in instance
439 pthread_mutex_init(&inst->clone_mutex, NULL);
441 inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
442 memset(inst->thread_key,0,sizeof(*inst->thread_key));
444 rlm_perl_make_key(inst->thread_key);
450 if (inst->perl_flags) {
451 embed[1] = inst->perl_flags;
452 embed[2] = inst->module;
456 embed[1] = inst->module;
461 PERL_SYS_INIT3(&argc, &embed, &envp);
463 if ((inst->perl = perl_alloc()) == NULL) {
464 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
470 perl_construct(inst->perl);
471 PL_perl_destruct_level = 2;
476 PERL_SET_CONTEXT(inst->perl);
478 if ((inst->perl = perl_alloc()) == NULL) {
479 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
485 perl_construct(inst->perl);
488 #if PERL_REVISION >= 5 && PERL_VERSION >=8
489 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
492 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
497 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl");
500 exitstatus = perl_run(inst->perl);
502 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
510 rad_reply_hv = newHV();
511 rad_check_hv = newHV();
512 rad_config_hv = newHV();
513 rad_request_hv = newHV();
515 rad_request_proxy_hv = newHV();
516 rad_request_proxy_reply_hv = newHV();
519 rad_reply_hv = get_hv("RAD_REPLY",1);
520 rad_check_hv = get_hv("RAD_CHECK",1);
521 rad_config_hv = get_hv("RAD_CONFIG",1);
522 rad_request_hv = get_hv("RAD_REQUEST",1);
524 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
525 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
528 xlat_name = cf_section_name2(conf);
529 if (xlat_name == NULL)
530 xlat_name = cf_section_name1(conf);
532 inst->xlat_name = strdup(xlat_name);
533 xlat_register(xlat_name, perl_xlat, inst);
542 * get the vps and put them in perl hash
543 * If one VP have multiple values it is added as array_ref
544 * Example for this is Cisco-AVPair that holds multiple values.
545 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
547 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
549 VALUE_PAIR *nvp, *vpa, *vpn;
559 * Copy the valuepair list so we can remove attributes we've
564 while (nvp != NULL) {
566 * Tagged attributes are added to the hash with name
567 * <attribute>:<tag>, others just use the normal attribute
570 if (nvp->flags.has_tag && (nvp->flags.tag != 0)) {
571 snprintf(namebuf, sizeof(namebuf), "%s:%d",
572 nvp->name, nvp->flags.tag);
579 * Create a new list with all the attributes like this one
580 * which are in the same tag group.
582 vpa = paircopy2(nvp, nvp->attribute, nvp->vendor, nvp->flags.tag);
585 * Attribute has multiple values
589 for (vpn = vpa; vpn; vpn = vpn->next) {
590 len = vp_prints_value(buffer, sizeof(buffer), vpn, FALSE);
591 av_push(av, newSVpv(buffer, len));
593 (void)hv_store(rad_hv, name, strlen(name), newRV_noinc((SV *)av), 0);
596 * Attribute has a single value, so its value just gets
600 len = vp_prints_value(buffer, sizeof(buffer), vpa, FALSE);
601 (void)hv_store(rad_hv, name, strlen(name), newSVpv(buffer, len), 0);
607 * Find the next attribute which we won't have processed,
608 * we need to do this so we know it won't be freed on
613 while ((vpa != NULL) &&
614 (vpa->attribute == nvp->attribute) &&
615 (vpa->vendor == nvp->vendor) &&
616 (vpa->flags.tag == nvp->flags.tag)) {
621 * Finally remove all the VPs we processed from our copy
624 pairdelete(&nvp, nvp->attribute, nvp->vendor, nvp->flags.tag);
632 * Verify that a Perl SV is a string and save it in FreeRadius
636 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, FR_TOKEN op) {
641 val = SvPV_nolen(sv);
642 vpp = pairmake(key, val, op);
646 "rlm_perl: Added pair %s = %s", key, val);
650 "rlm_perl: ERROR: Failed to create pair %s = %s",
659 * Gets the content from hashes
661 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
666 I32 key_len, len, i, j;
670 for (i = hv_iterinit(my_hv); i > 0; i--) {
671 res_sv = hv_iternextsv(my_hv,&key,&key_len);
672 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
673 av = (AV*)SvRV(res_sv);
675 for (j = 0; j <= len; j++) {
676 av_sv = av_fetch(av, j, 0);
677 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
679 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
686 * Call the function_name inside the module
687 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
690 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
693 PERL_INST *inst = instance;
695 int exitstatus=0, count;
703 HV *rad_request_proxy_hv;
704 HV *rad_request_proxy_reply_hv;
708 pthread_mutex_lock(&inst->clone_mutex);
710 PerlInterpreter *interp;
712 interp = rlm_perl_clone(inst->perl,inst->thread_key);
715 PERL_SET_CONTEXT(interp);
718 pthread_mutex_unlock(&inst->clone_mutex);
720 PERL_SET_CONTEXT(inst->perl);
731 * Radius has told us to call this function, but none
734 if (!function_name) {
735 return RLM_MODULE_FAIL;
738 rad_reply_hv = get_hv("RAD_REPLY",1);
739 rad_check_hv = get_hv("RAD_CHECK",1);
740 rad_config_hv = get_hv("RAD_CONFIG",1);
741 rad_request_hv = get_hv("RAD_REQUEST",1);
743 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
744 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
747 perl_store_vps(request->reply->vps, rad_reply_hv);
748 perl_store_vps(request->config_items, rad_check_hv);
749 perl_store_vps(request->packet->vps, rad_request_hv);
750 perl_store_vps(request->config_items, rad_config_hv);
753 if (request->proxy != NULL) {
754 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
756 hv_undef(rad_request_proxy_hv);
759 if (request->proxy_reply !=NULL) {
760 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
762 hv_undef(rad_request_proxy_reply_hv);
768 * This way %RAD_xx can be pushed onto stack as sub parameters.
769 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
770 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
771 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
775 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
780 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
782 function_name, SvPV(ERRSV,n_a));
788 if (exitstatus >= 100 || exitstatus < 0) {
789 exitstatus = RLM_MODULE_FAIL;
799 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
800 pairfree(&request->packet->vps);
801 request->packet->vps = vp;
805 * Update cached copies
807 request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
808 request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
809 if (!request->password)
810 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
813 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
814 pairfree(&request->reply->vps);
815 request->reply->vps = vp;
819 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
820 pairfree(&request->config_items);
821 request->config_items = vp;
826 if (request->proxy &&
827 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
828 pairfree(&request->proxy->vps);
829 request->proxy->vps = vp;
833 if (request->proxy_reply &&
834 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
835 pairfree(&request->proxy_reply->vps);
836 request->proxy_reply->vps = vp;
846 * Find the named user in this modules database. Create the set
847 * of attribute-value pairs to check and reply with for this user
848 * from the database. The authentication code only needs to check
849 * the password, the rest is done here.
851 static rlm_rcode_t perl_authorize(void *instance, REQUEST *request)
853 return rlmperl_call(instance, request,
854 ((PERL_INST *)instance)->func_authorize);
858 * Authenticate the user with the given password.
860 static rlm_rcode_t perl_authenticate(void *instance, REQUEST *request)
862 return rlmperl_call(instance, request,
863 ((PERL_INST *)instance)->func_authenticate);
866 * Massage the request before recording it or proxying it
868 static rlm_rcode_t perl_preacct(void *instance, REQUEST *request)
870 return rlmperl_call(instance, request,
871 ((PERL_INST *)instance)->func_preacct);
874 * Write accounting information to this modules database.
876 static rlm_rcode_t perl_accounting(void *instance, REQUEST *request)
879 int acctstatustype=0;
881 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0, TAG_ANY)) != NULL) {
882 acctstatustype = pair->vp_integer;
884 radlog(L_ERR, "Invalid Accounting Packet");
885 return RLM_MODULE_INVALID;
888 switch (acctstatustype) {
890 case PW_STATUS_START:
892 if (((PERL_INST *)instance)->func_start_accounting) {
893 return rlmperl_call(instance, request,
894 ((PERL_INST *)instance)->func_start_accounting);
896 return rlmperl_call(instance, request,
897 ((PERL_INST *)instance)->func_accounting);
903 if (((PERL_INST *)instance)->func_stop_accounting) {
904 return rlmperl_call(instance, request,
905 ((PERL_INST *)instance)->func_stop_accounting);
907 return rlmperl_call(instance, request,
908 ((PERL_INST *)instance)->func_accounting);
912 return rlmperl_call(instance, request,
913 ((PERL_INST *)instance)->func_accounting);
918 * Check for simultaneouse-use
920 static rlm_rcode_t perl_checksimul(void *instance, REQUEST *request)
922 return rlmperl_call(instance, request,
923 ((PERL_INST *)instance)->func_checksimul);
930 static rlm_rcode_t perl_pre_proxy(void *instance, REQUEST *request)
932 return rlmperl_call(instance, request,
933 ((PERL_INST *)instance)->func_pre_proxy);
938 static rlm_rcode_t perl_post_proxy(void *instance, REQUEST *request)
940 return rlmperl_call(instance, request,
941 ((PERL_INST *)instance)->func_post_proxy);
948 static rlm_rcode_t perl_post_auth(void *instance, REQUEST *request)
950 return rlmperl_call(instance, request,
951 ((PERL_INST *)instance)->func_post_auth);
957 static rlm_rcode_t perl_recv_coa(void *instance, REQUEST *request)
959 return rlmperl_call(instance, request,
960 ((PERL_INST *)instance)->func_recv_coa);
965 static rlm_rcode_t perl_send_coa(void *instance, REQUEST *request)
967 return rlmperl_call(instance, request,
968 ((PERL_INST *)instance)->func_send_coa);
972 * Detach a instance give a chance to a module to make some internal setup ...
974 static int perl_detach(void *instance)
976 PERL_INST *inst = (PERL_INST *) instance;
977 int exitstatus = 0, count = 0;
981 * FIXME: Call this in the destruct function?
984 dTHXa(handle->clone);
985 PERL_SET_CONTEXT(handle->clone);
987 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
988 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
997 if (exitstatus >= 100 || exitstatus < 0) {
998 exitstatus = RLM_MODULE_FAIL;
1008 if (inst->func_detach) {
1010 PERL_SET_CONTEXT(inst->perl);
1012 dSP; ENTER; SAVETMPS;
1015 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1020 if (exitstatus >= 100 || exitstatus < 0) {
1021 exitstatus = RLM_MODULE_FAIL;
1030 xlat_unregister(inst->xlat_name, perl_xlat, instance);
1031 free(inst->xlat_name);
1034 rlm_perl_destruct(inst->perl);
1035 pthread_mutex_destroy(&inst->clone_mutex);
1037 perl_destruct(inst->perl);
1038 perl_free(inst->perl);
1048 * The module name should be the only globally exported symbol.
1049 * That is, everything else should be 'static'.
1051 * If the module needs to temporarily modify it's instantiation
1052 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1053 * The server will then take care of ensuring that the module
1054 * is single-threaded.
1056 module_t rlm_perl = {
1060 RLM_TYPE_THREAD_SAFE, /* type */
1062 RLM_TYPE_THREAD_UNSAFE,
1064 perl_instantiate, /* instantiation */
1065 perl_detach, /* detach */
1067 perl_authenticate, /* authenticate */
1068 perl_authorize, /* authorize */
1069 perl_preacct, /* preacct */
1070 perl_accounting, /* accounting */
1071 perl_checksimul, /* check simul */
1073 perl_pre_proxy, /* pre-proxy */
1074 perl_post_proxy, /* post-proxy */
1078 perl_post_auth /* post-auth */