]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
more fixes related to ITS#3499
[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         struct monitorinfo      *mi =
42                 (struct monitorinfo *) op->o_bd->be_private;
43         Entry                   *e, *e_tmp, *e_ch;
44         struct monitorentrypriv *mp;
45         int                     rc;
46
47         mp = ( struct monitorentrypriv * )e_parent->e_private;
48         e = mp->mp_children;
49
50         e_ch = NULL;
51         if ( MONITOR_HAS_VOLATILE_CH( mp ) ) {
52                 monitor_entry_create( op, NULL, e_parent, &e_ch );
53         }
54         monitor_cache_release( mi, e_parent );
55
56         /* no volatile entries? */
57         if ( e_ch == NULL ) {
58                 /* no persistent entries? return */
59                 if ( e == NULL ) {
60                         return( 0 );
61                 }
62         
63         /* volatile entries */
64         } else {
65                 /* if no persistent, return only volatile */
66                 if ( e == NULL ) {
67                         e = e_ch;
68                         monitor_cache_lock( e_ch );
69
70                 /* else append persistent to volatile */
71                 } else {
72                         e_tmp = e_ch;
73                         do {
74                                 mp = ( struct monitorentrypriv * )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 ( ; e != NULL; ) {
88                 mp = ( struct monitorentrypriv * )e->e_private;
89
90                 monitor_entry_update( op, e );
91                 
92                 rc = test_filter( op, e, op->oq_search.rs_filter );
93                 if ( rc == LDAP_COMPARE_TRUE ) {
94                         rs->sr_entry = e;
95                         rs->sr_flags = 0;
96                         send_search_entry( op, rs );
97                         rs->sr_entry = NULL;
98                 }
99
100                 if ( ( mp->mp_children || MONITOR_HAS_VOLATILE_CH( mp ) )
101                                 && sub ) {
102                         rc = monitor_send_children( op, rs, e, sub );
103                         if ( rc ) {
104                                 return( rc );
105                         }
106                 }
107
108                 e_tmp = mp->mp_next;
109                 if ( e_tmp != NULL ) {
110                         monitor_cache_lock( e_tmp );
111                 }
112                 monitor_cache_release( mi, e );
113                 e = e_tmp;
114         }
115         
116         return( 0 );
117 }
118
119 int
120 monitor_back_search( Operation *op, SlapReply *rs )
121 {
122         struct monitorinfo      *mi
123                 = (struct monitorinfo *) op->o_bd->be_private;
124         int             rc = LDAP_SUCCESS;
125         Entry           *e, *matched = NULL;
126
127 #ifdef NEW_LOGGING
128         LDAP_LOG( BACK_MON, ENTRY,
129                    "monitor_back_search: enter\n", 0, 0, 0 );
130 #else
131         Debug(LDAP_DEBUG_TRACE, "=> monitor_back_search\n%s%s%s", "", "", "");
132 #endif
133
134
135         /* get entry with reader lock */
136         monitor_cache_dn2entry( op, &op->o_req_ndn, &e, &matched );
137         if ( e == NULL ) {
138                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
139                 if ( matched ) {
140                         rs->sr_matched = matched->e_dn;
141                 }
142
143                 send_ldap_result( op, rs );
144                 if ( matched ) {
145                         monitor_cache_release( mi, matched );
146                         rs->sr_matched = NULL;
147                 }
148
149                 return( 0 );
150         }
151
152         rs->sr_attrs = op->oq_search.rs_attrs;
153         switch ( op->oq_search.rs_scope ) {
154         case LDAP_SCOPE_BASE:
155                 monitor_entry_update( op, e );
156                 rc = test_filter( op, e, op->oq_search.rs_filter );
157                 if ( rc == LDAP_COMPARE_TRUE ) {
158                         rs->sr_entry = e;
159                         rs->sr_flags = 0;
160                         send_search_entry( op, rs );
161                         rs->sr_entry = NULL;
162                 }
163                 rc = LDAP_SUCCESS;
164                 monitor_cache_release( mi, e );
165                 break;
166
167         case LDAP_SCOPE_ONELEVEL:
168                 rc = monitor_send_children( op, rs, e, 0 );
169                 if ( rc ) {
170                         rc = LDAP_OTHER;
171                 }
172                 
173                 break;
174
175         case LDAP_SCOPE_SUBTREE:
176                 monitor_entry_update( op, e );
177                 rc = test_filter( op, e, op->oq_search.rs_filter );
178                 if ( rc == LDAP_COMPARE_TRUE ) {
179                         rs->sr_entry = e;
180                         rs->sr_flags = 0;
181                         send_search_entry( op, rs );
182                         rs->sr_entry = NULL;
183                 }
184
185                 rc = monitor_send_children( op, rs, e, 1 );
186                 if ( rc ) {
187                         rc = LDAP_OTHER;
188                 }
189
190                 break;
191         }
192         
193         rs->sr_attrs = NULL;
194         rs->sr_err = rc;
195         send_ldap_result( op, rs );
196
197         return( rc == LDAP_SUCCESS ? 0 : 1 );
198 }
199