/[ \t]+$//
[freeradius.git] / src / modules / rlm_perl / rlm_perl.c
1 /*
2  *   This program is is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License, version 2 if the
4  *   License as published by the Free Software Foundation.
5  *
6  *   This program is distributed in the hope that it will be useful,
7  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *   GNU General Public License for more details.
10  *
11  *   You should have received a copy of the GNU General Public License
12  *   along with this program; if not, write to the Free Software
13  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
14  */
15
16 /**
17  * $Id$
18  * @file rlm_perl.c
19  * @brief Translates requests between the server an a perl interpreter.
20  *
21  * @copyright 2002,2006  The FreeRADIUS server project
22  * @copyright 2002  Boian Jordanov <bjordanov@orbitel.bg>
23  */
24 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29
30 #ifdef INADDR_ANY
31 #undef INADDR_ANY
32 #endif
33 #include <EXTERN.h>
34 #include <perl.h>
35 #include <XSUB.h>
36 #include <dlfcn.h>
37 #include <semaphore.h>
38
39 #ifdef __APPLE__
40 extern char **environ;
41 #endif
42
43 /*
44  *      Define a structure for our module configuration.
45  *
46  *      These variables do not need to be in a structure, but it's
47  *      a lot cleaner to do so, and a pointer to the structure can
48  *      be used as the instance handle.
49  */
50 typedef struct rlm_perl_t {
51         /* Name of the perl module */
52         char    *module;
53
54         /* Name of the functions for each module method */
55         char    *func_authorize;
56         char    *func_authenticate;
57         char    *func_accounting;
58         char    *func_start_accounting;
59         char    *func_stop_accounting;
60         char    *func_preacct;
61         char    *func_checksimul;
62         char    *func_detach;
63         char    *func_xlat;
64 #ifdef WITH_PROXY
65         char    *func_pre_proxy;
66         char    *func_post_proxy;
67 #endif
68         char    *func_post_auth;
69 #ifdef WITH_COA
70         char    *func_recv_coa;
71         char    *func_send_coa;
72 #endif
73         char    *xlat_name;
74         char    *perl_flags;
75         PerlInterpreter *perl;
76         pthread_key_t   *thread_key;
77
78         pthread_mutex_t clone_mutex;
79 } rlm_perl_t;
80 /*
81  *      A mapping of configuration file names to internal variables.
82  */
83 #define RLM_PERL_CONF(_x) { "func_" Stringify(_x), PW_TYPE_STRING_PTR, \
84                         offsetof(rlm_perl_t,func_##_x), NULL, Stringify(_x)}
85
86 static const CONF_PARSER module_config[] = {
87         { "module",  PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED,
88           offsetof(rlm_perl_t,module), NULL,  NULL},
89         { "filename",  PW_TYPE_FILE_INPUT | PW_TYPE_REQUIRED,
90           offsetof(rlm_perl_t,module), NULL,  NULL},
91
92         RLM_PERL_CONF(authorize),
93         RLM_PERL_CONF(authenticate),
94         RLM_PERL_CONF(post_auth),
95         RLM_PERL_CONF(accounting),
96         RLM_PERL_CONF(preacct),
97         RLM_PERL_CONF(checksimul),
98         RLM_PERL_CONF(detach),
99         RLM_PERL_CONF(xlat),
100
101 #ifdef WITH_PROXY
102         RLM_PERL_CONF(pre_proxy),
103         RLM_PERL_CONF(post_proxy),
104 #endif
105 #ifdef WITH_COA
106         RLM_PERL_CONF(recv_coa),
107         RLM_PERL_CONF(send_coa),
108 #endif
109         { "perl_flags", PW_TYPE_STRING_PTR,
110           offsetof(rlm_perl_t,perl_flags), NULL, NULL},
111
112         { "func_start_accounting", PW_TYPE_STRING_PTR,
113           offsetof(rlm_perl_t,func_start_accounting), NULL, NULL},
114
115         { "func_stop_accounting", PW_TYPE_STRING_PTR,
116           offsetof(rlm_perl_t,func_stop_accounting), NULL, NULL},
117
118         { NULL, -1, 0, NULL, NULL }             /* end the list */
119 };
120
121 /*
122  * man perlembed
123  */
124 EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
125
126 #ifdef USE_ITHREADS
127 #define dl_librefs "DynaLoader::dl_librefs"
128 #define dl_modules "DynaLoader::dl_modules"
129 static void rlm_perl_clear_handles(pTHX)
130 {
131         AV *librefs = get_av(dl_librefs, false);
132         if (librefs) {
133                 av_clear(librefs);
134         }
135 }
136
137 static void **rlm_perl_get_handles(pTHX)
138 {
139         I32 i;
140         AV *librefs = get_av(dl_librefs, false);
141         AV *modules = get_av(dl_modules, false);
142         void **handles;
143
144         if (!librefs) return NULL;
145
146         if (!(AvFILL(librefs) >= 0)) {
147                 return NULL;
148         }
149
150         handles = (void **)rad_malloc(sizeof(void *) * (AvFILL(librefs)+2));
151
152         for (i=0; i<=AvFILL(librefs); i++) {
153                 void *handle;
154                 SV *handle_sv = *av_fetch(librefs, i, false);
155
156                 if(!handle_sv) {
157                         ERROR("Could not fetch $%s[%d]!\n",
158                                dl_librefs, (int)i);
159                         continue;
160                 }
161                 handle = (void *)SvIV(handle_sv);
162
163                 if (handle) {
164                         handles[i] = handle;
165                 }
166         }
167
168         av_clear(modules);
169         av_clear(librefs);
170
171         handles[i] = (void *)0;
172
173         return handles;
174 }
175
176 static void rlm_perl_close_handles(void **handles)
177 {
178         int i;
179
180         if (!handles) {
181                 return;
182         }
183
184         for (i=0; handles[i]; i++) {
185                 DEBUG("close %p\n", handles[i]);
186                 dlclose(handles[i]);
187         }
188
189         free(handles);
190 }
191
192 DIAG_OFF(shadow)
193 static void rlm_perl_destruct(PerlInterpreter *perl)
194 {
195         dTHXa(perl);
196
197         PERL_SET_CONTEXT(perl);
198
199         PL_perl_destruct_level = 2;
200
201         PL_origenviron = environ;
202
203
204         {
205                 dTHXa(perl);
206         }
207         /*
208          * FIXME: This shouldn't happen
209          *
210          */
211         while (PL_scopestack_ix > 1 ){
212                 LEAVE;
213         }
214
215         perl_destruct(perl);
216         perl_free(perl);
217 }
218 DIAG_ON(shadow)
219
220 static void rlm_destroy_perl(PerlInterpreter *perl)
221 {
222         void    **handles;
223
224         dTHXa(perl);
225         PERL_SET_CONTEXT(perl);
226
227         handles = rlm_perl_get_handles(aTHX);
228         if (handles) rlm_perl_close_handles(handles);
229         rlm_perl_destruct(perl);
230 }
231
232 /* Create Key */
233 static void rlm_perl_make_key(pthread_key_t *key)
234 {
235         pthread_key_create(key, (void*)rlm_destroy_perl);
236 }
237
238 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key)
239 {
240         int ret;
241
242         PerlInterpreter *interp;
243         UV clone_flags = 0;
244
245         PERL_SET_CONTEXT(perl);
246
247         interp = pthread_getspecific(*key);
248         if (interp) return interp;
249
250         interp = perl_clone(perl, clone_flags);
251         {
252                 dTHXa(interp);
253         }
254 #if PERL_REVISION >= 5 && PERL_VERSION <8
255         call_pv("CLONE",0);
256 #endif
257         ptr_table_free(PL_ptr_table);
258         PL_ptr_table = NULL;
259
260         PERL_SET_CONTEXT(aTHX);
261         rlm_perl_clear_handles(aTHX);
262
263         ret = pthread_setspecific(*key, interp);
264         if (ret != 0) {
265                 DEBUG("rlm_perl: Failed associating interpretor with thread %s", strerror(ret));
266
267                 rlm_perl_destruct(interp);
268                 return NULL;
269         }
270
271         return interp;
272 }
273 #endif
274
275 /*
276  *
277  * This is wrapper for radlog
278  * Now users can call radiusd::radlog(level,msg) wich is the same
279  * calling radlog from C code.
280  * Boyan
281  */
282 static XS(XS_radiusd_radlog)
283 {
284         dXSARGS;
285         if (items !=2)
286                 croak("Usage: radiusd::radlog(level, message)");
287         {
288                 int     level;
289                 char    *msg;
290
291                 level = (int) SvIV(ST(0));
292                 msg   = (char *) SvPV(ST(1), PL_na);
293
294                 /*
295                  *      Because 'msg' is a 'char *', we don't want '%s', etc.
296                  *      in it to give us printf-style vulnerabilities.
297                  */
298                 radlog(level, "rlm_perl: %s", msg);
299         }
300         XSRETURN_NO;
301 }
302
303 static void xs_init(pTHX)
304 {
305         char const *file = __FILE__;
306
307         /* DynaLoader is a special case */
308         newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
309
310         newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl");
311 }
312
313 /*
314  * The xlat function
315  */
316 static ssize_t perl_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
317 {
318
319         rlm_perl_t      *inst= (rlm_perl_t *) instance;
320         PerlInterpreter *perl;
321         char            *tmp;
322         char const      *p, *q;
323         int             count;
324         size_t          ret = 0;
325         STRLEN          n_a;
326
327 #ifndef WITH_ITHREADS
328         perl = inst->perl;
329 #else
330         perl = rlm_perl_clone(inst->perl,inst->thread_key);
331         {
332                 dTHXa(perl);
333         }
334 #endif
335         PERL_SET_CONTEXT(perl);
336         {
337                 dSP;
338                 ENTER;SAVETMPS;
339
340                 PUSHMARK(SP);
341
342                 p = fmt;
343                 while ((q = strchr(p, ' '))) {
344                         XPUSHs(sv_2mortal(newSVpv(p, p - q)));
345
346                         p = q + 1;
347                 }
348
349                 PUTBACK;
350
351                 count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
352
353                 SPAGAIN;
354                 if (SvTRUE(ERRSV)) {
355                         REDEBUG("Exit %s", SvPV(ERRSV,n_a));
356                         (void)POPs;
357                 } else if (count > 0) {
358                         tmp = POPp;
359                         strlcpy(out, tmp, freespace);
360                         ret = strlen(out);
361
362                         RDEBUG("Len is %zu , out is %s freespace is %zu", ret, out, freespace);
363                 }
364
365                 PUTBACK ;
366                 FREETMPS ;
367                 LEAVE ;
368
369         }
370
371         return ret;
372 }
373 /*
374  *      Do any per-module initialization that is separate to each
375  *      configured instance of the module.  e.g. set up connections
376  *      to external databases, read configuration files, set up
377  *      dictionary entries, etc.
378  *
379  *      If configuration information is given in the config section
380  *      that must be referenced in later calls, store a handle to it
381  *      in *instance otherwise put a null pointer there.
382  *
383  *      Boyan:
384  *      Setup a hashes wich we will use later
385  *      parse a module and give him a chance to live
386  *
387  */
388 static int mod_instantiate(CONF_SECTION *conf, void *instance)
389 {
390         rlm_perl_t       *inst = instance;
391         AV              *end_AV;
392
393         char **embed;
394         char **envp = NULL;
395         char const *xlat_name;
396         int exitstatus = 0, argc=0;
397
398         MEM(embed = talloc_zero_array(inst, char *, 4));
399
400         /*
401          *      Create pthread key. This key will be stored in instance
402          */
403
404 #ifdef USE_ITHREADS
405         pthread_mutex_init(&inst->clone_mutex, NULL);
406
407         inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
408         memset(inst->thread_key,0,sizeof(*inst->thread_key));
409
410         rlm_perl_make_key(inst->thread_key);
411 #endif
412
413         char arg[] = "0";
414
415         embed[0] = NULL;
416         if (inst->perl_flags) {
417                 embed[1] = inst->perl_flags;
418                 embed[2] = inst->module;
419                 embed[3] = arg;
420                 argc = 4;
421         } else {
422                 embed[1] = inst->module;
423                 embed[2] = arg;
424                 argc = 3;
425         }
426
427         PERL_SYS_INIT3(&argc, &embed, &envp);
428
429         if ((inst->perl = perl_alloc()) == NULL) {
430                 ERROR("rlm_perl: No memory for allocating new perl !");
431                 return (-1);
432         }
433
434         perl_construct(inst->perl);
435
436 #ifdef USE_ITHREADS
437         PL_perl_destruct_level = 2;
438
439         {
440                 dTHXa(inst->perl);
441         }
442         PERL_SET_CONTEXT(inst->perl);
443 #endif
444
445 #if PERL_REVISION >= 5 && PERL_VERSION >=8
446         PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
447 #endif
448
449         exitstatus = perl_parse(inst->perl, xs_init, argc, embed, NULL);
450
451         end_AV = PL_endav;
452         PL_endav = Nullav;
453
454         if(!exitstatus) {
455                 exitstatus = perl_run(inst->perl);
456         } else {
457                 ERROR("rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
458                 return (-1);
459         }
460
461         PL_endav = end_AV;
462
463         xlat_name = cf_section_name2(conf);
464         if (!xlat_name)
465                 xlat_name = cf_section_name1(conf);
466         if (xlat_name) {
467                 xlat_register(xlat_name, perl_xlat, NULL, inst);
468         }
469
470         return 0;
471 }
472
473 /*
474  *      get the vps and put them in perl hash
475  *      If one VP have multiple values it is added as array_ref
476  *      Example for this is Cisco-AVPair that holds multiple values.
477  *      Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
478  */
479 static void perl_store_vps(TALLOC_CTX *ctx, VALUE_PAIR *vps, HV *rad_hv)
480 {
481         VALUE_PAIR *head, *sublist;
482         AV *av;
483         char const *name;
484         char namebuf[256];
485         char buffer[1024];
486         int len;
487
488         hv_undef(rad_hv);
489
490         /*
491          *      Copy the valuepair list so we can remove attributes
492          *      we've already processed.  This is a horrible hack to
493          *      get around various other stupidity.
494          */
495         head = paircopy(ctx, vps);
496
497         while (head) {
498                 vp_cursor_t cursor;
499                 /*
500                  *      Tagged attributes are added to the hash with name
501                  *      <attribute>:<tag>, others just use the normal attribute
502                  *      name as the key.
503                  */
504                 if (head->da->flags.has_tag && (head->tag != 0)) {
505                         snprintf(namebuf, sizeof(namebuf), "%s:%d",
506                                  head->da->name, head->tag);
507                         name = namebuf;
508                 } else {
509                         name = head->da->name;
510                 }
511
512                 /*
513                  *      Create a new list with all the attributes like this one
514                  *      which are in the same tag group.
515                  */
516                 sublist = NULL;
517                 pairfilter(ctx, &sublist, &head, head->da->attr, head->da->vendor, head->tag);
518
519                 paircursor(&cursor, &sublist);
520                 /*
521                  *      Attribute has multiple values
522                  */
523                 if (pairnext(&cursor)) {
524                         VALUE_PAIR *vp;
525
526                         av = newAV();
527                         for (vp = pairfirst(&cursor);
528                              vp;
529                              vp = pairnext(&cursor)) {
530                                 len = vp_prints_value(buffer, sizeof(buffer), vp, 0);
531                                 av_push(av, newSVpv(buffer, len));
532                         }
533                         (void)hv_store(rad_hv, name, strlen(name), newRV_noinc((SV *)av), 0);
534
535                         /*
536                          *      Attribute has a single value, so its value just gets
537                          *      added to the hash.
538                          */
539                 } else {
540                         len = vp_prints_value(buffer, sizeof(buffer), sublist, 0);
541                         (void)hv_store(rad_hv, name, strlen(name), newSVpv(buffer, len), 0);
542                 }
543
544                 pairfree(&sublist);
545         }
546
547         rad_assert(!head);
548 }
549
550 /*
551  *
552  *     Verify that a Perl SV is a string and save it in FreeRadius
553  *     Value Pair Format
554  *
555  */
556 static int pairadd_sv(TALLOC_CTX *ctx, VALUE_PAIR **vps, char *key, SV *sv, FR_TOKEN op)
557 {
558         char        *val;
559         VALUE_PAIR      *vp;
560
561         if (SvOK(sv)) {
562                 val = SvPV_nolen(sv);
563                 vp = pairmake(ctx, vps, key, val, op);
564                 if (vp != NULL) {
565                         DEBUG("rlm_perl: Added pair %s = %s", key, val);
566                         return 1;
567                 } else {
568                         EDEBUG("rlm_perl: Failed to create pair %s = %s", key, val);
569                 }
570         }
571         return 0;
572 }
573
574 /*
575  *     Boyan :
576  *     Gets the content from hashes
577  */
578 static int get_hv_content(TALLOC_CTX *ctx, HV *my_hv, VALUE_PAIR **vps)
579 {
580         SV              *res_sv, **av_sv;
581         AV              *av;
582         char            *key;
583         I32             key_len, len, i, j;
584         int             ret=0;
585
586         *vps = NULL;
587         for (i = hv_iterinit(my_hv); i > 0; i--) {
588                 res_sv = hv_iternextsv(my_hv,&key,&key_len);
589                 if (SvROK(res_sv) && (SvTYPE(SvRV(res_sv)) == SVt_PVAV)) {
590                         av = (AV*)SvRV(res_sv);
591                         len = av_len(av);
592                         for (j = 0; j <= len; j++) {
593                                 av_sv = av_fetch(av, j, 0);
594                                 ret = pairadd_sv(ctx, vps, key, *av_sv, T_OP_ADD) + ret;
595                         }
596                 } else ret = pairadd_sv(ctx, vps, key, res_sv, T_OP_EQ) + ret;
597         }
598
599         return ret;
600 }
601
602 /*
603  *      Call the function_name inside the module
604  *      Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
605  *
606  */
607 static int do_perl(void *instance, REQUEST *request, char *function_name)
608 {
609
610         rlm_perl_t      *inst = instance;
611         VALUE_PAIR      *vp;
612         int             exitstatus=0, count;
613         STRLEN          n_a;
614
615         HV              *rad_reply_hv;
616         HV              *rad_check_hv;
617         HV              *rad_config_hv;
618         HV              *rad_request_hv;
619 #ifdef WITH_PROXY
620         HV              *rad_request_proxy_hv;
621         HV              *rad_request_proxy_reply_hv;
622 #endif
623
624         /*
625          *      Radius has told us to call this function, but none
626          *      is defined.
627          */
628         if (!function_name) return RLM_MODULE_FAIL;
629
630 #ifdef USE_ITHREADS
631         pthread_mutex_lock(&inst->clone_mutex);
632
633         PerlInterpreter *interp;
634
635         interp = rlm_perl_clone(inst->perl,inst->thread_key);
636         {
637                 dTHXa(interp);
638                 PERL_SET_CONTEXT(interp);
639         }
640
641         pthread_mutex_unlock(&inst->clone_mutex);
642 #else
643         PERL_SET_CONTEXT(inst->perl);
644 #endif
645
646         {
647                 dSP;
648
649                 ENTER;
650                 SAVETMPS;
651
652                 rad_reply_hv = get_hv("RAD_REPLY",1);
653                 rad_check_hv = get_hv("RAD_CHECK",1);
654                 rad_config_hv = get_hv("RAD_CONFIG",1);
655                 rad_request_hv = get_hv("RAD_REQUEST",1);
656
657                 perl_store_vps(request->reply, request->reply->vps, rad_reply_hv);
658                 perl_store_vps(request, request->config_items, rad_check_hv);
659                 perl_store_vps(request->packet, request->packet->vps, rad_request_hv);
660                 perl_store_vps(request, request->config_items, rad_config_hv);
661
662 #ifdef WITH_PROXY
663                 rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
664                 rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
665
666                 if (request->proxy != NULL) {
667                         perl_store_vps(request->proxy, request->proxy->vps, rad_request_proxy_hv);
668                 } else {
669                         hv_undef(rad_request_proxy_hv);
670                 }
671
672                 if (request->proxy_reply !=NULL) {
673                         perl_store_vps(request->proxy_reply, request->proxy_reply->vps, rad_request_proxy_reply_hv);
674                 } else {
675                         hv_undef(rad_request_proxy_reply_hv);
676                 }
677 #endif
678
679                 PUSHMARK(SP);
680                 /*
681                  * This way %RAD_xx can be pushed onto stack as sub parameters.
682                  * XPUSHs( newRV_noinc((SV *)rad_request_hv) );
683                  * XPUSHs( newRV_noinc((SV *)rad_reply_hv) );
684                  * XPUSHs( newRV_noinc((SV *)rad_check_hv) );
685                  * PUTBACK;
686                  */
687
688                 count = call_pv(function_name, G_SCALAR | G_EVAL | G_NOARGS);
689
690                 SPAGAIN;
691
692                 if (SvTRUE(ERRSV)) {
693                         ERROR("rlm_perl: perl_embed:: module = %s , func = %s exit status= %s\n",
694                                inst->module,
695                                function_name, SvPV(ERRSV,n_a));
696                         (void)POPs;
697                 }
698
699                 if (count == 1) {
700                         exitstatus = POPi;
701                         if (exitstatus >= 100 || exitstatus < 0) {
702                                 exitstatus = RLM_MODULE_FAIL;
703                         }
704                 }
705
706
707                 PUTBACK;
708                 FREETMPS;
709                 LEAVE;
710
711                 vp = NULL;
712                 if ((get_hv_content(request->packet, rad_request_hv, &vp)) > 0 ) {
713                         pairfree(&request->packet->vps);
714                         request->packet->vps = vp;
715                         vp = NULL;
716
717                         /*
718                          *      Update cached copies
719                          */
720                         request->username = pairfind(request->packet->vps, PW_USER_NAME, 0, TAG_ANY);
721                         request->password = pairfind(request->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
722                         if (!request->password)
723                                 request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
724                 }
725
726                 if ((get_hv_content(request->reply, rad_reply_hv, &vp)) > 0 ) {
727                         pairfree(&request->reply->vps);
728                         request->reply->vps = vp;
729                         vp = NULL;
730                 }
731
732                 if ((get_hv_content(request, rad_check_hv, &vp)) > 0 ) {
733                         pairfree(&request->config_items);
734                         request->config_items = vp;
735                         vp = NULL;
736                 }
737
738 #ifdef WITH_PROXY
739                 if (request->proxy &&
740                     (get_hv_content(request->proxy, rad_request_proxy_hv, &vp) > 0)) {
741                         pairfree(&request->proxy->vps);
742                         request->proxy->vps = vp;
743                         vp = NULL;
744                 }
745
746                 if (request->proxy_reply &&
747                     (get_hv_content(request->proxy_reply, rad_request_proxy_reply_hv, &vp) > 0)) {
748                         pairfree(&request->proxy_reply->vps);
749                         request->proxy_reply->vps = vp;
750                         vp = NULL;
751                 }
752 #endif
753
754         }
755         return exitstatus;
756 }
757
758 #define RLM_PERL_FUNC(_x) static rlm_rcode_t mod_##_x(void *instance, REQUEST *request) \
759         {                                                               \
760                 return do_perl(instance, request,                       \
761                                ((rlm_perl_t *)instance)->func_##_x); \
762         }
763
764 RLM_PERL_FUNC(authorize)
765 RLM_PERL_FUNC(authenticate)
766 RLM_PERL_FUNC(post_auth)
767
768 RLM_PERL_FUNC(checksimul)
769
770 #ifdef WITH_PROXY
771 RLM_PERL_FUNC(pre_proxy)
772 RLM_PERL_FUNC(post_proxy)
773 #endif
774
775 #ifdef WITH_COA
776 RLM_PERL_FUNC(recv_coa)
777 RLM_PERL_FUNC(send_coa)
778 #endif
779
780 RLM_PERL_FUNC(preacct)
781
782 /*
783  *      Write accounting information to this modules database.
784  */
785 static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
786 {
787         VALUE_PAIR      *pair;
788         int             acctstatustype=0;
789
790         if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0, TAG_ANY)) != NULL) {
791                 acctstatustype = pair->vp_integer;
792         } else {
793                 ERROR("Invalid Accounting Packet");
794                 return RLM_MODULE_INVALID;
795         }
796
797         switch (acctstatustype) {
798
799         case PW_STATUS_START:
800
801                 if (((rlm_perl_t *)instance)->func_start_accounting) {
802                         return do_perl(instance, request,
803                                        ((rlm_perl_t *)instance)->func_start_accounting);
804                 } else {
805                         return do_perl(instance, request,
806                                        ((rlm_perl_t *)instance)->func_accounting);
807                 }
808                 break;
809
810         case PW_STATUS_STOP:
811
812                 if (((rlm_perl_t *)instance)->func_stop_accounting) {
813                         return do_perl(instance, request,
814                                        ((rlm_perl_t *)instance)->func_stop_accounting);
815                 } else {
816                         return do_perl(instance, request,
817                                        ((rlm_perl_t *)instance)->func_accounting);
818                 }
819                 break;
820         default:
821                 return do_perl(instance, request,
822                                ((rlm_perl_t *)instance)->func_accounting);
823
824         }
825 }
826
827
828 /*
829  * Detach a instance give a chance to a module to make some internal setup ...
830  */
831 static int mod_detach(void *instance)
832 {
833         rlm_perl_t      *inst = (rlm_perl_t *) instance;
834         int             exitstatus = 0, count = 0;
835
836 #if 0
837         /*
838          *      FIXME: Call this in the destruct function?
839          */
840         {
841                 dTHXa(handle->clone);
842                 PERL_SET_CONTEXT(handle->clone);
843                 {
844                         dSP; ENTER; SAVETMPS; PUSHMARK(SP);
845                         count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
846                         SPAGAIN;
847
848                         if (count == 1) {
849                                 exitstatus = POPi;
850                                 /*
851                                  * FIXME: bug in perl
852                                  *
853                                  */
854                                 if (exitstatus >= 100 || exitstatus < 0) {
855                                         exitstatus = RLM_MODULE_FAIL;
856                                 }
857                         }
858                         PUTBACK;
859                         FREETMPS;
860                         LEAVE;
861                 }
862         }
863 #endif
864
865         if (inst->func_detach) {
866                 dTHXa(inst->perl);
867                 PERL_SET_CONTEXT(inst->perl);
868                 {
869                         dSP; ENTER; SAVETMPS;
870                         PUSHMARK(SP);
871
872                         count = call_pv(inst->func_detach, G_SCALAR | G_EVAL );
873                         SPAGAIN;
874
875                         if (count == 1) {
876                                 exitstatus = POPi;
877                                 if (exitstatus >= 100 || exitstatus < 0) {
878                                         exitstatus = RLM_MODULE_FAIL;
879                                 }
880                         }
881                         PUTBACK;
882                         FREETMPS;
883                         LEAVE;
884                 }
885         }
886
887 #ifdef USE_ITHREADS
888         rlm_perl_destruct(inst->perl);
889         pthread_mutex_destroy(&inst->clone_mutex);
890 #else
891         perl_destruct(inst->perl);
892         perl_free(inst->perl);
893 #endif
894
895         PERL_SYS_TERM();
896         return exitstatus;
897 }
898
899
900 /*
901  *      The module name should be the only globally exported symbol.
902  *      That is, everything else should be 'static'.
903  *
904  *      If the module needs to temporarily modify it's instantiation
905  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
906  *      The server will then take care of ensuring that the module
907  *      is single-threaded.
908  */
909 module_t rlm_perl = {
910         RLM_MODULE_INIT,
911         "perl",                         /* Name */
912 #ifdef USE_ITHREADS
913         RLM_TYPE_THREAD_SAFE,           /* type */
914 #else
915         RLM_TYPE_THREAD_UNSAFE,
916 #endif
917         sizeof(rlm_perl_t),
918         module_config,
919         mod_instantiate,                /* instantiation */
920         mod_detach,                     /* detach */
921         {
922                 mod_authenticate,       /* authenticate */
923                 mod_authorize,          /* authorize */
924                 mod_preacct,            /* preacct */
925                 mod_accounting, /* accounting */
926                 mod_checksimul,         /* check simul */
927 #ifdef WITH_PROXY
928                 mod_pre_proxy,          /* pre-proxy */
929                 mod_post_proxy, /* post-proxy */
930 #else
931                 NULL, NULL,
932 #endif
933                 mod_post_auth           /* post-auth */
934 #ifdef WITH_COA
935                 , mod_recv_coa,
936                 mod_send_coa
937 #endif
938         },
939 };