xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-perl/search.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-perl/search.c,v 1.31.2.3 2008/02/11 23:26:47 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2008 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 #if defined(HAVE_WIN32_ASPERL) || defined(USE_ITHREADS)
38 	PERL_SET_CONTEXT( PERL_INTERPRETER );
39 #endif
40 	ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );
41 
42 	{
43 		dSP; ENTER; SAVETMPS;
44 
45 		PUSHMARK(sp) ;
46 		XPUSHs( perl_back->pb_obj_ref );
47 		XPUSHs(sv_2mortal(newSVpv( op->o_req_ndn.bv_val , 0)));
48 		XPUSHs(sv_2mortal(newSViv( op->ors_scope )));
49 		XPUSHs(sv_2mortal(newSViv( op->ors_deref )));
50 		XPUSHs(sv_2mortal(newSViv( op->ors_slimit )));
51 		XPUSHs(sv_2mortal(newSViv( op->ors_tlimit )));
52 		XPUSHs(sv_2mortal(newSVpv( op->ors_filterstr.bv_val , 0)));
53 		XPUSHs(sv_2mortal(newSViv( op->ors_attrsonly )));
54 
55 		for ( an = op->ors_attrs; an && an->an_name.bv_val; an++ ) {
56 			XPUSHs(sv_2mortal(newSVpv( an->an_name.bv_val , 0)));
57 		}
58 		PUTBACK;
59 
60 #ifdef PERL_IS_5_6
61 		count = call_method("search", G_ARRAY );
62 #else
63 		count = perl_call_method("search", G_ARRAY );
64 #endif
65 
66 		SPAGAIN;
67 
68 		if (count < 1) {
69 			croak("Big trouble in back_search\n") ;
70 		}
71 
72 		if ( count > 1 ) {
73 
74 			for ( i = 1; i < count; i++ ) {
75 
76 				buf = POPp;
77 
78 				if ( (e = str2entry( buf )) == NULL ) {
79 					Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", buf, 0, 0 );
80 
81 				} else {
82 					int send_entry;
83 
84 					if (perl_back->pb_filter_search_results)
85 						send_entry = (test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE);
86 					else
87 						send_entry = 1;
88 
89 					if (send_entry) {
90 						rs->sr_entry = e;
91 						rs->sr_attrs = op->ors_attrs;
92 						rs->sr_flags = REP_ENTRY_MODIFIABLE;
93 						rs->sr_err = LDAP_SUCCESS;
94 						rs->sr_err = send_search_entry( op, rs );
95 						if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
96 							rs->sr_entry = NULL;
97 							goto done;
98 						}
99 					}
100 
101 					entry_free( e );
102 				}
103 			}
104 		}
105 
106 		/*
107 		 * We grab the return code last because the stack comes
108 		 * from perl in reverse order.
109 		 *
110 		 * ex perl: return ( 0, $res_1, $res_2 );
111 		 *
112 		 * ex stack: <$res_2> <$res_1> <0>
113 		 */
114 
115 		rs->sr_err = POPi;
116 
117 done:;
118 		PUTBACK; FREETMPS; LEAVE;
119 	}
120 
121 	ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );
122 
123 	send_ldap_result( op, rs );
124 
125 	return 0;
126 }
127 
128