]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/search.c
Completely untested built-in EXTERNAL implementation
[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-2003 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                         send_search_entry( op, rs );
96                         rs->sr_entry = NULL;
97                 }
98
99                 if ( ( mp->mp_children || MONITOR_HAS_VOLATILE_CH( mp ) )
100                                 && sub ) {
101                         rc = monitor_send_children( op, rs, e, sub );
102                         if ( rc ) {
103                                 return( rc );
104                         }
105                 }
106
107                 e_tmp = mp->mp_next;
108                 if ( e_tmp != NULL ) {
109                         monitor_cache_lock( e_tmp );
110                 }
111                 monitor_cache_release( mi, e );
112                 e = e_tmp;
113         }
114         
115         return( 0 );
116 }
117
118 int
119 monitor_back_search( Operation *op, SlapReply *rs )
120 {
121         struct monitorinfo      *mi
122                 = (struct monitorinfo *) op->o_bd->be_private;
123         int             rc = LDAP_SUCCESS;
124         Entry           *e, *matched = NULL;
125
126 #ifdef NEW_LOGGING
127         LDAP_LOG( BACK_MON, ENTRY,
128                    "monitor_back_search: enter\n", 0, 0, 0 );
129 #else
130         Debug(LDAP_DEBUG_TRACE, "=> monitor_back_search\n%s%s%s", "", "", "");
131 #endif
132
133
134         /* get entry with reader lock */
135         monitor_cache_dn2entry( op, &op->o_req_ndn, &e, &matched );
136         if ( e == NULL ) {
137                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
138                 if ( matched ) {
139                         rs->sr_matched = matched->e_dn;
140                 }
141
142                 send_ldap_result( op, rs );
143                 if ( matched ) {
144                         monitor_cache_release( mi, matched );
145                         rs->sr_matched = NULL;
146                 }
147
148                 return( 0 );
149         }
150
151         rs->sr_attrs = op->oq_search.rs_attrs;
152         switch ( op->oq_search.rs_scope ) {
153         case LDAP_SCOPE_BASE:
154                 monitor_entry_update( op, e );
155                 rc = test_filter( op, e, op->oq_search.rs_filter );
156                 if ( rc == LDAP_COMPARE_TRUE ) {
157                         rs->sr_entry = e;
158                         send_search_entry( op, rs );
159                         rs->sr_entry = NULL;
160                 }
161                 rc = LDAP_SUCCESS;
162                 monitor_cache_release( mi, e );
163                 break;
164
165         case LDAP_SCOPE_ONELEVEL:
166                 rc = monitor_send_children( op, rs, e, 0 );
167                 if ( rc ) {
168                         rc = LDAP_OTHER;
169                 }
170                 
171                 break;
172
173         case LDAP_SCOPE_SUBTREE:
174                 monitor_entry_update( op, e );
175                 rc = test_filter( op, e, op->oq_search.rs_filter );
176                 if ( rc == LDAP_COMPARE_TRUE ) {
177                         rs->sr_entry = e;
178                         send_search_entry( op, rs );
179                         rs->sr_entry = NULL;
180                 }
181
182                 rc = monitor_send_children( op, rs, e, 1 );
183                 if ( rc ) {
184                         rc = LDAP_OTHER;
185                 }
186
187                 break;
188         }
189         
190         rs->sr_attrs = NULL;
191         rs->sr_err = rc;
192         send_ldap_result( op, rs );
193
194         return( rc == LDAP_SUCCESS ? 0 : 1 );
195 }
196