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