1 /* add.c - ldap ldbm back-end add routine */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
22 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23 char *dn = NULL, *pdn = NULL;
27 dn = dn_normalize( ch_strdup( e->e_dn ) );
29 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
31 pthread_mutex_lock(&li->li_add_mutex);
33 if ( ( dn2id( be, dn ) ) != NOID ) {
34 pthread_mutex_unlock(&li->li_add_mutex);
37 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
41 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
42 pthread_mutex_unlock(&li->li_add_mutex);
44 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
49 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
55 * Get the parent dn and see if the corresponding entry exists.
56 * If the parent does not exist, only allow the "root" user to
60 if ( (pdn = dn_parent( be, dn )) != NULL ) {
64 /* get entry with reader lock */
65 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
66 pthread_mutex_unlock(&li->li_add_mutex);
67 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
69 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
72 if ( matched != NULL ) {
81 if ( ! access_allowed( be, conn, op, p, "children", NULL,
82 op->o_dn, ACL_WRITE ) )
84 pthread_mutex_unlock(&li->li_add_mutex);
85 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
87 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
96 if ( ! be_isroot( be, op->o_dn ) ) {
97 pthread_mutex_unlock(&li->li_add_mutex);
98 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
100 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
110 * Try to add the entry to the cache, assign it a new dnid
111 * and mark it locked. This should only fail if the entry
115 e->e_id = next_id( be );
116 if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING ) != 0 ) {
117 pthread_mutex_unlock(&li->li_add_mutex);
119 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
121 next_id_return( be, e->e_id );
123 /* XXX this should be ok, no other thread should have access
124 * because e hasn't been added to the cache yet
128 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
133 * add it to the id2children index for the parent
136 if ( id2children_add( be, p, e ) != 0 ) {
137 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
139 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
146 * Add the entry to the attribute indexes, then add it to
147 * the id2children index, dn2id index, and the id2entry index.
150 /* attribute indexes */
151 if ( index_add_entry( be, e ) != 0 ) {
152 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
154 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
161 if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
162 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
164 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
170 /* acquire writer lock */
171 entry_rdwr_lock(e, 1);
174 if ( id2entry_add( be, e ) != 0 ) {
175 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
177 (void) dn2id_delete( be, dn );
178 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
184 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
194 cache_set_state( &li->li_cache, e, 0 );
196 /* free entry and writer lock */
197 cache_return_entry_w( &li->li_cache, e );
199 /* free entry and reader lock */
201 cache_return_entry_r( &li->li_cache, p );
204 /* it might actually be okay to release this lock sooner */
205 pthread_mutex_unlock(&li->li_add_mutex);