]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/search.c
Happy new year
[openldap] / servers / slapd / back-perl / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 John C. Quillan.
6  * Portions Copyright 2002 myinternet Limited.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include <EXTERN.h>
19 #include <perl.h>
20 #undef _ /* #defined by both Perl and ac/localize.h */
21
22 #ifdef HAVE_WIN32_ASPERL
23 #include "asperl_undefs.h"
24 #endif
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include "slap.h"
31
32 #include "perl_back.h"
33
34 /**********************************************************
35  *
36  * Search
37  *
38  **********************************************************/
39 int
40 perl_back_search(
41         Operation *op,
42         SlapReply *rs )
43 {
44         PerlBackend *perl_back = (PerlBackend *)op->o_bd->be_private;
45         int count ;
46         AttributeName *an;
47         Entry   *e;
48         char *buf;
49         int i;
50
51         ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );  
52
53         {
54                 dSP; ENTER; SAVETMPS;
55
56                 PUSHMARK(sp) ;
57                 XPUSHs( perl_back->pb_obj_ref );
58                 XPUSHs(sv_2mortal(newSVpv( op->o_req_ndn.bv_val , 0)));
59                 XPUSHs(sv_2mortal(newSViv( op->ors_scope )));
60                 XPUSHs(sv_2mortal(newSViv( op->ors_deref )));
61                 XPUSHs(sv_2mortal(newSViv( op->ors_slimit )));
62                 XPUSHs(sv_2mortal(newSViv( op->ors_tlimit )));
63                 XPUSHs(sv_2mortal(newSVpv( op->ors_filterstr.bv_val , 0)));
64                 XPUSHs(sv_2mortal(newSViv( op->ors_attrsonly )));
65
66                 for ( an = op->ors_attrs; an && an->an_name.bv_val; an++ ) {
67                         XPUSHs(sv_2mortal(newSVpv( an->an_name.bv_val , 0)));
68                 }
69                 PUTBACK;
70
71 #ifdef PERL_IS_5_6
72                 count = call_method("search", G_ARRAY );
73 #else
74                 count = perl_call_method("search", G_ARRAY );
75 #endif
76
77                 SPAGAIN;
78
79                 if (count < 1) {
80                         croak("Big trouble in back_search\n") ;
81                 }
82
83                 if ( count > 1 ) {
84                                                          
85                         for ( i = 1; i < count; i++ ) {
86
87                                 buf = POPp;
88
89                                 if ( (e = str2entry( buf )) == NULL ) {
90                                         Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", buf, 0, 0 );
91
92                                 } else {
93                                         int send_entry;
94
95                                         if (perl_back->pb_filter_search_results)
96                                                 send_entry = (test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE);
97                                         else
98                                                 send_entry = 1;
99
100                                         if (send_entry) {
101                                                 rs->sr_entry = e;
102                                                 rs->sr_attrs = op->ors_attrs;
103                                                 send_search_entry( op, rs );
104                                         }
105
106                                         entry_free( e );
107                                 }
108                         }
109                 }
110
111                 /*
112                  * We grab the return code last because the stack comes
113                  * from perl in reverse order. 
114                  *
115                  * ex perl: return ( 0, $res_1, $res_2 );
116                  *
117                  * ex stack: <$res_2> <$res_1> <0>
118                  */
119
120                 rs->sr_err = POPi;
121
122
123
124                 PUTBACK; FREETMPS; LEAVE;
125         }
126
127         ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );        
128
129         send_ldap_result( op, rs );
130
131         return 0;
132 }
133