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>
25 #include "libradius.h"
47 #include <semaphore.h>
49 static const char rcsid[] = "$Id$";
54 * Pool of Perl's clones (genetically cloned) ;)
57 typedef struct pool_handle {
58 struct pool_handle *next;
59 struct pool_handle *prev;
60 enum {busy, idle} status;
61 unsigned int request_count;
62 PerlInterpreter *clone;
66 typedef struct PERL_POOL {
76 int max_request_per_clone;
80 time_t time_when_last_added;
86 * Define a structure for our module configuration.
88 * These variables do not need to be in a structure, but it's
89 * a lot cleaner to do so, and a pointer to the structure can
90 * be used as the instance handle.
92 typedef struct perl_inst {
93 /* Name of the perl module */
96 /* Name of the functions for each module method */
98 char *func_authenticate;
99 char *func_accounting;
100 char *func_start_accounting;
101 char *func_stop_accounting;
103 char *func_checksimul;
106 char *func_pre_proxy;
107 char *func_post_proxy;
108 char *func_post_auth;
111 PerlInterpreter *perl;
113 PERL_POOL *perl_pool;
117 * A mapping of configuration file names to internal variables.
119 * Note that the string is dynamically allocated, so it MUST
120 * be freed. When the configuration file parse re-reads the string,
121 * it free's the old one, and strdup's the new one, placing the pointer
122 * to the strdup'd string into 'config.string'. This gets around
125 static CONF_PARSER module_config[] = {
126 { "module", PW_TYPE_STRING_PTR,
127 offsetof(PERL_INST,module), NULL, "module"},
128 { "func_authorize", PW_TYPE_STRING_PTR,
129 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
130 { "func_authenticate", PW_TYPE_STRING_PTR,
131 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
132 { "func_accounting", PW_TYPE_STRING_PTR,
133 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
134 { "func_preacct", PW_TYPE_STRING_PTR,
135 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
136 { "func_checksimul", PW_TYPE_STRING_PTR,
137 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
138 { "func_detach", PW_TYPE_STRING_PTR,
139 offsetof(PERL_INST,func_detach), NULL, "detach"},
140 { "func_xlat", PW_TYPE_STRING_PTR,
141 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
142 { "func_pre_proxy", PW_TYPE_STRING_PTR,
143 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
144 { "func_post_proxy", PW_TYPE_STRING_PTR,
145 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
146 { "func_post_auth", PW_TYPE_STRING_PTR,
147 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
148 { "perl_flags", PW_TYPE_STRING_PTR,
149 offsetof(PERL_INST,perl_flags), NULL, NULL},
150 { "func_start_accounting", PW_TYPE_STRING_PTR,
151 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
152 { "func_stop_accounting", PW_TYPE_STRING_PTR,
153 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
155 { NULL, -1, 0, NULL, NULL } /* end the list */
161 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
165 * We use one perl to clone from it i.e. main boss
166 * We clone it for every instance if we have perl
167 * with -Duseithreads compiled in
169 static PerlInterpreter *interp;
171 static const CONF_PARSER pool_conf[] = {
172 { "max_clones", PW_TYPE_INTEGER, offsetof(PERL_POOL, max_clones), NULL, "32"},
173 { "start_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, start_clones), NULL, "5"},
174 { "min_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, min_spare_clones),NULL, "3"},
175 { "max_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_spare_clones),NULL, "3"},
176 { "cleanup_delay",PW_TYPE_INTEGER, offsetof(PERL_POOL,cleanup_delay),NULL, "5"},
177 { "max_request_per_clone",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_request_per_clone),NULL, "0"},
178 { NULL, -1, 0, NULL, NULL } /* end the list */
182 #define dl_librefs "DynaLoader::dl_librefs"
183 #define dl_modules "DynaLoader::dl_modules"
184 static void rlm_perl_clear_handles(pTHX)
186 AV *librefs = get_av(dl_librefs, FALSE);
192 static void **rlm_perl_get_handles(pTHX)
195 AV *librefs = get_av(dl_librefs, FALSE);
196 AV *modules = get_av(dl_modules, FALSE);
201 "Could not get @%s for unloading.\n",
206 if (!(AvFILL(librefs) >= 0)) {
210 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
212 for (i=0; i<=AvFILL(librefs); i++) {
214 SV *handle_sv = *av_fetch(librefs, i, FALSE);
218 "Could not fetch $%s[%d]!\n",
222 handle = (void *)SvIV(handle_sv);
232 handles[i] = (void *)0;
237 static void rlm_perl_close_handles(void **handles)
245 for (i=0; handles[i]; i++) {
246 radlog(L_DBG, "close 0x%lx\n", (unsigned long)handles[i]);
253 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
255 PerlInterpreter *clone;
256 UV clone_flags = NULL;
258 PERL_SET_CONTEXT(perl);
260 clone = perl_clone(perl, clone_flags);
264 #if PERL_REVISION >= 5 && PERL_VERSION <8
267 ptr_table_free(PL_ptr_table);
270 PERL_SET_CONTEXT(aTHX);
271 rlm_perl_clear_handles(aTHX);
276 static void rlm_perl_destruct(PerlInterpreter *perl)
278 char **orig_environ = NULL;
281 PERL_SET_CONTEXT(perl);
283 PL_perl_destruct_level = 2;
285 PL_origenviron = environ;
291 * FIXME: This shouldn't happen
294 while (PL_scopestack_ix > 1 ){
302 environ = orig_environ;
306 static void rlm_destroy_perl(PerlInterpreter *perl)
311 PERL_SET_CONTEXT(perl);
313 handles = rlm_perl_get_handles(aTHX);
314 rlm_perl_destruct(perl);
315 rlm_perl_close_handles(handles);
318 static void delete_pool_handle(POOL_HANDLE *handle, PERL_INST *inst)
327 inst->perl_pool->head = next;
333 inst->perl_pool->tail = prev;
337 inst->perl_pool->current_clones--;
338 MUTEX_DESTROY(&handle->lock);
342 static void move2tail(POOL_HANDLE *handle, PERL_INST *inst)
347 if (inst->perl_pool->head == NULL) {
351 inst->perl_pool->head = handle;
352 inst->perl_pool->tail = handle;
356 if (inst->perl_pool->tail == handle) {
363 if ((next != NULL) ||
370 inst->perl_pool->head = next;
381 prev = inst->perl_pool->tail;
383 inst->perl_pool->tail = handle;
389 static POOL_HANDLE *pool_grow (PERL_INST *inst) {
393 if (inst->perl_pool->max_clones == inst->perl_pool->current_clones) {
396 if (inst->perl_pool->detach == yes ) {
400 handle = (POOL_HANDLE *)rad_malloc(sizeof(POOL_HANDLE));
403 radlog(L_ERR,"Could not find free memory for pool. Aborting");
409 handle->status = idle;
410 handle->clone = rlm_perl_clone(inst->perl);
411 handle->request_count = 0;
412 MUTEX_INIT(&handle->lock);
413 inst->perl_pool->current_clones++;
414 move2tail(handle, inst);
417 inst->perl_pool->time_when_last_added = now;
422 static POOL_HANDLE *pool_pop(PERL_INST *inst)
428 * Lock the pool and be fast other thread maybe
429 * waiting for us to finish
431 MUTEX_LOCK(&inst->perl_pool->mutex);
435 for (handle = inst->perl_pool->head; handle ; handle = tmp) {
438 if (handle->status == idle){
445 if (inst->perl_pool->current_clones < inst->perl_pool->max_clones ) {
447 found = pool_grow(inst);
450 radlog(L_ERR,"Cannot grow pool returning");
451 MUTEX_UNLOCK(&inst->perl_pool->mutex);
455 radlog(L_ERR,"rlm_perl:: reached maximum clones %d cannot grow",
456 inst->perl_pool->current_clones);
457 MUTEX_UNLOCK(&inst->perl_pool->mutex);
462 move2tail(found, inst);
463 found->status = busy;
464 MUTEX_LOCK(&found->lock);
465 inst->perl_pool->active_clones++;
466 found->request_count++;
470 MUTEX_UNLOCK(&inst->perl_pool->mutex);
471 radlog(L_DBG,"perl_pool: item 0x%lx asigned new request. Handled so far: %d",
472 (unsigned long) found->clone, found->request_count);
475 static int pool_release(POOL_HANDLE *handle, PERL_INST *inst) {
477 POOL_HANDLE *tmp, *tmp2;
483 MUTEX_LOCK(&inst->perl_pool->mutex);
486 * If detach is set then just release the mutex
488 if (inst->perl_pool->detach == yes ) {
489 handle->status = idle;
490 MUTEX_UNLOCK(&handle->lock);
491 MUTEX_UNLOCK(&inst->perl_pool->mutex);
495 MUTEX_UNLOCK(&handle->lock);
496 handle->status = idle;
497 inst->perl_pool->active_clones--;
499 spare = inst->perl_pool->current_clones - inst->perl_pool->active_clones;
501 radlog(L_DBG,"perl_pool total/active/spare [%d/%d/%d]"
502 , inst->perl_pool->current_clones, inst->perl_pool->active_clones, spare);
504 if (spare < inst->perl_pool->min_spare_clones) {
505 t = inst->perl_pool->min_spare_clones - spare;
507 if ((tmp = pool_grow(inst)) == NULL) {
508 MUTEX_UNLOCK(&inst->perl_pool->mutex);
512 MUTEX_UNLOCK(&inst->perl_pool->mutex);
516 if ((now - inst->perl_pool->time_when_last_added) < inst->perl_pool->cleanup_delay) {
517 MUTEX_UNLOCK(&inst->perl_pool->mutex);
520 if (spare > inst->perl_pool->max_spare_clones) {
521 spare -= inst->perl_pool->max_spare_clones;
522 for (tmp = inst->perl_pool->head; (tmp !=NULL ) && (spare > 0) ; tmp = tmp2) {
525 if(tmp->status == idle) {
526 rlm_destroy_perl(tmp->clone);
527 delete_pool_handle(tmp,inst);
534 * If the clone have reached max_request_per_clone clean it.
536 if (inst->perl_pool->max_request_per_clone > 0 ) {
537 if (handle->request_count > inst->perl_pool->max_request_per_clone) {
538 rlm_destroy_perl(handle->clone);
539 delete_pool_handle(handle,inst);
545 MUTEX_UNLOCK(&inst->perl_pool->mutex);
548 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)
592 if ((interp = perl_alloc()) == NULL) {
593 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
597 perl_construct(interp);
598 PL_perl_destruct_level = 2;
604 static void xs_init(pTHX)
606 char *file = __FILE__;
608 /* DynaLoader is a special case */
609 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
614 * This is wrapper for radlog
615 * Now users can call radiusd::radlog(level,msg) wich is the same
616 * calling radlog from C code.
619 static XS(XS_radiusd_radlog)
623 croak("Usage: radiusd::radlog(level, message)");
628 level = (int) SvIV(ST(0));
629 msg = (char *) SvPV(ST(1), PL_na);
632 * Because 'msg' is a 'char *', we don't want '%s', etc.
633 * in it to give us printf-style vulnerabilities.
635 radlog(level, "rlm_perl: %s", msg);
643 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
644 int freespace, RADIUS_ESCAPE_STRING func)
647 PERL_INST *inst= (PERL_INST *) instance;
648 PerlInterpreter *perl;
649 char params[1024], *ptr, *tmp;
658 if ((handle = pool_pop(instance)) == NULL) {
662 perl = handle->clone;
664 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
669 PERL_SET_CONTEXT(perl);
675 * Do an xlat on the provided string (nice recursive operation).
677 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
678 radlog(L_ERR, "rlm_perl: xlat failed.");
681 ptr = strtok(params, " ");
685 while (ptr != NULL) {
686 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
687 ptr = strtok(NULL, " ");
692 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
696 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
704 strncpy(out,tmp,ret);
706 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
716 pool_release(handle, instance);
721 * Do any per-module initialization that is separate to each
722 * configured instance of the module. e.g. set up connections
723 * to external databases, read configuration files, set up
724 * dictionary entries, etc.
726 * If configuration information is given in the config section
727 * that must be referenced in later calls, store a handle to it
728 * in *instance otherwise put a null pointer there.
731 * Setup a hashes wich we will use later
732 * parse a module and give him a chance to live
735 static int perl_instantiate(CONF_SECTION *conf, void **instance)
737 PERL_INST *inst = (PERL_INST *) instance;
741 HV *rad_request_proxy_hv;
742 HV *rad_request_proxy_reply_hv;
745 char *embed[4], *xlat_name;
746 int exitstatus = 0, argc=0;
749 * Set up a storage area for instance data
751 inst = rad_malloc(sizeof(PERL_INST));
752 memset(inst, 0, sizeof(PERL_INST));
755 * If the configuration parameters can't be parsed, then
758 if (cf_section_parse(conf, inst, module_config) < 0) {
765 if (inst->perl_flags) {
766 embed[1] = inst->perl_flags;
767 embed[2] = inst->module;
771 embed[1] = inst->module;
777 inst->perl = perl_clone(interp ,CLONEf_KEEP_PTR_TABLE);
781 PERL_SET_CONTEXT(inst->perl);
783 if ((inst->perl = perl_alloc()) == NULL) {
784 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
788 perl_construct(inst->perl);
791 #if PERL_REVISION >= 5 && PERL_VERSION >=8
792 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
795 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
801 exitstatus = perl_run(inst->perl);
803 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
809 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
811 rad_reply_hv = newHV();
812 rad_check_hv = newHV();
813 rad_request_hv = newHV();
814 rad_request_proxy_hv = newHV();
815 rad_request_proxy_reply_hv = newHV();
817 rad_reply_hv = get_hv("RAD_REPLY",1);
818 rad_check_hv = get_hv("RAD_CHECK",1);
819 rad_request_hv = get_hv("RAD_REQUEST",1);
820 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
821 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
823 xlat_name = cf_section_name2(conf);
824 if (xlat_name == NULL)
825 xlat_name = cf_section_name1(conf);
827 inst->xlat_name = strdup(xlat_name);
828 xlat_register(xlat_name, perl_xlat, inst);
832 if ((init_pool(conf, inst)) == -1) {
833 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
844 * get the vps and put them in perl hash
845 * If one VP have multiple values it is added as array_ref
846 * Example for this is Cisco-AVPair that holds multiple values.
847 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
849 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
851 VALUE_PAIR *nvp, *vpa, *vpn;
859 while (nvp != NULL) {
860 attr = nvp->attribute;
861 vpa = paircopy2(nvp,attr);
866 len = vp_prints_value(buffer, sizeof(buffer),
868 av_push(av, newSVpv(buffer, len));
871 hv_store(rad_hv, nvp->name, strlen(nvp->name),
872 newRV_noinc((SV *) av), 0);
874 len = vp_prints_value(buffer, sizeof(buffer),
876 hv_store(rad_hv, vpa->name, strlen(vpa->name),
877 newSVpv(buffer, len), 0);
881 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
883 pairdelete(&nvp, attr);
890 * Verify that a Perl SV is a string and save it in FreeRadius
894 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv) {
899 val = SvPV_nolen(sv);
900 vpp = pairmake(key, val, T_OP_EQ);
904 "rlm_perl: Added pair %s = %s", key, val);
908 "rlm_perl: ERROR: Failed to create pair %s = %s",
917 * Gets the content from hashes
919 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
924 I32 key_len, len, i, j;
927 for (i = hv_iterinit(my_hv); i > 0; i--) {
928 res_sv = hv_iternextsv(my_hv,&key,&key_len);
929 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
930 av = (AV*)SvRV(res_sv);
932 for (j = 0; j <= len; j++) {
933 av_sv = av_fetch(av, j, 0);
934 ret = pairadd_sv(vp, key, *av_sv) + ret;
936 } else ret = pairadd_sv(vp, key, res_sv) + ret;
943 * Call the function_name inside the module
944 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
947 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
950 PERL_INST *inst = instance;
952 int exitstatus=0, count;
958 HV *rad_request_proxy_hv;
959 HV *rad_request_proxy_reply_hv;
964 if ((handle = pool_pop(instance)) == NULL) {
965 return RLM_MODULE_FAIL;
968 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
970 dTHXa(handle->clone);
971 PERL_SET_CONTEXT(handle->clone);
974 PERL_SET_CONTEXT(inst->perl);
975 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
985 * Radius has told us to call this function, but none
988 if (!function_name) {
989 return RLM_MODULE_FAIL;
992 rad_reply_hv = get_hv("RAD_REPLY",1);
993 rad_check_hv = get_hv("RAD_CHECK",1);
994 rad_request_hv = get_hv("RAD_REQUEST",1);
995 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
996 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
999 perl_store_vps(request->reply->vps, rad_reply_hv);
1000 perl_store_vps(request->config_items, rad_check_hv);
1001 perl_store_vps(request->packet->vps, rad_request_hv);
1003 if (request->proxy != NULL) {
1004 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
1006 hv_undef(rad_request_proxy_hv);
1009 if (request->proxy_reply !=NULL) {
1010 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1012 hv_undef(rad_request_proxy_reply_hv);
1020 * This way %RAD_xx can be pushed onto stack as sub parameters.
1021 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1022 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1023 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1027 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1033 if (exitstatus >= 100 || exitstatus < 0) {
1034 exitstatus = RLM_MODULE_FAIL;
1042 if (SvTRUE(ERRSV)) {
1043 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1045 function_name, SvPV(ERRSV,n_a));
1048 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1049 pairmove(&request->reply->vps, &vp);
1053 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1054 pairmove(&request->config_items, &vp);
1058 if ((get_hv_content(rad_request_proxy_reply_hv, &vp)) > 0 && request->proxy_reply != NULL) {
1059 pairfree(&request->proxy_reply->vps);
1060 pairmove(&request->proxy_reply->vps, &vp);
1065 pool_release(handle,instance);
1066 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1073 * Find the named user in this modules database. Create the set
1074 * of attribute-value pairs to check and reply with for this user
1075 * from the database. The authentication code only needs to check
1076 * the password, the rest is done here.
1078 static int perl_authorize(void *instance, REQUEST *request)
1080 return rlmperl_call(instance, request,
1081 ((PERL_INST *)instance)->func_authorize);
1085 * Authenticate the user with the given password.
1087 static int perl_authenticate(void *instance, REQUEST *request)
1089 return rlmperl_call(instance, request,
1090 ((PERL_INST *)instance)->func_authenticate);
1093 * Massage the request before recording it or proxying it
1095 static int perl_preacct(void *instance, REQUEST *request)
1097 return rlmperl_call(instance, request,
1098 ((PERL_INST *)instance)->func_preacct);
1101 * Write accounting information to this modules database.
1103 static int perl_accounting(void *instance, REQUEST *request)
1106 int acctstatustype=0;
1108 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1109 acctstatustype = pair->lvalue;
1111 radlog(L_ERR, "Invalid Accounting Packet");
1112 return RLM_MODULE_INVALID;
1115 switch (acctstatustype) {
1117 case PW_STATUS_START:
1119 if (((PERL_INST *)instance)->func_start_accounting) {
1120 return rlmperl_call(instance, request,
1121 ((PERL_INST *)instance)->func_start_accounting);
1123 return rlmperl_call(instance, request,
1124 ((PERL_INST *)instance)->func_accounting);
1128 case PW_STATUS_STOP:
1130 if (((PERL_INST *)instance)->func_stop_accounting) {
1131 return rlmperl_call(instance, request,
1132 ((PERL_INST *)instance)->func_stop_accounting);
1134 return rlmperl_call(instance, request,
1135 ((PERL_INST *)instance)->func_accounting);
1139 return rlmperl_call(instance, request,
1140 ((PERL_INST *)instance)->func_accounting);
1145 * Check for simultaneouse-use
1147 static int perl_checksimul(void *instance, REQUEST *request)
1149 return rlmperl_call(instance, request,
1150 ((PERL_INST *)instance)->func_checksimul);
1155 static int perl_pre_proxy(void *instance, REQUEST *request)
1157 return rlmperl_call(instance, request,
1158 ((PERL_INST *)instance)->func_pre_proxy);
1161 * Post-Proxy request
1163 static int perl_post_proxy(void *instance, REQUEST *request)
1165 return rlmperl_call(instance, request,
1166 ((PERL_INST *)instance)->func_post_proxy);
1171 static int perl_post_auth(void *instance, REQUEST *request)
1173 return rlmperl_call(instance, request,
1174 ((PERL_INST *)instance)->func_post_auth);
1177 * Detach a instance give a chance to a module to make some internal setup ...
1179 static int perl_detach(void *instance)
1181 PERL_INST *inst = (PERL_INST *) instance;
1182 int exitstatus=0,count=0, i=0;
1185 POOL_HANDLE *handle, *tmp, *tmp2;
1187 MUTEX_LOCK(&inst->perl_pool->mutex);
1188 inst->perl_pool->detach = yes;
1189 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1191 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1193 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1195 * Wait until clone becomes idle
1197 MUTEX_LOCK(&handle->lock);
1200 * Give a clones chance to run detach function
1203 dTHXa(handle->clone);
1204 PERL_SET_CONTEXT(handle->clone);
1206 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1207 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1213 * FIXME: bug in perl
1216 if (exitstatus >= 100 || exitstatus < 0) {
1217 exitstatus = RLM_MODULE_FAIL;
1223 radlog(L_DBG,"detach at 0x%lx returned status %d",
1224 (unsigned long) handle->clone, exitstatus);
1227 MUTEX_UNLOCK(&handle->lock);
1233 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1235 radlog(L_DBG,"rlm_perl:: Destroy perl");
1236 rlm_perl_destruct(tmp->clone);
1237 delete_pool_handle(tmp,inst);
1242 #endif /* USE_ITHREADS */
1243 PERL_SET_CONTEXT(inst->perl);
1245 dSP; ENTER; SAVETMPS;
1248 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1253 if (exitstatus >= 100 || exitstatus < 0) {
1254 exitstatus = RLM_MODULE_FAIL;
1265 xlat_unregister(inst->xlat_name, perl_xlat);
1266 free(inst->xlat_name);
1268 if (inst->func_authorize) free(inst->func_authorize);
1269 if (inst->func_authenticate) free(inst->func_authenticate);
1270 if (inst->func_accounting) free(inst->func_accounting);
1271 if (inst->func_preacct) free(inst->func_preacct);
1272 if (inst->func_checksimul) free(inst->func_checksimul);
1273 if (inst->func_detach) free(inst->func_detach);
1276 free(inst->perl_pool->head);
1277 free(inst->perl_pool->tail);
1278 MUTEX_DESTROY(&inst->perl_pool->mutex);
1279 free(inst->perl_pool);
1280 rlm_perl_destruct(inst->perl);
1282 perl_destruct(inst->perl);
1283 perl_free(inst->perl);
1290 * The module name should be the only globally exported symbol.
1291 * That is, everything else should be 'static'.
1293 * If the module needs to temporarily modify it's instantiation
1294 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1295 * The server will then take care of ensuring that the module
1296 * is single-threaded.
1298 module_t rlm_perl = {
1301 RLM_TYPE_THREAD_SAFE, /* type */
1303 RLM_TYPE_THREAD_UNSAFE,
1305 perl_init, /* initialization */
1306 perl_instantiate, /* instantiation */
1312 perl_checksimul, /* check simul */
1313 perl_pre_proxy, /* pre-proxy */
1314 perl_post_proxy, /* post-proxy */
1315 perl_post_auth /* post-auth */
1317 perl_detach, /* detach */