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