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