]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/operational.c
remove unnecessary data from search_info
[openldap] / servers / slapd / back-sql / operational.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 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 the 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 Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27
28 #include "slap.h"
29 #include "proto-sql.h"
30 #include "lutil.h"
31
32 /*
33  * sets the supported operational attributes (if required)
34  */
35
36 Attribute *
37 backsql_operational_entryUUID( backsql_info *bi, backsql_entryID *id )
38 {
39         int                     rc;
40         struct berval           val, nval;
41         AttributeDescription    *desc = slap_schema.si_ad_entryUUID;
42         Attribute               *a;
43
44         backsql_entryUUID( bi, id, &val, NULL );
45
46         rc = (*desc->ad_type->sat_equality->smr_normalize)(
47                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
48                         desc->ad_type->sat_syntax,
49                         desc->ad_type->sat_equality,
50                         &val, &nval, NULL );
51         if ( rc != LDAP_SUCCESS ) {
52                 ber_memfree( val.bv_val );
53                 return NULL;
54         }
55
56         a = ch_malloc( sizeof( Attribute ) );
57         a->a_desc = desc;
58
59         a->a_vals = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
60         a->a_vals[ 0 ] = val;
61         BER_BVZERO( &a->a_vals[ 1 ] );
62
63         a->a_nvals = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
64         a->a_nvals[ 0 ] = nval;
65         BER_BVZERO( &a->a_nvals[ 1 ] );
66         
67         a->a_next = NULL;
68         a->a_flags = 0;
69
70         return a;
71 }
72
73 Attribute *
74 backsql_operational_entryCSN( Operation *op )
75 {
76         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
77         struct berval   entryCSN;
78         Attribute       *a;
79
80         a = ch_malloc( sizeof( Attribute ) );
81         a->a_desc = slap_schema.si_ad_entryCSN;
82         a->a_vals = ch_malloc( 2 * sizeof( struct berval ) );
83         BER_BVZERO( &a->a_vals[ 1 ] );
84
85 #ifdef BACKSQL_SYNCPROV
86         if ( op->o_sync && op->o_tag == LDAP_REQ_SEARCH ) {
87                 assert( op->o_private != NULL );
88
89                 entryCSN = *((struct berval *)op->o_private);
90
91         } else
92 #endif /* BACKSQL_SYNCPROV */
93         {
94                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &entryCSN, 0 );
95         }
96
97         ber_dupbv( &a->a_vals[ 0 ], &entryCSN );
98
99         a->a_nvals = a->a_vals;
100
101         a->a_next = NULL;
102         a->a_flags = 0;
103
104         return a;
105 }
106
107 int
108 backsql_operational(
109         Operation       *op,
110         SlapReply       *rs )
111 {
112
113         backsql_info    *bi = (backsql_info*)op->o_bd->be_private;
114         SQLHDBC         dbh = SQL_NULL_HDBC;
115         int             rc = 0;
116         Attribute       **ap;
117         enum {
118                 BACKSQL_OP_HASSUBORDINATES = 0,
119                 BACKSQL_OP_ENTRYUUID,
120                 BACKSQL_OP_ENTRYCSN,
121
122                 BACKSQL_OP_LAST
123         };
124         int             get_conn = BACKSQL_OP_LAST,
125                         got[ BACKSQL_OP_LAST ] = { 0 };
126
127         Debug( LDAP_DEBUG_TRACE, "==>backsql_operational(): entry \"%s\"\n",
128                         rs->sr_entry->e_nname.bv_val, 0, 0 );
129
130         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next ) {
131                 if ( (*ap)->a_desc == slap_schema.si_ad_hasSubordinates ) {
132                         get_conn--;
133                         got[ BACKSQL_OP_HASSUBORDINATES ] = 1;
134
135                 } else if ( (*ap)->a_desc == slap_schema.si_ad_entryUUID ) {
136                         get_conn--;
137                         got[ BACKSQL_OP_ENTRYUUID ] = 1;
138
139                 } else if ( (*ap)->a_desc == slap_schema.si_ad_entryCSN ) {
140                         get_conn--;
141                         got[ BACKSQL_OP_ENTRYCSN ] = 1;
142                 }
143         }
144
145         if ( !get_conn ) {
146                 return 0;
147         }
148
149         rc = backsql_get_db_conn( op, &dbh );
150         if ( rc != LDAP_SUCCESS ) {
151                 Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
152                         "could not get connection handle - exiting\n", 
153                         0, 0, 0 );
154                 return 1;
155         }
156
157         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) ) 
158                         && !got[ BACKSQL_OP_HASSUBORDINATES ]
159                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL )
160         {
161                 rc = backsql_has_children( op, dbh, &rs->sr_entry->e_nname );
162
163                 switch( rc ) {
164                 case LDAP_COMPARE_TRUE:
165                 case LDAP_COMPARE_FALSE:
166                         *ap = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
167                         assert( *ap != NULL );
168                         ap = &(*ap)->a_next;
169                         rc = 0;
170                         break;
171
172                 default:
173                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
174                                 "has_children failed( %d)\n", rc, 0, 0 );
175                         return 1;
176                 }
177         }
178
179         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_entryUUID, rs->sr_attrs ) ) 
180                         && !got[ BACKSQL_OP_ENTRYUUID ]
181                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID ) == NULL )
182         {
183                 backsql_srch_info       bsi = { 0 };
184
185                 rc = backsql_init_search( &bsi, &rs->sr_entry->e_nname,
186                                 LDAP_SCOPE_BASE,
187                                 (time_t)(-1), NULL, dbh, op, rs, NULL,
188                                 BACKSQL_ISF_GET_ID );
189                 if ( rc != LDAP_SUCCESS ) {
190                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
191                                 "could not retrieve entry ID - no such entry\n", 
192                                 0, 0, 0 );
193                         return 1;
194                 }
195
196                 *ap = backsql_operational_entryUUID( bi, &bsi.bsi_base_id );
197
198                 (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
199
200                 if ( bsi.bsi_attrs != NULL ) {
201                         op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
202                 }
203
204                 if ( *ap == NULL ) {
205                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
206                                 "could not retrieve entryUUID\n", 
207                                 0, 0, 0 );
208                         return 1;
209                 }
210
211                 ap = &(*ap)->a_next;
212         }
213
214         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_entryCSN, rs->sr_attrs ) ) 
215                         && !got[ BACKSQL_OP_ENTRYCSN ]
216                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryCSN ) == NULL )
217         {
218                 *ap = backsql_operational_entryCSN( op );
219                 if ( *ap == NULL ) {
220                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
221                                 "could not retrieve entryCSN\n", 
222                                 0, 0, 0 );
223                         return 1;
224                 }
225
226                 ap = &(*ap)->a_next;
227         }
228
229         Debug( LDAP_DEBUG_TRACE, "<==backsql_operational(%d)\n", rc, 0, 0);
230
231         return rc;
232 }
233