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>
32 #include <freeradius-devel/conffile.h>
46 #include <semaphore.h>
49 extern char **environ;
52 static const char rcsid[] = "$Id$";
57 * Pool of Perl's clones (genetically cloned) ;)
60 typedef struct pool_handle {
61 struct pool_handle *next;
62 struct pool_handle *prev;
63 enum {busy, idle} status;
64 unsigned int request_count;
65 PerlInterpreter *clone;
69 typedef struct PERL_POOL {
79 int max_request_per_clone;
83 time_t time_when_last_added;
89 * Define a structure for our module configuration.
91 * These variables do not need to be in a structure, but it's
92 * a lot cleaner to do so, and a pointer to the structure can
93 * be used as the instance handle.
95 typedef struct perl_inst {
96 /* Name of the perl module */
99 /* Name of the functions for each module method */
100 char *func_authorize;
101 char *func_authenticate;
102 char *func_accounting;
103 char *func_start_accounting;
104 char *func_stop_accounting;
106 char *func_checksimul;
109 char *func_pre_proxy;
110 char *func_post_proxy;
111 char *func_post_auth;
114 PerlInterpreter *perl;
116 PERL_POOL *perl_pool;
120 * A mapping of configuration file names to internal variables.
122 * Note that the string is dynamically allocated, so it MUST
123 * be freed. When the configuration file parse re-reads the string,
124 * it free's the old one, and strdup's the new one, placing the pointer
125 * to the strdup'd string into 'config.string'. This gets around
128 static const CONF_PARSER module_config[] = {
129 { "module", PW_TYPE_FILENAME,
130 offsetof(PERL_INST,module), NULL, "module"},
131 { "func_authorize", PW_TYPE_STRING_PTR,
132 offsetof(PERL_INST,func_authorize), NULL, "authorize"},
133 { "func_authenticate", PW_TYPE_STRING_PTR,
134 offsetof(PERL_INST,func_authenticate), NULL, "authenticate"},
135 { "func_accounting", PW_TYPE_STRING_PTR,
136 offsetof(PERL_INST,func_accounting), NULL, "accounting"},
137 { "func_preacct", PW_TYPE_STRING_PTR,
138 offsetof(PERL_INST,func_preacct), NULL, "preacct"},
139 { "func_checksimul", PW_TYPE_STRING_PTR,
140 offsetof(PERL_INST,func_checksimul), NULL, "checksimul"},
141 { "func_detach", PW_TYPE_STRING_PTR,
142 offsetof(PERL_INST,func_detach), NULL, "detach"},
143 { "func_xlat", PW_TYPE_STRING_PTR,
144 offsetof(PERL_INST,func_xlat), NULL, "xlat"},
145 { "func_pre_proxy", PW_TYPE_STRING_PTR,
146 offsetof(PERL_INST,func_pre_proxy), NULL, "pre_proxy"},
147 { "func_post_proxy", PW_TYPE_STRING_PTR,
148 offsetof(PERL_INST,func_post_proxy), NULL, "post_proxy"},
149 { "func_post_auth", PW_TYPE_STRING_PTR,
150 offsetof(PERL_INST,func_post_auth), NULL, "post_auth"},
151 { "perl_flags", PW_TYPE_STRING_PTR,
152 offsetof(PERL_INST,perl_flags), NULL, NULL},
153 { "func_start_accounting", PW_TYPE_STRING_PTR,
154 offsetof(PERL_INST,func_start_accounting), NULL, NULL},
155 { "func_stop_accounting", PW_TYPE_STRING_PTR,
156 offsetof(PERL_INST,func_stop_accounting), NULL, NULL},
158 { NULL, -1, 0, NULL, NULL } /* end the list */
164 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
168 * We use one perl to clone from it i.e. main boss
169 * We clone it for every instance if we have perl
170 * with -Duseithreads compiled in
172 static PerlInterpreter *interp = NULL;
174 static const CONF_PARSER pool_conf[] = {
175 { "max_clones", PW_TYPE_INTEGER, offsetof(PERL_POOL, max_clones), NULL, "32"},
176 { "start_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, start_clones), NULL, "32"},
177 { "min_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL, min_spare_clones),NULL, "0"},
178 { "max_spare_clones",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_spare_clones),NULL, "32"},
179 { "cleanup_delay",PW_TYPE_INTEGER, offsetof(PERL_POOL,cleanup_delay),NULL, "5"},
180 { "max_request_per_clone",PW_TYPE_INTEGER, offsetof(PERL_POOL,max_request_per_clone),NULL, "0"},
181 { NULL, -1, 0, NULL, NULL } /* end the list */
185 #define dl_librefs "DynaLoader::dl_librefs"
186 #define dl_modules "DynaLoader::dl_modules"
187 static void rlm_perl_clear_handles(pTHX)
189 AV *librefs = get_av(dl_librefs, FALSE);
195 static void **rlm_perl_get_handles(pTHX)
198 AV *librefs = get_av(dl_librefs, FALSE);
199 AV *modules = get_av(dl_modules, FALSE);
204 "Could not get @%s for unloading.\n",
209 if (!(AvFILL(librefs) >= 0)) {
213 handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
215 for (i=0; i<=AvFILL(librefs); i++) {
217 SV *handle_sv = *av_fetch(librefs, i, FALSE);
221 "Could not fetch $%s[%d]!\n",
225 handle = (void *)SvIV(handle_sv);
235 handles[i] = (void *)0;
240 static void rlm_perl_close_handles(void **handles)
248 for (i=0; handles[i]; i++) {
249 radlog(L_DBG, "close 0x%lx\n", (unsigned long)handles[i]);
256 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl)
258 PerlInterpreter *clone;
261 PERL_SET_CONTEXT(perl);
263 clone = perl_clone(perl, clone_flags);
267 #if PERL_REVISION >= 5 && PERL_VERSION <8
270 ptr_table_free(PL_ptr_table);
273 PERL_SET_CONTEXT(aTHX);
274 rlm_perl_clear_handles(aTHX);
279 static void rlm_perl_destruct(PerlInterpreter *perl)
281 char **orig_environ = NULL;
284 PERL_SET_CONTEXT(perl);
286 PL_perl_destruct_level = 2;
288 PL_origenviron = environ;
294 * FIXME: This shouldn't happen
297 while (PL_scopestack_ix > 1 ){
305 environ = orig_environ;
309 static void rlm_destroy_perl(PerlInterpreter *perl)
314 PERL_SET_CONTEXT(perl);
316 handles = rlm_perl_get_handles(aTHX);
317 rlm_perl_destruct(perl);
318 rlm_perl_close_handles(handles);
321 static void delete_pool_handle(POOL_HANDLE *handle, PERL_INST *inst)
330 inst->perl_pool->head = next;
336 inst->perl_pool->tail = prev;
340 inst->perl_pool->current_clones--;
341 MUTEX_DESTROY(&handle->lock);
345 static void move2tail(POOL_HANDLE *handle, PERL_INST *inst)
350 if (inst->perl_pool->head == NULL) {
354 inst->perl_pool->head = handle;
355 inst->perl_pool->tail = handle;
359 if (inst->perl_pool->tail == handle) {
366 if ((next != NULL) ||
373 inst->perl_pool->head = next;
384 prev = inst->perl_pool->tail;
386 inst->perl_pool->tail = handle;
392 static POOL_HANDLE *pool_grow (PERL_INST *inst) {
396 if (inst->perl_pool->max_clones == inst->perl_pool->current_clones) {
399 if (inst->perl_pool->detach == yes ) {
403 handle = (POOL_HANDLE *)rad_malloc(sizeof(POOL_HANDLE));
406 radlog(L_ERR,"Could not find free memory for pool. Aborting");
412 handle->status = idle;
413 handle->clone = rlm_perl_clone(inst->perl);
414 handle->request_count = 0;
415 MUTEX_INIT(&handle->lock);
416 inst->perl_pool->current_clones++;
417 move2tail(handle, inst);
420 inst->perl_pool->time_when_last_added = now;
425 static POOL_HANDLE *pool_pop(PERL_INST *inst)
431 * Lock the pool and be fast other thread maybe
432 * waiting for us to finish
434 MUTEX_LOCK(&inst->perl_pool->mutex);
438 for (handle = inst->perl_pool->head; handle ; handle = tmp) {
441 if (handle->status == idle){
448 if (inst->perl_pool->current_clones < inst->perl_pool->max_clones ) {
450 found = pool_grow(inst);
453 radlog(L_ERR,"Cannot grow pool returning");
454 MUTEX_UNLOCK(&inst->perl_pool->mutex);
458 radlog(L_ERR,"rlm_perl:: reached maximum clones %d cannot grow",
459 inst->perl_pool->current_clones);
460 MUTEX_UNLOCK(&inst->perl_pool->mutex);
465 move2tail(found, inst);
466 found->status = busy;
467 MUTEX_LOCK(&found->lock);
468 inst->perl_pool->active_clones++;
469 found->request_count++;
473 MUTEX_UNLOCK(&inst->perl_pool->mutex);
474 radlog(L_DBG,"perl_pool: item 0x%lx asigned new request. Handled so far: %d",
475 (unsigned long) found->clone, found->request_count);
478 static int pool_release(POOL_HANDLE *handle, PERL_INST *inst) {
480 POOL_HANDLE *tmp, *tmp2;
486 MUTEX_LOCK(&inst->perl_pool->mutex);
489 * If detach is set then just release the mutex
491 if (inst->perl_pool->detach == yes ) {
492 handle->status = idle;
493 MUTEX_UNLOCK(&handle->lock);
494 MUTEX_UNLOCK(&inst->perl_pool->mutex);
498 MUTEX_UNLOCK(&handle->lock);
499 handle->status = idle;
500 inst->perl_pool->active_clones--;
502 spare = inst->perl_pool->current_clones - inst->perl_pool->active_clones;
504 radlog(L_DBG,"perl_pool total/active/spare [%d/%d/%d]"
505 , inst->perl_pool->current_clones, inst->perl_pool->active_clones, spare);
507 if (spare < inst->perl_pool->min_spare_clones) {
508 t = inst->perl_pool->min_spare_clones - spare;
510 if ((tmp = pool_grow(inst)) == NULL) {
511 MUTEX_UNLOCK(&inst->perl_pool->mutex);
515 MUTEX_UNLOCK(&inst->perl_pool->mutex);
519 if ((now - inst->perl_pool->time_when_last_added) < inst->perl_pool->cleanup_delay) {
520 MUTEX_UNLOCK(&inst->perl_pool->mutex);
523 if (spare > inst->perl_pool->max_spare_clones) {
524 spare -= inst->perl_pool->max_spare_clones;
525 for (tmp = inst->perl_pool->head; (tmp !=NULL ) && (spare > 0) ; tmp = tmp2) {
528 if(tmp->status == idle) {
529 rlm_destroy_perl(tmp->clone);
530 delete_pool_handle(tmp,inst);
537 * If the clone have reached max_request_per_clone clean it.
539 if (inst->perl_pool->max_request_per_clone > 0 ) {
540 if (handle->request_count > inst->perl_pool->max_request_per_clone) {
541 rlm_destroy_perl(handle->clone);
542 delete_pool_handle(handle,inst);
548 MUTEX_UNLOCK(&inst->perl_pool->mutex);
551 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 static void xs_init(pTHX)
586 char *file = __FILE__;
588 /* DynaLoader is a special case */
589 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
594 * This is wrapper for radlog
595 * Now users can call radiusd::radlog(level,msg) wich is the same
596 * calling radlog from C code.
599 static XS(XS_radiusd_radlog)
603 croak("Usage: radiusd::radlog(level, message)");
608 level = (int) SvIV(ST(0));
609 msg = (char *) SvPV(ST(1), PL_na);
612 * Because 'msg' is a 'char *', we don't want '%s', etc.
613 * in it to give us printf-style vulnerabilities.
615 radlog(level, "rlm_perl: %s", msg);
623 static int perl_xlat(void *instance, REQUEST *request, char *fmt, char * out,
624 size_t freespace, RADIUS_ESCAPE_STRING func)
627 PERL_INST *inst= (PERL_INST *) instance;
628 PerlInterpreter *perl;
629 char params[1024], *ptr, *tmp;
634 * Do an xlat on the provided string (nice recursive operation).
636 if (!radius_xlat(params, sizeof(params), fmt, request, func)) {
637 radlog(L_ERR, "rlm_perl: xlat failed.");
646 if ((handle = pool_pop(instance)) == NULL) {
650 perl = handle->clone;
652 radlog(L_DBG,"Found a interpetator 0x%lx",(unsigned long) perl);
657 PERL_SET_CONTEXT(perl);
662 ptr = strtok(params, " ");
666 while (ptr != NULL) {
667 XPUSHs(sv_2mortal(newSVpv(ptr,0)));
668 ptr = strtok(NULL, " ");
673 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
677 radlog(L_ERR, "rlm_perl: perl_xlat exit %s\n",
680 } else if (count > 0) {
683 strncpy(out,tmp,ret);
685 radlog(L_DBG,"rlm_perl: Len is %d , out is %s freespace is %d",
695 pool_release(handle, instance);
700 * Do any per-module initialization that is separate to each
701 * configured instance of the module. e.g. set up connections
702 * to external databases, read configuration files, set up
703 * dictionary entries, etc.
705 * If configuration information is given in the config section
706 * that must be referenced in later calls, store a handle to it
707 * in *instance otherwise put a null pointer there.
710 * Setup a hashes wich we will use later
711 * parse a module and give him a chance to live
714 static int perl_instantiate(CONF_SECTION *conf, void **instance)
716 PERL_INST *inst = (PERL_INST *) instance;
720 HV *rad_request_proxy_hv;
721 HV *rad_request_proxy_reply_hv;
725 const char *xlat_name;
726 int exitstatus = 0, argc=0;
729 * Set up a storage area for instance data
731 inst = rad_malloc(sizeof(PERL_INST));
732 memset(inst, 0, sizeof(PERL_INST));
735 * If the configuration parameters can't be parsed, then
738 if (cf_section_parse(conf, inst, module_config) < 0) {
745 if (inst->perl_flags) {
746 embed[1] = inst->perl_flags;
747 embed[2] = inst->module;
751 embed[1] = inst->module;
759 if ((inst->perl = perl_alloc()) == NULL) {
760 radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
764 perl_construct(inst->perl);
765 PL_perl_destruct_level = 2;
770 PERL_SET_CONTEXT(inst->perl);
772 if ((inst->perl = perl_alloc()) == NULL) {
773 radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
777 perl_construct(inst->perl);
780 #if PERL_REVISION >= 5 && PERL_VERSION >=8
781 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
784 exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
790 exitstatus = perl_run(inst->perl);
792 radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
798 newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl.c");
800 rad_reply_hv = newHV();
801 rad_check_hv = newHV();
802 rad_request_hv = newHV();
803 rad_request_proxy_hv = newHV();
804 rad_request_proxy_reply_hv = newHV();
806 rad_reply_hv = get_hv("RAD_REPLY",1);
807 rad_check_hv = get_hv("RAD_CHECK",1);
808 rad_request_hv = get_hv("RAD_REQUEST",1);
809 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
810 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
812 xlat_name = cf_section_name2(conf);
813 if (xlat_name == NULL)
814 xlat_name = cf_section_name1(conf);
816 inst->xlat_name = strdup(xlat_name);
817 xlat_register(xlat_name, perl_xlat, inst);
821 if ((init_pool(conf, inst)) == -1) {
822 radlog(L_ERR,"Couldn't init a pool of perl clones. Exiting");
833 * get the vps and put them in perl hash
834 * If one VP have multiple values it is added as array_ref
835 * Example for this is Cisco-AVPair that holds multiple values.
836 * Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
838 static void perl_store_vps(VALUE_PAIR *vp, HV *rad_hv)
840 VALUE_PAIR *nvp, *vpa, *vpn;
848 while (nvp != NULL) {
849 attr = nvp->attribute;
850 vpa = paircopy2(nvp,attr);
855 len = vp_prints_value(buffer, sizeof(buffer),
857 av_push(av, newSVpv(buffer, len));
860 hv_store(rad_hv, nvp->name, strlen(nvp->name),
861 newRV_noinc((SV *) av), 0);
863 len = vp_prints_value(buffer, sizeof(buffer),
865 hv_store(rad_hv, vpa->name, strlen(vpa->name),
866 newSVpv(buffer, len), 0);
870 vpa = nvp; while ((vpa != NULL) && (vpa->attribute == attr))
872 pairdelete(&nvp, attr);
879 * Verify that a Perl SV is a string and save it in FreeRadius
883 static int pairadd_sv(VALUE_PAIR **vp, char *key, SV *sv, int operator) {
888 val = SvPV_nolen(sv);
889 vpp = pairmake(key, val, operator);
893 "rlm_perl: Added pair %s = %s", key, val);
897 "rlm_perl: ERROR: Failed to create pair %s = %s",
906 * Gets the content from hashes
908 static int get_hv_content(HV *my_hv, VALUE_PAIR **vp)
910 SV *res_sv, **av_sv, **operator_sv, **sv;
914 char buf[MAX_STRING_LEN];
915 I32 key_len, len, i, j;
917 LRAD_TOKEN token, operator = T_EOL;
919 for (i = hv_iterinit(my_hv); i > 0; i--) {
921 res_sv = hv_iternextsv(my_hv,&key,&key_len);
923 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVHV)) {
924 hv = (HV*)SvRV(res_sv);
925 if (hv_exists(hv,key,key_len)) {
926 sv = hv_fetch(hv,key,key_len,FALSE);
928 * Check if there is hash Key Operator and if not
929 * use T_OP_EQ as default
931 if (hv_exists(hv,"Operator",strlen("Operator"))) {
932 operator_sv = hv_fetch(hv,"Operator", strlen("Operator"), FALSE);
934 if (SvOK(*operator_sv))
935 ptr = SvPV_nolen(*operator_sv);
937 operator = gettoken(&ptr, buf, sizeof(buf));
942 if (operator <= T_EOL ) {
943 radlog(L_ERR,"rlm_perl: Invalid Operator for attribute %s", key);
947 radlog(L_ERR,"rlm_perl: Operator key doesn't exist. Setting Operator to = for key %s", key);
950 ret = pairadd_sv(vp,key, *sv, operator) + ret;
952 radlog(L_ERR,"rlm_perl: Wrong or missing key in hash ref for %s, Skipping", key );
955 else if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
956 av = (AV*)SvRV(res_sv);
958 for (j = 0; j <= len; j++) {
959 av_sv = av_fetch(av, j, 0);
960 ret = pairadd_sv(vp, key, *av_sv, T_OP_ADD) + ret;
963 ret = pairadd_sv(vp, key, res_sv, T_OP_EQ) + ret;
970 * Call the function_name inside the module
971 * Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
974 static int rlmperl_call(void *instance, REQUEST *request, char *function_name)
977 PERL_INST *inst = instance;
979 int exitstatus=0, count;
985 HV *rad_request_proxy_hv;
986 HV *rad_request_proxy_reply_hv;
991 if ((handle = pool_pop(instance)) == NULL) {
992 return RLM_MODULE_FAIL;
995 radlog(L_DBG,"found interpetator at address 0x%lx",(unsigned long) handle->clone);
997 dTHXa(handle->clone);
998 PERL_SET_CONTEXT(handle->clone);
1001 PERL_SET_CONTEXT(inst->perl);
1002 radlog(L_DBG,"Using perl at 0x%lx",(unsigned long) inst->perl);
1012 * Radius has told us to call this function, but none
1015 if (!function_name) {
1016 return RLM_MODULE_FAIL;
1019 rad_reply_hv = get_hv("RAD_REPLY",1);
1020 rad_check_hv = get_hv("RAD_CHECK",1);
1021 rad_request_hv = get_hv("RAD_REQUEST",1);
1022 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
1023 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
1026 perl_store_vps(request->reply->vps, rad_reply_hv);
1027 perl_store_vps(request->config_items, rad_check_hv);
1028 perl_store_vps(request->packet->vps, rad_request_hv);
1030 if (request->proxy != NULL) {
1031 perl_store_vps(request->proxy->vps, rad_request_proxy_hv);
1033 hv_undef(rad_request_proxy_hv);
1036 if (request->proxy_reply !=NULL) {
1037 perl_store_vps(request->proxy_reply->vps, rad_request_proxy_reply_hv);
1039 hv_undef(rad_request_proxy_reply_hv);
1044 * This way %RAD_xx can be pushed onto stack as sub parameters.
1045 * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
1046 * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
1047 * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
1051 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
1055 if (SvTRUE(ERRSV)) {
1056 radlog(L_ERR, "rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
1058 function_name, SvPV(ERRSV,n_a));
1064 if (exitstatus >= 100 || exitstatus < 0) {
1065 exitstatus = RLM_MODULE_FAIL;
1075 if ((get_hv_content(rad_request_hv, &vp)) > 0 ) {
1076 pairfree(&request->packet->vps);
1077 request->packet->vps = vp;
1081 if ((get_hv_content(rad_reply_hv, &vp)) > 0 ) {
1082 pairfree(&request->reply->vps);
1083 request->reply->vps = vp;
1087 if ((get_hv_content(rad_check_hv, &vp)) > 0 ) {
1088 pairfree(&request->config_items);
1089 request->config_items = vp;
1093 if (request->proxy &&
1094 (get_hv_content(rad_request_proxy_hv, &vp) > 0)) {
1095 pairfree(&request->proxy->vps);
1096 request->proxy->vps = vp;
1100 if (request->proxy_reply &&
1101 (get_hv_content(rad_request_proxy_reply_hv, &vp) > 0)) {
1102 pairfree(&request->proxy_reply->vps);
1103 request->proxy_reply->vps = vp;
1109 pool_release(handle,instance);
1110 radlog(L_DBG,"Unreserve perl at address 0x%lx", (unsigned long) handle->clone);
1117 * Find the named user in this modules database. Create the set
1118 * of attribute-value pairs to check and reply with for this user
1119 * from the database. The authentication code only needs to check
1120 * the password, the rest is done here.
1122 static int perl_authorize(void *instance, REQUEST *request)
1124 return rlmperl_call(instance, request,
1125 ((PERL_INST *)instance)->func_authorize);
1129 * Authenticate the user with the given password.
1131 static int perl_authenticate(void *instance, REQUEST *request)
1133 return rlmperl_call(instance, request,
1134 ((PERL_INST *)instance)->func_authenticate);
1137 * Massage the request before recording it or proxying it
1139 static int perl_preacct(void *instance, REQUEST *request)
1141 return rlmperl_call(instance, request,
1142 ((PERL_INST *)instance)->func_preacct);
1145 * Write accounting information to this modules database.
1147 static int perl_accounting(void *instance, REQUEST *request)
1150 int acctstatustype=0;
1152 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL) {
1153 acctstatustype = pair->lvalue;
1155 radlog(L_ERR, "Invalid Accounting Packet");
1156 return RLM_MODULE_INVALID;
1159 switch (acctstatustype) {
1161 case PW_STATUS_START:
1163 if (((PERL_INST *)instance)->func_start_accounting) {
1164 return rlmperl_call(instance, request,
1165 ((PERL_INST *)instance)->func_start_accounting);
1167 return rlmperl_call(instance, request,
1168 ((PERL_INST *)instance)->func_accounting);
1172 case PW_STATUS_STOP:
1174 if (((PERL_INST *)instance)->func_stop_accounting) {
1175 return rlmperl_call(instance, request,
1176 ((PERL_INST *)instance)->func_stop_accounting);
1178 return rlmperl_call(instance, request,
1179 ((PERL_INST *)instance)->func_accounting);
1183 return rlmperl_call(instance, request,
1184 ((PERL_INST *)instance)->func_accounting);
1189 * Check for simultaneouse-use
1191 static int perl_checksimul(void *instance, REQUEST *request)
1193 return rlmperl_call(instance, request,
1194 ((PERL_INST *)instance)->func_checksimul);
1199 static int perl_pre_proxy(void *instance, REQUEST *request)
1201 return rlmperl_call(instance, request,
1202 ((PERL_INST *)instance)->func_pre_proxy);
1205 * Post-Proxy request
1207 static int perl_post_proxy(void *instance, REQUEST *request)
1209 return rlmperl_call(instance, request,
1210 ((PERL_INST *)instance)->func_post_proxy);
1215 static int perl_post_auth(void *instance, REQUEST *request)
1217 return rlmperl_call(instance, request,
1218 ((PERL_INST *)instance)->func_post_auth);
1221 * Detach a instance give a chance to a module to make some internal setup ...
1223 static int perl_detach(void *instance)
1225 PERL_INST *inst = (PERL_INST *) instance;
1226 int exitstatus = 0, count = 0;
1229 POOL_HANDLE *handle, *tmp, *tmp2;
1231 MUTEX_LOCK(&inst->perl_pool->mutex);
1232 inst->perl_pool->detach = yes;
1233 MUTEX_UNLOCK(&inst->perl_pool->mutex);
1235 for (handle = inst->perl_pool->head; handle != NULL; handle = handle->next) {
1237 radlog(L_DBG,"Detach perl 0x%lx", (unsigned long) handle->clone);
1239 * Wait until clone becomes idle
1241 MUTEX_LOCK(&handle->lock);
1244 * Give a clones chance to run detach function
1247 dTHXa(handle->clone);
1248 PERL_SET_CONTEXT(handle->clone);
1250 dSP; ENTER; SAVETMPS; PUSHMARK(SP);
1251 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1257 * FIXME: bug in perl
1260 if (exitstatus >= 100 || exitstatus < 0) {
1261 exitstatus = RLM_MODULE_FAIL;
1267 radlog(L_DBG,"detach at 0x%lx returned status %d",
1268 (unsigned long) handle->clone, exitstatus);
1271 MUTEX_UNLOCK(&handle->lock);
1277 for (tmp = inst->perl_pool->head; tmp !=NULL ; tmp = tmp2) {
1279 radlog(L_DBG,"rlm_perl:: Destroy perl");
1280 rlm_perl_destruct(tmp->clone);
1281 delete_pool_handle(tmp,inst);
1286 #endif /* USE_ITHREADS */
1287 PERL_SET_CONTEXT(inst->perl);
1289 dSP; ENTER; SAVETMPS;
1292 count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
1297 if (exitstatus >= 100 || exitstatus < 0) {
1298 exitstatus = RLM_MODULE_FAIL;
1309 xlat_unregister(inst->xlat_name, perl_xlat);
1310 free(inst->xlat_name);
1312 if (inst->func_authorize) free(inst->func_authorize);
1313 if (inst->func_authenticate) free(inst->func_authenticate);
1314 if (inst->func_accounting) free(inst->func_accounting);
1315 if (inst->func_preacct) free(inst->func_preacct);
1316 if (inst->func_checksimul) free(inst->func_checksimul);
1317 if (inst->func_pre_proxy) free(inst->func_pre_proxy);
1318 if (inst->func_post_proxy) free(inst->func_post_proxy);
1319 if (inst->func_post_auth) free(inst->func_post_auth);
1320 if (inst->func_detach) free(inst->func_detach);
1323 free(inst->perl_pool->head);
1324 free(inst->perl_pool->tail);
1325 MUTEX_DESTROY(&inst->perl_pool->mutex);
1326 free(inst->perl_pool);
1327 rlm_perl_destruct(inst->perl);
1329 perl_destruct(inst->perl);
1330 perl_free(inst->perl);
1337 * The module name should be the only globally exported symbol.
1338 * That is, everything else should be 'static'.
1340 * If the module needs to temporarily modify it's instantiation
1341 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
1342 * The server will then take care of ensuring that the module
1343 * is single-threaded.
1345 module_t rlm_perl = {
1349 RLM_TYPE_THREAD_SAFE, /* type */
1351 RLM_TYPE_THREAD_UNSAFE,
1353 perl_instantiate, /* instantiation */
1354 perl_detach, /* detach */
1356 perl_authenticate, /* authenticate */
1357 perl_authorize, /* authorize */
1358 perl_preacct, /* preacct */
1359 perl_accounting, /* accounting */
1360 perl_checksimul, /* check simul */
1361 perl_pre_proxy, /* pre-proxy */
1362 perl_post_proxy, /* post-proxy */
1363 perl_post_auth /* post-auth */