| 1 |
/* OpenLDAP WiredTiger backend */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2002-2024 The OpenLDAP Foundation. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted only as authorized by the OpenLDAP |
| 10 |
* Public License. |
| 11 |
* |
| 12 |
* A copy of this license is available in the file LICENSE in the |
| 13 |
* top-level directory of the distribution or, alternatively, at |
| 14 |
* <http://www.OpenLDAP.org/license.html>. |
| 15 |
*/ |
| 16 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was developed by HAMANO Tsukasa <[email protected]> |
| 18 |
* based on back-bdb for inclusion in OpenLDAP Software. |
| 19 |
* WiredTiger is a product of MongoDB Inc. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "back-wt.h" |
| 23 |
#include "slap-config.h" |
| 24 |
|
| 25 |
static int wt_id2entry_put( |
| 26 |
Operation *op, |
| 27 |
wt_ctx *wc, |
| 28 |
Entry *e, |
| 29 |
WT_CURSOR *cursor) |
| 30 |
{ |
| 31 |
struct berval bv; |
| 32 |
WT_ITEM item; |
| 33 |
int rc; |
| 34 |
|
| 35 |
rc = entry_encode( e, &bv ); |
| 36 |
if(rc != LDAP_SUCCESS){ |
| 37 |
return -1; |
| 38 |
} |
| 39 |
item.size = bv.bv_len; |
| 40 |
item.data = bv.bv_val; |
| 41 |
|
| 42 |
cursor->set_key(cursor, e->e_id); |
| 43 |
cursor->set_value(cursor, e->e_ndn, &item); |
| 44 |
rc = cursor->insert(cursor); |
| 45 |
if ( rc ) { |
| 46 |
Debug( LDAP_DEBUG_ANY, |
| 47 |
"wt_id2entry_put: insert failed: %s (%d)\n", |
| 48 |
wiredtiger_strerror(rc), rc ); |
| 49 |
goto done; |
| 50 |
} |
| 51 |
|
| 52 |
done: |
| 53 |
ch_free( bv.bv_val ); |
| 54 |
|
| 55 |
return rc; |
| 56 |
} |
| 57 |
|
| 58 |
int wt_id2entry_add( |
| 59 |
Operation *op, |
| 60 |
wt_ctx *wc, |
| 61 |
Entry *e ) |
| 62 |
{ |
| 63 |
WT_SESSION *session = wc->session; |
| 64 |
WT_CURSOR *cursor = wc->id2entry_add; |
| 65 |
int rc; |
| 66 |
|
| 67 |
if(!cursor){ |
| 68 |
rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, |
| 69 |
"overwrite=false", &cursor); |
| 70 |
if ( rc ) { |
| 71 |
Debug( LDAP_DEBUG_ANY, |
| 72 |
"wt_id2entry_put: open_cursor failed: %s (%d)\n", |
| 73 |
wiredtiger_strerror(rc), rc ); |
| 74 |
return rc; |
| 75 |
} |
| 76 |
wc->id2entry_add = cursor; |
| 77 |
} |
| 78 |
|
| 79 |
rc = wt_id2entry_put(op, wc, e, cursor); |
| 80 |
|
| 81 |
#ifdef WT_CURSOR_CACHE |
| 82 |
if(cursor){ |
| 83 |
cursor->reset(cursor); |
| 84 |
} |
| 85 |
#else |
| 86 |
if(cursor){ |
| 87 |
cursor->close(cursor); |
| 88 |
wc->id2entry_add = NULL; |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
return rc; |
| 93 |
} |
| 94 |
|
| 95 |
int wt_id2entry_update( |
| 96 |
Operation *op, |
| 97 |
wt_ctx *wc, |
| 98 |
Entry *e ) |
| 99 |
{ |
| 100 |
WT_SESSION *session = wc->session; |
| 101 |
WT_CURSOR *cursor = wc->id2entry_update; |
| 102 |
int rc; |
| 103 |
|
| 104 |
if(!cursor){ |
| 105 |
rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, |
| 106 |
"overwrite=true", &cursor); |
| 107 |
if ( rc ) { |
| 108 |
Debug( LDAP_DEBUG_ANY, |
| 109 |
"wt_id2entry_put: open_cursor failed: %s (%d)\n", |
| 110 |
wiredtiger_strerror(rc), rc ); |
| 111 |
return rc; |
| 112 |
} |
| 113 |
wc->id2entry_update = cursor; |
| 114 |
} |
| 115 |
rc = wt_id2entry_put(op, wc, e, cursor); |
| 116 |
|
| 117 |
#ifdef WT_CURSOR_CACHE |
| 118 |
if(cursor){ |
| 119 |
cursor->reset(cursor); |
| 120 |
} |
| 121 |
#else |
| 122 |
if(cursor){ |
| 123 |
cursor->close(cursor); |
| 124 |
wc->id2entry_update = NULL; |
| 125 |
} |
| 126 |
#endif |
| 127 |
return rc; |
| 128 |
} |
| 129 |
|
| 130 |
int wt_id2entry_delete( |
| 131 |
Operation *op, |
| 132 |
wt_ctx *wc, |
| 133 |
Entry *e ) |
| 134 |
{ |
| 135 |
int rc; |
| 136 |
WT_SESSION *session = wc->session; |
| 137 |
WT_CURSOR *cursor = NULL; |
| 138 |
|
| 139 |
rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, |
| 140 |
NULL, &cursor); |
| 141 |
if ( rc ) { |
| 142 |
Debug( LDAP_DEBUG_ANY, |
| 143 |
"wt_id2entry_delete: open_cursor failed: %s (%d)\n", |
| 144 |
wiredtiger_strerror(rc), rc ); |
| 145 |
goto done; |
| 146 |
} |
| 147 |
cursor->set_key(cursor, e->e_id); |
| 148 |
rc = cursor->remove(cursor); |
| 149 |
if ( rc ) { |
| 150 |
Debug( LDAP_DEBUG_ANY, |
| 151 |
"wt_id2entry_delete: remove failed: %s (%d)\n", |
| 152 |
wiredtiger_strerror(rc), rc ); |
| 153 |
goto done; |
| 154 |
} |
| 155 |
|
| 156 |
done: |
| 157 |
if(cursor){ |
| 158 |
cursor->close(cursor); |
| 159 |
} |
| 160 |
return rc; |
| 161 |
} |
| 162 |
|
| 163 |
int wt_id2entry( BackendDB *be, |
| 164 |
wt_ctx *wc, |
| 165 |
ID id, |
| 166 |
Entry **ep ){ |
| 167 |
int rc; |
| 168 |
WT_SESSION *session = wc->session; |
| 169 |
WT_CURSOR *cursor = wc->id2entry; |
| 170 |
WT_ITEM item; |
| 171 |
EntryHeader eh; |
| 172 |
int eoff; |
| 173 |
Entry *e = NULL; |
| 174 |
|
| 175 |
if(!cursor){ |
| 176 |
rc = session->open_cursor(session, WT_TABLE_ID2ENTRY"(entry)", NULL, |
| 177 |
NULL, &cursor); |
| 178 |
if ( rc ) { |
| 179 |
Debug( LDAP_DEBUG_ANY, |
| 180 |
"wt_id2entry: open_cursor failed: %s (%d)\n", |
| 181 |
wiredtiger_strerror(rc), rc ); |
| 182 |
goto done; |
| 183 |
} |
| 184 |
wc->id2entry = cursor; |
| 185 |
} |
| 186 |
|
| 187 |
cursor->set_key(cursor, id); |
| 188 |
rc = cursor->search(cursor); |
| 189 |
if ( rc ) { |
| 190 |
goto done; |
| 191 |
} |
| 192 |
|
| 193 |
cursor->get_value(cursor, &item); |
| 194 |
rc = wt_entry_header( &item, &eh ); |
| 195 |
eoff = eh.data - (char *)item.data; |
| 196 |
eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size; |
| 197 |
eh.bv.bv_val = ch_malloc( eh.bv.bv_len ); |
| 198 |
memset(eh.bv.bv_val, 0xff, eh.bv.bv_len); |
| 199 |
eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval ); |
| 200 |
memcpy(eh.data, item.data, item.size); |
| 201 |
eh.data += eoff; |
| 202 |
rc = entry_decode( &eh, &e ); |
| 203 |
if ( rc ) { |
| 204 |
Debug( LDAP_DEBUG_ANY, |
| 205 |
"wt_id2entry: entry decode error: %s (%d)\n", |
| 206 |
wiredtiger_strerror(rc), rc ); |
| 207 |
goto done; |
| 208 |
} |
| 209 |
e->e_id = id; |
| 210 |
*ep = e; |
| 211 |
|
| 212 |
done: |
| 213 |
|
| 214 |
#ifdef WT_CURSOR_CACHE |
| 215 |
if(cursor){ |
| 216 |
cursor->reset(cursor); |
| 217 |
} |
| 218 |
#else |
| 219 |
if(cursor){ |
| 220 |
cursor->close(cursor); |
| 221 |
wc->id2entry = NULL; |
| 222 |
} |
| 223 |
#endif |
| 224 |
return rc; |
| 225 |
} |
| 226 |
|
| 227 |
int wt_entry_return( |
| 228 |
Entry *e |
| 229 |
) |
| 230 |
{ |
| 231 |
if ( !e ) { |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
/* Our entries are allocated in two blocks; the data comes from |
| 236 |
* the db itself and the Entry structure and associated pointers |
| 237 |
* are allocated in entry_decode. The db data pointer is saved |
| 238 |
* in e_bv. |
| 239 |
*/ |
| 240 |
if ( e->e_bv.bv_val ) { |
| 241 |
#if 0 |
| 242 |
/* See if the DNs were changed by modrdn */ |
| 243 |
if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val > |
| 244 |
e->e_bv.bv_val + e->e_bv.bv_len ) { |
| 245 |
ch_free(e->e_name.bv_val); |
| 246 |
ch_free(e->e_nname.bv_val); |
| 247 |
} |
| 248 |
#endif |
| 249 |
e->e_name.bv_val = NULL; |
| 250 |
e->e_nname.bv_val = NULL; |
| 251 |
/* In tool mode the e_bv buffer is realloc'd, leave it alone */ |
| 252 |
if( !(slapMode & SLAP_TOOL_MODE) ) { |
| 253 |
free( e->e_bv.bv_val ); |
| 254 |
} |
| 255 |
BER_BVZERO( &e->e_bv ); |
| 256 |
} |
| 257 |
|
| 258 |
entry_free( e ); |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
int wt_entry_release( |
| 263 |
Operation *op, |
| 264 |
Entry *e, |
| 265 |
int rw ) |
| 266 |
{ |
| 267 |
return wt_entry_return( e ); |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* return LDAP_SUCCESS IFF we can retrieve the specified entry. |
| 272 |
*/ |
| 273 |
int wt_entry_get( |
| 274 |
Operation *op, |
| 275 |
struct berval *ndn, |
| 276 |
ObjectClass *oc, |
| 277 |
AttributeDescription *at, |
| 278 |
int rw, |
| 279 |
Entry **ent ) |
| 280 |
{ |
| 281 |
struct wt_info *wi = (struct wt_info *) op->o_bd->be_private; |
| 282 |
wt_ctx *wc; |
| 283 |
Entry *e = NULL; |
| 284 |
int rc; |
| 285 |
const char *at_name = at ? at->ad_cname.bv_val : "(null)"; |
| 286 |
|
| 287 |
Debug( LDAP_DEBUG_ARGS, |
| 288 |
"wt_entry_get: ndn: \"%s\"\n", ndn->bv_val ); |
| 289 |
Debug( LDAP_DEBUG_ARGS, |
| 290 |
"wt_entry_get: oc: \"%s\", at: \"%s\"\n", |
| 291 |
oc ? oc->soc_cname.bv_val : "(null)", at_name ); |
| 292 |
|
| 293 |
wc = wt_ctx_get(op, wi); |
| 294 |
if( !wc ){ |
| 295 |
Debug( LDAP_DEBUG_ANY, |
| 296 |
"wt_entry_get: wt_ctx_get failed\n" ); |
| 297 |
return LDAP_OTHER; |
| 298 |
} |
| 299 |
rc = wt_dn2entry(op->o_bd, wc, ndn, &e); |
| 300 |
switch( rc ) { |
| 301 |
case 0: |
| 302 |
break; |
| 303 |
case WT_NOTFOUND: |
| 304 |
Debug( LDAP_DEBUG_ACL, |
| 305 |
"wt_entry_get: cannot find entry: \"%s\"\n", |
| 306 |
ndn->bv_val ); |
| 307 |
return LDAP_NO_SUCH_OBJECT; |
| 308 |
default: |
| 309 |
Debug( LDAP_DEBUG_ANY, |
| 310 |
"wt_entry_get: wt_dn2entry failed %s rc=%d\n", |
| 311 |
wiredtiger_strerror(rc), rc ); |
| 312 |
rc = LDAP_OTHER; |
| 313 |
} |
| 314 |
|
| 315 |
Debug( LDAP_DEBUG_ACL, |
| 316 |
"wt_entry_get: found entry: \"%s\"\n", ndn->bv_val ); |
| 317 |
|
| 318 |
if ( oc && !is_entry_objectclass( e, oc, 0 )) { |
| 319 |
Debug( LDAP_DEBUG_ACL, |
| 320 |
"wt_entry_get: failed to find objectClass %s\n", |
| 321 |
oc->soc_cname.bv_val ); |
| 322 |
rc = LDAP_NO_SUCH_ATTRIBUTE; |
| 323 |
goto return_results; |
| 324 |
} |
| 325 |
|
| 326 |
/* NOTE: attr_find() or attrs_find()? */ |
| 327 |
if ( at && attr_find( e->e_attrs, at ) == NULL ) { |
| 328 |
Debug( LDAP_DEBUG_ACL, |
| 329 |
"wt_entry_get: failed to find attribute %s\n", |
| 330 |
at->ad_cname.bv_val ); |
| 331 |
rc = LDAP_NO_SUCH_ATTRIBUTE; |
| 332 |
goto return_results; |
| 333 |
} |
| 334 |
|
| 335 |
return_results: |
| 336 |
if( rc != LDAP_SUCCESS ) { |
| 337 |
wt_entry_return( e ); |
| 338 |
}else{ |
| 339 |
*ent = e; |
| 340 |
} |
| 341 |
|
| 342 |
Debug( LDAP_DEBUG_TRACE, "wt_entry_get: rc=%d\n", rc ); |
| 343 |
|
| 344 |
return rc; |
| 345 |
} |
| 346 |
|
| 347 |
/* |
| 348 |
* Local variables: |
| 349 |
* indent-tabs-mode: t |
| 350 |
* tab-width: 4 |
| 351 |
* c-basic-offset: 4 |
| 352 |
* End: |
| 353 |
*/ |