openldap/servers/slapd/back-bdb/dn2id.c

346 lines
7.0 KiB
C
Raw Normal View History

2000-09-20 15:21:09 +08:00
/* dn2id.c - routines to deal with the dn2id index */
/* $OpenLDAP$ */
/*
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "portable.h"
#include <stdio.h>
#include <ac/string.h>
#include "back-bdb.h"
int
2000-09-22 14:46:32 +08:00
bdb_dn2id_add(
BackendDB *be,
2000-09-20 15:21:09 +08:00
DB_TXN *txn,
const char *dn,
ID id
2000-09-20 15:21:09 +08:00
)
{
int rc;
DBT key, data;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
2000-09-27 03:26:08 +08:00
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_add( \"%s\", 0x%08lx )\n",
dn, id, 0 );
2000-09-20 15:21:09 +08:00
assert( id != NOID );
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
((char *)key.data)[0] = DN_BASE_PREFIX;
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
DBTzero( &data );
data.data = (char *) &id;
data.size = sizeof( id );
/* store it -- don't override */
rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_add: put failed: %s %d\n",
db_strerror(rc), rc, 0 );
2000-09-20 15:21:09 +08:00
goto done;
}
{
char *pdn = dn_parent( NULL, dn );
((char *)(key.data))[0] = DN_ONE_PREFIX;
if( pdn != NULL ) {
key.size = strlen( pdn ) + 2;
AC_MEMCPY( &((char*)key.data)[1],
pdn, key.size - 1 );
rc = bdb_idl_insert_key( be, db, txn, &key, id );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY,
"=> bdb_dn2id_add: parent (%s) insert failed: %d\n",
pdn, rc, 0 );
free( pdn );
2000-09-20 15:21:09 +08:00
goto done;
}
free( pdn );
2000-09-20 15:21:09 +08:00
}
}
{
2000-09-20 15:21:09 +08:00
char **subtree = dn_subtree( NULL, dn );
if( subtree != NULL ) {
int i;
((char *)key.data)[0] = DN_SUBTREE_PREFIX;
for( i=0; subtree[i] != NULL; i++ ) {
key.size = strlen( subtree[i] ) + 2;
AC_MEMCPY( &((char *)key.data)[1],
subtree[i], key.size - 1 );
rc = bdb_idl_insert_key( be, db, txn, &key, id );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY,
"=> bdb_dn2id_add: subtree (%s) insert failed: %d\n",
subtree[i], rc, 0 );
break;
2000-09-20 15:21:09 +08:00
}
}
charray_free( subtree );
}
}
done:
ch_free( key.data );
Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_add: %d\n", rc, 0, 0 );
2000-09-24 14:04:58 +08:00
return rc;
}
int
bdb_dn2id_delete(
BackendDB *be,
2000-09-24 14:04:58 +08:00
DB_TXN *txn,
const char *dn,
ID id )
2000-09-24 14:04:58 +08:00
{
int rc;
DBT key;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
2000-09-27 03:26:08 +08:00
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_delete( \"%s\", 0x%08lx )\n",
dn, id, 0 );
2000-09-24 14:04:58 +08:00
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
((char *)key.data)[0] = DN_BASE_PREFIX;
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
/* store it -- don't override */
rc = db->del( db, txn, &key, 0 );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY, "=> bdb_dn2id_delete: delete failed: %s %d\n",
db_strerror(rc), rc, 0 );
2000-09-24 14:04:58 +08:00
goto done;
}
{
char *pdn = dn_parent( NULL, dn );
((char *)(key.data))[0] = DN_ONE_PREFIX;
if( pdn != NULL ) {
key.size = strlen( pdn ) + 2;
AC_MEMCPY( &((char*)key.data)[1],
pdn, key.size - 1 );
rc = bdb_idl_delete_key( be, db, txn, &key, id );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY,
"=> bdb_dn2id_delete: parent (%s) delete failed: %d\n",
pdn, rc, 0 );
free( pdn );
2000-09-24 14:04:58 +08:00
goto done;
}
free( pdn );
2000-09-24 14:04:58 +08:00
}
}
{
char **subtree = dn_subtree( NULL, dn );
if( subtree != NULL ) {
int i;
((char *)key.data)[0] = DN_SUBTREE_PREFIX;
for( i=0; subtree[i] != NULL; i++ ) {
key.size = strlen( subtree[i] ) + 2;
AC_MEMCPY( &((char *)key.data)[1],
subtree[i], key.size - 1 );
rc = bdb_idl_delete_key( be, db, txn, &key, id );
if( rc != 0 ) {
Debug( LDAP_DEBUG_ANY,
"=> bdb_dn2id_delete: subtree (%s) delete failed: %d\n",
subtree[i], rc, 0 );
charray_free( subtree );
2000-09-24 14:04:58 +08:00
goto done;
}
}
charray_free( subtree );
}
}
done:
ch_free( key.data );
Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_delete %d\n", rc, 0, 0 );
return rc;
}
int
2000-09-25 06:48:13 +08:00
bdb_dn2id(
BackendDB *be,
2000-09-24 14:04:58 +08:00
DB_TXN *txn,
const char *dn,
2000-09-25 06:48:13 +08:00
ID *id )
2000-09-24 14:04:58 +08:00
{
int rc;
DBT key, data;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
2000-09-25 06:48:13 +08:00
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id( \"%s\" )\n", dn, 0, 0 );
2000-09-24 14:04:58 +08:00
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
2000-09-25 06:48:13 +08:00
((char *)key.data)[0] = DN_BASE_PREFIX;
2000-09-24 14:04:58 +08:00
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
2000-09-25 06:48:13 +08:00
/* store the ID */
2000-09-24 14:04:58 +08:00
DBTzero( &data );
2000-09-25 06:48:13 +08:00
data.data = id;
data.ulen = sizeof(ID);
2000-09-24 14:04:58 +08:00
data.flags = DB_DBT_USERMEM;
2000-09-25 06:48:13 +08:00
/* fetch it */
2000-09-24 14:04:58 +08:00
rc = db->get( db, txn, &key, &data, 0 );
2000-09-27 03:26:08 +08:00
Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id: id=0x%08lx: %s (%d)\n",
2000-09-25 06:48:13 +08:00
id, db_strerror( rc ), rc );
2000-09-24 14:04:58 +08:00
2000-09-25 06:48:13 +08:00
ch_free( key.data );
return rc;
2000-09-20 15:21:09 +08:00
}
2000-09-25 06:48:13 +08:00
int
bdb_dn2id_matched(
BackendDB *be,
2000-09-25 06:48:13 +08:00
DB_TXN *txn,
const char *in,
2000-09-25 06:48:13 +08:00
ID *id,
char **matchedDN )
2000-09-20 15:21:09 +08:00
{
2000-09-25 06:48:13 +08:00
int rc;
DBT key, data;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
const char *dn = in;
char *tmp = NULL;
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_matched( \"%s\" )\n", dn, 0, 0 );
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
((char *)key.data)[0] = DN_BASE_PREFIX;
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
/* store the ID */
DBTzero( &data );
data.data = id;
data.ulen = sizeof(ID);
data.flags = DB_DBT_USERMEM;
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
*matchedDN = NULL;
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
while(1) {
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
2000-09-20 15:21:09 +08:00
2000-09-27 03:26:08 +08:00
*id = NOID;
2000-09-25 06:48:13 +08:00
/* fetch it */
rc = db->get( db, txn, &key, &data, 0 );
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
if( rc == DB_NOTFOUND ) {
char *pdn = dn_parent( be, dn );
ch_free( tmp );
tmp = NULL;
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
if( pdn == NULL || *pdn == '\0' ) {
2000-09-27 03:26:08 +08:00
Debug( LDAP_DEBUG_TRACE,
"<= bdb_dn2id_matched: no match\n",
0, 0, 0 );
2000-09-25 06:48:13 +08:00
ch_free( pdn );
break;
}
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
dn = pdn;
tmp = pdn;
key.size = strlen( dn ) + 2;
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
} else if ( rc == 0 ) {
2000-09-27 03:26:08 +08:00
if( data.size != sizeof( ID ) ) {
Debug( LDAP_DEBUG_ANY,
"<= bdb_dn2id_matched: get size mismatch: "
"expected %ld, got %ld\n",
(long) sizeof(ID), (long) data.size, 0 );
ch_free( tmp );
}
2000-09-25 06:48:13 +08:00
if( in != dn ) {
*matchedDN = (char *) dn;
}
2000-09-27 03:26:08 +08:00
2000-09-25 06:48:13 +08:00
Debug( LDAP_DEBUG_TRACE,
2000-09-27 03:26:08 +08:00
"<= bdb_dn2id_matched: id=0x%08lx: %s\n",
*id, dn, 0 );
2000-09-25 06:48:13 +08:00
break;
} else {
2000-09-27 03:26:08 +08:00
Debug( LDAP_DEBUG_ANY,
"<= bdb_dn2id_matched: get failed: %s (%d)\n",
db_strerror(rc), rc, 0 );
2000-09-25 06:48:13 +08:00
ch_free( tmp );
break;
}
2000-09-20 15:21:09 +08:00
}
2000-09-25 06:48:13 +08:00
ch_free( key.data );
return rc;
2000-09-20 15:21:09 +08:00
}
2000-09-25 06:48:13 +08:00
int
bdb_dn2id_children(
BackendDB *be,
2000-09-25 06:48:13 +08:00
DB_TXN *txn,
const char *dn )
2000-09-20 15:21:09 +08:00
{
2000-09-25 06:48:13 +08:00
int rc;
DBT key, data;
struct bdb_info *bdb = (struct bdb_info *) be->be_private;
DB *db = bdb->bi_dn2id->bdi_db;
2000-09-20 15:21:09 +08:00
ID id;
2000-09-25 06:48:13 +08:00
Debug( LDAP_DEBUG_TRACE, "=> bdb_dn2id_children( %s )\n",
dn, 0, 0 );
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
DBTzero( &key );
key.size = strlen( dn ) + 2;
key.data = ch_malloc( key.size );
((char *)key.data)[0] = DN_ONE_PREFIX;
AC_MEMCPY( &((char *)key.data)[1], dn, key.size - 1 );
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
/* we actually could do a empty get... */
DBTzero( &data );
data.data = &id;
data.ulen = sizeof(id);
data.flags = DB_DBT_USERMEM;
data.doff = 0;
data.dlen = sizeof(id);
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
rc = db->get( db, txn, &key, &data, 0 );
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
Debug( LDAP_DEBUG_TRACE, "<= bdb_dn2id_children( %s ): %s (%d)\n",
dn,
rc == 0 ? "yes" : ( rc == DB_NOTFOUND ? "no" :
db_strerror(rc) ), rc );
2000-09-20 15:21:09 +08:00
2000-09-25 06:48:13 +08:00
return rc;
2000-09-20 15:21:09 +08:00
}