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>
50 extern char **environ;
53 static const char rcsid[] = "$Id$";
58 * Pool of Perl's clones (genetically cloned) ;)
61 typedef struct pool_handle {
62 struct pool_handle *next;
63 struct pool_handle *prev;
64 enum {busy, idle} status;
65 unsigned int request_count;
66 PerlInterpreter *clone;
70 typedef struct PERL_POOL {
80 int max_request_per_clone;
84 time_t time_when_last_added;
90 * Define a structure for our module configuration.
92 * These variables do not need to be in a structure, but it's
93 * a lot cleaner to do so, and a pointer to the structure can
94 * be used as the instance handle.
96 typedef struct perl_inst {
97 /* Name of the perl module */
100 /* Name of the functions for each module method */
101 char *func_authorize;
102 char *func_authenticate;
103 char *func_accounting;
104 char *func_start_accounting;
105 char *func_stop_accounting;
107 char *func_checksimul;
110 char *func_pre_proxy;
111 char *func_post_proxy;
112 char *func_post_auth;
115 PerlInterpreter *perl;
117 PERL_POOL *perl_pool;
121 * A mapping of configuration file names to internal variables.
123 * Note that the string is dynamically allocated, so it MUST
124 * be freed. When the configuration file parse re-reads the string,
125 * it free's the old one, and strdup's the new one, placing the pointer
126 * to the strdup'd string into 'config.string'. This gets around
129 static const CONF_PARSER module_config[] = {
130 { "module", PW_TYPE_STRING_PTR,
131 offsetof(PERL_INST,module), NULL, "module"},
132 { "func_authorize", PW_TYPE_STRING_PTR,
133 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
134 { "func_authenticate", PW_TYPE_STRING_PTR,
135 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
136 { "func_accounting", PW_TYPE_STRING_PTR,
137 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
138 { "func_preacct", PW_TYPE_STRING_PTR,
139 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
140 { "func_checksimul", PW_TYPE_STRING_PTR,
141 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
142 { "func_detach", PW_TYPE_STRING_PTR,
143 offsetof(PERL_INST,func_detach), NULL, "detach"},
144 { "func_xlat", PW_TYPE_STRING_PTR,
145 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
146 { "func_pre_proxy", PW_TYPE_STRING_PTR,
147 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
148 { "func_post_proxy", PW_TYPE_STRING_PTR,
149 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
150 { "func_post_auth", PW_TYPE_STRING_PTR,
151 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
152 { "perl_flags", PW_TYPE_STRING_PTR,
153 offsetof(PERL_INST,perl_flags), NULL, NULL},
154 { "func_start_accounting", PW_TYPE_STRING_PTR,
155 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
156 { "func_stop_accounting", PW_TYPE_STRING_PTR,
157 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
159 { NULL, -1, 0, NULL, NULL } /* end the list */
165 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
169 * We use one perl to clone from it i.e. main boss
170 * We clone it for every instance if we have perl
171 * with -Duseithreads compiled in
173 static PerlInterpreter *interp;
175 static const CONF_PARSER pool_conf[] = {
176 { "max_clones", PW_TYPE_INTEGER, offsetof(PERL_POOL, max_clones), NULL, "32"},
177 { "start_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, start_clones), NULL, "5"},
178 { "min_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, min_spare_clones),NULL, "3"},
179 { "max_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_spare_clones),NULL, "3"},
180 { "cleanup_delay",PW_TYPE_INTEGER, offsetof(PERL_POOL,cleanup_delay),NULL, "5"},
181 { "max_request_per_clone",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_request_per_clone),NULL, "0"},
182 { NULL, -1, 0, NULL, NULL } /* end the list */
186 #define dl_librefs "DynaLoader::dl_librefs"
187 #define dl_modules "DynaLoader::dl_modules"
188 static void rlm_perl_clear_handles(pTHX)
190 AV *librefs = get_av(dl_librefs, FALSE);
196 static void **rlm_perl_get_handles(pTHX)
199 AV *librefs = get_av(dl_librefs, FALSE);
200 AV *modules = get_av(dl_modules, FALSE);
205 "Could not get @%s for unloading.\n",
210 if (!(AvFILL(librefs) >= 0)) {
214 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
216 for (i=0; i<=AvFILL(librefs); i++) {
218 SV *handle_sv = *av_fetch(librefs, i, FALSE);
222 "Could not fetch $%s[%d]!\n",
226 handle = (void *)SvIV(handle_sv);
236 handles[i] = (void *)0;
241 static void rlm_perl_close_handles(void **handles)
249 for (i=0; handles[i]; i++) {
250 radlog(L_DBG, "close 0x%lx\n", (unsigned long)handles[i]);
257 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
259 PerlInterpreter *clone;
262 PERL_SET_CONTEXT(perl);
264 clone = perl_clone(perl, clone_flags);
268 #if PERL_REVISION >= 5 && PERL_VERSION <8
271 ptr_table_free(PL_ptr_table);
274 PERL_SET_CONTEXT(aTHX);
275 rlm_perl_clear_handles(aTHX);
280 static void rlm_perl_destruct(PerlInterpreter *perl)
282 char **orig_environ = NULL;
285 PERL_SET_CONTEXT(perl);
287 PL_perl_destruct_level = 2;
289 PL_origenviron = environ;
295 * FIXME: This shouldn't happen
298 while (PL_scopestack_ix > 1 ){
306 environ = orig_environ;
310 static void rlm_destroy_perl(PerlInterpreter *perl)
315 PERL_SET_CONTEXT(perl);
317 handles = rlm_perl_get_handles(aTHX);
318 rlm_perl_destruct(perl);
319 rlm_perl_close_handles(handles);
322 static void delete_pool_handle(POOL_HANDLE *handle, PERL_INST *inst)
331 inst->perl_pool->head = next;
337 inst->perl_pool->tail = prev;
341 inst->perl_pool->current_clones--;
342 MUTEX_DESTROY(&handle->lock);
346 static void move2tail(POOL_HANDLE *handle, PERL_INST *inst)
351 if (inst->perl_pool->head == NULL) {
355 inst->perl_pool->head = handle;
356 inst->perl_pool->tail = handle;
360 if (inst->perl_pool->tail == handle) {
367 if ((next != NULL) ||
374 inst->perl_pool->head = next;
385 prev = inst->perl_pool->tail;
387 inst->perl_pool->tail = handle;
393 static POOL_HANDLE *pool_grow (PERL_INST *inst) {
397 if (inst->perl_pool->max_clones == inst->perl_pool->current_clones) {
400 if (inst->perl_pool->detach == yes ) {
404 handle = (POOL_HANDLE *)rad_malloc(sizeof(POOL_HANDLE));
407 radlog(L_ERR,"Could not find free memory for pool. Aborting");
413 handle->status = idle;
414 handle->clone = rlm_perl_clone(inst->perl);
415 handle->request_count = 0;
416 MUTEX_INIT(&handle->lock);
417 inst->perl_pool->current_clones++;
418 move2tail(handle, inst);
421 inst->perl_pool->time_when_last_added = now;
426 static POOL_HANDLE *pool_pop(PERL_INST *inst)
432 * Lock the pool and be fast other thread maybe
433 * waiting for us to finish
435 MUTEX_LOCK(&inst->perl_pool->mutex);
439 for (handle = inst->perl_pool->head; handle ; handle = tmp) {
442 if (handle->status == idle){
449 if (inst->perl_pool->current_clones < inst->perl_pool->max_clones ) {
451 found = pool_grow(inst);
454 radlog(L_ERR,"Cannot grow pool returning");
455 MUTEX_UNLOCK(&inst->perl_pool->mutex);
459 radlog(L_ERR,"rlm_perl:: reached maximum clones %d cannot grow",
460 inst->perl_pool->current_clones);
461 MUTEX_UNLOCK(&inst->perl_pool->mutex);
466 move2tail(found, inst);
467 found->status = busy;
468 MUTEX_LOCK(&found->lock);
469 inst->perl_pool->active_clones++;
470 found->request_count++;
474 MUTEX_UNLOCK(&inst->perl_pool->mutex);
475 radlog(L_DBG,"perl_pool: item 0x%lx asigned new request. Handled so far: %d",
476 (unsigned long) found->clone, found->request_count);
479 static int pool_release(POOL_HANDLE *handle, PERL_INST *inst) {
481 POOL_HANDLE *tmp, *tmp2;
487 MUTEX_LOCK(&inst->perl_pool->mutex);
490 * If detach is set then just release the mutex
492 if (inst->perl_pool->detach == yes ) {
493 handle->status = idle;
494 MUTEX_UNLOCK(&handle->lock);
495 MUTEX_UNLOCK(&inst->perl_pool->mutex);
499 MUTEX_UNLOCK(&handle->lock);
500 handle->status = idle;
501 inst->perl_pool->active_clones--;
503 spare = inst->perl_pool->current_clones - inst->perl_pool->active_clones;
505 radlog(L_DBG,"perl_pool total/active/spare [%d/%d/%d]"
506 , inst->perl_pool->current_clones, inst->perl_pool->active_clones, spare);
508 if (spare < inst->perl_pool->min_spare_clones) {
509 t = inst->perl_pool->min_spare_clones - spare;
511 if ((tmp = pool_grow(inst)) == NULL) {
512 MUTEX_UNLOCK(&inst->perl_pool->mutex);
516 MUTEX_UNLOCK(&inst->perl_pool->mutex);
520 if ((now - inst->perl_pool->time_when_last_added) < inst->perl_pool->cleanup_delay) {
521 MUTEX_UNLOCK(&inst->perl_pool->mutex);
524 if (spare > inst->perl_pool->max_spare_clones) {
525 spare -= inst->perl_pool->max_spare_clones;
526 for (tmp = inst->perl_pool->head; (tmp !=NULL ) && (spare > 0) ; tmp = tmp2) {
529 if(tmp->status == idle) {
530 rlm_destroy_perl(tmp->clone);
531 delete_pool_handle(tmp,inst);
538 * If the clone have reached max_request_per_clone clean it.
540 if (inst->perl_pool->max_request_per_clone > 0 ) {
541 if (handle->request_count > inst->perl_pool->max_request_per_clone) {
542 rlm_destroy_perl(handle->clone);
543 delete_pool_handle(handle,inst);
549 MUTEX_UNLOCK(&inst->perl_pool->mutex);
552 static int init_pool (CONF_SECTION *conf, PERL_INST *inst) {
557 pool = rad_malloc(sizeof(PERL_POOL));
558 memset(pool,0,sizeof(PERL_POOL));
560 inst->perl_pool = pool;
562 MUTEX_INIT(&pool->mutex);
569 cf_section_parse(conf,pool,pool_conf);
570 inst->perl_pool = pool;
571 inst->perl_pool->detach = no;
573 for(t = 0;t < inst->perl_pool->start_clones ;t++){
574 if ((handle = pool_grow(inst)) == NULL) {
584 * Do any per-module initialization. e.g. set up connections
585 * to external databases, read configuration files, set up
586 * dictionary entries, etc.
588 * Try to avoid putting too much stuff in here - it's better to
589 * do it in instantiate() where it is not global.
590 * I use one global interpetator to make things more fastest for
591 * Threading env I clone new perl from this interp.
593 static int perl_init(void)
598 static void xs_init(pTHX)
600 char *file = __FILE__;
602 /* DynaLoader is a special case */
603 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
608 * This is wrapper for radlog
609 * Now users can call radiusd::radlog(level,msg) wich is the same
610 * calling radlog from C code.
613 static XS(XS_radiusd_radlog)
617 croak("Usage: radiusd::radlog(level, message)");
622 level = (int) SvIV(ST(0));
623 msg = (char *) SvPV(ST(1), PL_na);
626 * Because 'msg' is a 'char *', we don't want '%s', etc.
627 * in it to give us printf-style vulnerabilities.
629 radlog(level, "rlm_perl: %s", msg);
637 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
638 size_t freespace, RADIUS_ESCAPE_STRING func)
641 PERL_INST *inst= (PERL_INST *) instance;
642 PerlInterpreter *perl;
643 char params[1024], *ptr, *tmp;
648 * Do an xlat on the provided string (nice recursive operation).
650 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
651 radlog(L_ERR, "rlm_perl: xlat failed.");
660 if ((handle = pool_pop(instance)) == NULL) {
664 perl = handle->clone;
666 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
671 PERL_SET_CONTEXT(perl);
676 ptr = strtok(params, " ");
680 while (ptr != NULL) {
681 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
682 ptr = strtok(NULL, " ");
687 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
691 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
694 } else if (count > 0) {
696 strNcpy(out,tmp,freespace);
699 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
709 pool_release(handle, instance);
714 * Do any per-module initialization that is separate to each
715 * configured instance of the module. e.g. set up connections
716 * to external databases, read configuration files, set up
717 * dictionary entries, etc.
719 * If configuration information is given in the config section
720 * that must be referenced in later calls, store a handle to it
721 * in *instance otherwise put a null pointer there.
724 * Setup a hashes wich we will use later
725 * parse a module and give him a chance to live
728 static int perl_instantiate(CONF_SECTION *conf, void **instance)
730 PERL_INST *inst = (PERL_INST *) instance;
734 HV *rad_request_proxy_hv;
735 HV *rad_request_proxy_reply_hv;
738 char *embed[4], *xlat_name;
739 int exitstatus = 0, argc=0;
742 * Set up a storage area for instance data
744 inst = rad_malloc(sizeof(PERL_INST));
745 memset(inst, 0, sizeof(PERL_INST));
748 * If the configuration parameters can't be parsed, then
751 if (cf_section_parse(conf, inst, module_config) < 0) {
758 if (inst->perl_flags) {
759 embed[1] = inst->perl_flags;
760 embed[2] = inst->module;
764 embed[1] = inst->module;
772 if ((inst->perl = perl_alloc()) == NULL) {
773 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
777 perl_construct(inst->perl);
778 PL_perl_destruct_level = 2;
783 PERL_SET_CONTEXT(inst->perl);
785 if ((inst->perl = perl_alloc()) == NULL) {
786 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
790 perl_construct(inst->perl);
793 #if PERL_REVISION >= 5 && PERL_VERSION >=8
794 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
797 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
803 exitstatus = perl_run(inst->perl);
805 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
811 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
813 rad_reply_hv = newHV();
814 rad_check_hv = newHV();
815 rad_request_hv = newHV();
816 rad_request_proxy_hv = newHV();
817 rad_request_proxy_reply_hv = newHV();
819 rad_reply_hv = get_hv("RAD_REPLY",1);
820 rad_check_hv = get_hv("RAD_CHECK",1);
821 rad_request_hv = get_hv("RAD_REQUEST",1);
822 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
823 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
825 xlat_name = cf_section_name2(conf);
826 if (xlat_name == NULL)
827 xlat_name = cf_section_name1(conf);
829 inst->xlat_name = strdup(xlat_name);
830 xlat_register(xlat_name, perl_xlat, inst);
834 if ((init_pool(conf, inst)) == -1) {
835 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
846 * get the vps and put them in perl hash
847 * If one VP have multiple values it is added as array_ref
848 * Example for this is Cisco-AVPair that holds multiple values.
849 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
851 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
853 VALUE_PAIR *nvp, *vpa, *vpn;
861 while (nvp != NULL) {
862 attr = nvp->attribute;
863 vpa = paircopy2(nvp,attr);
868 len = vp_prints_value(buffer, sizeof(buffer),
870 av_push(av, newSVpv(buffer, len));
873 hv_store(rad_hv, nvp->name, strlen(nvp->name),
874 newRV_noinc((SV *) av), 0);
876 len = vp_prints_value(buffer, sizeof(buffer),
878 hv_store(rad_hv, vpa->name, strlen(vpa->name),
879 newSVpv(buffer, len), 0);
883 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
885 pairdelete(&nvp, attr);
892 * Verify that a Perl SV is a string and save it in FreeRadius
896 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
901 val = SvPV_nolen(sv);
902 vpp = pairmake(key, val, operator);
906 "rlm_perl: Added pair %s = %s", key, val);
910 "rlm_perl: ERROR: Failed to create pair %s = %s",
919 * Gets the content from hashes
921 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
926 I32 key_len, len, i, j;
929 for (i = hv_iterinit(my_hv); i > 0; i--) {
930 res_sv = hv_iternextsv(my_hv,&key,&key_len);
931 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
932 av = (AV*)SvRV(res_sv);
934 for (j = 0; j <= len; j++) {
935 av_sv = av_fetch(av, j, 0);
936 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
938 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
945 * Call the function_name inside the module
946 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
949 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
952 PERL_INST *inst = instance;
954 int exitstatus=0, count;
960 HV *rad_request_proxy_hv;
961 HV *rad_request_proxy_reply_hv;
966 if ((handle = pool_pop(instance)) == NULL) {
967 return RLM_MODULE_FAIL;
970 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
972 dTHXa(handle->clone);
973 PERL_SET_CONTEXT(handle->clone);
976 PERL_SET_CONTEXT(inst->perl);
977 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
987 * Radius has told us to call this function, but none
990 if (!function_name) {
991 return RLM_MODULE_FAIL;
994 rad_reply_hv = get_hv("RAD_REPLY",1);
995 rad_check_hv = get_hv("RAD_CHECK",1);
996 rad_request_hv = get_hv("RAD_REQUEST",1);
997 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
998 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
1001 perl_store_vps(request->reply->vps, rad_reply_hv);
1002 perl_store_vps(request->config_items, rad_check_hv);
1003 perl_store_vps(request->packet->vps, rad_request_hv);
1005 if (request->proxy != NULL) {
1006 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
1008 hv_undef(rad_request_proxy_hv);
1011 if (request->proxy_reply !=NULL) {
1012 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1014 hv_undef(rad_request_proxy_reply_hv);
1022 * This way %RAD_xx can be pushed onto stack as sub parameters.
1023 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1024 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1025 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1029 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1033 if (SvTRUE(ERRSV)) {
1034 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1036 function_name, SvPV(ERRSV,n_a));
1042 if (exitstatus >= 100 || exitstatus < 0) {
1043 exitstatus = RLM_MODULE_FAIL;
1053 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1054 pairmove(&request->reply->vps, &vp);
1058 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1059 pairmove(&request->config_items, &vp);
1063 if ((get_hv_content(rad_request_proxy_reply_hv, &vp)) > 0 && request->proxy_reply != NULL) {
1064 pairfree(&request->proxy_reply->vps);
1065 pairmove(&request->proxy_reply->vps, &vp);
1070 pool_release(handle,instance);
1071 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1078 * Find the named user in this modules database. Create the set
1079 * of attribute-value pairs to check and reply with for this user
1080 * from the database. The authentication code only needs to check
1081 * the password, the rest is done here.
1083 static int perl_authorize(void *instance, REQUEST *request)
1085 return rlmperl_call(instance, request,
1086 ((PERL_INST *)instance)->func_authorize);
1090 * Authenticate the user with the given password.
1092 static int perl_authenticate(void *instance, REQUEST *request)
1094 return rlmperl_call(instance, request,
1095 ((PERL_INST *)instance)->func_authenticate);
1098 * Massage the request before recording it or proxying it
1100 static int perl_preacct(void *instance, REQUEST *request)
1102 return rlmperl_call(instance, request,
1103 ((PERL_INST *)instance)->func_preacct);
1106 * Write accounting information to this modules database.
1108 static int perl_accounting(void *instance, REQUEST *request)
1111 int acctstatustype=0;
1113 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1114 acctstatustype = pair->lvalue;
1116 radlog(L_ERR, "Invalid Accounting Packet");
1117 return RLM_MODULE_INVALID;
1120 switch (acctstatustype) {
1122 case PW_STATUS_START:
1124 if (((PERL_INST *)instance)->func_start_accounting) {
1125 return rlmperl_call(instance, request,
1126 ((PERL_INST *)instance)->func_start_accounting);
1128 return rlmperl_call(instance, request,
1129 ((PERL_INST *)instance)->func_accounting);
1133 case PW_STATUS_STOP:
1135 if (((PERL_INST *)instance)->func_stop_accounting) {
1136 return rlmperl_call(instance, request,
1137 ((PERL_INST *)instance)->func_stop_accounting);
1139 return rlmperl_call(instance, request,
1140 ((PERL_INST *)instance)->func_accounting);
1144 return rlmperl_call(instance, request,
1145 ((PERL_INST *)instance)->func_accounting);
1150 * Check for simultaneouse-use
1152 static int perl_checksimul(void *instance, REQUEST *request)
1154 return rlmperl_call(instance, request,
1155 ((PERL_INST *)instance)->func_checksimul);
1160 static int perl_pre_proxy(void *instance, REQUEST *request)
1162 return rlmperl_call(instance, request,
1163 ((PERL_INST *)instance)->func_pre_proxy);
1166 * Post-Proxy request
1168 static int perl_post_proxy(void *instance, REQUEST *request)
1170 return rlmperl_call(instance, request,
1171 ((PERL_INST *)instance)->func_post_proxy);
1176 static int perl_post_auth(void *instance, REQUEST *request)
1178 return rlmperl_call(instance, request,
1179 ((PERL_INST *)instance)->func_post_auth);
1182 * Detach a instance give a chance to a module to make some internal setup ...
1184 static int perl_detach(void *instance)
1186 PERL_INST *inst = (PERL_INST *) instance;
1187 int exitstatus=0,count=0;
1190 POOL_HANDLE *handle, *tmp, *tmp2;
1192 MUTEX_LOCK(&inst->perl_pool->mutex);
1193 inst->perl_pool->detach = yes;
1194 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1196 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1198 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1200 * Wait until clone becomes idle
1202 MUTEX_LOCK(&handle->lock);
1205 * Give a clones chance to run detach function
1208 dTHXa(handle->clone);
1209 PERL_SET_CONTEXT(handle->clone);
1211 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1212 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1218 * FIXME: bug in perl
1221 if (exitstatus >= 100 || exitstatus < 0) {
1222 exitstatus = RLM_MODULE_FAIL;
1228 radlog(L_DBG,"detach at 0x%lx returned status %d",
1229 (unsigned long) handle->clone, exitstatus);
1232 MUTEX_UNLOCK(&handle->lock);
1238 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1240 radlog(L_DBG,"rlm_perl:: Destroy perl");
1241 rlm_perl_destruct(tmp->clone);
1242 delete_pool_handle(tmp,inst);
1247 #endif /* USE_ITHREADS */
1248 PERL_SET_CONTEXT(inst->perl);
1250 dSP; ENTER; SAVETMPS;
1253 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1258 if (exitstatus >= 100 || exitstatus < 0) {
1259 exitstatus = RLM_MODULE_FAIL;
1270 xlat_unregister(inst->xlat_name, perl_xlat);
1271 free(inst->xlat_name);
1273 if (inst->func_authorize) free(inst->func_authorize);
1274 if (inst->func_authenticate) free(inst->func_authenticate);
1275 if (inst->func_accounting) free(inst->func_accounting);
1276 if (inst->func_preacct) free(inst->func_preacct);
1277 if (inst->func_checksimul) free(inst->func_checksimul);
1278 if (inst->func_pre_proxy) free(inst->func_pre_proxy);
1279 if (inst->func_post_proxy) free(inst->func_post_proxy);
1280 if (inst->func_post_auth) free(inst->func_post_auth);
1281 if (inst->func_detach) free(inst->func_detach);
1284 free(inst->perl_pool->head);
1285 free(inst->perl_pool->tail);
1286 MUTEX_DESTROY(&inst->perl_pool->mutex);
1287 free(inst->perl_pool);
1288 rlm_perl_destruct(inst->perl);
1290 perl_destruct(inst->perl);
1291 perl_free(inst->perl);
1298 * The module name should be the only globally exported symbol.
1299 * That is, everything else should be 'static'.
1301 * If the module needs to temporarily modify it's instantiation
1302 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1303 * The server will then take care of ensuring that the module
1304 * is single-threaded.
1306 module_t rlm_perl = {
1309 RLM_TYPE_THREAD_SAFE, /* type */
1311 RLM_TYPE_THREAD_UNSAFE,
1313 perl_init, /* initialization */
1314 perl_instantiate, /* instantiation */
1320 perl_checksimul, /* check simul */
1321 perl_pre_proxy, /* pre-proxy */
1322 perl_post_proxy, /* post-proxy */
1323 perl_post_auth /* post-auth */
1325 perl_detach, /* detach */