]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
address ITS#4332; might remove dynamicObject counting
[openldap] / servers / slapd / back-monitor / search.c
1 /* search.c - monitor backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
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 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/string.h>
27 #include <ac/socket.h>
28
29 #include "slap.h"
30 #include "back-monitor.h"
31 #include "proto-back-monitor.h"
32
33 static int
34 monitor_send_children(
35         Operation       *op,
36         SlapReply       *rs,
37         Entry           *e_parent,
38         int             sub
39 )
40 {
41         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
42         Entry                   *e,
43                                 *e_tmp,
44                                 *e_ch = NULL,
45                                 *e_nonvolatile = NULL;
46         monitor_entry_t *mp;
47         int                     rc,
48                                 nonvolatile = 0;
49
50         mp = ( monitor_entry_t * )e_parent->e_private;
51         e_nonvolatile = e = mp->mp_children;
52
53         if ( MONITOR_HAS_VOLATILE_CH( mp ) ) {
54                 monitor_entry_create( op, rs, NULL, e_parent, &e_ch );
55         }
56         monitor_cache_release( mi, e_parent );
57
58         /* no volatile entries? */
59         if ( e_ch == NULL ) {
60                 /* no persistent entries? return */
61                 if ( e == NULL ) {
62                         return LDAP_SUCCESS;
63                 }
64         
65         /* volatile entries */
66         } else {
67                 /* if no persistent, return only volatile */
68                 if ( e == NULL ) {
69                         e = e_ch;
70
71                 /* else append persistent to volatile */
72                 } else {
73                         e_tmp = e_ch;
74                         do {
75                                 mp = ( monitor_entry_t * )e_tmp->e_private;
76                                 e_tmp = mp->mp_next;
77         
78                                 if ( e_tmp == NULL ) {
79                                         mp->mp_next = e;
80                                         break;
81                                 }
82                         } while ( e_tmp );
83                         e = e_ch;
84                 }
85         }
86
87         /* return entries */
88         for ( monitor_cache_lock( e ); e != NULL; ) {
89                 monitor_entry_update( op, rs, e );
90
91                 if ( op->o_abandon ) {
92                         /* FIXME: may leak generated children */
93                         if ( nonvolatile == 0 ) {
94                                 for ( e_tmp = e; e_tmp != NULL; ) {
95                                         mp = ( monitor_entry_t * )e_tmp->e_private;
96                                         e = e_tmp;
97                                         e_tmp = mp->mp_next;
98                                         monitor_cache_release( mi, e );
99
100                                         if ( e_tmp == e_nonvolatile ) {
101                                                 break;
102                                         }
103                                 }
104
105                         } else {
106                                 monitor_cache_release( mi, e );
107                         }
108
109                         return SLAPD_ABANDON;
110                 }
111                 
112                 rc = test_filter( op, e, op->oq_search.rs_filter );
113                 if ( rc == LDAP_COMPARE_TRUE ) {
114                         rs->sr_entry = e;
115                         rs->sr_flags = 0;
116                         rc = send_search_entry( op, rs );
117                         rs->sr_entry = NULL;
118                 }
119
120                 mp = ( monitor_entry_t * )e->e_private;
121                 e_tmp = mp->mp_next;
122
123                 if ( sub ) {
124                         rc = monitor_send_children( op, rs, e, sub );
125                         if ( rc ) {
126                                 /* FIXME: may leak generated children */
127                                 if ( nonvolatile == 0 ) {
128                                         for ( ; e_tmp != NULL; ) {
129                                                 mp = ( monitor_entry_t * )e_tmp->e_private;
130                                                 e = e_tmp;
131                                                 e_tmp = mp->mp_next;
132                                                 monitor_cache_release( mi, e );
133         
134                                                 if ( e_tmp == e_nonvolatile ) {
135                                                         break;
136                                                 }
137                                         }
138                                 }
139
140                                 return( rc );
141                         }
142                 }
143
144                 if ( e_tmp != NULL ) {
145                         monitor_cache_lock( e_tmp );
146                 }
147
148                 if ( !sub ) {
149                         /* otherwise the recursive call already released */
150                         monitor_cache_release( mi, e );
151                 }
152
153                 e = e_tmp;
154                 if ( e == e_nonvolatile ) {
155                         nonvolatile = 1;
156                 }
157         }
158         
159         return LDAP_SUCCESS;
160 }
161
162 int
163 monitor_back_search( Operation *op, SlapReply *rs )
164 {
165         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
166         int             rc = LDAP_SUCCESS;
167         Entry           *e = NULL, *matched = NULL;
168         slap_mask_t     mask;
169
170         Debug( LDAP_DEBUG_TRACE, "=> monitor_back_search\n", 0, 0, 0 );
171
172
173         /* get entry with reader lock */
174         monitor_cache_dn2entry( op, rs, &op->o_req_ndn, &e, &matched );
175         if ( e == NULL ) {
176                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
177                 if ( matched ) {
178 #ifdef SLAP_ACL_HONOR_DISCLOSE
179                         if ( !access_allowed_mask( op, matched,
180                                         slap_schema.si_ad_entry,
181                                         NULL, ACL_DISCLOSE, NULL, NULL ) )
182                         {
183                                 /* do nothing */ ;
184                         } else 
185 #endif /* SLAP_ACL_HONOR_DISCLOSE */
186                         {
187                                 rs->sr_matched = matched->e_dn;
188                         }
189                 }
190
191                 send_ldap_result( op, rs );
192                 if ( matched ) {
193                         monitor_cache_release( mi, matched );
194                         rs->sr_matched = NULL;
195                 }
196
197                 return rs->sr_err;
198         }
199
200         /* NOTE: __NEW__ "search" access is required
201          * on searchBase object */
202         if ( !access_allowed_mask( op, e, slap_schema.si_ad_entry,
203                                 NULL, ACL_SEARCH, NULL, &mask ) )
204         {
205                 monitor_cache_release( mi, e );
206
207 #ifdef SLAP_ACL_HONOR_DISCLOSE
208                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
209                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
210                 } else 
211 #endif /* SLAP_ACL_HONOR_DISCLOSE */
212                 {
213                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
214                 }
215
216                 send_ldap_result( op, rs );
217
218                 return rs->sr_err;
219         }
220
221         rs->sr_attrs = op->oq_search.rs_attrs;
222         switch ( op->oq_search.rs_scope ) {
223         case LDAP_SCOPE_BASE:
224                 monitor_entry_update( op, rs, e );
225                 rc = test_filter( op, e, op->oq_search.rs_filter );
226                 if ( rc == LDAP_COMPARE_TRUE ) {
227                         rs->sr_entry = e;
228                         rs->sr_flags = 0;
229                         send_search_entry( op, rs );
230                         rs->sr_entry = NULL;
231                 }
232                 rc = LDAP_SUCCESS;
233                 monitor_cache_release( mi, e );
234                 break;
235
236         case LDAP_SCOPE_ONELEVEL:
237                 rc = monitor_send_children( op, rs, e, 0 );
238                 break;
239
240         case LDAP_SCOPE_SUBTREE:
241                 monitor_entry_update( op, rs, e );
242                 rc = test_filter( op, e, op->oq_search.rs_filter );
243                 if ( rc == LDAP_COMPARE_TRUE ) {
244                         rs->sr_entry = e;
245                         rs->sr_flags = 0;
246                         send_search_entry( op, rs );
247                         rs->sr_entry = NULL;
248                 }
249
250                 rc = monitor_send_children( op, rs, e, 1 );
251                 break;
252         }
253
254         rs->sr_attrs = NULL;
255         rs->sr_err = rc;
256         if ( rs->sr_err != SLAPD_ABANDON ) {
257                 send_ldap_result( op, rs );
258         }
259
260         return rs->sr_err;
261 }
262