]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/config.c
Latest changes from HEAD.
[openldap] / servers / slapd / back-perl / config.c
1 /* $OpenLDAP$ */
2 /*
3  *       Copyright 1999, John C. Quillan, All rights reserved.
4  *       Portions Copyright 2002, myinternet Limited. 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         
14 #include <stdio.h>
15
16 #include "slap.h"
17 #ifdef HAVE_WIN32_ASPERL
18 #include "asperl_undefs.h"
19 #endif
20
21 #include <EXTERN.h>
22 #include <perl.h>
23
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                         STRLEN n_a;
63
64                         fprintf(stderr , "Error %s\n", SvPV(ERRSV, n_a)) ;
65                 }
66 #else
67                 snprintf( eval_str, EVAL_BUF_SIZE, "%s.pm", argv[1] );
68                 perl_require_pv( eval_str );
69
70                 if (SvTRUE(GvSV(errgv))) {
71                         fprintf(stderr , "Error %s\n", SvPV(GvSV(errgv), na)) ;
72                 }
73 #endif /* PERL_IS_5_6 */
74                 else {
75                         dSP; ENTER; SAVETMPS;
76                         PUSHMARK(sp);
77                         XPUSHs(sv_2mortal(newSVpv(argv[1], 0)));
78                         PUTBACK;
79
80 #ifdef PERL_IS_5_6
81                         count = call_method("new", G_SCALAR);
82 #else
83                         count = perl_call_method("new", G_SCALAR);
84 #endif
85
86                         SPAGAIN;
87
88                         if (count != 1) {
89                                 croak("Big trouble in config\n") ;
90                         }
91
92                         perl_back->pb_obj_ref = newSVsv(POPs);
93
94                         PUTBACK; FREETMPS; LEAVE ;
95                 }
96
97         } else if ( strcasecmp( argv[0], "perlModulePath" ) == 0 ) {
98                 if ( argc < 2 ) {
99                         fprintf( stderr,
100                                 "%s: line %d: missing module in \"PerlModulePath <module>\" line\n",
101                                 fname, lineno );
102                         return( 1 );
103                 }
104
105                 snprintf( eval_str, EVAL_BUF_SIZE, "push @INC, '%s';", argv[1] );
106 #ifdef PERL_IS_5_6
107                 loc_sv = eval_pv( eval_str, 0 );
108 #else
109                 loc_sv = perl_eval_pv( eval_str, 0 );
110 #endif
111
112                 /* XXX loc_sv return value is ignored. */
113
114         } else if ( strcasecmp( argv[0], "filterSearchResults" ) == 0 ) {
115                 perl_back->pb_filter_search_results = 1;
116         } else {
117                 /*
118                  * Pass it to Perl module if defined
119                  */
120
121                 {
122                         dSP ;  ENTER ; SAVETMPS;
123
124                         PUSHMARK(sp) ;
125                         XPUSHs( perl_back->pb_obj_ref );
126
127                         /* Put all arguments on the perl stack */
128                         for( args = 0; args < argc; args++ ) {
129                                 XPUSHs(sv_2mortal(newSVpv(argv[args], 0)));
130                         }
131
132                         PUTBACK ;
133
134 #ifdef PERL_IS_5_6
135                         count = call_method("config", G_SCALAR);
136 #else
137                         count = perl_call_method("config", G_SCALAR);
138 #endif
139
140                         SPAGAIN ;
141
142                         if (count != 1) {
143                                 croak("Big trouble in config\n") ;
144                         }
145
146                         return_code = POPi;
147
148                         PUTBACK ; FREETMPS ;  LEAVE ;
149
150                 }
151
152                 /* if the module rejected it then we should reject it */
153                 if ( return_code != 0 ) {
154                         fprintf( stderr,
155                                  "Unknown perl backend config: %s\n", argv[0]);
156                         exit( EXIT_FAILURE );
157                 }
158         }
159
160         return 0;
161 }