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