6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2002 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>
45 #include <semaphore.h>
48 extern char **environ;
51 static const char rcsid[] = "$Id$";
56 * Pool of Perl's clones (genetically cloned) ;)
59 typedef struct pool_handle {
60 struct pool_handle *next;
61 struct pool_handle *prev;
62 enum {busy, idle} status;
63 unsigned int request_count;
64 PerlInterpreter *clone;
68 typedef struct PERL_POOL {
78 int max_request_per_clone;
82 time_t time_when_last_added;
88 * Define a structure for our module configuration.
90 * These variables do not need to be in a structure, but it's
91 * a lot cleaner to do so, and a pointer to the structure can
92 * be used as the instance handle.
94 typedef struct perl_inst {
95 /* Name of the perl module */
98 /* Name of the functions for each module method */
100 char *func_authenticate;
101 char *func_accounting;
102 char *func_start_accounting;
103 char *func_stop_accounting;
105 char *func_checksimul;
108 char *func_pre_proxy;
109 char *func_post_proxy;
110 char *func_post_auth;
113 PerlInterpreter *perl;
115 PERL_POOL *perl_pool;
119 * A mapping of configuration file names to internal variables.
121 * Note that the string is dynamically allocated, so it MUST
122 * be freed. When the configuration file parse re-reads the string,
123 * it free's the old one, and strdup's the new one, placing the pointer
124 * to the strdup'd string into 'config.string'. This gets around
127 static const CONF_PARSER module_config[] = {
128 { "module", PW_TYPE_FILENAME,
129 offsetof(PERL_INST,module), NULL, "module"},
130 { "func_authorize", PW_TYPE_STRING_PTR,
131 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
132 { "func_authenticate", PW_TYPE_STRING_PTR,
133 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
134 { "func_accounting", PW_TYPE_STRING_PTR,
135 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
136 { "func_preacct", PW_TYPE_STRING_PTR,
137 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
138 { "func_checksimul", PW_TYPE_STRING_PTR,
139 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
140 { "func_detach", PW_TYPE_STRING_PTR,
141 offsetof(PERL_INST,func_detach), NULL, "detach"},
142 { "func_xlat", PW_TYPE_STRING_PTR,
143 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
144 { "func_pre_proxy", PW_TYPE_STRING_PTR,
145 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
146 { "func_post_proxy", PW_TYPE_STRING_PTR,
147 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
148 { "func_post_auth", PW_TYPE_STRING_PTR,
149 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
150 { "perl_flags", PW_TYPE_STRING_PTR,
151 offsetof(PERL_INST,perl_flags), NULL, NULL},
152 { "func_start_accounting", PW_TYPE_STRING_PTR,
153 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
154 { "func_stop_accounting", PW_TYPE_STRING_PTR,
155 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
157 { NULL, -1, 0, NULL, NULL } /* end the list */
163 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
167 * We use one perl to clone from it i.e. main boss
168 * We clone it for every instance if we have perl
169 * with -Duseithreads compiled in
171 static PerlInterpreter *interp = NULL;
173 static const CONF_PARSER pool_conf[] = {
174 { "max_clones", PW_TYPE_INTEGER, offsetof(PERL_POOL, max_clones), NULL, "32"},
175 { "start_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, start_clones), NULL, "32"},
176 { "min_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, min_spare_clones),NULL, "0"},
177 { "max_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_spare_clones),NULL, "32"},
178 { "cleanup_delay",PW_TYPE_INTEGER, offsetof(PERL_POOL,cleanup_delay),NULL, "5"},
179 { "max_request_per_clone",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_request_per_clone),NULL, "0"},
180 { NULL, -1, 0, NULL, NULL } /* end the list */
184 #define dl_librefs "DynaLoader::dl_librefs"
185 #define dl_modules "DynaLoader::dl_modules"
186 static void rlm_perl_clear_handles(pTHX)
188 AV *librefs = get_av(dl_librefs, FALSE);
194 static void **rlm_perl_get_handles(pTHX)
197 AV *librefs = get_av(dl_librefs, FALSE);
198 AV *modules = get_av(dl_modules, FALSE);
203 "Could not get @%s for unloading.\n",
208 if (!(AvFILL(librefs) >= 0)) {
212 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
214 for (i=0; i<=AvFILL(librefs); i++) {
216 SV *handle_sv = *av_fetch(librefs, i, FALSE);
220 "Could not fetch $%s[%d]!\n",
224 handle = (void *)SvIV(handle_sv);
234 handles[i] = (void *)0;
239 static void rlm_perl_close_handles(void **handles)
247 for (i=0; handles[i]; i++) {
248 radlog(L_DBG, "close 0x%lx\n", (unsigned long)handles[i]);
255 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
257 PerlInterpreter *clone;
260 PERL_SET_CONTEXT(perl);
262 clone = perl_clone(perl, clone_flags);
266 #if PERL_REVISION >= 5 && PERL_VERSION <8
269 ptr_table_free(PL_ptr_table);
272 PERL_SET_CONTEXT(aTHX);
273 rlm_perl_clear_handles(aTHX);
278 static void rlm_perl_destruct(PerlInterpreter *perl)
280 char **orig_environ = NULL;
283 PERL_SET_CONTEXT(perl);
285 PL_perl_destruct_level = 2;
287 PL_origenviron = environ;
293 * FIXME: This shouldn't happen
296 while (PL_scopestack_ix > 1 ){
304 environ = orig_environ;
308 static void rlm_destroy_perl(PerlInterpreter *perl)
313 PERL_SET_CONTEXT(perl);
315 handles = rlm_perl_get_handles(aTHX);
316 rlm_perl_destruct(perl);
317 rlm_perl_close_handles(handles);
320 static void delete_pool_handle(POOL_HANDLE *handle, PERL_INST *inst)
329 inst->perl_pool->head = next;
335 inst->perl_pool->tail = prev;
339 inst->perl_pool->current_clones--;
340 MUTEX_DESTROY(&handle->lock);
344 static void move2tail(POOL_HANDLE *handle, PERL_INST *inst)
349 if (inst->perl_pool->head == NULL) {
353 inst->perl_pool->head = handle;
354 inst->perl_pool->tail = handle;
358 if (inst->perl_pool->tail == handle) {
365 if ((next != NULL) ||
372 inst->perl_pool->head = next;
383 prev = inst->perl_pool->tail;
385 inst->perl_pool->tail = handle;
391 static POOL_HANDLE *pool_grow (PERL_INST *inst) {
395 if (inst->perl_pool->max_clones == inst->perl_pool->current_clones) {
398 if (inst->perl_pool->detach == yes ) {
402 handle = (POOL_HANDLE *)rad_malloc(sizeof(POOL_HANDLE));
405 radlog(L_ERR,"Could not find free memory for pool. Aborting");
411 handle->status = idle;
412 handle->clone = rlm_perl_clone(inst->perl);
413 handle->request_count = 0;
414 MUTEX_INIT(&handle->lock);
415 inst->perl_pool->current_clones++;
416 move2tail(handle, inst);
419 inst->perl_pool->time_when_last_added = now;
424 static POOL_HANDLE *pool_pop(PERL_INST *inst)
430 * Lock the pool and be fast other thread maybe
431 * waiting for us to finish
433 MUTEX_LOCK(&inst->perl_pool->mutex);
437 for (handle = inst->perl_pool->head; handle ; handle = tmp) {
440 if (handle->status == idle){
447 if (inst->perl_pool->current_clones < inst->perl_pool->max_clones ) {
449 found = pool_grow(inst);
452 radlog(L_ERR,"Cannot grow pool returning");
453 MUTEX_UNLOCK(&inst->perl_pool->mutex);
457 radlog(L_ERR,"rlm_perl:: reached maximum clones %d cannot grow",
458 inst->perl_pool->current_clones);
459 MUTEX_UNLOCK(&inst->perl_pool->mutex);
464 move2tail(found, inst);
465 found->status = busy;
466 MUTEX_LOCK(&found->lock);
467 inst->perl_pool->active_clones++;
468 found->request_count++;
472 MUTEX_UNLOCK(&inst->perl_pool->mutex);
473 radlog(L_DBG,"perl_pool: item 0x%lx asigned new request. Handled so far: %d",
474 (unsigned long) found->clone, found->request_count);
477 static int pool_release(POOL_HANDLE *handle, PERL_INST *inst) {
479 POOL_HANDLE *tmp, *tmp2;
485 MUTEX_LOCK(&inst->perl_pool->mutex);
488 * If detach is set then just release the mutex
490 if (inst->perl_pool->detach == yes ) {
491 handle->status = idle;
492 MUTEX_UNLOCK(&handle->lock);
493 MUTEX_UNLOCK(&inst->perl_pool->mutex);
497 MUTEX_UNLOCK(&handle->lock);
498 handle->status = idle;
499 inst->perl_pool->active_clones--;
501 spare = inst->perl_pool->current_clones - inst->perl_pool->active_clones;
503 radlog(L_DBG,"perl_pool total/active/spare [%d/%d/%d]"
504 , inst->perl_pool->current_clones, inst->perl_pool->active_clones, spare);
506 if (spare < inst->perl_pool->min_spare_clones) {
507 t = inst->perl_pool->min_spare_clones - spare;
509 if ((tmp = pool_grow(inst)) == NULL) {
510 MUTEX_UNLOCK(&inst->perl_pool->mutex);
514 MUTEX_UNLOCK(&inst->perl_pool->mutex);
518 if ((now - inst->perl_pool->time_when_last_added) < inst->perl_pool->cleanup_delay) {
519 MUTEX_UNLOCK(&inst->perl_pool->mutex);
522 if (spare > inst->perl_pool->max_spare_clones) {
523 spare -= inst->perl_pool->max_spare_clones;
524 for (tmp = inst->perl_pool->head; (tmp !=NULL ) && (spare > 0) ; tmp = tmp2) {
527 if(tmp->status == idle) {
528 rlm_destroy_perl(tmp->clone);
529 delete_pool_handle(tmp,inst);
536 * If the clone have reached max_request_per_clone clean it.
538 if (inst->perl_pool->max_request_per_clone > 0 ) {
539 if (handle->request_count > inst->perl_pool->max_request_per_clone) {
540 rlm_destroy_perl(handle->clone);
541 delete_pool_handle(handle,inst);
547 MUTEX_UNLOCK(&inst->perl_pool->mutex);
550 static int init_pool (CONF_SECTION *conf, PERL_INST *inst) {
556 pool = rad_malloc(sizeof(PERL_POOL));
557 memset(pool,0,sizeof(PERL_POOL));
559 inst->perl_pool = pool;
561 MUTEX_INIT(&pool->mutex);
568 cf_section_parse(conf,pool,pool_conf);
569 inst->perl_pool = pool;
570 inst->perl_pool->detach = no;
572 for(t = 0;t < inst->perl_pool->start_clones ;t++){
573 if ((handle = pool_grow(inst)) == NULL) {
583 static void xs_init(pTHX)
585 char *file = __FILE__;
587 /* DynaLoader is a special case */
588 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
593 * This is wrapper for radlog
594 * Now users can call radiusd::radlog(level,msg) wich is the same
595 * calling radlog from C code.
598 static XS(XS_radiusd_radlog)
602 croak("Usage: radiusd::radlog(level, message)");
607 level = (int) SvIV(ST(0));
608 msg = (char *) SvPV(ST(1), PL_na);
611 * Because 'msg' is a 'char *', we don't want '%s', etc.
612 * in it to give us printf-style vulnerabilities.
614 radlog(level, "rlm_perl: %s", msg);
622 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
623 size_t freespace, RADIUS_ESCAPE_STRING func)
626 PERL_INST *inst= (PERL_INST *) instance;
627 PerlInterpreter *perl;
628 char params[1024], *ptr, *tmp;
633 * Do an xlat on the provided string (nice recursive operation).
635 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
636 radlog(L_ERR, "rlm_perl: xlat failed.");
645 if ((handle = pool_pop(instance)) == NULL) {
649 perl = handle->clone;
651 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
656 PERL_SET_CONTEXT(perl);
661 ptr = strtok(params, " ");
665 while (ptr != NULL) {
666 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
667 ptr = strtok(NULL, " ");
672 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
676 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
679 } else if (count > 0) {
682 strncpy(out,tmp,ret);
684 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
694 pool_release(handle, instance);
699 * Do any per-module initialization that is separate to each
700 * configured instance of the module. e.g. set up connections
701 * to external databases, read configuration files, set up
702 * dictionary entries, etc.
704 * If configuration information is given in the config section
705 * that must be referenced in later calls, store a handle to it
706 * in *instance otherwise put a null pointer there.
709 * Setup a hashes wich we will use later
710 * parse a module and give him a chance to live
713 static int perl_instantiate(CONF_SECTION *conf, void **instance)
715 PERL_INST *inst = (PERL_INST *) instance;
719 HV *rad_request_proxy_hv;
720 HV *rad_request_proxy_reply_hv;
724 const char *xlat_name;
725 int exitstatus = 0, argc=0;
728 * Set up a storage area for instance data
730 inst = rad_malloc(sizeof(PERL_INST));
731 memset(inst, 0, sizeof(PERL_INST));
734 * If the configuration parameters can't be parsed, then
737 if (cf_section_parse(conf, inst, module_config) < 0) {
744 if (inst->perl_flags) {
745 embed[1] = inst->perl_flags;
746 embed[2] = inst->module;
750 embed[1] = inst->module;
758 if ((inst->perl = perl_alloc()) == NULL) {
759 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
763 perl_construct(inst->perl);
764 PL_perl_destruct_level = 2;
769 PERL_SET_CONTEXT(inst->perl);
771 if ((inst->perl = perl_alloc()) == NULL) {
772 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
776 perl_construct(inst->perl);
779 #if PERL_REVISION >= 5 && PERL_VERSION >=8
780 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
783 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
789 exitstatus = perl_run(inst->perl);
791 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
797 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
799 rad_reply_hv = newHV();
800 rad_check_hv = newHV();
801 rad_request_hv = newHV();
802 rad_request_proxy_hv = newHV();
803 rad_request_proxy_reply_hv = newHV();
805 rad_reply_hv = get_hv("RAD_REPLY",1);
806 rad_check_hv = get_hv("RAD_CHECK",1);
807 rad_request_hv = get_hv("RAD_REQUEST",1);
808 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
809 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
811 xlat_name = cf_section_name2(conf);
812 if (xlat_name == NULL)
813 xlat_name = cf_section_name1(conf);
815 inst->xlat_name = strdup(xlat_name);
816 xlat_register(xlat_name, perl_xlat, inst);
820 if ((init_pool(conf, inst)) == -1) {
821 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
832 * get the vps and put them in perl hash
833 * If one VP have multiple values it is added as array_ref
834 * Example for this is Cisco-AVPair that holds multiple values.
835 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
837 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
839 VALUE_PAIR *nvp, *vpa, *vpn;
847 while (nvp != NULL) {
848 attr = nvp->attribute;
849 vpa = paircopy2(nvp,attr);
854 len = vp_prints_value(buffer, sizeof(buffer),
856 av_push(av, newSVpv(buffer, len));
859 hv_store(rad_hv, nvp->name, strlen(nvp->name),
860 newRV_noinc((SV *) av), 0);
862 len = vp_prints_value(buffer, sizeof(buffer),
864 hv_store(rad_hv, vpa->name, strlen(vpa->name),
865 newSVpv(buffer, len), 0);
869 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
871 pairdelete(&nvp, attr);
878 * Verify that a Perl SV is a string and save it in FreeRadius
882 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
887 val = SvPV_nolen(sv);
888 vpp = pairmake(key, val, operator);
892 "rlm_perl: Added pair %s = %s", key, val);
896 "rlm_perl: ERROR: Failed to create pair %s = %s",
905 * Gets the content from hashes
907 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
912 I32 key_len, len, i, j;
916 for (i = hv_iterinit(my_hv); i > 0; i--) {
917 res_sv = hv_iternextsv(my_hv,&key,&key_len);
918 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
919 av = (AV*)SvRV(res_sv);
921 for (j = 0; j <= len; j++) {
922 av_sv = av_fetch(av, j, 0);
923 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
925 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
932 * Call the function_name inside the module
933 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
936 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
939 PERL_INST *inst = instance;
941 int exitstatus=0, count;
947 HV *rad_request_proxy_hv;
948 HV *rad_request_proxy_reply_hv;
953 if ((handle = pool_pop(instance)) == NULL) {
954 return RLM_MODULE_FAIL;
957 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
959 dTHXa(handle->clone);
960 PERL_SET_CONTEXT(handle->clone);
963 PERL_SET_CONTEXT(inst->perl);
964 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
974 * Radius has told us to call this function, but none
977 if (!function_name) {
978 return RLM_MODULE_FAIL;
981 rad_reply_hv = get_hv("RAD_REPLY",1);
982 rad_check_hv = get_hv("RAD_CHECK",1);
983 rad_request_hv = get_hv("RAD_REQUEST",1);
984 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
985 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
988 perl_store_vps(request->reply->vps, rad_reply_hv);
989 perl_store_vps(request->config_items, rad_check_hv);
990 perl_store_vps(request->packet->vps, rad_request_hv);
992 if (request->proxy != NULL) {
993 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
995 hv_undef(rad_request_proxy_hv);
998 if (request->proxy_reply !=NULL) {
999 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1001 hv_undef(rad_request_proxy_reply_hv);
1006 * This way %RAD_xx can be pushed onto stack as sub parameters.
1007 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1008 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1009 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1013 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1017 if (SvTRUE(ERRSV)) {
1018 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1020 function_name, SvPV(ERRSV,n_a));
1026 if (exitstatus >= 100 || exitstatus < 0) {
1027 exitstatus = RLM_MODULE_FAIL;
1037 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
1038 pairfree(&request->packet->vps);
1039 request->packet->vps = vp;
1043 * Update cached copies
1045 request->username = pairfind(request->packet->vps,
1047 request->password = pairfind(request->packet->vps,
1051 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1052 pairfree(&request->reply->vps);
1053 request->reply->vps = vp;
1057 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1058 pairfree(&request->config_items);
1059 request->config_items = vp;
1063 if (request->proxy &&
1064 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
1065 pairfree(&request->proxy->vps);
1066 request->proxy->vps = vp;
1070 if (request->proxy_reply &&
1071 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
1072 pairfree(&request->proxy_reply->vps);
1073 request->proxy_reply->vps = vp;
1079 pool_release(handle,instance);
1080 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1087 * Find the named user in this modules database. Create the set
1088 * of attribute-value pairs to check and reply with for this user
1089 * from the database. The authentication code only needs to check
1090 * the password, the rest is done here.
1092 static int perl_authorize(void *instance, REQUEST *request)
1094 return rlmperl_call(instance, request,
1095 ((PERL_INST *)instance)->func_authorize);
1099 * Authenticate the user with the given password.
1101 static int perl_authenticate(void *instance, REQUEST *request)
1103 return rlmperl_call(instance, request,
1104 ((PERL_INST *)instance)->func_authenticate);
1107 * Massage the request before recording it or proxying it
1109 static int perl_preacct(void *instance, REQUEST *request)
1111 return rlmperl_call(instance, request,
1112 ((PERL_INST *)instance)->func_preacct);
1115 * Write accounting information to this modules database.
1117 static int perl_accounting(void *instance, REQUEST *request)
1120 int acctstatustype=0;
1122 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1123 acctstatustype = pair->lvalue;
1125 radlog(L_ERR, "Invalid Accounting Packet");
1126 return RLM_MODULE_INVALID;
1129 switch (acctstatustype) {
1131 case PW_STATUS_START:
1133 if (((PERL_INST *)instance)->func_start_accounting) {
1134 return rlmperl_call(instance, request,
1135 ((PERL_INST *)instance)->func_start_accounting);
1137 return rlmperl_call(instance, request,
1138 ((PERL_INST *)instance)->func_accounting);
1142 case PW_STATUS_STOP:
1144 if (((PERL_INST *)instance)->func_stop_accounting) {
1145 return rlmperl_call(instance, request,
1146 ((PERL_INST *)instance)->func_stop_accounting);
1148 return rlmperl_call(instance, request,
1149 ((PERL_INST *)instance)->func_accounting);
1153 return rlmperl_call(instance, request,
1154 ((PERL_INST *)instance)->func_accounting);
1159 * Check for simultaneouse-use
1161 static int perl_checksimul(void *instance, REQUEST *request)
1163 return rlmperl_call(instance, request,
1164 ((PERL_INST *)instance)->func_checksimul);
1169 static int perl_pre_proxy(void *instance, REQUEST *request)
1171 return rlmperl_call(instance, request,
1172 ((PERL_INST *)instance)->func_pre_proxy);
1175 * Post-Proxy request
1177 static int perl_post_proxy(void *instance, REQUEST *request)
1179 return rlmperl_call(instance, request,
1180 ((PERL_INST *)instance)->func_post_proxy);
1185 static int perl_post_auth(void *instance, REQUEST *request)
1187 return rlmperl_call(instance, request,
1188 ((PERL_INST *)instance)->func_post_auth);
1191 * Detach a instance give a chance to a module to make some internal setup ...
1193 static int perl_detach(void *instance)
1195 PERL_INST *inst = (PERL_INST *) instance;
1196 int exitstatus = 0, count = 0;
1199 POOL_HANDLE *handle, *tmp, *tmp2;
1201 MUTEX_LOCK(&inst->perl_pool->mutex);
1202 inst->perl_pool->detach = yes;
1203 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1205 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1207 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1209 * Wait until clone becomes idle
1211 MUTEX_LOCK(&handle->lock);
1214 * Give a clones chance to run detach function
1217 dTHXa(handle->clone);
1218 PERL_SET_CONTEXT(handle->clone);
1220 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1221 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1227 * FIXME: bug in perl
1230 if (exitstatus >= 100 || exitstatus < 0) {
1231 exitstatus = RLM_MODULE_FAIL;
1237 radlog(L_DBG,"detach at 0x%lx returned status %d",
1238 (unsigned long) handle->clone, exitstatus);
1241 MUTEX_UNLOCK(&handle->lock);
1247 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1249 radlog(L_DBG,"rlm_perl:: Destroy perl");
1250 rlm_perl_destruct(tmp->clone);
1251 delete_pool_handle(tmp,inst);
1256 #endif /* USE_ITHREADS */
1257 PERL_SET_CONTEXT(inst->perl);
1259 dSP; ENTER; SAVETMPS;
1262 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1267 if (exitstatus >= 100 || exitstatus < 0) {
1268 exitstatus = RLM_MODULE_FAIL;
1279 xlat_unregister(inst->xlat_name, perl_xlat);
1280 free(inst->xlat_name);
1282 if (inst->func_authorize) free(inst->func_authorize);
1283 if (inst->func_authenticate) free(inst->func_authenticate);
1284 if (inst->func_accounting) free(inst->func_accounting);
1285 if (inst->func_preacct) free(inst->func_preacct);
1286 if (inst->func_checksimul) free(inst->func_checksimul);
1287 if (inst->func_pre_proxy) free(inst->func_pre_proxy);
1288 if (inst->func_post_proxy) free(inst->func_post_proxy);
1289 if (inst->func_post_auth) free(inst->func_post_auth);
1290 if (inst->func_detach) free(inst->func_detach);
1293 free(inst->perl_pool->head);
1294 free(inst->perl_pool->tail);
1295 MUTEX_DESTROY(&inst->perl_pool->mutex);
1296 free(inst->perl_pool);
1297 rlm_perl_destruct(inst->perl);
1299 perl_destruct(inst->perl);
1300 perl_free(inst->perl);
1307 * The module name should be the only globally exported symbol.
1308 * That is, everything else should be 'static'.
1310 * If the module needs to temporarily modify it's instantiation
1311 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1312 * The server will then take care of ensuring that the module
1313 * is single-threaded.
1315 module_t rlm_perl = {
1319 RLM_TYPE_THREAD_SAFE, /* type */
1321 RLM_TYPE_THREAD_UNSAFE,
1323 perl_instantiate, /* instantiation */
1324 perl_detach, /* detach */
1326 perl_authenticate, /* authenticate */
1327 perl_authorize, /* authorize */
1328 perl_preacct, /* preacct */
1329 perl_accounting, /* accounting */
1330 perl_checksimul, /* check simul */
1331 perl_pre_proxy, /* pre-proxy */
1332 perl_post_proxy, /* post-proxy */
1333 perl_post_auth /* post-auth */