]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/operational.c
re-fix previus commit the other way 'round (rids are < 1000)
[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-2007 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 = attr_alloc( desc );
57
58         a->a_vals = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
59         a->a_vals[ 0 ] = val;
60         BER_BVZERO( &a->a_vals[ 1 ] );
61
62         a->a_nvals = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
63         a->a_nvals[ 0 ] = nval;
64         BER_BVZERO( &a->a_nvals[ 1 ] );
65
66         return a;
67 }
68
69 Attribute *
70 backsql_operational_entryCSN( Operation *op )
71 {
72         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
73         struct berval   entryCSN;
74         Attribute       *a;
75
76         a = attr_alloc( slap_schema.si_ad_entryCSN );
77         a->a_vals = ch_malloc( 2 * sizeof( struct berval ) );
78         BER_BVZERO( &a->a_vals[ 1 ] );
79
80 #ifdef BACKSQL_SYNCPROV
81         if ( op->o_sync && op->o_tag == LDAP_REQ_SEARCH && op->o_private != NULL ) {
82                 assert( op->o_private != NULL );
83
84                 entryCSN = *((struct berval *)op->o_private);
85
86         } else
87 #endif /* BACKSQL_SYNCPROV */
88         {
89                 entryCSN.bv_val = csnbuf;
90                 entryCSN.bv_len = sizeof( csnbuf );
91                 slap_get_csn( op, &entryCSN, 0 );
92         }
93
94         ber_dupbv( &a->a_vals[ 0 ], &entryCSN );
95
96         a->a_nvals = a->a_vals;
97
98         return a;
99 }
100
101 int
102 backsql_operational(
103         Operation       *op,
104         SlapReply       *rs )
105 {
106
107         backsql_info    *bi = (backsql_info*)op->o_bd->be_private;
108         SQLHDBC         dbh = SQL_NULL_HDBC;
109         int             rc = 0;
110         Attribute       **ap;
111         enum {
112                 BACKSQL_OP_HASSUBORDINATES = 0,
113                 BACKSQL_OP_ENTRYUUID,
114                 BACKSQL_OP_ENTRYCSN,
115
116                 BACKSQL_OP_LAST
117         };
118         int             get_conn = BACKSQL_OP_LAST,
119                         got[ BACKSQL_OP_LAST ] = { 0 };
120
121         Debug( LDAP_DEBUG_TRACE, "==>backsql_operational(): entry \"%s\"\n",
122                         rs->sr_entry->e_nname.bv_val, 0, 0 );
123
124         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next ) {
125                 if ( (*ap)->a_desc == slap_schema.si_ad_hasSubordinates ) {
126                         get_conn--;
127                         got[ BACKSQL_OP_HASSUBORDINATES ] = 1;
128
129                 } else if ( (*ap)->a_desc == slap_schema.si_ad_entryUUID ) {
130                         get_conn--;
131                         got[ BACKSQL_OP_ENTRYUUID ] = 1;
132
133                 } else if ( (*ap)->a_desc == slap_schema.si_ad_entryCSN ) {
134                         get_conn--;
135                         got[ BACKSQL_OP_ENTRYCSN ] = 1;
136                 }
137         }
138
139         if ( !get_conn ) {
140                 return 0;
141         }
142
143         rc = backsql_get_db_conn( op, &dbh );
144         if ( rc != LDAP_SUCCESS ) {
145                 Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
146                         "could not get connection handle - exiting\n", 
147                         0, 0, 0 );
148                 return 1;
149         }
150
151         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) ) 
152                         && !got[ BACKSQL_OP_HASSUBORDINATES ]
153                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL )
154         {
155                 rc = backsql_has_children( op, dbh, &rs->sr_entry->e_nname );
156
157                 switch( rc ) {
158                 case LDAP_COMPARE_TRUE:
159                 case LDAP_COMPARE_FALSE:
160                         *ap = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
161                         assert( *ap != NULL );
162                         ap = &(*ap)->a_next;
163                         rc = 0;
164                         break;
165
166                 default:
167                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
168                                 "has_children failed( %d)\n", rc, 0, 0 );
169                         return 1;
170                 }
171         }
172
173         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_entryUUID, rs->sr_attrs ) ) 
174                         && !got[ BACKSQL_OP_ENTRYUUID ]
175                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID ) == NULL )
176         {
177                 backsql_srch_info       bsi = { 0 };
178
179                 rc = backsql_init_search( &bsi, &rs->sr_entry->e_nname,
180                                 LDAP_SCOPE_BASE,
181                                 (time_t)(-1), NULL, dbh, op, rs, NULL,
182                                 BACKSQL_ISF_GET_ID );
183                 if ( rc != LDAP_SUCCESS ) {
184                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
185                                 "could not retrieve entry ID - no such entry\n", 
186                                 0, 0, 0 );
187                         return 1;
188                 }
189
190                 *ap = backsql_operational_entryUUID( bi, &bsi.bsi_base_id );
191
192                 (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
193
194                 if ( bsi.bsi_attrs != NULL ) {
195                         op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
196                 }
197
198                 if ( *ap == NULL ) {
199                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
200                                 "could not retrieve entryUUID\n", 
201                                 0, 0, 0 );
202                         return 1;
203                 }
204
205                 ap = &(*ap)->a_next;
206         }
207
208         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_entryCSN, rs->sr_attrs ) ) 
209                         && !got[ BACKSQL_OP_ENTRYCSN ]
210                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryCSN ) == NULL )
211         {
212                 *ap = backsql_operational_entryCSN( op );
213                 if ( *ap == NULL ) {
214                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
215                                 "could not retrieve entryCSN\n", 
216                                 0, 0, 0 );
217                         return 1;
218                 }
219
220                 ap = &(*ap)->a_next;
221         }
222
223         Debug( LDAP_DEBUG_TRACE, "<==backsql_operational(%d)\n", rc, 0, 0);
224
225         return rc;
226 }
227