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