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,2006 The FreeRADIUS server project
21 * Copyright 2002 Boian Jordanov <bjordanov@orbitel.bg>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
42 #include <semaphore.h>
45 extern char **environ;
51 * Pool of Perl's clones (genetically cloned) ;)
54 typedef struct pool_handle {
55 struct pool_handle *next;
56 struct pool_handle *prev;
57 enum {busy, idle} status;
58 unsigned int request_count;
59 PerlInterpreter *clone;
63 typedef struct PERL_POOL {
73 int max_request_per_clone;
77 time_t time_when_last_added;
83 * Define a structure for our module configuration.
85 * These variables do not need to be in a structure, but it's
86 * a lot cleaner to do so, and a pointer to the structure can
87 * be used as the instance handle.
89 typedef struct perl_inst {
90 /* Name of the perl module */
93 /* Name of the functions for each module method */
95 char *func_authenticate;
96 char *func_accounting;
97 char *func_start_accounting;
98 char *func_stop_accounting;
100 char *func_checksimul;
103 char *func_pre_proxy;
104 char *func_post_proxy;
105 char *func_post_auth;
108 PerlInterpreter *perl;
110 PERL_POOL *perl_pool;
114 * A mapping of configuration file names to internal variables.
116 * Note that the string is dynamically allocated, so it MUST
117 * be freed. When the configuration file parse re-reads the string,
118 * it free's the old one, and strdup's the new one, placing the pointer
119 * to the strdup'd string into 'config.string'. This gets around
122 static const CONF_PARSER module_config[] = {
123 { "module", PW_TYPE_FILENAME,
124 offsetof(PERL_INST,module), NULL, "module"},
125 { "func_authorize", PW_TYPE_STRING_PTR,
126 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
127 { "func_authenticate", PW_TYPE_STRING_PTR,
128 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
129 { "func_accounting", PW_TYPE_STRING_PTR,
130 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
131 { "func_preacct", PW_TYPE_STRING_PTR,
132 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
133 { "func_checksimul", PW_TYPE_STRING_PTR,
134 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
135 { "func_detach", PW_TYPE_STRING_PTR,
136 offsetof(PERL_INST,func_detach), NULL, "detach"},
137 { "func_xlat", PW_TYPE_STRING_PTR,
138 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
139 { "func_pre_proxy", PW_TYPE_STRING_PTR,
140 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
141 { "func_post_proxy", PW_TYPE_STRING_PTR,
142 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
143 { "func_post_auth", PW_TYPE_STRING_PTR,
144 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
145 { "perl_flags", PW_TYPE_STRING_PTR,
146 offsetof(PERL_INST,perl_flags), NULL, NULL},
147 { "func_start_accounting", PW_TYPE_STRING_PTR,
148 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
149 { "func_stop_accounting", PW_TYPE_STRING_PTR,
150 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
152 { NULL, -1, 0, NULL, NULL } /* end the list */
158 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
162 * We use one perl to clone from it i.e. main boss
163 * We clone it for every instance if we have perl
164 * with -Duseithreads compiled in
166 static PerlInterpreter *interp = NULL;
168 static const CONF_PARSER pool_conf[] = {
169 { "max_clones", PW_TYPE_INTEGER, offsetof(PERL_POOL, max_clones), NULL, "32"},
170 { "start_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, start_clones), NULL, "32"},
171 { "min_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, min_spare_clones),NULL, "0"},
172 { "max_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_spare_clones),NULL, "32"},
173 { "cleanup_delay",PW_TYPE_INTEGER, offsetof(PERL_POOL,cleanup_delay),NULL, "5"},
174 { "max_request_per_clone",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_request_per_clone),NULL, "0"},
175 { NULL, -1, 0, NULL, NULL } /* end the list */
179 #define dl_librefs "DynaLoader::dl_librefs"
180 #define dl_modules "DynaLoader::dl_modules"
181 static void rlm_perl_clear_handles(pTHX)
183 AV *librefs = get_av(dl_librefs, FALSE);
189 static void **rlm_perl_get_handles(pTHX)
192 AV *librefs = get_av(dl_librefs, FALSE);
193 AV *modules = get_av(dl_modules, FALSE);
198 "Could not get @%s for unloading.\n",
203 if (!(AvFILL(librefs) >= 0)) {
207 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
209 for (i=0; i<=AvFILL(librefs); i++) {
211 SV *handle_sv = *av_fetch(librefs, i, FALSE);
215 "Could not fetch $%s[%d]!\n",
219 handle = (void *)SvIV(handle_sv);
229 handles[i] = (void *)0;
234 static void rlm_perl_close_handles(void **handles)
242 for (i=0; handles[i]; i++) {
243 radlog(L_DBG, "close 0x%lx\n", (unsigned long)handles[i]);
250 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
252 PerlInterpreter *clone;
255 PERL_SET_CONTEXT(perl);
257 clone = perl_clone(perl, clone_flags);
261 #if PERL_REVISION >= 5 && PERL_VERSION <8
264 ptr_table_free(PL_ptr_table);
267 PERL_SET_CONTEXT(aTHX);
268 rlm_perl_clear_handles(aTHX);
273 static void rlm_perl_destruct(PerlInterpreter *perl)
275 char **orig_environ = NULL;
278 PERL_SET_CONTEXT(perl);
280 PL_perl_destruct_level = 2;
282 PL_origenviron = environ;
288 * FIXME: This shouldn't happen
291 while (PL_scopestack_ix > 1 ){
299 environ = orig_environ;
303 static void rlm_destroy_perl(PerlInterpreter *perl)
308 PERL_SET_CONTEXT(perl);
310 handles = rlm_perl_get_handles(aTHX);
311 rlm_perl_destruct(perl);
312 rlm_perl_close_handles(handles);
315 static void delete_pool_handle(POOL_HANDLE *handle, PERL_INST *inst)
324 inst->perl_pool->head = next;
330 inst->perl_pool->tail = prev;
334 inst->perl_pool->current_clones--;
335 MUTEX_DESTROY(&handle->lock);
339 static void move2tail(POOL_HANDLE *handle, PERL_INST *inst)
344 if (inst->perl_pool->head == NULL) {
348 inst->perl_pool->head = handle;
349 inst->perl_pool->tail = handle;
353 if (inst->perl_pool->tail == handle) {
360 if ((next != NULL) ||
367 inst->perl_pool->head = next;
378 prev = inst->perl_pool->tail;
380 inst->perl_pool->tail = handle;
386 static POOL_HANDLE *pool_grow (PERL_INST *inst) {
390 if (inst->perl_pool->max_clones == inst->perl_pool->current_clones) {
393 if (inst->perl_pool->detach == yes ) {
397 handle = (POOL_HANDLE *)rad_malloc(sizeof(POOL_HANDLE));
400 radlog(L_ERR,"Could not find free memory for pool. Aborting");
406 handle->status = idle;
407 handle->clone = rlm_perl_clone(inst->perl);
408 handle->request_count = 0;
409 MUTEX_INIT(&handle->lock);
410 inst->perl_pool->current_clones++;
411 move2tail(handle, inst);
414 inst->perl_pool->time_when_last_added = now;
419 static POOL_HANDLE *pool_pop(PERL_INST *inst)
425 * Lock the pool and be fast other thread maybe
426 * waiting for us to finish
428 MUTEX_LOCK(&inst->perl_pool->mutex);
432 for (handle = inst->perl_pool->head; handle ; handle = tmp) {
435 if (handle->status == idle){
442 if (inst->perl_pool->current_clones < inst->perl_pool->max_clones ) {
444 found = pool_grow(inst);
447 radlog(L_ERR,"Cannot grow pool returning");
448 MUTEX_UNLOCK(&inst->perl_pool->mutex);
452 radlog(L_ERR,"rlm_perl:: reached maximum clones %d cannot grow",
453 inst->perl_pool->current_clones);
454 MUTEX_UNLOCK(&inst->perl_pool->mutex);
459 move2tail(found, inst);
460 found->status = busy;
461 MUTEX_LOCK(&found->lock);
462 inst->perl_pool->active_clones++;
463 found->request_count++;
467 MUTEX_UNLOCK(&inst->perl_pool->mutex);
468 radlog(L_DBG,"perl_pool: item 0x%lx asigned new request. Handled so far: %d",
469 (unsigned long) found->clone, found->request_count);
472 static int pool_release(POOL_HANDLE *handle, PERL_INST *inst) {
474 POOL_HANDLE *tmp, *tmp2;
480 MUTEX_LOCK(&inst->perl_pool->mutex);
483 * If detach is set then just release the mutex
485 if (inst->perl_pool->detach == yes ) {
486 handle->status = idle;
487 MUTEX_UNLOCK(&handle->lock);
488 MUTEX_UNLOCK(&inst->perl_pool->mutex);
492 MUTEX_UNLOCK(&handle->lock);
493 handle->status = idle;
494 inst->perl_pool->active_clones--;
496 spare = inst->perl_pool->current_clones - inst->perl_pool->active_clones;
498 radlog(L_DBG,"perl_pool total/active/spare [%d/%d/%d]"
499 , inst->perl_pool->current_clones, inst->perl_pool->active_clones, spare);
501 if (spare < inst->perl_pool->min_spare_clones) {
502 t = inst->perl_pool->min_spare_clones - spare;
504 if ((tmp = pool_grow(inst)) == NULL) {
505 MUTEX_UNLOCK(&inst->perl_pool->mutex);
509 MUTEX_UNLOCK(&inst->perl_pool->mutex);
513 if ((now - inst->perl_pool->time_when_last_added) < inst->perl_pool->cleanup_delay) {
514 MUTEX_UNLOCK(&inst->perl_pool->mutex);
517 if (spare > inst->perl_pool->max_spare_clones) {
518 spare -= inst->perl_pool->max_spare_clones;
519 for (tmp = inst->perl_pool->head; (tmp !=NULL ) && (spare > 0) ; tmp = tmp2) {
522 if(tmp->status == idle) {
523 rlm_destroy_perl(tmp->clone);
524 delete_pool_handle(tmp,inst);
531 * If the clone have reached max_request_per_clone clean it.
533 if (inst->perl_pool->max_request_per_clone > 0 ) {
534 if (handle->request_count > inst->perl_pool->max_request_per_clone) {
535 rlm_destroy_perl(handle->clone);
536 delete_pool_handle(handle,inst);
542 MUTEX_UNLOCK(&inst->perl_pool->mutex);
545 static int init_pool (CONF_SECTION *conf, PERL_INST *inst) {
551 pool = rad_malloc(sizeof(PERL_POOL));
552 memset(pool,0,sizeof(PERL_POOL));
554 inst->perl_pool = pool;
556 MUTEX_INIT(&pool->mutex);
563 cf_section_parse(conf,pool,pool_conf);
564 inst->perl_pool = pool;
565 inst->perl_pool->detach = no;
567 for(t = 0;t < inst->perl_pool->start_clones ;t++){
568 if ((handle = pool_grow(inst)) == NULL) {
578 static void xs_init(pTHX)
580 char *file = __FILE__;
582 /* DynaLoader is a special case */
583 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
588 * This is wrapper for radlog
589 * Now users can call radiusd::radlog(level,msg) wich is the same
590 * calling radlog from C code.
593 static XS(XS_radiusd_radlog)
597 croak("Usage: radiusd::radlog(level, message)");
602 level = (int) SvIV(ST(0));
603 msg = (char *) SvPV(ST(1), PL_na);
606 * Because 'msg' is a 'char *', we don't want '%s', etc.
607 * in it to give us printf-style vulnerabilities.
609 radlog(level, "rlm_perl: %s", msg);
617 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
618 size_t freespace, RADIUS_ESCAPE_STRING func)
621 PERL_INST *inst= (PERL_INST *) instance;
622 PerlInterpreter *perl;
623 char params[1024], *ptr, *tmp;
628 * Do an xlat on the provided string (nice recursive operation).
630 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
631 radlog(L_ERR, "rlm_perl: xlat failed.");
640 if ((handle = pool_pop(instance)) == NULL) {
644 perl = handle->clone;
646 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
651 PERL_SET_CONTEXT(perl);
656 ptr = strtok(params, " ");
660 while (ptr != NULL) {
661 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
662 ptr = strtok(NULL, " ");
667 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
671 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
674 } else if (count > 0) {
676 strlcpy(out, tmp, freespace);
679 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
689 pool_release(handle, instance);
694 * Do any per-module initialization that is separate to each
695 * configured instance of the module. e.g. set up connections
696 * to external databases, read configuration files, set up
697 * dictionary entries, etc.
699 * If configuration information is given in the config section
700 * that must be referenced in later calls, store a handle to it
701 * in *instance otherwise put a null pointer there.
704 * Setup a hashes wich we will use later
705 * parse a module and give him a chance to live
708 static int perl_instantiate(CONF_SECTION *conf, void **instance)
710 PERL_INST *inst = (PERL_INST *) instance;
715 HV *rad_request_proxy_hv;
716 HV *rad_request_proxy_reply_hv;
720 const char *xlat_name;
721 int exitstatus = 0, argc=0;
724 * Set up a storage area for instance data
726 inst = rad_malloc(sizeof(PERL_INST));
727 memset(inst, 0, sizeof(PERL_INST));
730 * If the configuration parameters can't be parsed, then
733 if (cf_section_parse(conf, inst, module_config) < 0) {
740 if (inst->perl_flags) {
741 embed[1] = inst->perl_flags;
742 embed[2] = inst->module;
746 embed[1] = inst->module;
754 if ((inst->perl = perl_alloc()) == NULL) {
755 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
759 perl_construct(inst->perl);
760 PL_perl_destruct_level = 2;
765 PERL_SET_CONTEXT(inst->perl);
767 if ((inst->perl = perl_alloc()) == NULL) {
768 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
772 perl_construct(inst->perl);
775 #if PERL_REVISION >= 5 && PERL_VERSION >=8
776 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
779 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
785 exitstatus = perl_run(inst->perl);
787 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
793 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
795 rad_reply_hv = newHV();
796 rad_check_hv = newHV();
797 rad_config_hv = newHV();
798 rad_request_hv = newHV();
799 rad_request_proxy_hv = newHV();
800 rad_request_proxy_reply_hv = newHV();
802 rad_reply_hv = get_hv("RAD_REPLY",1);
803 rad_check_hv = get_hv("RAD_CHECK",1);
804 rad_config_hv = get_hv("RAD_CONFIG",1);
805 rad_request_hv = get_hv("RAD_REQUEST",1);
806 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
807 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
809 xlat_name = cf_section_name2(conf);
810 if (xlat_name == NULL)
811 xlat_name = cf_section_name1(conf);
813 inst->xlat_name = strdup(xlat_name);
814 xlat_register(xlat_name, perl_xlat, inst);
818 if ((init_pool(conf, inst)) == -1) {
819 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
830 * get the vps and put them in perl hash
831 * If one VP have multiple values it is added as array_ref
832 * Example for this is Cisco-AVPair that holds multiple values.
833 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
835 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
837 VALUE_PAIR *nvp, *vpa, *vpn;
845 while (nvp != NULL) {
846 attr = nvp->attribute;
847 vpa = paircopy2(nvp,attr);
852 len = vp_prints_value(buffer, sizeof(buffer),
854 av_push(av, newSVpv(buffer, len));
857 hv_store(rad_hv, nvp->name, strlen(nvp->name),
858 newRV_noinc((SV *) av), 0);
860 len = vp_prints_value(buffer, sizeof(buffer),
862 hv_store(rad_hv, vpa->name, strlen(vpa->name),
863 newSVpv(buffer, len), 0);
867 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
869 pairdelete(&nvp, attr);
876 * Verify that a Perl SV is a string and save it in FreeRadius
880 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
885 val = SvPV_nolen(sv);
886 vpp = pairmake(key, val, operator);
890 "rlm_perl: Added pair %s = %s", key, val);
894 "rlm_perl: ERROR: Failed to create pair %s = %s",
903 * Gets the content from hashes
905 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
910 I32 key_len, len, i, j;
914 for (i = hv_iterinit(my_hv); i > 0; i--) {
915 res_sv = hv_iternextsv(my_hv,&key,&key_len);
916 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
917 av = (AV*)SvRV(res_sv);
919 for (j = 0; j <= len; j++) {
920 av_sv = av_fetch(av, j, 0);
921 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
923 } else ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
930 * Call the function_name inside the module
931 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
934 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
937 PERL_INST *inst = instance;
939 int exitstatus=0, count;
946 HV *rad_request_proxy_hv;
947 HV *rad_request_proxy_reply_hv;
952 if ((handle = pool_pop(instance)) == NULL) {
953 return RLM_MODULE_FAIL;
956 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
958 dTHXa(handle->clone);
959 PERL_SET_CONTEXT(handle->clone);
962 PERL_SET_CONTEXT(inst->perl);
963 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
973 * Radius has told us to call this function, but none
976 if (!function_name) {
977 return RLM_MODULE_FAIL;
980 rad_reply_hv = get_hv("RAD_REPLY",1);
981 rad_check_hv = get_hv("RAD_CHECK",1);
982 rad_config_hv = get_hv("RAD_CONFIG",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);
991 perl_store_vps(request->config_items, rad_config_hv);
993 if (request->proxy != NULL) {
994 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
996 hv_undef(rad_request_proxy_hv);
999 if (request->proxy_reply !=NULL) {
1000 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1002 hv_undef(rad_request_proxy_reply_hv);
1007 * This way %RAD_xx can be pushed onto stack as sub parameters.
1008 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1009 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1010 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1014 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1018 if (SvTRUE(ERRSV)) {
1019 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1021 function_name, SvPV(ERRSV,n_a));
1027 if (exitstatus >= 100 || exitstatus < 0) {
1028 exitstatus = RLM_MODULE_FAIL;
1038 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
1039 pairfree(&request->packet->vps);
1040 request->packet->vps = vp;
1044 * Update cached copies
1046 request->username = pairfind(request->packet->vps,
1048 request->password = pairfind(request->packet->vps,
1050 if (!request->password)
1051 request->password = pairfind(request->packet->vps,
1055 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1056 pairfree(&request->reply->vps);
1057 request->reply->vps = vp;
1061 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1062 pairfree(&request->config_items);
1063 request->config_items = vp;
1067 if (request->proxy &&
1068 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
1069 pairfree(&request->proxy->vps);
1070 request->proxy->vps = vp;
1074 if (request->proxy_reply &&
1075 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
1076 pairfree(&request->proxy_reply->vps);
1077 request->proxy_reply->vps = vp;
1083 pool_release(handle,instance);
1084 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1091 * Find the named user in this modules database. Create the set
1092 * of attribute-value pairs to check and reply with for this user
1093 * from the database. The authentication code only needs to check
1094 * the password, the rest is done here.
1096 static int perl_authorize(void *instance, REQUEST *request)
1098 return rlmperl_call(instance, request,
1099 ((PERL_INST *)instance)->func_authorize);
1103 * Authenticate the user with the given password.
1105 static int perl_authenticate(void *instance, REQUEST *request)
1107 return rlmperl_call(instance, request,
1108 ((PERL_INST *)instance)->func_authenticate);
1111 * Massage the request before recording it or proxying it
1113 static int perl_preacct(void *instance, REQUEST *request)
1115 return rlmperl_call(instance, request,
1116 ((PERL_INST *)instance)->func_preacct);
1119 * Write accounting information to this modules database.
1121 static int perl_accounting(void *instance, REQUEST *request)
1124 int acctstatustype=0;
1126 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1127 acctstatustype = pair->lvalue;
1129 radlog(L_ERR, "Invalid Accounting Packet");
1130 return RLM_MODULE_INVALID;
1133 switch (acctstatustype) {
1135 case PW_STATUS_START:
1137 if (((PERL_INST *)instance)->func_start_accounting) {
1138 return rlmperl_call(instance, request,
1139 ((PERL_INST *)instance)->func_start_accounting);
1141 return rlmperl_call(instance, request,
1142 ((PERL_INST *)instance)->func_accounting);
1146 case PW_STATUS_STOP:
1148 if (((PERL_INST *)instance)->func_stop_accounting) {
1149 return rlmperl_call(instance, request,
1150 ((PERL_INST *)instance)->func_stop_accounting);
1152 return rlmperl_call(instance, request,
1153 ((PERL_INST *)instance)->func_accounting);
1157 return rlmperl_call(instance, request,
1158 ((PERL_INST *)instance)->func_accounting);
1163 * Check for simultaneouse-use
1165 static int perl_checksimul(void *instance, REQUEST *request)
1167 return rlmperl_call(instance, request,
1168 ((PERL_INST *)instance)->func_checksimul);
1173 static int perl_pre_proxy(void *instance, REQUEST *request)
1175 return rlmperl_call(instance, request,
1176 ((PERL_INST *)instance)->func_pre_proxy);
1179 * Post-Proxy request
1181 static int perl_post_proxy(void *instance, REQUEST *request)
1183 return rlmperl_call(instance, request,
1184 ((PERL_INST *)instance)->func_post_proxy);
1189 static int perl_post_auth(void *instance, REQUEST *request)
1191 return rlmperl_call(instance, request,
1192 ((PERL_INST *)instance)->func_post_auth);
1195 * Detach a instance give a chance to a module to make some internal setup ...
1197 static int perl_detach(void *instance)
1199 PERL_INST *inst = (PERL_INST *) instance;
1200 int exitstatus = 0, count = 0;
1203 POOL_HANDLE *handle, *tmp, *tmp2;
1205 MUTEX_LOCK(&inst->perl_pool->mutex);
1206 inst->perl_pool->detach = yes;
1207 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1209 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1211 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1213 * Wait until clone becomes idle
1215 MUTEX_LOCK(&handle->lock);
1218 * Give a clones chance to run detach function
1221 dTHXa(handle->clone);
1222 PERL_SET_CONTEXT(handle->clone);
1224 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1225 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1231 * FIXME: bug in perl
1234 if (exitstatus >= 100 || exitstatus < 0) {
1235 exitstatus = RLM_MODULE_FAIL;
1241 radlog(L_DBG,"detach at 0x%lx returned status %d",
1242 (unsigned long) handle->clone, exitstatus);
1245 MUTEX_UNLOCK(&handle->lock);
1251 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1253 radlog(L_DBG,"rlm_perl:: Destroy perl");
1254 rlm_perl_destruct(tmp->clone);
1255 delete_pool_handle(tmp,inst);
1260 #endif /* USE_ITHREADS */
1261 PERL_SET_CONTEXT(inst->perl);
1263 dSP; ENTER; SAVETMPS;
1266 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1271 if (exitstatus >= 100 || exitstatus < 0) {
1272 exitstatus = RLM_MODULE_FAIL;
1283 xlat_unregister(inst->xlat_name, perl_xlat);
1284 free(inst->xlat_name);
1288 free(inst->perl_pool->head);
1289 free(inst->perl_pool->tail);
1290 MUTEX_DESTROY(&inst->perl_pool->mutex);
1291 free(inst->perl_pool);
1292 rlm_perl_destruct(inst->perl);
1294 perl_destruct(inst->perl);
1295 perl_free(inst->perl);
1302 * The module name should be the only globally exported symbol.
1303 * That is, everything else should be 'static'.
1305 * If the module needs to temporarily modify it's instantiation
1306 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1307 * The server will then take care of ensuring that the module
1308 * is single-threaded.
1310 module_t rlm_perl = {
1314 RLM_TYPE_THREAD_SAFE, /* type */
1316 RLM_TYPE_THREAD_UNSAFE,
1318 perl_instantiate, /* instantiation */
1319 perl_detach, /* detach */
1321 perl_authenticate, /* authenticate */
1322 perl_authorize, /* authorize */
1323 perl_preacct, /* preacct */
1324 perl_accounting, /* accounting */
1325 perl_checksimul, /* check simul */
1326 perl_pre_proxy, /* pre-proxy */
1327 perl_post_proxy, /* post-proxy */
1328 perl_post_auth /* post-auth */