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