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;
30 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
32 /* nobody else can add until we lock our parent */
33 pthread_mutex_lock(&li->li_add_mutex);
35 if ( ( dn2id( be, dn ) ) != NOID ) {
36 pthread_mutex_unlock(&li->li_add_mutex);
38 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
42 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
43 pthread_mutex_unlock(&li->li_add_mutex);
45 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 ) {
63 /* get parent with writer lock */
64 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
65 pthread_mutex_unlock(&li->li_add_mutex);
66 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
68 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
71 if ( matched != NULL ) {
80 /* don't need the add lock anymore */
81 pthread_mutex_unlock(&li->li_add_mutex);
85 if ( matched != NULL ) {
89 if ( ! access_allowed( be, conn, op, p,
90 "children", NULL, ACL_WRITE ) )
92 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
94 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
97 /* free parent and writer lock */
98 cache_return_entry_w( &li->li_cache, p );
105 /* no parent, must be adding entry to root */
106 if ( ! be_isroot( be, op->o_ndn ) ) {
107 pthread_mutex_unlock(&li->li_add_mutex);
108 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
110 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
118 * no parent, acquire the root write lock
119 * and release the add lock.
121 pthread_mutex_lock(&li->li_root_mutex);
123 pthread_mutex_unlock(&li->li_add_mutex);
126 /* acquire required reader/writer lock */
127 if (entry_rdwr_lock(e, 1)) {
129 /* free parent and writer lock */
130 cache_return_entry_w( &li->li_cache, p );
134 /* release root lock */
135 pthread_mutex_unlock(&li->li_root_mutex);
138 Debug( LDAP_DEBUG_ANY, "add: could not lock entry\n",
143 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
147 e->e_id = next_id( be );
150 * Try to add the entry to the cache, assign it a new dnid.
151 * This should only fail if the entry already exists.
154 if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING ) != 0 ) {
156 /* free parent and writer lock */
157 cache_return_entry_w( &li->li_cache, p );
160 /* release root lock */
161 pthread_mutex_unlock(&li->li_root_mutex);
164 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
166 next_id_return( be, e->e_id );
168 entry_rdwr_unlock(e, 1);;
171 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
176 * add it to the id2children index for the parent
179 if ( id2children_add( be, p, e ) != 0 ) {
180 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
182 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
188 * Add the entry to the attribute indexes, then add it to
189 * the id2children index, dn2id index, and the id2entry index.
192 /* attribute indexes */
193 if ( index_add_entry( be, e ) != 0 ) {
194 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
196 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
202 if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
203 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
205 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
211 if ( id2entry_add( be, e ) != 0 ) {
212 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
214 (void) dn2id_delete( be, dn );
215 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
220 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
224 cache_set_state( &li->li_cache, e, 0 );
227 /* free parent and writer lock */
228 cache_return_entry_w( &li->li_cache, p );
232 /* release root lock */
233 pthread_mutex_unlock(&li->li_root_mutex);
236 /* free entry and writer lock */
237 cache_return_entry_w( &li->li_cache, e );