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