]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/search.c
group rewrite/map stuff in one structure and optimize more function calls
[openldap] / servers / slapd / back-perl / search.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  * Search
29  *
30  **********************************************************/
31 int
32 perl_back_search(
33         Operation *op,
34         SlapReply *rs )
35 {
36         PerlBackend *perl_back = (PerlBackend *)op->o_bd->be_private;
37         int count ;
38         AttributeName *an;
39         Entry   *e;
40         char *buf;
41         int i;
42
43         ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );  
44
45         {
46                 dSP; ENTER; SAVETMPS;
47
48                 PUSHMARK(sp) ;
49                 XPUSHs( perl_back->pb_obj_ref );
50                 XPUSHs(sv_2mortal(newSVpv( op->o_req_ndn.bv_val , 0)));
51                 XPUSHs(sv_2mortal(newSViv( op->ors_scope )));
52                 XPUSHs(sv_2mortal(newSViv( op->ors_deref )));
53                 XPUSHs(sv_2mortal(newSViv( op->ors_slimit )));
54                 XPUSHs(sv_2mortal(newSViv( op->ors_tlimit )));
55                 XPUSHs(sv_2mortal(newSVpv( op->ors_filterstr.bv_val , 0)));
56                 XPUSHs(sv_2mortal(newSViv( op->ors_attrsonly )));
57
58                 for ( an = op->ors_attrs; an && an->an_name.bv_val; an++ ) {
59                         XPUSHs(sv_2mortal(newSVpv( an->an_name.bv_val , 0)));
60                 }
61                 PUTBACK;
62
63 #ifdef PERL_IS_5_6
64                 count = call_method("search", G_ARRAY );
65 #else
66                 count = perl_call_method("search", G_ARRAY );
67 #endif
68
69                 SPAGAIN;
70
71                 if (count < 1) {
72                         croak("Big trouble in back_search\n") ;
73                 }
74
75                 if ( count > 1 ) {
76                                                          
77                         for ( i = 1; i < count; i++ ) {
78
79                                 buf = POPp;
80
81                                 if ( (e = str2entry( buf )) == NULL ) {
82                                         Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", buf, 0, 0 );
83
84                                 } else {
85                                         int send_entry;
86
87                                         if (perl_back->pb_filter_search_results)
88                                                 send_entry = (test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE);
89                                         else
90                                                 send_entry = 1;
91
92                                         if (send_entry) {
93                                                 rs->sr_entry = e;
94                                                 rs->sr_attrs = op->ors_attrs;
95                                                 send_search_entry( op, rs );
96                                         }
97
98                                         entry_free( e );
99                                 }
100                         }
101                 }
102
103                 /*
104                  * We grab the return code last because the stack comes
105                  * from perl in reverse order. 
106                  *
107                  * ex perl: return ( 0, $res_1, $res_2 );
108                  *
109                  * ex stack: <$res_2> <$res_1> <0>
110                  */
111
112                 rs->sr_err = POPi;
113
114
115
116                 PUTBACK; FREETMPS; LEAVE;
117         }
118
119         ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );        
120
121         send_ldap_result( op, rs );
122
123         return 0;
124 }
125