xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-perl/search.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: search.c,v 1.3 2021/08/14 16:15:01 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2021 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  **********************************************************/
27 int
perl_back_search(Operation * op,SlapReply * rs)28 perl_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 	PERL_SET_CONTEXT( PERL_INTERPRETER );
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 , op->o_req_ndn.bv_len)));
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 , op->ors_filterstr.bv_len)));
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 , an->an_name.bv_len)));
57 		}
58 		PUTBACK;
59 
60 		count = call_method("search", G_ARRAY );
61 
62 		SPAGAIN;
63 
64 		if (count < 1) {
65 			croak("Big trouble in back_search\n") ;
66 		}
67 
68 		if ( count > 1 ) {
69 
70 			for ( i = 1; i < count; i++ ) {
71 
72 				buf = POPp;
73 
74 				if ( (e = str2entry( buf )) == NULL ) {
75 					Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", buf );
76 
77 				} else {
78 					int send_entry;
79 
80 					if (perl_back->pb_filter_search_results)
81 						send_entry = (test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE);
82 					else
83 						send_entry = 1;
84 
85 					if (send_entry) {
86 						rs->sr_entry = e;
87 						rs->sr_attrs = op->ors_attrs;
88 						rs->sr_flags = REP_ENTRY_MODIFIABLE;
89 						rs->sr_err = LDAP_SUCCESS;
90 						rs->sr_err = send_search_entry( op, rs );
91 						rs->sr_flags = 0;
92 						rs->sr_attrs = NULL;
93 						rs->sr_entry = NULL;
94 						if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED || rs->sr_err == LDAP_BUSY ) {
95 							goto done;
96 						}
97 					}
98 
99 					entry_free( e );
100 				}
101 			}
102 		}
103 
104 		/*
105 		 * We grab the return code last because the stack comes
106 		 * from perl in reverse order.
107 		 *
108 		 * ex perl: return ( 0, $res_1, $res_2 );
109 		 *
110 		 * ex stack: <$res_2> <$res_1> <0>
111 		 */
112 
113 		rs->sr_err = POPi;
114 
115 done:;
116 		PUTBACK; FREETMPS; LEAVE;
117 	}
118 
119 	ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );
120 
121 	send_ldap_result( op, rs );
122 
123 	return 0;
124 }
125