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