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