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