]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/search.c
5670a4e9f477e4e38206ae844155c75d1b033895
[openldap] / servers / slapd / back-perl / search.c
1 /* $OpenLDAP$ */
2 /*
3  *       Copyright 1999, John C. Quillan, All rights reserved.
4  *       Portions Copyright 2002, myinternet pty ltd. 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 /*      #include <ac/types.h>
16         #include <ac/socket.h>
17 */
18
19 #include <EXTERN.h>
20 #include <perl.h>
21
22 #include "slap.h"
23 #include "perl_back.h"
24
25 /**********************************************************
26  *
27  * Search
28  *
29  **********************************************************/
30 int
31 perl_back_search(
32         Backend *be,
33         Connection *conn,
34         Operation *op,
35         struct berval *base,
36         struct berval *nbase,
37         int scope,
38         int deref,
39         int sizelimit,
40         int timelimit,
41         Filter *filter,
42         struct berval *filterstr,
43         AttributeName *attrs,
44         int attrsonly
45         )
46 {
47         char test[500];
48         int count ;
49         int err = 0;
50         char *matched = NULL, *info = NULL;
51         PerlBackend *perl_back = (PerlBackend *)be->be_private;
52         AttributeName *an;
53         Entry   *e;
54         char *buf;
55         int i;
56         int return_code;
57
58         ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );  
59
60         {
61                 dSP; ENTER; SAVETMPS;
62
63                 PUSHMARK(sp) ;
64                 XPUSHs( perl_back->pb_obj_ref );
65                 XPUSHs(sv_2mortal(newSVpv( nbase->bv_val , 0)));
66                 XPUSHs(sv_2mortal(newSViv( scope )));
67                 XPUSHs(sv_2mortal(newSViv( deref )));
68                 XPUSHs(sv_2mortal(newSViv( sizelimit )));
69                 XPUSHs(sv_2mortal(newSViv( timelimit )));
70                 XPUSHs(sv_2mortal(newSVpv( filterstr->bv_val , 0)));
71                 XPUSHs(sv_2mortal(newSViv( attrsonly )));
72
73                 for ( an = attrs; an && an->an_name.bv_val; an++ ) {
74                         XPUSHs(sv_2mortal(newSVpv( an->an_name.bv_val , 0)));
75                 }
76                 PUTBACK;
77
78 #ifdef PERL_IS_5_6
79                 count = call_method("search", G_ARRAY );
80 #else
81                 count = perl_call_method("search", G_ARRAY );
82 #endif
83
84                 SPAGAIN;
85
86                 if (count < 1) {
87                         croak("Big trouble in back_search\n") ;
88                 }
89
90                 if ( count > 1 ) {
91                                                          
92                         for ( i = 1; i < count; i++ ) {
93
94                                 buf = POPp;
95
96                                 if ( (e = str2entry( buf )) == NULL ) {
97                                         Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", buf, 0, 0 );
98
99                                 } else {
100                                         int send_entry;
101
102                                         if (perl_back->pb_filter_search_results)
103                                                 send_entry = (test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE);
104                                         else
105                                                 send_entry = 1;
106
107                                         if (send_entry) {
108                                                 send_search_entry( be, conn, op,
109                                                         e, attrs, attrsonly, NULL );
110                                         }
111
112                                         entry_free( e );
113                                 }
114                         }
115                 }
116
117                 /*
118                  * We grab the return code last because the stack comes
119                  * from perl in reverse order. 
120                  *
121                  * ex perl: return ( 0, $res_1, $res_2 );
122                  *
123                  * ex stack: <$res_2> <$res_1> <0>
124                  */
125
126                 return_code = POPi;
127
128
129
130                 PUTBACK; FREETMPS; LEAVE;
131         }
132
133         ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );        
134
135         send_ldap_result( conn, op, return_code,
136                 NULL, NULL, NULL, NULL );
137 }
138