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