]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
4df9d2ac87c96232400c673b81fcf9109c28b835
[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-2011 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 void
34 monitor_find_children(
35         Operation *op,
36         SlapReply *rs,
37         Entry *e_parent,
38         Entry **nonv,
39         Entry **vol
40 )
41 {
42         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
43         monitor_entry_t *mp;
44         
45         mp = ( monitor_entry_t * )e_parent->e_private;
46         *nonv = mp->mp_children;
47
48         if ( MONITOR_HAS_VOLATILE_CH( mp ) ) {
49                 monitor_entry_create( op, rs, NULL, e_parent, vol );
50         }
51 }
52
53 static int
54 monitor_send_children(
55         Operation       *op,
56         SlapReply       *rs,
57         Entry           *e_nonvolatile,
58         Entry           *e_ch,
59         int             sub )
60 {
61         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
62         Entry                   *e,
63                                 *e_tmp;
64         monitor_entry_t *mp;
65         int                     rc,
66                                 nonvolatile = 0;
67
68         e = e_nonvolatile;
69
70         /* no volatile entries? */
71         if ( e_ch == NULL ) {
72                 /* no persistent entries? return */
73                 if ( e == NULL ) {
74                         return LDAP_SUCCESS;
75                 }
76
77         /* volatile entries */
78         } else {
79                 /* if no persistent, return only volatile */
80                 if ( e == NULL ) {
81                         e = e_ch;
82
83                 /* else append persistent to volatile */
84                 } else {
85                         e_tmp = e_ch;
86                         do {
87                                 mp = ( monitor_entry_t * )e_tmp->e_private;
88                                 e_tmp = mp->mp_next;
89         
90                                 if ( e_tmp == NULL ) {
91                                         mp->mp_next = e;
92                                         break;
93                                 }
94                         } while ( e_tmp );
95                         e = e_ch;
96                 }
97         }
98
99         /* return entries */
100         for ( ; e != NULL; e = e_tmp ) {
101                 Entry *sub_nv = NULL, *sub_ch = NULL;
102
103                 monitor_cache_lock( e );
104                 monitor_entry_update( op, rs, e );
105
106                 if ( e == e_nonvolatile )
107                         nonvolatile = 1;
108
109                 mp = ( monitor_entry_t * )e->e_private;
110                 e_tmp = mp->mp_next;
111
112                 if ( op->o_abandon ) {
113                         monitor_cache_release( mi, e );
114                         rc = SLAPD_ABANDON;
115                         goto freeout;
116                 }
117
118                 if ( sub )
119                         monitor_find_children( op, rs, e, &sub_nv, &sub_ch );
120
121                 rc = test_filter( op, e, op->oq_search.rs_filter );
122                 if ( rc == LDAP_COMPARE_TRUE ) {
123                         rs->sr_entry = e;
124                         rs->sr_flags = REP_ENTRY_MUSTRELEASE;
125                         rc = send_search_entry( op, rs );
126                         if ( rc ) {
127                                 for ( e = sub_ch; e != NULL; e = sub_nv ) {
128                                         mp = ( monitor_entry_t * )e->e_private;
129                                         sub_nv = mp->mp_next;
130                                         monitor_cache_lock( e );
131                                         monitor_cache_release( mi, e );
132                                 }
133                                 goto freeout;
134                         }
135                 } else {
136                         monitor_cache_release( mi, e );
137                 }
138
139                 if ( sub ) {
140                         rc = monitor_send_children( op, rs, sub_nv, sub_ch, sub );
141                         if ( rc ) {
142 freeout:
143                                 if ( nonvolatile == 0 ) {
144                                         for ( ; e_tmp != NULL; ) {
145                                                 mp = ( monitor_entry_t * )e_tmp->e_private;
146                                                 e = e_tmp;
147                                                 e_tmp = mp->mp_next;
148                                                 monitor_cache_lock( e );
149                                                 monitor_cache_release( mi, e );
150         
151                                                 if ( e_tmp == e_nonvolatile ) {
152                                                         break;
153                                                 }
154                                         }
155                                 }
156
157                                 return( rc );
158                         }
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         Entry           *e_nv = NULL, *e_ch = NULL;
172         slap_mask_t     mask;
173
174         Debug( LDAP_DEBUG_TRACE, "=> monitor_back_search\n", 0, 0, 0 );
175
176
177         /* get entry with reader lock */
178         monitor_cache_dn2entry( op, rs, &op->o_req_ndn, &e, &matched );
179         if ( e == NULL ) {
180                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
181                 if ( matched ) {
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                                 rs->sr_matched = matched->e_dn;
189                         }
190                 }
191
192                 send_ldap_result( op, rs );
193                 if ( matched ) {
194                         monitor_cache_release( mi, matched );
195                         rs->sr_matched = NULL;
196                 }
197
198                 return rs->sr_err;
199         }
200
201         /* NOTE: __NEW__ "search" access is required
202          * on searchBase object */
203         if ( !access_allowed_mask( op, e, slap_schema.si_ad_entry,
204                                 NULL, ACL_SEARCH, NULL, &mask ) )
205         {
206                 monitor_cache_release( mi, e );
207
208                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
209                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
210                 } else {
211                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
212                 }
213
214                 send_ldap_result( op, rs );
215
216                 return rs->sr_err;
217         }
218
219         rs->sr_attrs = op->oq_search.rs_attrs;
220         switch ( op->oq_search.rs_scope ) {
221         case LDAP_SCOPE_BASE:
222                 monitor_entry_update( op, rs, e );
223                 rc = test_filter( op, e, op->oq_search.rs_filter );
224                 if ( rc == LDAP_COMPARE_TRUE ) {
225                         rs->sr_entry = e;
226                         rs->sr_flags = REP_ENTRY_MUSTRELEASE;
227                         send_search_entry( op, rs );
228                         rs->sr_entry = NULL;
229                 } else {
230                         monitor_cache_release( mi, e );
231                 }
232                 rc = LDAP_SUCCESS;
233                 break;
234
235         case LDAP_SCOPE_ONELEVEL:
236         case LDAP_SCOPE_SUBORDINATE:
237                 monitor_find_children( op, rs, e, &e_nv, &e_ch );
238                 monitor_cache_release( mi, e );
239                 rc = monitor_send_children( op, rs, e_nv, e_ch,
240                         op->oq_search.rs_scope == LDAP_SCOPE_SUBORDINATE );
241                 break;
242
243         case LDAP_SCOPE_SUBTREE:
244                 monitor_entry_update( op, rs, e );
245                 monitor_find_children( op, rs, e, &e_nv, &e_ch );
246                 rc = test_filter( op, e, op->oq_search.rs_filter );
247                 if ( rc == LDAP_COMPARE_TRUE ) {
248                         rs->sr_entry = e;
249                         rs->sr_flags = REP_ENTRY_MUSTRELEASE;
250                         send_search_entry( op, rs );
251                         rs->sr_entry = NULL;
252                 } else {
253                         monitor_cache_release( mi, e );
254                 }
255
256                 rc = monitor_send_children( op, rs, e_nv, e_ch, 1 );
257                 break;
258
259         default:
260                 rc = LDAP_UNWILLING_TO_PERFORM;
261                 monitor_cache_release( mi, e );
262         }
263
264         rs->sr_attrs = NULL;
265         rs->sr_err = rc;
266         if ( rs->sr_err != SLAPD_ABANDON ) {
267                 send_ldap_result( op, rs );
268         }
269
270         return rs->sr_err;
271 }
272