]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
a7aad3bd1049ad15bc1c775f2c413accfd9d2e02
[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                 nonvolatile = 1;
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                 mp = ( monitor_entry_t * )e->e_private;
92                 e_tmp = mp->mp_next;
93
94                 if ( op->o_abandon ) {
95                         monitor_cache_release( mi, e );
96                         rc = SLAPD_ABANDON;
97                         goto freeout;
98                 }
99
100                 rc = test_filter( op, e, op->oq_search.rs_filter );
101                 if ( rc == LDAP_COMPARE_TRUE ) {
102                         rs->sr_entry = e;
103                         rs->sr_flags = 0;
104                         rc = send_search_entry( op, rs );
105                         rs->sr_entry = NULL;
106                         if ( rc ) {
107                                 monitor_cache_release( mi, e );
108                                 goto freeout;
109                         }
110                 }
111
112                 if ( sub ) {
113                         rc = monitor_send_children( op, rs, e, sub );
114                         if ( rc ) {
115 freeout:
116                                 /* FIXME: may leak generated children */
117                                 if ( nonvolatile == 0 ) {
118                                         for ( ; e_tmp != NULL; ) {
119                                                 mp = ( monitor_entry_t * )e_tmp->e_private;
120                                                 e = e_tmp;
121                                                 e_tmp = mp->mp_next;
122                                                 monitor_cache_lock( mi, e );
123                                                 monitor_cache_release( mi, e );
124         
125                                                 if ( e_tmp == e_nonvolatile ) {
126                                                         break;
127                                                 }
128                                         }
129                                 }
130
131                                 return( rc );
132                         }
133                 }
134
135                 if ( e_tmp != NULL ) {
136                         monitor_cache_lock( e_tmp );
137                 }
138
139                 if ( !sub ) {
140                         /* otherwise the recursive call already released */
141                         monitor_cache_release( mi, e );
142                 }
143
144                 e = e_tmp;
145                 if ( e == e_nonvolatile ) {
146                         nonvolatile = 1;
147                 }
148         }
149         
150         return LDAP_SUCCESS;
151 }
152
153 int
154 monitor_back_search( Operation *op, SlapReply *rs )
155 {
156         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
157         int             rc = LDAP_SUCCESS;
158         Entry           *e = NULL, *matched = NULL;
159         slap_mask_t     mask;
160
161         Debug( LDAP_DEBUG_TRACE, "=> monitor_back_search\n", 0, 0, 0 );
162
163
164         /* get entry with reader lock */
165         monitor_cache_dn2entry( op, rs, &op->o_req_ndn, &e, &matched );
166         if ( e == NULL ) {
167                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
168                 if ( matched ) {
169                         if ( !access_allowed_mask( op, matched,
170                                         slap_schema.si_ad_entry,
171                                         NULL, ACL_DISCLOSE, NULL, NULL ) )
172                         {
173                                 /* do nothing */ ;
174                         } else {
175                                 rs->sr_matched = matched->e_dn;
176                         }
177                 }
178
179                 send_ldap_result( op, rs );
180                 if ( matched ) {
181                         monitor_cache_release( mi, matched );
182                         rs->sr_matched = NULL;
183                 }
184
185                 return rs->sr_err;
186         }
187
188         /* NOTE: __NEW__ "search" access is required
189          * on searchBase object */
190         if ( !access_allowed_mask( op, e, slap_schema.si_ad_entry,
191                                 NULL, ACL_SEARCH, NULL, &mask ) )
192         {
193                 monitor_cache_release( mi, e );
194
195                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
196                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
197                 } else {
198                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
199                 }
200
201                 send_ldap_result( op, rs );
202
203                 return rs->sr_err;
204         }
205
206         rs->sr_attrs = op->oq_search.rs_attrs;
207         switch ( op->oq_search.rs_scope ) {
208         case LDAP_SCOPE_BASE:
209                 monitor_entry_update( op, rs, e );
210                 rc = test_filter( op, e, op->oq_search.rs_filter );
211                 if ( rc == LDAP_COMPARE_TRUE ) {
212                         rs->sr_entry = e;
213                         rs->sr_flags = 0;
214                         send_search_entry( op, rs );
215                         rs->sr_entry = NULL;
216                 }
217                 rc = LDAP_SUCCESS;
218                 monitor_cache_release( mi, e );
219                 break;
220
221         case LDAP_SCOPE_ONELEVEL:
222         case LDAP_SCOPE_SUBORDINATE:
223                 rc = monitor_send_children( op, rs, e,
224                         op->oq_search.rs_scope == LDAP_SCOPE_SUBORDINATE );
225                 break;
226
227         case LDAP_SCOPE_SUBTREE:
228                 monitor_entry_update( op, rs, e );
229                 rc = test_filter( op, e, op->oq_search.rs_filter );
230                 if ( rc == LDAP_COMPARE_TRUE ) {
231                         rs->sr_entry = e;
232                         rs->sr_flags = 0;
233                         send_search_entry( op, rs );
234                         rs->sr_entry = NULL;
235                 }
236
237                 rc = monitor_send_children( op, rs, e, 1 );
238                 break;
239
240         default:
241                 rc = LDAP_UNWILLING_TO_PERFORM;
242                 monitor_cache_release( mi, e );
243         }
244
245         rs->sr_attrs = NULL;
246         rs->sr_err = rc;
247         if ( rs->sr_err != SLAPD_ABANDON ) {
248                 send_ldap_result( op, rs );
249         }
250
251         return rs->sr_err;
252 }
253