]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/config.c
ITS#1659
[openldap] / servers / slapd / back-perl / config.c
1 /* $OpenLDAP$ */
2 /*
3  *       Copyright 1999, John C. Quillan, All rights reserved.
4  *       Portions Copyright 2002, myinternet pty ltd. All rights reserved.
5  *
6  *       Redistribution and use in source and binary forms are permitted only
7  *       as authorized by the OpenLDAP Public License.  A copy of this
8  *       license is available at http://www.OpenLDAP.org/license.html or
9  *       in file LICENSE in the top-level directory of the distribution.
10  */
11
12 #include "portable.h"
13 /* init.c - initialize shell backend */
14         
15 #include <stdio.h>
16 /*      #include <ac/types.h>
17         #include <ac/socket.h>
18 */
19
20 #include <EXTERN.h>
21 #include <perl.h>
22
23 #include "slap.h"
24 #include "perl_back.h"
25
26
27 /**********************************************************
28  *
29  * Config
30  *
31  **********************************************************/
32 int
33 perl_back_db_config(
34          BackendDB *be,
35          const char *fname,
36          int lineno,
37          int argc,
38          char **argv
39 )
40 {
41         SV* loc_sv;
42         PerlBackend *perl_back = (PerlBackend *) be->be_private;
43         char eval_str[EVAL_BUF_SIZE];
44         int count ;
45         int args;
46         int return_code;
47         
48
49         if ( strcasecmp( argv[0], "perlModule" ) == 0 ) {
50                 if ( argc < 2 ) {
51                         Debug( LDAP_DEBUG_ANY,
52                                  "%s.pm: line %d: missing module in \"perlModule <module>\" line\n",
53                                 fname, lineno, 0 );
54                         return( 1 );
55                 }
56
57 #ifdef PERL_IS_5_6
58                 snprintf( eval_str, EVAL_BUF_SIZE, "use %s;", argv[1] );
59                 eval_pv( eval_str, 0 );
60
61                 if (SvTRUE(ERRSV)) {
62                         fprintf(stderr , "Error %s\n", SvPV(ERRSV,  na)) ;
63 #else
64                 snprintf( eval_str, EVAL_BUF_SIZE, "%s", argv[1] );
65
66                 perl_require_pv( strcat( eval_str, ".pm" ));
67
68                 if (SvTRUE(GvSV(errgv))) {
69                         fprintf(stderr , "Error %s\n", SvPV(GvSV(errgv), na)) ;
70 #endif /* PERL_IS_5_6 */
71                 } else {
72                         dSP; ENTER; SAVETMPS;
73                         PUSHMARK(sp);
74                         XPUSHs(sv_2mortal(newSVpv(argv[1], 0)));
75                         PUTBACK;
76
77 #ifdef PERL_IS_5_6
78                         count = call_method("new", G_SCALAR);
79 #else
80                         count = perl_call_method("new", G_SCALAR);
81 #endif
82
83                         SPAGAIN;
84
85                         if (count != 1) {
86                                 croak("Big trouble in config\n") ;
87                         }
88
89                         perl_back->pb_obj_ref = newSVsv(POPs);
90
91                         PUTBACK; FREETMPS; LEAVE ;
92                 }
93
94         } else if ( strcasecmp( argv[0], "perlModulePath" ) == 0 ) {
95                 if ( argc < 2 ) {
96                         fprintf( stderr,
97                                 "%s: line %d: missing module in \"PerlModulePath <module>\" line\n",
98                                 fname, lineno );
99                         return( 1 );
100                 }
101
102                 snprintf( eval_str, EVAL_BUF_SIZE, "push @INC, '%s';", argv[1] );
103 #ifdef PERL_IS_5_6
104                 loc_sv = eval_pv( eval_str, 0 );
105 #else
106                 loc_sv = perl_eval_pv( eval_str, 0 );
107 #endif
108
109         } else if ( strcasecmp( argv[0], "filterSearchResults" ) == 0 ) {
110                 perl_back->pb_filter_search_results = 1;
111         } else {
112                 /*
113                  * Pass it to Perl module if defined
114                  */
115
116                 {
117                         dSP ;  ENTER ; SAVETMPS;
118
119                         PUSHMARK(sp) ;
120                         XPUSHs( perl_back->pb_obj_ref );
121
122                         /* Put all arguments on the perl stack */
123                         for( args = 0; args < argc; args++ ) {
124                                 XPUSHs(sv_2mortal(newSVpv(argv[args], 0)));
125                         }
126
127                         PUTBACK ;
128
129 #ifdef PERL_IS_5_6
130                         count = call_method("config", G_SCALAR);
131 #else
132                         count = perl_call_method("config", G_SCALAR);
133 #endif
134
135                         SPAGAIN ;
136
137                         if (count != 1) {
138                                 croak("Big trouble in config\n") ;
139                         }
140
141                         return_code = POPi;
142
143                         PUTBACK ; FREETMPS ;  LEAVE ;
144
145                 }
146
147                 /* if the module rejected it then we should reject it */
148                 if ( return_code != 0 ) {
149                         fprintf( stderr,
150                                  "Unknown perl backend config: %s\n", argv[0]);
151                         exit( EXIT_FAILURE );
152                 }
153         }
154
155         return 0;
156 }