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