]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
Add validation.
[openldap] / servers / slapd / back-monitor / search.c
1 /* search.c - monitor backend search function */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/string.h>
39 #include <ac/socket.h>
40
41 #include "slap.h"
42 #include "back-monitor.h"
43 #include "proto-back-monitor.h"
44
45 static int
46 monitor_send_children(
47         Backend         *be,
48         Connection      *conn,
49         Operation       *op,
50         Filter          *filter,
51         AttributeName   *attrs,
52         int             attrsonly,
53         Entry           *e_parent,
54         int             sub,
55         int             *nentriesp
56 )
57 {
58         struct monitorinfo      *mi = (struct monitorinfo *) be->be_private;
59         Entry                   *e, *e_tmp, *e_ch;
60         struct monitorentrypriv *mp;
61         int                     nentries;
62         int                     rc;
63
64         mp = ( struct monitorentrypriv * )e_parent->e_private;
65         e = mp->mp_children;
66
67         e_ch = NULL;
68         if ( MONITOR_HAS_VOLATILE_CH( mp ) ) {
69                 monitor_entry_create( mi, NULL, e_parent, &e_ch );
70         }
71         monitor_cache_release( mi, e_parent );
72
73         /* no volatile entries? */
74         if ( e_ch == NULL ) {
75                 /* no persistent entries? return */
76                 if ( e == NULL ) {
77                         return( 0 );
78                 }
79         
80         /* volatile entries */
81         } else {
82                 /* if no persistent, return only volatile */
83                 if ( e == NULL ) {
84                         e = e_ch;
85                         monitor_cache_lock( e_ch );
86
87                 /* else append persistent to volatile */
88                 } else {
89                         e_tmp = e_ch;
90                         do {
91                                 mp = ( struct monitorentrypriv * )e_tmp->e_private;
92                                 e_tmp = mp->mp_next;
93         
94                                 if ( e_tmp == NULL ) {
95                                         mp->mp_next = e;
96                                         break;
97                                 }
98                         } while ( e_tmp );
99                         e = e_ch;
100                 }
101         }
102
103         /* return entries */
104         for ( nentries = *nentriesp; e != NULL; ) {
105                 mp = ( struct monitorentrypriv * )e->e_private;
106
107                 monitor_entry_update( mi, e );
108                 
109                 rc = test_filter( be, conn, op, e, filter );
110                 if ( rc == LDAP_COMPARE_TRUE ) {
111                         send_search_entry( be, conn, op, e, 
112                                         attrs, attrsonly, NULL );
113                         nentries++;
114                 }
115
116                 if ( ( mp->mp_children || MONITOR_HAS_VOLATILE_CH( mp ) )
117                                 && sub ) {
118                         rc = monitor_send_children( be, conn, op, filter, 
119                                         attrs, attrsonly, 
120                                         e, sub, &nentries );
121                         if ( rc ) {
122                                 return( rc );
123                         }
124                 }
125
126                 e_tmp = mp->mp_next;
127                 if ( e_tmp != NULL ) {
128                         monitor_cache_lock( e_tmp );
129                 }
130                 monitor_cache_release( mi, e );
131                 e = e_tmp;
132         }
133         
134         return( 0 );
135 }
136
137 int
138 monitor_back_search(
139         Backend         *be,
140         Connection      *conn,
141         Operation       *op,
142         struct berval   *base,
143         struct berval   *nbase,
144         int             scope,
145         int             deref,
146         int             slimit,
147         int             tlimit,
148         Filter          *filter,
149         struct berval   *filterstr,
150         AttributeName   *attrs,
151         int             attrsonly 
152 )
153 {
154         struct monitorinfo      *mi = (struct monitorinfo *) be->be_private;
155         int             rc;
156         Entry           *e, *matched = NULL;
157         int             nentries = 0;
158
159 #ifdef NEW_LOGGING
160         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
161                    "monitor_back_search: enter\n" ));
162 #else
163         Debug(LDAP_DEBUG_TRACE, "=> monitor_back_search\n%s%s%s", "", "", "");
164 #endif
165
166
167         /* get entry with reader lock */
168         monitor_cache_dn2entry( mi, nbase, &e, &matched );
169         if ( e == NULL ) {
170                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
171                         matched ? matched->e_dn : NULL, 
172                         NULL, NULL, NULL );
173                 if ( matched ) {
174                         monitor_cache_release( mi, matched );
175                 }
176
177                 return( 0 );
178         }
179
180         nentries = 0;
181         switch ( scope ) {
182         case LDAP_SCOPE_BASE:
183                 monitor_entry_update( mi, e );
184                 rc = test_filter( be, conn, op, e, filter );
185                 if ( rc == LDAP_COMPARE_TRUE ) {                        
186                         send_search_entry( be, conn, op, e, attrs, 
187                                         attrsonly, NULL );
188                         nentries = 1;
189                 }
190                 monitor_cache_release( mi, e );
191                 break;
192
193         case LDAP_SCOPE_ONELEVEL:
194                 rc = monitor_send_children( be, conn, op, filter,
195                                 attrs, attrsonly,
196                                 e, 0, &nentries );
197                 if ( rc ) {
198                         // error
199                 }               
200                 
201                 break;
202
203         case LDAP_SCOPE_SUBTREE:
204                 monitor_entry_update( mi, e );
205                 rc = test_filter( be, conn, op, e, filter );
206                 if ( rc == LDAP_COMPARE_TRUE ) {
207                         send_search_entry( be, conn, op, e, attrs,
208                                         attrsonly, NULL );
209                         nentries++;
210                 }
211
212                 rc = monitor_send_children( be, conn, op, filter,
213                                 attrs, attrsonly,
214                                 e, 1, &nentries );
215                 if ( rc ) {
216                         // error
217                 }
218
219                 break;
220         }
221         
222         send_search_result( conn, op, LDAP_SUCCESS,
223                         NULL, NULL, NULL, NULL, nentries );
224
225         return( 0 );
226 }