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)
596 if ((interp = perl_alloc()) == NULL) {
597 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
601 perl_construct(interp);
602 PL_perl_destruct_level = 2;
608 static void xs_init(pTHX)
610 char *file = __FILE__;
612 /* DynaLoader is a special case */
613 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
618 * This is wrapper for radlog
619 * Now users can call radiusd::radlog(level,msg) wich is the same
620 * calling radlog from C code.
623 static XS(XS_radiusd_radlog)
627 croak("Usage: radiusd::radlog(level, message)");
632 level = (int) SvIV(ST(0));
633 msg = (char *) SvPV(ST(1), PL_na);
636 * Because 'msg' is a 'char *', we don't want '%s', etc.
637 * in it to give us printf-style vulnerabilities.
639 radlog(level, "rlm_perl: %s", msg);
647 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
648 size_t freespace, RADIUS_ESCAPE_STRING func)
651 PERL_INST *inst= (PERL_INST *) instance;
652 PerlInterpreter *perl;
653 char params[1024], *ptr, *tmp;
662 if ((handle = pool_pop(instance)) == NULL) {
666 perl = handle->clone;
668 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
673 PERL_SET_CONTEXT(perl);
679 * Do an xlat on the provided string (nice recursive operation).
681 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
682 radlog(L_ERR, "rlm_perl: xlat failed.");
685 ptr = strtok(params, " ");
689 while (ptr != NULL) {
690 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
691 ptr = strtok(NULL, " ");
696 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
700 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
708 strncpy(out,tmp,ret);
710 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
720 pool_release(handle, instance);
725 * Do any per-module initialization that is separate to each
726 * configured instance of the module. e.g. set up connections
727 * to external databases, read configuration files, set up
728 * dictionary entries, etc.
730 * If configuration information is given in the config section
731 * that must be referenced in later calls, store a handle to it
732 * in *instance otherwise put a null pointer there.
735 * Setup a hashes wich we will use later
736 * parse a module and give him a chance to live
739 static int perl_instantiate(CONF_SECTION *conf, void **instance)
741 PERL_INST *inst = (PERL_INST *) instance;
745 HV *rad_request_proxy_hv;
746 HV *rad_request_proxy_reply_hv;
749 char *embed[4], *xlat_name;
750 int exitstatus = 0, argc=0;
753 * Set up a storage area for instance data
755 inst = rad_malloc(sizeof(PERL_INST));
756 memset(inst, 0, sizeof(PERL_INST));
759 * If the configuration parameters can't be parsed, then
762 if (cf_section_parse(conf, inst, module_config) < 0) {
769 if (inst->perl_flags) {
770 embed[1] = inst->perl_flags;
771 embed[2] = inst->module;
775 embed[1] = inst->module;
785 PERL_SET_CONTEXT(inst->perl);
787 if ((inst->perl = perl_alloc()) == NULL) {
788 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
792 perl_construct(inst->perl);
795 #if PERL_REVISION >= 5 && PERL_VERSION >=8
796 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
799 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
805 exitstatus = perl_run(inst->perl);
807 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
813 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
815 rad_reply_hv = newHV();
816 rad_check_hv = newHV();
817 rad_request_hv = newHV();
818 rad_request_proxy_hv = newHV();
819 rad_request_proxy_reply_hv = newHV();
821 rad_reply_hv = get_hv("RAD_REPLY",1);
822 rad_check_hv = get_hv("RAD_CHECK",1);
823 rad_request_hv = get_hv("RAD_REQUEST",1);
824 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
825 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
827 xlat_name = cf_section_name2(conf);
828 if (xlat_name == NULL)
829 xlat_name = cf_section_name1(conf);
831 inst->xlat_name = strdup(xlat_name);
832 xlat_register(xlat_name, perl_xlat, inst);
836 if ((init_pool(conf, inst)) == -1) {
837 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
848 * get the vps and put them in perl hash
849 * If one VP have multiple values it is added as array_ref
850 * Example for this is Cisco-AVPair that holds multiple values.
851 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
853 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
855 VALUE_PAIR *nvp, *vpa, *vpn;
863 while (nvp != NULL) {
864 attr = nvp->attribute;
865 vpa = paircopy2(nvp,attr);
870 len = vp_prints_value(buffer, sizeof(buffer),
872 av_push(av, newSVpv(buffer, len));
875 hv_store(rad_hv, nvp->name, strlen(nvp->name),
876 newRV_noinc((SV *) av), 0);
878 len = vp_prints_value(buffer, sizeof(buffer),
880 hv_store(rad_hv, vpa->name, strlen(vpa->name),
881 newSVpv(buffer, len), 0);
885 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
887 pairdelete(&nvp, attr);
894 * Verify that a Perl SV is a string and save it in FreeRadius
898 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv) {
903 val = SvPV_nolen(sv);
904 vpp = pairmake(key, val, T_OP_EQ);
908 "rlm_perl: Added pair %s = %s", key, val);
912 "rlm_perl: ERROR: Failed to create pair %s = %s",
921 * Gets the content from hashes
923 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
928 I32 key_len, len, i, j;
931 for (i = hv_iterinit(my_hv); i > 0; i--) {
932 res_sv = hv_iternextsv(my_hv,&key,&key_len);
933 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
934 av = (AV*)SvRV(res_sv);
936 for (j = 0; j <= len; j++) {
937 av_sv = av_fetch(av, j, 0);
938 ret = pairadd_sv(vp, key, *av_sv) + ret;
940 } else ret = pairadd_sv(vp, key, res_sv) + ret;
947 * Call the function_name inside the module
948 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
951 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
954 PERL_INST *inst = instance;
956 int exitstatus=0, count;
962 HV *rad_request_proxy_hv;
963 HV *rad_request_proxy_reply_hv;
968 if ((handle = pool_pop(instance)) == NULL) {
969 return RLM_MODULE_FAIL;
972 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
974 dTHXa(handle->clone);
975 PERL_SET_CONTEXT(handle->clone);
978 PERL_SET_CONTEXT(inst->perl);
979 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
989 * Radius has told us to call this function, but none
992 if (!function_name) {
993 return RLM_MODULE_FAIL;
996 rad_reply_hv = get_hv("RAD_REPLY",1);
997 rad_check_hv = get_hv("RAD_CHECK",1);
998 rad_request_hv = get_hv("RAD_REQUEST",1);
999 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
1000 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
1003 perl_store_vps(request->reply->vps, rad_reply_hv);
1004 perl_store_vps(request->config_items, rad_check_hv);
1005 perl_store_vps(request->packet->vps, rad_request_hv);
1007 if (request->proxy != NULL) {
1008 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
1010 hv_undef(rad_request_proxy_hv);
1013 if (request->proxy_reply !=NULL) {
1014 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1016 hv_undef(rad_request_proxy_reply_hv);
1024 * This way %RAD_xx can be pushed onto stack as sub parameters.
1025 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1026 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1027 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1031 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1037 if (exitstatus >= 100 || exitstatus < 0) {
1038 exitstatus = RLM_MODULE_FAIL;
1046 if (SvTRUE(ERRSV)) {
1047 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1049 function_name, SvPV(ERRSV,n_a));
1052 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1053 pairmove(&request->reply->vps, &vp);
1057 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1058 pairmove(&request->config_items, &vp);
1062 if ((get_hv_content(rad_request_proxy_reply_hv, &vp)) > 0 && request->proxy_reply != NULL) {
1063 pairfree(&request->proxy_reply->vps);
1064 pairmove(&request->proxy_reply->vps, &vp);
1069 pool_release(handle,instance);
1070 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1077 * Find the named user in this modules database. Create the set
1078 * of attribute-value pairs to check and reply with for this user
1079 * from the database. The authentication code only needs to check
1080 * the password, the rest is done here.
1082 static int perl_authorize(void *instance, REQUEST *request)
1084 return rlmperl_call(instance, request,
1085 ((PERL_INST *)instance)->func_authorize);
1089 * Authenticate the user with the given password.
1091 static int perl_authenticate(void *instance, REQUEST *request)
1093 return rlmperl_call(instance, request,
1094 ((PERL_INST *)instance)->func_authenticate);
1097 * Massage the request before recording it or proxying it
1099 static int perl_preacct(void *instance, REQUEST *request)
1101 return rlmperl_call(instance, request,
1102 ((PERL_INST *)instance)->func_preacct);
1105 * Write accounting information to this modules database.
1107 static int perl_accounting(void *instance, REQUEST *request)
1110 int acctstatustype=0;
1112 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1113 acctstatustype = pair->lvalue;
1115 radlog(L_ERR, "Invalid Accounting Packet");
1116 return RLM_MODULE_INVALID;
1119 switch (acctstatustype) {
1121 case PW_STATUS_START:
1123 if (((PERL_INST *)instance)->func_start_accounting) {
1124 return rlmperl_call(instance, request,
1125 ((PERL_INST *)instance)->func_start_accounting);
1127 return rlmperl_call(instance, request,
1128 ((PERL_INST *)instance)->func_accounting);
1132 case PW_STATUS_STOP:
1134 if (((PERL_INST *)instance)->func_stop_accounting) {
1135 return rlmperl_call(instance, request,
1136 ((PERL_INST *)instance)->func_stop_accounting);
1138 return rlmperl_call(instance, request,
1139 ((PERL_INST *)instance)->func_accounting);
1143 return rlmperl_call(instance, request,
1144 ((PERL_INST *)instance)->func_accounting);
1149 * Check for simultaneouse-use
1151 static int perl_checksimul(void *instance, REQUEST *request)
1153 return rlmperl_call(instance, request,
1154 ((PERL_INST *)instance)->func_checksimul);
1159 static int perl_pre_proxy(void *instance, REQUEST *request)
1161 return rlmperl_call(instance, request,
1162 ((PERL_INST *)instance)->func_pre_proxy);
1165 * Post-Proxy request
1167 static int perl_post_proxy(void *instance, REQUEST *request)
1169 return rlmperl_call(instance, request,
1170 ((PERL_INST *)instance)->func_post_proxy);
1175 static int perl_post_auth(void *instance, REQUEST *request)
1177 return rlmperl_call(instance, request,
1178 ((PERL_INST *)instance)->func_post_auth);
1181 * Detach a instance give a chance to a module to make some internal setup ...
1183 static int perl_detach(void *instance)
1185 PERL_INST *inst = (PERL_INST *) instance;
1186 int exitstatus=0,count=0;
1189 POOL_HANDLE *handle, *tmp, *tmp2;
1191 MUTEX_LOCK(&inst->perl_pool->mutex);
1192 inst->perl_pool->detach = yes;
1193 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1195 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1197 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1199 * Wait until clone becomes idle
1201 MUTEX_LOCK(&handle->lock);
1204 * Give a clones chance to run detach function
1207 dTHXa(handle->clone);
1208 PERL_SET_CONTEXT(handle->clone);
1210 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1211 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1217 * FIXME: bug in perl
1220 if (exitstatus >= 100 || exitstatus < 0) {
1221 exitstatus = RLM_MODULE_FAIL;
1227 radlog(L_DBG,"detach at 0x%lx returned status %d",
1228 (unsigned long) handle->clone, exitstatus);
1231 MUTEX_UNLOCK(&handle->lock);
1237 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1239 radlog(L_DBG,"rlm_perl:: Destroy perl");
1240 rlm_perl_destruct(tmp->clone);
1241 delete_pool_handle(tmp,inst);
1246 #endif /* USE_ITHREADS */
1247 PERL_SET_CONTEXT(inst->perl);
1249 dSP; ENTER; SAVETMPS;
1252 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1257 if (exitstatus >= 100 || exitstatus < 0) {
1258 exitstatus = RLM_MODULE_FAIL;
1269 xlat_unregister(inst->xlat_name, perl_xlat);
1270 free(inst->xlat_name);
1272 if (inst->func_authorize) free(inst->func_authorize);
1273 if (inst->func_authenticate) free(inst->func_authenticate);
1274 if (inst->func_accounting) free(inst->func_accounting);
1275 if (inst->func_preacct) free(inst->func_preacct);
1276 if (inst->func_checksimul) free(inst->func_checksimul);
1277 if (inst->func_pre_proxy) free(inst->func_pre_proxy);
1278 if (inst->func_post_proxy) free(inst->func_post_proxy);
1279 if (inst->func_post_auth) free(inst->func_post_auth);
1280 if (inst->func_detach) free(inst->func_detach);
1283 free(inst->perl_pool->head);
1284 free(inst->perl_pool->tail);
1285 MUTEX_DESTROY(&inst->perl_pool->mutex);
1286 free(inst->perl_pool);
1287 rlm_perl_destruct(inst->perl);
1289 perl_destruct(inst->perl);
1290 perl_free(inst->perl);
1297 * The module name should be the only globally exported symbol.
1298 * That is, everything else should be 'static'.
1300 * If the module needs to temporarily modify it's instantiation
1301 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1302 * The server will then take care of ensuring that the module
1303 * is single-threaded.
1305 module_t rlm_perl = {
1308 RLM_TYPE_THREAD_SAFE, /* type */
1310 RLM_TYPE_THREAD_UNSAFE,
1312 perl_init, /* initialization */
1313 perl_instantiate, /* instantiation */
1319 perl_checksimul, /* check simul */
1320 perl_pre_proxy, /* pre-proxy */
1321 perl_post_proxy, /* post-proxy */
1322 perl_post_auth /* post-auth */
1324 perl_detach, /* detach */