1999-02-06 00:23:03 +08:00
|
|
|
/* add.c - ldap bdb2 back-end add routine */
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "back-bdb2.h"
|
|
|
|
#include "proto-back-bdb2.h"
|
|
|
|
|
1999-04-19 18:47:03 +08:00
|
|
|
static DB_LOCK lock;
|
|
|
|
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
static int
|
|
|
|
bdb2i_back_add_internal(
|
1999-02-08 19:42:14 +08:00
|
|
|
BackendDB *be,
|
1999-02-06 00:23:03 +08:00
|
|
|
Connection *conn,
|
|
|
|
Operation *op,
|
|
|
|
Entry *e
|
|
|
|
)
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
char *pdn;
|
|
|
|
Entry *p = NULL;
|
1999-02-12 20:13:03 +08:00
|
|
|
int rc;
|
1999-02-22 19:22:44 +08:00
|
|
|
struct timeval time1;
|
1999-02-06 00:23:03 +08:00
|
|
|
|
|
|
|
Debug(LDAP_DEBUG_ARGS, "==> bdb2i_back_add: %s\n", e->e_dn, 0, 0);
|
|
|
|
|
|
|
|
if ( ( bdb2i_dn2id( be, e->e_ndn ) ) != NOID ) {
|
|
|
|
entry_free( e );
|
|
|
|
send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
|
|
|
|
entry_free( e );
|
|
|
|
send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
|
|
|
|
"" );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the parent dn and see if the corresponding entry exists.
|
|
|
|
* If the parent does not exist, only allow the "root" user to
|
|
|
|
* add the entry.
|
|
|
|
*/
|
|
|
|
|
1999-06-19 13:11:18 +08:00
|
|
|
pdn = dn_parent( be, e->e_ndn );
|
|
|
|
|
|
|
|
if( pdn != NULL && *pdn != '\0' && !be_issuffix(be, "") ) {
|
1999-02-06 00:23:03 +08:00
|
|
|
char *matched = NULL;
|
|
|
|
|
1999-06-19 13:11:18 +08:00
|
|
|
assert( *pdn != '\0' );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/* get parent with writer lock */
|
|
|
|
if ( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
|
|
|
|
0, 0 );
|
|
|
|
send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
|
|
|
|
matched, "" );
|
|
|
|
|
|
|
|
if ( matched != NULL ) {
|
|
|
|
free( matched );
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_free( e );
|
|
|
|
free( pdn );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(pdn);
|
|
|
|
|
|
|
|
if ( matched != NULL ) {
|
|
|
|
free( matched );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! access_allowed( be, conn, op, p,
|
|
|
|
"children", NULL, ACL_WRITE ) )
|
|
|
|
{
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
|
|
|
|
0, 0 );
|
|
|
|
send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
|
|
|
|
"", "" );
|
|
|
|
|
|
|
|
/* free parent and writer lock */
|
|
|
|
bdb2i_cache_return_entry_w( &li->li_cache, p );
|
|
|
|
|
|
|
|
entry_free( e );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
1999-06-19 13:11:18 +08:00
|
|
|
if(pdn != NULL) {
|
|
|
|
assert( *pdn == '\0' );
|
|
|
|
free(pdn);
|
|
|
|
}
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/* no parent, must be adding entry to root */
|
|
|
|
if ( ! be_isroot( be, op->o_ndn ) ) {
|
1999-06-19 13:11:18 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
|
|
|
|
pdn == NULL ? "suffix" : "entry at root",
|
|
|
|
0, 0 );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
|
|
|
|
"", "" );
|
|
|
|
|
|
|
|
entry_free( e );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e->e_id = bdb2i_next_id( be );
|
|
|
|
|
|
|
|
/*
|
1999-02-12 20:13:03 +08:00
|
|
|
* Try to add the entry to the cache, assign it a new dnid.
|
1999-02-06 00:23:03 +08:00
|
|
|
*/
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_start_timing( be->bd_info, &time1 );
|
|
|
|
|
1999-02-12 20:13:03 +08:00
|
|
|
rc = bdb2i_cache_add_entry_rw( &li->li_cache, e, CACHE_WRITE_LOCK );
|
1999-02-06 00:23:03 +08:00
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-CACHE", conn, op );
|
|
|
|
|
1999-02-12 20:13:03 +08:00
|
|
|
if ( rc != 0 ) {
|
1999-02-06 00:23:03 +08:00
|
|
|
if( p != NULL) {
|
|
|
|
/* free parent and writer lock */
|
|
|
|
bdb2i_cache_return_entry_w( &li->li_cache, p );
|
|
|
|
}
|
1999-02-12 20:13:03 +08:00
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
|
|
|
|
0 );
|
1999-02-12 20:13:03 +08:00
|
|
|
|
|
|
|
/* return the id */
|
1999-02-06 00:23:03 +08:00
|
|
|
bdb2i_next_id_return( be, e->e_id );
|
|
|
|
|
1999-02-12 20:13:03 +08:00
|
|
|
/* free the entry */
|
1999-02-06 00:23:03 +08:00
|
|
|
entry_free( e );
|
|
|
|
|
1999-02-12 20:13:03 +08:00
|
|
|
if(rc > 0) {
|
|
|
|
send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
|
|
|
|
} else {
|
|
|
|
send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
|
|
|
|
}
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-02-12 20:13:03 +08:00
|
|
|
rc = -1;
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/*
|
|
|
|
* add it to the id2children index for the parent
|
|
|
|
*/
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_start_timing( be->bd_info, &time1 );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
if ( bdb2i_id2children_add( be, p, e ) != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "bdb2i_id2children_add failed\n", 0,
|
|
|
|
0, 0 );
|
|
|
|
send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2CHILDREN", conn, op );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
goto return_results;
|
|
|
|
}
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2CHILDREN", conn, op );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/*
|
|
|
|
* Add the entry to the attribute indexes, then add it to
|
|
|
|
* the id2children index, dn2id index, and the id2entry index.
|
|
|
|
*/
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_start_timing( be->bd_info, &time1 );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/* attribute indexes */
|
|
|
|
if ( bdb2i_index_add_entry( be, e ) != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "bdb2i_index_add_entry failed\n", 0,
|
|
|
|
0, 0 );
|
|
|
|
send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
goto return_results;
|
|
|
|
}
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-INDEX", conn, op );
|
|
|
|
|
|
|
|
bdb2i_start_timing( be->bd_info, &time1 );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/* dn2id index */
|
|
|
|
if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "bdb2i_dn2id_add failed\n", 0,
|
|
|
|
0, 0 );
|
|
|
|
send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
goto return_results;
|
|
|
|
}
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-DN2ID", conn, op );
|
|
|
|
|
|
|
|
bdb2i_start_timing( be->bd_info, &time1 );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
/* id2entry index */
|
|
|
|
if ( bdb2i_id2entry_add( be, e ) != 0 ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "bdb2i_id2entry_add failed\n", 0,
|
|
|
|
0, 0 );
|
|
|
|
(void) bdb2i_dn2id_delete( be, e->e_ndn );
|
|
|
|
send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
goto return_results;
|
|
|
|
}
|
|
|
|
|
1999-02-22 19:22:44 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD-ID2ENTRY", conn, op );
|
|
|
|
|
1999-02-06 00:23:03 +08:00
|
|
|
send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
return_results:;
|
|
|
|
if (p != NULL) {
|
|
|
|
/* free parent and writer lock */
|
|
|
|
bdb2i_cache_return_entry_w( &li->li_cache, p );
|
|
|
|
}
|
|
|
|
|
1999-04-19 18:47:03 +08:00
|
|
|
if ( rc ) {
|
1999-04-13 14:21:30 +08:00
|
|
|
/* free entry and writer lock */
|
|
|
|
bdb2i_cache_return_entry_w( &li->li_cache, e );
|
|
|
|
}
|
1999-02-06 00:23:03 +08:00
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
bdb2_back_add(
|
1999-02-08 19:42:14 +08:00
|
|
|
BackendDB *be,
|
1999-02-06 00:23:03 +08:00
|
|
|
Connection *conn,
|
|
|
|
Operation *op,
|
|
|
|
Entry *e
|
|
|
|
)
|
|
|
|
{
|
1999-02-08 19:42:14 +08:00
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
1999-04-19 18:47:03 +08:00
|
|
|
struct timeval time1;
|
1999-02-12 22:36:16 +08:00
|
|
|
int ret;
|
1999-02-06 00:23:03 +08:00
|
|
|
|
1999-02-12 23:22:43 +08:00
|
|
|
bdb2i_start_timing( be->bd_info, &time1 );
|
1999-02-06 00:23:03 +08:00
|
|
|
|
1999-03-05 18:25:55 +08:00
|
|
|
if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
|
1999-02-06 00:23:03 +08:00
|
|
|
|
|
|
|
send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check, if a new default attribute index will be created,
|
|
|
|
in which case we have to open the index file BEFORE TP */
|
1999-02-12 22:36:16 +08:00
|
|
|
switch ( slapMode ) {
|
|
|
|
case SLAP_SERVER_MODE:
|
|
|
|
case SLAP_TIMEDSERVER_MODE:
|
|
|
|
case SLAP_TOOL_MODE:
|
1999-03-01 19:30:18 +08:00
|
|
|
case SLAP_TOOLID_MODE:
|
1999-02-12 22:36:16 +08:00
|
|
|
bdb2i_check_default_attr_index_add( li, e );
|
|
|
|
break;
|
|
|
|
}
|
1999-02-06 00:23:03 +08:00
|
|
|
|
|
|
|
ret = bdb2i_back_add_internal( be, conn, op, e );
|
1999-04-19 18:47:03 +08:00
|
|
|
|
|
|
|
/* if the operation was successful, we will delay the unlock */
|
|
|
|
if ( ret )
|
|
|
|
(void) bdb2i_leave_backend_w( lock );
|
|
|
|
|
1999-02-12 23:22:43 +08:00
|
|
|
bdb2i_stop_timing( be->bd_info, time1, "ADD", conn, op );
|
1999-02-06 00:23:03 +08:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-19 18:47:03 +08:00
|
|
|
int
|
|
|
|
bdb2i_release_add_lock( void )
|
|
|
|
{
|
|
|
|
(void) bdb2i_leave_backend_w( lock );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|