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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2002 The FreeRADIUS server project
21 * Copyright 2002 Boian Jordanov <bjordanov@orbitel.bg>
24 #include <freeradius-devel/autoconf.h>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
32 #include <freeradius-devel/conffile.h>
46 #include <semaphore.h>
48 static const char rcsid[] = "$Id$";
53 * Pool of Perl's clones (genetically cloned) ;)
56 typedef struct pool_handle {
57 struct pool_handle *next;
58 struct pool_handle *prev;
59 enum {busy, idle} status;
60 unsigned int request_count;
61 PerlInterpreter *clone;
65 typedef struct PERL_POOL {
75 int max_request_per_clone;
79 time_t time_when_last_added;
85 * Define a structure for our module configuration.
87 * These variables do not need to be in a structure, but it's
88 * a lot cleaner to do so, and a pointer to the structure can
89 * be used as the instance handle.
91 typedef struct perl_inst {
92 /* Name of the perl module */
95 /* Name of the functions for each module method */
97 char *func_authenticate;
98 char *func_accounting;
99 char *func_start_accounting;
100 char *func_stop_accounting;
102 char *func_checksimul;
105 char *func_pre_proxy;
106 char *func_post_proxy;
107 char *func_post_auth;
110 PerlInterpreter *perl;
112 PERL_POOL *perl_pool;
116 * A mapping of configuration file names to internal variables.
118 * Note that the string is dynamically allocated, so it MUST
119 * be freed. When the configuration file parse re-reads the string,
120 * it free's the old one, and strdup's the new one, placing the pointer
121 * to the strdup'd string into 'config.string'. This gets around
124 static const CONF_PARSER module_config[] = {
125 { "module", PW_TYPE_FILENAME,
126 offsetof(PERL_INST,module), NULL, "module"},
127 { "func_authorize", PW_TYPE_STRING_PTR,
128 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
129 { "func_authenticate", PW_TYPE_STRING_PTR,
130 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
131 { "func_accounting", PW_TYPE_STRING_PTR,
132 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
133 { "func_preacct", PW_TYPE_STRING_PTR,
134 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
135 { "func_checksimul", PW_TYPE_STRING_PTR,
136 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
137 { "func_detach", PW_TYPE_STRING_PTR,
138 offsetof(PERL_INST,func_detach), NULL, "detach"},
139 { "func_xlat", PW_TYPE_STRING_PTR,
140 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
141 { "func_pre_proxy", PW_TYPE_STRING_PTR,
142 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
143 { "func_post_proxy", PW_TYPE_STRING_PTR,
144 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
145 { "func_post_auth", PW_TYPE_STRING_PTR,
146 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
147 { "perl_flags", PW_TYPE_STRING_PTR,
148 offsetof(PERL_INST,perl_flags), NULL, NULL},
149 { "func_start_accounting", PW_TYPE_STRING_PTR,
150 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
151 { "func_stop_accounting", PW_TYPE_STRING_PTR,
152 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
154 { NULL, -1, 0, NULL, NULL } /* end the list */
160 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
164 * We use one perl to clone from it i.e. main boss
165 * We clone it for every instance if we have perl
166 * with -Duseithreads compiled in
168 static PerlInterpreter *interp = NULL;
170 static const CONF_PARSER pool_conf[] = {
171 { "max_clones", PW_TYPE_INTEGER, offsetof(PERL_POOL, max_clones), NULL, "32"},
172 { "start_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, start_clones), NULL, "32"},
173 { "min_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, min_spare_clones),NULL, "0"},
174 { "max_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_spare_clones),NULL, "32"},
175 { "cleanup_delay",PW_TYPE_INTEGER, offsetof(PERL_POOL,cleanup_delay),NULL, "5"},
176 { "max_request_per_clone",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_request_per_clone),NULL, "0"},
177 { NULL, -1, 0, NULL, NULL } /* end the list */
181 #define dl_librefs "DynaLoader::dl_librefs"
182 #define dl_modules "DynaLoader::dl_modules"
183 static void rlm_perl_clear_handles(pTHX)
185 AV *librefs = get_av(dl_librefs, FALSE);
191 static void **rlm_perl_get_handles(pTHX)
194 AV *librefs = get_av(dl_librefs, FALSE);
195 AV *modules = get_av(dl_modules, FALSE);
200 "Could not get @%s for unloading.\n",
205 if (!(AvFILL(librefs) >= 0)) {
209 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
211 for (i=0; i<=AvFILL(librefs); i++) {
213 SV *handle_sv = *av_fetch(librefs, i, FALSE);
217 "Could not fetch $%s[%d]!\n",
221 handle = (void *)SvIV(handle_sv);
231 handles[i] = (void *)0;
236 static void rlm_perl_close_handles(void **handles)
244 for (i=0; handles[i]; i++) {
245 radlog(L_DBG, "close 0x%lx\n", (unsigned long)handles[i]);
252 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
254 PerlInterpreter *clone;
257 PERL_SET_CONTEXT(perl);
259 clone = 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);
275 static void rlm_perl_destruct(PerlInterpreter *perl)
277 char **orig_environ = NULL;
280 PERL_SET_CONTEXT(perl);
282 PL_perl_destruct_level = 2;
284 PL_origenviron = environ;
290 * FIXME: This shouldn't happen
293 while (PL_scopestack_ix > 1 ){
301 environ = orig_environ;
305 static void rlm_destroy_perl(PerlInterpreter *perl)
310 PERL_SET_CONTEXT(perl);
312 handles = rlm_perl_get_handles(aTHX);
313 rlm_perl_destruct(perl);
314 rlm_perl_close_handles(handles);
317 static void delete_pool_handle(POOL_HANDLE *handle, PERL_INST *inst)
326 inst->perl_pool->head = next;
332 inst->perl_pool->tail = prev;
336 inst->perl_pool->current_clones--;
337 MUTEX_DESTROY(&handle->lock);
341 static void move2tail(POOL_HANDLE *handle, PERL_INST *inst)
346 if (inst->perl_pool->head == NULL) {
350 inst->perl_pool->head = handle;
351 inst->perl_pool->tail = handle;
355 if (inst->perl_pool->tail == handle) {
362 if ((next != NULL) ||
369 inst->perl_pool->head = next;
380 prev = inst->perl_pool->tail;
382 inst->perl_pool->tail = handle;
388 static POOL_HANDLE *pool_grow (PERL_INST *inst) {
392 if (inst->perl_pool->max_clones == inst->perl_pool->current_clones) {
395 if (inst->perl_pool->detach == yes ) {
399 handle = (POOL_HANDLE *)rad_malloc(sizeof(POOL_HANDLE));
402 radlog(L_ERR,"Could not find free memory for pool. Aborting");
408 handle->status = idle;
409 handle->clone = rlm_perl_clone(inst->perl);
410 handle->request_count = 0;
411 MUTEX_INIT(&handle->lock);
412 inst->perl_pool->current_clones++;
413 move2tail(handle, inst);
416 inst->perl_pool->time_when_last_added = now;
421 static POOL_HANDLE *pool_pop(PERL_INST *inst)
427 * Lock the pool and be fast other thread maybe
428 * waiting for us to finish
430 MUTEX_LOCK(&inst->perl_pool->mutex);
434 for (handle = inst->perl_pool->head; handle ; handle = tmp) {
437 if (handle->status == idle){
444 if (inst->perl_pool->current_clones < inst->perl_pool->max_clones ) {
446 found = pool_grow(inst);
449 radlog(L_ERR,"Cannot grow pool returning");
450 MUTEX_UNLOCK(&inst->perl_pool->mutex);
454 radlog(L_ERR,"rlm_perl:: reached maximum clones %d cannot grow",
455 inst->perl_pool->current_clones);
456 MUTEX_UNLOCK(&inst->perl_pool->mutex);
461 move2tail(found, inst);
462 found->status = busy;
463 MUTEX_LOCK(&found->lock);
464 inst->perl_pool->active_clones++;
465 found->request_count++;
469 MUTEX_UNLOCK(&inst->perl_pool->mutex);
470 radlog(L_DBG,"perl_pool: item 0x%lx asigned new request. Handled so far: %d",
471 (unsigned long) found->clone, found->request_count);
474 static int pool_release(POOL_HANDLE *handle, PERL_INST *inst) {
476 POOL_HANDLE *tmp, *tmp2;
482 MUTEX_LOCK(&inst->perl_pool->mutex);
485 * If detach is set then just release the mutex
487 if (inst->perl_pool->detach == yes ) {
488 handle->status = idle;
489 MUTEX_UNLOCK(&handle->lock);
490 MUTEX_UNLOCK(&inst->perl_pool->mutex);
494 MUTEX_UNLOCK(&handle->lock);
495 handle->status = idle;
496 inst->perl_pool->active_clones--;
498 spare = inst->perl_pool->current_clones - inst->perl_pool->active_clones;
500 radlog(L_DBG,"perl_pool total/active/spare [%d/%d/%d]"
501 , inst->perl_pool->current_clones, inst->perl_pool->active_clones, spare);
503 if (spare < inst->perl_pool->min_spare_clones) {
504 t = inst->perl_pool->min_spare_clones - spare;
506 if ((tmp = pool_grow(inst)) == NULL) {
507 MUTEX_UNLOCK(&inst->perl_pool->mutex);
511 MUTEX_UNLOCK(&inst->perl_pool->mutex);
515 if ((now - inst->perl_pool->time_when_last_added) < inst->perl_pool->cleanup_delay) {
516 MUTEX_UNLOCK(&inst->perl_pool->mutex);
519 if (spare > inst->perl_pool->max_spare_clones) {
520 spare -= inst->perl_pool->max_spare_clones;
521 for (tmp = inst->perl_pool->head; (tmp !=NULL ) && (spare > 0) ; tmp = tmp2) {
524 if(tmp->status == idle) {
525 rlm_destroy_perl(tmp->clone);
526 delete_pool_handle(tmp,inst);
533 * If the clone have reached max_request_per_clone clean it.
535 if (inst->perl_pool->max_request_per_clone > 0 ) {
536 if (handle->request_count > inst->perl_pool->max_request_per_clone) {
537 rlm_destroy_perl(handle->clone);
538 delete_pool_handle(handle,inst);
544 MUTEX_UNLOCK(&inst->perl_pool->mutex);
547 static int init_pool (CONF_SECTION *conf, PERL_INST *inst) {
553 pool = rad_malloc(sizeof(PERL_POOL));
554 memset(pool,0,sizeof(PERL_POOL));
556 inst->perl_pool = pool;
558 MUTEX_INIT(&pool->mutex);
565 cf_section_parse(conf,pool,pool_conf);
566 inst->perl_pool = pool;
567 inst->perl_pool->detach = no;
569 for(t = 0;t < inst->perl_pool->start_clones ;t++){
570 if ((handle = pool_grow(inst)) == NULL) {
580 * Do any per-module initialization. e.g. set up connections
581 * to external databases, read configuration files, set up
582 * dictionary entries, etc.
584 * Try to avoid putting too much stuff in here - it's better to
585 * do it in instantiate() where it is not global.
586 * I use one global interpetator to make things more fastest for
587 * Threading env I clone new perl from this interp.
589 static int perl_init(void)
595 static void xs_init(pTHX)
597 char *file = __FILE__;
599 /* DynaLoader is a special case */
600 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
605 * This is wrapper for radlog
606 * Now users can call radiusd::radlog(level,msg) wich is the same
607 * calling radlog from C code.
610 static XS(XS_radiusd_radlog)
614 croak("Usage: radiusd::radlog(level, message)");
619 level = (int) SvIV(ST(0));
620 msg = (char *) SvPV(ST(1), PL_na);
623 * Because 'msg' is a 'char *', we don't want '%s', etc.
624 * in it to give us printf-style vulnerabilities.
626 radlog(level, "rlm_perl: %s", msg);
634 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
635 int freespace, RADIUS_ESCAPE_STRING func)
638 PERL_INST *inst= (PERL_INST *) instance;
639 PerlInterpreter *perl;
640 char params[1024], *ptr, *tmp;
649 if ((handle = pool_pop(instance)) == NULL) {
653 perl = handle->clone;
655 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
660 PERL_SET_CONTEXT(perl);
666 * Do an xlat on the provided string (nice recursive operation).
668 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
669 radlog(L_ERR, "rlm_perl: xlat failed.");
672 ptr = strtok(params, " ");
676 while (ptr != NULL) {
677 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
678 ptr = strtok(NULL, " ");
683 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
687 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
695 strncpy(out,tmp,ret);
697 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
707 pool_release(handle, instance);
712 * Do any per-module initialization that is separate to each
713 * configured instance of the module. e.g. set up connections
714 * to external databases, read configuration files, set up
715 * dictionary entries, etc.
717 * If configuration information is given in the config section
718 * that must be referenced in later calls, store a handle to it
719 * in *instance otherwise put a null pointer there.
722 * Setup a hashes wich we will use later
723 * parse a module and give him a chance to live
726 static int perl_instantiate(CONF_SECTION *conf, void **instance)
728 PERL_INST *inst = (PERL_INST *) instance;
732 HV *rad_request_proxy_hv;
733 HV *rad_request_proxy_reply_hv;
736 char *embed[4], *xlat_name;
737 int exitstatus = 0, argc=0;
740 * Set up a storage area for instance data
742 inst = rad_malloc(sizeof(PERL_INST));
743 memset(inst, 0, sizeof(PERL_INST));
746 * If the configuration parameters can't be parsed, then
749 if (cf_section_parse(conf, inst, module_config) < 0) {
756 if (inst->perl_flags) {
757 embed[1] = inst->perl_flags;
758 embed[2] = inst->module;
762 embed[1] = inst->module;
769 if ((interp = perl_alloc()) == NULL) {
770 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
774 perl_construct(interp);
775 PL_perl_destruct_level = 2;
778 inst->perl = perl_clone(interp ,CLONEf_KEEP_PTR_TABLE);
782 PERL_SET_CONTEXT(inst->perl);
784 if ((inst->perl = perl_alloc()) == NULL) {
785 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
789 perl_construct(inst->perl);
792 #if PERL_REVISION >= 5 && PERL_VERSION >=8
793 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
796 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
802 exitstatus = perl_run(inst->perl);
804 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
810 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
812 rad_reply_hv = newHV();
813 rad_check_hv = newHV();
814 rad_request_hv = newHV();
815 rad_request_proxy_hv = newHV();
816 rad_request_proxy_reply_hv = newHV();
818 rad_reply_hv = get_hv("RAD_REPLY",1);
819 rad_check_hv = get_hv("RAD_CHECK",1);
820 rad_request_hv = get_hv("RAD_REQUEST",1);
821 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
822 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
824 xlat_name = cf_section_name2(conf);
825 if (xlat_name == NULL)
826 xlat_name = cf_section_name1(conf);
828 inst->xlat_name = strdup(xlat_name);
829 xlat_register(xlat_name, perl_xlat, inst);
833 if ((init_pool(conf, inst)) == -1) {
834 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
845 * get the vps and put them in perl hash
846 * If one VP have multiple values it is added as array_ref
847 * Example for this is Cisco-AVPair that holds multiple values.
848 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
850 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
852 VALUE_PAIR *nvp, *vpa, *vpn;
860 while (nvp != NULL) {
861 attr = nvp->attribute;
862 vpa = paircopy2(nvp,attr);
867 len = vp_prints_value(buffer, sizeof(buffer),
869 av_push(av, newSVpv(buffer, len));
872 hv_store(rad_hv, nvp->name, strlen(nvp->name),
873 newRV_noinc((SV *) av), 0);
875 len = vp_prints_value(buffer, sizeof(buffer),
877 hv_store(rad_hv, vpa->name, strlen(vpa->name),
878 newSVpv(buffer, len), 0);
882 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
884 pairdelete(&nvp, attr);
891 * Verify that a Perl SV is a string and save it in FreeRadius
895 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv) {
900 val = SvPV_nolen(sv);
901 vpp = pairmake(key, val, T_OP_EQ);
905 "rlm_perl: Added pair %s = %s", key, val);
909 "rlm_perl: ERROR: Failed to create pair %s = %s",
918 * Gets the content from hashes
920 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
925 I32 key_len, len, i, j;
928 for (i = hv_iterinit(my_hv); i > 0; i--) {
929 res_sv = hv_iternextsv(my_hv,&key,&key_len);
930 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
931 av = (AV*)SvRV(res_sv);
933 for (j = 0; j <= len; j++) {
934 av_sv = av_fetch(av, j, 0);
935 ret = pairadd_sv(vp, key, *av_sv) + ret;
937 } else ret = pairadd_sv(vp, key, res_sv) + ret;
944 * Call the function_name inside the module
945 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
948 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
951 PERL_INST *inst = instance;
953 int exitstatus=0, count;
959 HV *rad_request_proxy_hv;
960 HV *rad_request_proxy_reply_hv;
965 if ((handle = pool_pop(instance)) == NULL) {
966 return RLM_MODULE_FAIL;
969 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
971 dTHXa(handle->clone);
972 PERL_SET_CONTEXT(handle->clone);
975 PERL_SET_CONTEXT(inst->perl);
976 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
986 * Radius has told us to call this function, but none
989 if (!function_name) {
990 return RLM_MODULE_FAIL;
993 rad_reply_hv = get_hv("RAD_REPLY",1);
994 rad_check_hv = get_hv("RAD_CHECK",1);
995 rad_request_hv = get_hv("RAD_REQUEST",1);
996 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
997 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
1000 perl_store_vps(request->reply->vps, rad_reply_hv);
1001 perl_store_vps(request->config_items, rad_check_hv);
1002 perl_store_vps(request->packet->vps, rad_request_hv);
1004 if (request->proxy != NULL) {
1005 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
1007 hv_undef(rad_request_proxy_hv);
1010 if (request->proxy_reply !=NULL) {
1011 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1013 hv_undef(rad_request_proxy_reply_hv);
1021 * This way %RAD_xx can be pushed onto stack as sub parameters.
1022 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1023 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1024 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1028 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1034 if (exitstatus >= 100 || exitstatus < 0) {
1035 exitstatus = RLM_MODULE_FAIL;
1043 if (SvTRUE(ERRSV)) {
1044 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1046 function_name, SvPV(ERRSV,n_a));
1049 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1050 pairmove(&request->reply->vps, &vp);
1054 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1055 pairmove(&request->config_items, &vp);
1059 if ((get_hv_content(rad_request_proxy_reply_hv, &vp)) > 0 && request->proxy_reply != NULL) {
1060 pairfree(&request->proxy_reply->vps);
1061 pairmove(&request->proxy_reply->vps, &vp);
1066 pool_release(handle,instance);
1067 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1074 * Find the named user in this modules database. Create the set
1075 * of attribute-value pairs to check and reply with for this user
1076 * from the database. The authentication code only needs to check
1077 * the password, the rest is done here.
1079 static int perl_authorize(void *instance, REQUEST *request)
1081 return rlmperl_call(instance, request,
1082 ((PERL_INST *)instance)->func_authorize);
1086 * Authenticate the user with the given password.
1088 static int perl_authenticate(void *instance, REQUEST *request)
1090 return rlmperl_call(instance, request,
1091 ((PERL_INST *)instance)->func_authenticate);
1094 * Massage the request before recording it or proxying it
1096 static int perl_preacct(void *instance, REQUEST *request)
1098 return rlmperl_call(instance, request,
1099 ((PERL_INST *)instance)->func_preacct);
1102 * Write accounting information to this modules database.
1104 static int perl_accounting(void *instance, REQUEST *request)
1107 int acctstatustype=0;
1109 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1110 acctstatustype = pair->lvalue;
1112 radlog(L_ERR, "Invalid Accounting Packet");
1113 return RLM_MODULE_INVALID;
1116 switch (acctstatustype) {
1118 case PW_STATUS_START:
1120 if (((PERL_INST *)instance)->func_start_accounting) {
1121 return rlmperl_call(instance, request,
1122 ((PERL_INST *)instance)->func_start_accounting);
1124 return rlmperl_call(instance, request,
1125 ((PERL_INST *)instance)->func_accounting);
1129 case PW_STATUS_STOP:
1131 if (((PERL_INST *)instance)->func_stop_accounting) {
1132 return rlmperl_call(instance, request,
1133 ((PERL_INST *)instance)->func_stop_accounting);
1135 return rlmperl_call(instance, request,
1136 ((PERL_INST *)instance)->func_accounting);
1140 return rlmperl_call(instance, request,
1141 ((PERL_INST *)instance)->func_accounting);
1146 * Check for simultaneouse-use
1148 static int perl_checksimul(void *instance, REQUEST *request)
1150 return rlmperl_call(instance, request,
1151 ((PERL_INST *)instance)->func_checksimul);
1156 static int perl_pre_proxy(void *instance, REQUEST *request)
1158 return rlmperl_call(instance, request,
1159 ((PERL_INST *)instance)->func_pre_proxy);
1162 * Post-Proxy request
1164 static int perl_post_proxy(void *instance, REQUEST *request)
1166 return rlmperl_call(instance, request,
1167 ((PERL_INST *)instance)->func_post_proxy);
1172 static int perl_post_auth(void *instance, REQUEST *request)
1174 return rlmperl_call(instance, request,
1175 ((PERL_INST *)instance)->func_post_auth);
1178 * Detach a instance give a chance to a module to make some internal setup ...
1180 static int perl_detach(void *instance)
1182 PERL_INST *inst = (PERL_INST *) instance;
1183 int exitstatus = 0, count = 0;
1186 POOL_HANDLE *handle, *tmp, *tmp2;
1188 MUTEX_LOCK(&inst->perl_pool->mutex);
1189 inst->perl_pool->detach = yes;
1190 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1192 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1194 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1196 * Wait until clone becomes idle
1198 MUTEX_LOCK(&handle->lock);
1201 * Give a clones chance to run detach function
1204 dTHXa(handle->clone);
1205 PERL_SET_CONTEXT(handle->clone);
1207 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1208 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1214 * FIXME: bug in perl
1217 if (exitstatus >= 100 || exitstatus < 0) {
1218 exitstatus = RLM_MODULE_FAIL;
1224 radlog(L_DBG,"detach at 0x%lx returned status %d",
1225 (unsigned long) handle->clone, exitstatus);
1228 MUTEX_UNLOCK(&handle->lock);
1234 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1236 radlog(L_DBG,"rlm_perl:: Destroy perl");
1237 rlm_perl_destruct(tmp->clone);
1238 delete_pool_handle(tmp,inst);
1243 #endif /* USE_ITHREADS */
1244 PERL_SET_CONTEXT(inst->perl);
1246 dSP; ENTER; SAVETMPS;
1249 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1254 if (exitstatus >= 100 || exitstatus < 0) {
1255 exitstatus = RLM_MODULE_FAIL;
1266 xlat_unregister(inst->xlat_name, perl_xlat);
1267 free(inst->xlat_name);
1269 if (inst->func_authorize) free(inst->func_authorize);
1270 if (inst->func_authenticate) free(inst->func_authenticate);
1271 if (inst->func_accounting) free(inst->func_accounting);
1272 if (inst->func_preacct) free(inst->func_preacct);
1273 if (inst->func_checksimul) free(inst->func_checksimul);
1274 if (inst->func_pre_proxy) free(inst->func_pre_proxy);
1275 if (inst->func_post_proxy) free(inst->func_post_proxy);
1276 if (inst->func_post_auth) free(inst->func_post_auth);
1277 if (inst->func_detach) free(inst->func_detach);
1280 free(inst->perl_pool->head);
1281 free(inst->perl_pool->tail);
1282 MUTEX_DESTROY(&inst->perl_pool->mutex);
1283 free(inst->perl_pool);
1284 rlm_perl_destruct(inst->perl);
1286 perl_destruct(inst->perl);
1287 perl_free(inst->perl);
1294 * The module name should be the only globally exported symbol.
1295 * That is, everything else should be 'static'.
1297 * If the module needs to temporarily modify it's instantiation
1298 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1299 * The server will then take care of ensuring that the module
1300 * is single-threaded.
1302 module_t rlm_perl = {
1306 RLM_TYPE_THREAD_SAFE, /* type */
1308 RLM_TYPE_THREAD_UNSAFE,
1310 perl_instantiate, /* instantiation */
1311 perl_detach, /* detach */
1313 perl_authenticate, /* authenticate */
1314 perl_authorize, /* authorize */
1315 perl_preacct, /* preacct */
1316 perl_accounting, /* accounting */
1317 perl_checksimul, /* check simul */
1318 perl_pre_proxy, /* pre-proxy */
1319 perl_post_proxy, /* post-proxy */
1320 perl_post_auth /* post-auth */