| 1 |
/* $OpenLDAP$ */ |
| 2 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 3 |
* |
| 4 |
* Copyright 1999-2024 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_numvals = 1; |
| 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 |
return a; |
| 68 |
} |
| 69 |
|
| 70 |
Attribute * |
| 71 |
backsql_operational_entryCSN( Operation *op ) |
| 72 |
{ |
| 73 |
char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ]; |
| 74 |
struct berval entryCSN; |
| 75 |
Attribute *a; |
| 76 |
|
| 77 |
a = attr_alloc( slap_schema.si_ad_entryCSN ); |
| 78 |
a->a_numvals = 1; |
| 79 |
a->a_vals = ch_malloc( 2 * sizeof( struct berval ) ); |
| 80 |
BER_BVZERO( &a->a_vals[ 1 ] ); |
| 81 |
|
| 82 |
#ifdef BACKSQL_SYNCPROV |
| 83 |
if ( op->o_sync && op->o_tag == LDAP_REQ_SEARCH && op->o_private != NULL ) { |
| 84 |
assert( op->o_private != NULL ); |
| 85 |
|
| 86 |
entryCSN = *((struct berval *)op->o_private); |
| 87 |
|
| 88 |
} else |
| 89 |
#endif /* BACKSQL_SYNCPROV */ |
| 90 |
{ |
| 91 |
entryCSN.bv_val = csnbuf; |
| 92 |
entryCSN.bv_len = sizeof( csnbuf ); |
| 93 |
slap_get_csn( op, &entryCSN, 0 ); |
| 94 |
} |
| 95 |
|
| 96 |
ber_dupbv( &a->a_vals[ 0 ], &entryCSN ); |
| 97 |
|
| 98 |
a->a_nvals = a->a_vals; |
| 99 |
|
| 100 |
return a; |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
backsql_operational( |
| 105 |
Operation *op, |
| 106 |
SlapReply *rs ) |
| 107 |
{ |
| 108 |
|
| 109 |
backsql_info *bi = (backsql_info*)op->o_bd->be_private; |
| 110 |
SQLHDBC dbh = SQL_NULL_HDBC; |
| 111 |
int rc = 0; |
| 112 |
Attribute **ap; |
| 113 |
enum { |
| 114 |
BACKSQL_OP_HASSUBORDINATES = 0, |
| 115 |
BACKSQL_OP_ENTRYUUID, |
| 116 |
BACKSQL_OP_ENTRYCSN, |
| 117 |
|
| 118 |
BACKSQL_OP_LAST |
| 119 |
}; |
| 120 |
int get_conn = BACKSQL_OP_LAST, |
| 121 |
got[ BACKSQL_OP_LAST ] = { 0 }; |
| 122 |
|
| 123 |
Debug( LDAP_DEBUG_TRACE, "==>backsql_operational(): entry \"%s\"\n", |
| 124 |
rs->sr_entry->e_nname.bv_val ); |
| 125 |
|
| 126 |
for ( ap = &rs->sr_entry->e_attrs; *ap; ap = &(*ap)->a_next ) { |
| 127 |
if ( (*ap)->a_desc == slap_schema.si_ad_hasSubordinates ) { |
| 128 |
get_conn--; |
| 129 |
got[ BACKSQL_OP_HASSUBORDINATES ] = 1; |
| 130 |
|
| 131 |
} else if ( (*ap)->a_desc == slap_schema.si_ad_entryUUID ) { |
| 132 |
get_conn--; |
| 133 |
got[ BACKSQL_OP_ENTRYUUID ] = 1; |
| 134 |
|
| 135 |
} else if ( (*ap)->a_desc == slap_schema.si_ad_entryCSN ) { |
| 136 |
get_conn--; |
| 137 |
got[ BACKSQL_OP_ENTRYCSN ] = 1; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next ) { |
| 142 |
if ( !got[ BACKSQL_OP_HASSUBORDINATES ] && |
| 143 |
(*ap)->a_desc == slap_schema.si_ad_hasSubordinates ) |
| 144 |
{ |
| 145 |
get_conn--; |
| 146 |
got[ BACKSQL_OP_HASSUBORDINATES ] = 1; |
| 147 |
|
| 148 |
} else if ( !got[ BACKSQL_OP_ENTRYUUID ] && |
| 149 |
(*ap)->a_desc == slap_schema.si_ad_entryUUID ) |
| 150 |
{ |
| 151 |
get_conn--; |
| 152 |
got[ BACKSQL_OP_ENTRYUUID ] = 1; |
| 153 |
|
| 154 |
} else if ( !got[ BACKSQL_OP_ENTRYCSN ] && |
| 155 |
(*ap)->a_desc == slap_schema.si_ad_entryCSN ) |
| 156 |
{ |
| 157 |
get_conn--; |
| 158 |
got[ BACKSQL_OP_ENTRYCSN ] = 1; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( !get_conn ) { |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
rc = backsql_get_db_conn( op, &dbh ); |
| 167 |
if ( rc != LDAP_SUCCESS ) { |
| 168 |
Debug( LDAP_DEBUG_TRACE, "backsql_operational(): " |
| 169 |
"could not get connection handle - exiting\n" ); |
| 170 |
return 1; |
| 171 |
} |
| 172 |
|
| 173 |
if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) ) |
| 174 |
&& !got[ BACKSQL_OP_HASSUBORDINATES ] |
| 175 |
&& attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL ) |
| 176 |
{ |
| 177 |
rc = backsql_has_children( op, dbh, &rs->sr_entry->e_nname ); |
| 178 |
|
| 179 |
switch( rc ) { |
| 180 |
case LDAP_COMPARE_TRUE: |
| 181 |
case LDAP_COMPARE_FALSE: |
| 182 |
*ap = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE ); |
| 183 |
assert( *ap != NULL ); |
| 184 |
ap = &(*ap)->a_next; |
| 185 |
rc = 0; |
| 186 |
break; |
| 187 |
|
| 188 |
default: |
| 189 |
Debug( LDAP_DEBUG_TRACE, "backsql_operational(): " |
| 190 |
"has_children failed( %d)\n", rc ); |
| 191 |
return 1; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_entryUUID, rs->sr_attrs ) ) |
| 196 |
&& !got[ BACKSQL_OP_ENTRYUUID ] |
| 197 |
&& attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID ) == NULL ) |
| 198 |
{ |
| 199 |
backsql_srch_info bsi = { 0 }; |
| 200 |
|
| 201 |
rc = backsql_init_search( &bsi, &rs->sr_entry->e_nname, |
| 202 |
LDAP_SCOPE_BASE, |
| 203 |
(time_t)(-1), NULL, dbh, op, rs, NULL, |
| 204 |
BACKSQL_ISF_GET_ID ); |
| 205 |
if ( rc != LDAP_SUCCESS ) { |
| 206 |
Debug( LDAP_DEBUG_TRACE, "backsql_operational(): " |
| 207 |
"could not retrieve entry ID - no such entry\n" ); |
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
*ap = backsql_operational_entryUUID( bi, &bsi.bsi_base_id ); |
| 212 |
|
| 213 |
(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx ); |
| 214 |
|
| 215 |
if ( bsi.bsi_attrs != NULL ) { |
| 216 |
op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx ); |
| 217 |
} |
| 218 |
|
| 219 |
if ( *ap == NULL ) { |
| 220 |
Debug( LDAP_DEBUG_TRACE, "backsql_operational(): " |
| 221 |
"could not retrieve entryUUID\n" ); |
| 222 |
return 1; |
| 223 |
} |
| 224 |
|
| 225 |
ap = &(*ap)->a_next; |
| 226 |
} |
| 227 |
|
| 228 |
if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || ad_inlist( slap_schema.si_ad_entryCSN, rs->sr_attrs ) ) |
| 229 |
&& !got[ BACKSQL_OP_ENTRYCSN ] |
| 230 |
&& attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryCSN ) == NULL ) |
| 231 |
{ |
| 232 |
*ap = backsql_operational_entryCSN( op ); |
| 233 |
if ( *ap == NULL ) { |
| 234 |
Debug( LDAP_DEBUG_TRACE, "backsql_operational(): " |
| 235 |
"could not retrieve entryCSN\n" ); |
| 236 |
return 1; |
| 237 |
} |
| 238 |
|
| 239 |
ap = &(*ap)->a_next; |
| 240 |
} |
| 241 |
|
| 242 |
Debug( LDAP_DEBUG_TRACE, "<==backsql_operational(%d)\n", rc ); |
| 243 |
|
| 244 |
return rc; |
| 245 |
} |
| 246 |
|