1998-08-09 08:43:13 +08:00
|
|
|
/* dn2id.c - routines to deal with the dn2id index */
|
1999-08-07 07:07:46 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "slap.h"
|
|
|
|
#include "back-ldbm.h"
|
1998-09-21 04:22:46 +08:00
|
|
|
#include "proto-back-ldbm.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
dn2id_add(
|
|
|
|
Backend *be,
|
|
|
|
char *dn,
|
|
|
|
ID id
|
|
|
|
)
|
|
|
|
{
|
1998-08-19 08:10:02 +08:00
|
|
|
int rc, flags;
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db;
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum key, data;
|
1998-08-19 08:10:02 +08:00
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-30 01:28:45 +08:00
|
|
|
ldbm_datum_init( key );
|
|
|
|
ldbm_datum_init( data );
|
1998-09-03 08:50:13 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "=> dn2id_add( \"%s\", %ld )\n", dn, id, 0 );
|
|
|
|
|
|
|
|
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
|
|
|
|
== NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "Could not open/create dn2id%s\n",
|
|
|
|
LDBM_SUFFIX, 0, 0 );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-08-11 10:57:07 +08:00
|
|
|
#ifndef DN_INDICES
|
1998-08-09 08:43:13 +08:00
|
|
|
key.dptr = dn;
|
1999-08-11 10:57:07 +08:00
|
|
|
key.dsize = strlen( key.dptr ) + 1;
|
|
|
|
#else
|
|
|
|
key.dsize = strlen( dn ) + 2;
|
|
|
|
key.dptr = ch_malloc( key.dsize );
|
1999-08-13 08:55:08 +08:00
|
|
|
sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
|
1999-08-11 10:57:07 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
data.dptr = (char *) &id;
|
|
|
|
data.dsize = sizeof(ID);
|
|
|
|
|
1998-08-19 08:10:02 +08:00
|
|
|
flags = LDBM_INSERT;
|
1998-11-07 10:25:32 +08:00
|
|
|
if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
|
1998-08-19 08:10:02 +08:00
|
|
|
|
|
|
|
rc = ldbm_cache_store( db, key, data, flags );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-08-11 10:57:07 +08:00
|
|
|
#ifdef DN_INDICES
|
|
|
|
free( key.dptr );
|
1999-08-12 04:59:28 +08:00
|
|
|
|
|
|
|
if ( rc != -1 ) {
|
|
|
|
char *pdn = dn_parent( NULL, dn );
|
|
|
|
|
|
|
|
if( pdn != NULL ) {
|
|
|
|
key.dsize = strlen( pdn ) + 2;
|
|
|
|
key.dptr = ch_malloc( key.dsize );
|
1999-08-13 08:55:08 +08:00
|
|
|
sprintf( key.dptr, "%c%s", DN_ONE_PREFIX, pdn );
|
1999-08-12 04:59:28 +08:00
|
|
|
rc = idl_insert_key( be, db, key, id );
|
|
|
|
free( key.dptr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rc != -1 ) {
|
|
|
|
char **subtree = dn_subtree( NULL, dn );
|
|
|
|
|
|
|
|
if( subtree != NULL ) {
|
|
|
|
int i;
|
|
|
|
for( i=0; subtree[i] != NULL; i++ ) {
|
|
|
|
key.dsize = strlen( subtree[i] ) + 2;
|
|
|
|
key.dptr = ch_malloc( key.dsize );
|
1999-08-12 09:05:33 +08:00
|
|
|
sprintf( key.dptr, "%c%s",
|
|
|
|
DN_SUBTREE_PREFIX, subtree[i] );
|
1999-08-12 04:59:28 +08:00
|
|
|
|
|
|
|
rc = idl_insert_key( be, db, key, id );
|
|
|
|
|
|
|
|
free( key.dptr );
|
|
|
|
|
|
|
|
if(rc == -1) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
charray_free( subtree );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-08-11 10:57:07 +08:00
|
|
|
#endif
|
|
|
|
|
1999-08-12 04:59:28 +08:00
|
|
|
ldbm_cache_close( be, db );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= dn2id_add %d\n", rc, 0, 0 );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
ID
|
|
|
|
dn2id(
|
|
|
|
Backend *be,
|
|
|
|
char *dn
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db;
|
1998-08-09 08:43:13 +08:00
|
|
|
ID id;
|
|
|
|
Datum key, data;
|
|
|
|
|
1998-12-30 01:28:45 +08:00
|
|
|
ldbm_datum_init( key );
|
|
|
|
ldbm_datum_init( data );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-19 01:51:53 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "=> dn2id( \"%s\" )\n", dn, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* first check the cache */
|
1998-09-21 04:22:46 +08:00
|
|
|
if ( (id = cache_find_entry_dn2id( be, &li->li_cache, dn )) != NOID ) {
|
1999-02-12 02:19:52 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld (in cache)\n", id,
|
1998-09-21 04:22:46 +08:00
|
|
|
0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
|
1998-09-21 04:22:46 +08:00
|
|
|
== NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "<= dn2id could not open dn2id%s\n",
|
1998-09-21 04:22:46 +08:00
|
|
|
LDBM_SUFFIX, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NOID );
|
|
|
|
}
|
|
|
|
|
1999-08-11 10:57:07 +08:00
|
|
|
#ifndef DN_INDICES
|
1998-08-09 08:43:13 +08:00
|
|
|
key.dptr = dn;
|
1999-08-11 10:57:07 +08:00
|
|
|
key.dsize = strlen( key.dptr ) + 1;
|
|
|
|
#else
|
|
|
|
key.dsize = strlen( dn ) + 2;
|
|
|
|
key.dptr = ch_malloc( key.dsize );
|
1999-08-13 08:55:08 +08:00
|
|
|
sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
|
1999-08-11 10:57:07 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
data = ldbm_cache_fetch( db, key );
|
|
|
|
|
|
|
|
ldbm_cache_close( be, db );
|
1999-08-11 10:57:07 +08:00
|
|
|
|
|
|
|
#ifdef DN_INDICES
|
|
|
|
free( key.dptr );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( data.dptr == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= dn2id NOID\n", 0, 0, 0 );
|
|
|
|
return( NOID );
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) memcpy( (char *) &id, data.dptr, sizeof(ID) );
|
|
|
|
|
|
|
|
ldbm_datum_free( db->dbc_db, data );
|
|
|
|
|
1999-02-12 02:19:52 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= dn2id %ld\n", id, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( id );
|
|
|
|
}
|
|
|
|
|
1999-08-13 08:55:08 +08:00
|
|
|
#ifdef DN_INDICES
|
|
|
|
ID_BLOCK *
|
|
|
|
dn2idl(
|
|
|
|
Backend *be,
|
|
|
|
char *dn,
|
|
|
|
int prefix
|
|
|
|
)
|
|
|
|
{
|
|
|
|
DBCache *db;
|
|
|
|
Datum key;
|
|
|
|
ID_BLOCK *idl;
|
|
|
|
|
|
|
|
ldbm_datum_init( key );
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "=> dn2idl( \"%c%s\" )\n", prefix, dn, 0 );
|
|
|
|
|
|
|
|
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
|
|
|
|
== NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "<= dn2idl could not open dn2id%s\n",
|
|
|
|
LDBM_SUFFIX, 0, 0 );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
key.dsize = strlen( dn ) + 2;
|
|
|
|
key.dptr = ch_malloc( key.dsize );
|
|
|
|
sprintf( key.dptr, "%c%s", prefix, dn );
|
|
|
|
|
|
|
|
idl = idl_fetch( be, db, key );
|
|
|
|
|
|
|
|
ldbm_cache_close( be, db );
|
|
|
|
|
|
|
|
free( key.dptr );
|
|
|
|
|
|
|
|
return( idl );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
int
|
|
|
|
dn2id_delete(
|
|
|
|
Backend *be,
|
|
|
|
char *dn
|
|
|
|
)
|
|
|
|
{
|
1999-07-20 03:40:33 +08:00
|
|
|
DBCache *db;
|
1998-08-09 08:43:13 +08:00
|
|
|
Datum key;
|
|
|
|
int rc;
|
|
|
|
|
1998-12-30 01:28:45 +08:00
|
|
|
ldbm_datum_init( key );
|
1998-09-03 08:50:13 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "=> dn2id_delete( \"%s\" )\n", dn, 0, 0 );
|
|
|
|
|
|
|
|
if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_WRCREAT ))
|
|
|
|
== NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"<= dn2id_delete could not open dn2id%s\n", LDBM_SUFFIX,
|
|
|
|
0, 0 );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-08-11 10:57:07 +08:00
|
|
|
#ifndef DN_INDICES
|
1998-08-09 08:43:13 +08:00
|
|
|
key.dptr = dn;
|
1999-08-11 10:57:07 +08:00
|
|
|
key.dsize = strlen( key.dptr ) + 1;
|
|
|
|
#else
|
|
|
|
key.dsize = strlen( dn ) + 2;
|
|
|
|
key.dptr = ch_malloc( key.dsize );
|
1999-08-13 08:55:08 +08:00
|
|
|
sprintf( key.dptr, "%c%s", DN_BASE_PREFIX, dn );
|
1999-08-11 10:57:07 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
rc = ldbm_cache_delete( db, key );
|
|
|
|
|
1999-08-11 10:57:07 +08:00
|
|
|
#ifdef DN_INDICES
|
|
|
|
free( key.dptr );
|
|
|
|
#endif
|
1999-01-19 04:49:07 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
ldbm_cache_close( be, db );
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "<= dn2id_delete %d\n", rc, 0, 0 );
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dn2entry - look up dn in the cache/indexes and return the corresponding
|
|
|
|
* entry.
|
|
|
|
*/
|
|
|
|
|
1999-02-12 01:46:56 +08:00
|
|
|
Entry *
|
1999-02-11 02:28:25 +08:00
|
|
|
dn2entry_rw(
|
1998-08-09 08:43:13 +08:00
|
|
|
Backend *be,
|
|
|
|
char *dn,
|
1999-07-16 10:45:46 +08:00
|
|
|
Entry **matched,
|
1998-09-21 04:22:46 +08:00
|
|
|
int rw
|
1998-08-09 08:43:13 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
ID id;
|
1998-11-24 04:08:25 +08:00
|
|
|
Entry *e = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
char *pdn;
|
|
|
|
|
1999-01-19 01:33:36 +08:00
|
|
|
Debug(LDAP_DEBUG_TRACE, "dn2entry_%s: dn: \"%s\"\n",
|
1998-09-21 04:22:46 +08:00
|
|
|
rw ? "w" : "r", dn, 0);
|
|
|
|
|
1999-07-16 10:45:46 +08:00
|
|
|
if( matched != NULL ) {
|
|
|
|
/* caller cares about match */
|
|
|
|
*matched = NULL;
|
|
|
|
}
|
1998-11-24 04:08:25 +08:00
|
|
|
|
1998-09-21 04:22:46 +08:00
|
|
|
if ( (id = dn2id( be, dn )) != NOID &&
|
1999-02-11 02:28:25 +08:00
|
|
|
(e = id2entry_rw( be, id, rw )) != NULL )
|
1998-09-21 04:22:46 +08:00
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
return( e );
|
|
|
|
}
|
|
|
|
|
1999-01-24 03:33:53 +08:00
|
|
|
if ( id != NOID ) {
|
|
|
|
Debug(LDAP_DEBUG_ANY,
|
1999-02-12 02:19:52 +08:00
|
|
|
"dn2entry_%s: no entry for valid id (%ld), dn \"%s\"\n",
|
1999-01-24 03:33:53 +08:00
|
|
|
rw ? "w" : "r", id, dn);
|
|
|
|
/* must have been deleted from underneath us */
|
|
|
|
/* treat as if NOID was found */
|
|
|
|
}
|
|
|
|
|
1999-07-16 10:45:46 +08:00
|
|
|
/* caller doesn't care about match */
|
|
|
|
if( matched == NULL ) return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* entry does not exist - see how much of the dn does exist */
|
1999-07-16 10:45:46 +08:00
|
|
|
/* dn_parent checks returns NULL if dn is suffix */
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (pdn = dn_parent( be, dn )) != NULL ) {
|
1998-09-21 04:22:46 +08:00
|
|
|
/* get entry with reader lock */
|
|
|
|
if ( (e = dn2entry_r( be, pdn, matched )) != NULL ) {
|
1999-07-16 10:45:46 +08:00
|
|
|
*matched = e;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-07-16 10:45:46 +08:00
|
|
|
free( pdn );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-07-16 10:45:46 +08:00
|
|
|
return NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-09-21 04:22:46 +08:00
|
|
|
|