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