]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
Memory cleanup
[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-2005 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                         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 = e; 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                                 } else {
140                                         monitor_cache_release( mi, e );
141                                 }
142
143                                 return( rc );
144                         }
145                 }
146
147                 if ( e_tmp != NULL ) {
148                         monitor_cache_lock( e_tmp );
149                 }
150
151                 if ( !sub ) {
152                         /* otherwise the recursive call already released */
153                         monitor_cache_release( mi, e );
154                 }
155
156                 e = e_tmp;
157                 if ( e == e_nonvolatile ) {
158                         nonvolatile = 1;
159                 }
160         }
161         
162         return LDAP_SUCCESS;
163 }
164
165 int
166 monitor_back_search( Operation *op, SlapReply *rs )
167 {
168         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
169         int             rc = LDAP_SUCCESS;
170         Entry           *e = NULL, *matched = NULL;
171         slap_mask_t     mask;
172
173         Debug( LDAP_DEBUG_TRACE, "=> monitor_back_search\n", 0, 0, 0 );
174
175
176         /* get entry with reader lock */
177         monitor_cache_dn2entry( op, rs, &op->o_req_ndn, &e, &matched );
178         if ( e == NULL ) {
179                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
180                 if ( matched ) {
181 #ifdef SLAP_ACL_HONOR_DISCLOSE
182                         if ( !access_allowed_mask( op, matched,
183                                         slap_schema.si_ad_entry,
184                                         NULL, ACL_DISCLOSE, NULL, NULL ) )
185                         {
186                                 /* do nothing */ ;
187                         } else 
188 #endif /* SLAP_ACL_HONOR_DISCLOSE */
189                         {
190                                 rs->sr_matched = matched->e_dn;
191                         }
192                 }
193
194                 send_ldap_result( op, rs );
195                 if ( matched ) {
196                         monitor_cache_release( mi, matched );
197                         rs->sr_matched = NULL;
198                 }
199
200                 return rs->sr_err;
201         }
202
203         /* NOTE: __NEW__ "search" access is required
204          * on searchBase object */
205         if ( !access_allowed_mask( op, e, slap_schema.si_ad_entry,
206                                 NULL, ACL_SEARCH, NULL, &mask ) )
207         {
208                 monitor_cache_release( mi, e );
209
210 #ifdef SLAP_ACL_HONOR_DISCLOSE
211                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
212                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
213                 } else 
214 #endif /* SLAP_ACL_HONOR_DISCLOSE */
215                 {
216                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
217                 }
218
219                 send_ldap_result( op, rs );
220
221                 return rs->sr_err;
222         }
223
224         rs->sr_attrs = op->oq_search.rs_attrs;
225         switch ( op->oq_search.rs_scope ) {
226         case LDAP_SCOPE_BASE:
227                 monitor_entry_update( op, rs, e );
228                 rc = test_filter( op, e, op->oq_search.rs_filter );
229                 if ( rc == LDAP_COMPARE_TRUE ) {
230                         rs->sr_entry = e;
231                         rs->sr_flags = 0;
232                         send_search_entry( op, rs );
233                         rs->sr_entry = NULL;
234                 }
235                 rc = LDAP_SUCCESS;
236                 monitor_cache_release( mi, e );
237                 break;
238
239         case LDAP_SCOPE_ONELEVEL:
240                 rc = monitor_send_children( op, rs, e, 0 );
241                 break;
242
243         case LDAP_SCOPE_SUBTREE:
244                 monitor_entry_update( op, rs, e );
245                 rc = test_filter( op, e, op->oq_search.rs_filter );
246                 if ( rc == LDAP_COMPARE_TRUE ) {
247                         rs->sr_entry = e;
248                         rs->sr_flags = 0;
249                         send_search_entry( op, rs );
250                         rs->sr_entry = NULL;
251                 }
252
253                 rc = monitor_send_children( op, rs, e, 1 );
254                 break;
255         }
256
257         rs->sr_attrs = NULL;
258         rs->sr_err = rc;
259         if ( rs->sr_err != SLAPD_ABANDON ) {
260                 send_ldap_result( op, rs );
261         }
262
263         return rs->sr_err;
264 }
265