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;
28 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
30 /* nobody else can add until we lock our parent */
31 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
33 if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
34 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
36 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
40 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
41 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
43 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
47 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
53 * Get the parent dn and see if the corresponding entry exists.
54 * If the parent does not exist, only allow the "root" user to
58 pdn = dn_parent( be, e->e_ndn );
60 if( pdn != NULL && *pdn != '\0' && !be_issuffix(be, "") ) {
63 assert( *pdn != '\0' );
65 /* get parent with writer lock */
66 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
67 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
68 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
70 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
73 if ( matched != NULL ) {
82 /* don't need the add lock anymore */
83 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
87 if ( matched != NULL ) {
91 if ( ! access_allowed( be, conn, op, p,
92 "children", NULL, ACL_WRITE ) )
94 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
96 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
99 /* free parent and writer lock */
100 cache_return_entry_w( &li->li_cache, p );
108 assert( *pdn == '\0' );
112 /* no parent, must be adding entry to root */
113 if ( ! be_isroot( be, op->o_ndn ) ) {
114 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
116 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
117 pdn == NULL ? "suffix" : "entry at root",
120 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
128 * no parent, acquire the root write lock
129 * and release the add lock.
131 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
133 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
136 e->e_id = next_id( be );
139 * Try to add the entry to the cache, assign it a new dnid.
141 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
145 /* free parent and writer lock */
146 cache_return_entry_w( &li->li_cache, p );
150 /* release root lock */
151 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
154 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
158 next_id_return( be, e->e_id );
164 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
166 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
175 * add it to the id2children index for the parent
178 if ( id2children_add( be, p, e ) != 0 ) {
179 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
181 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
187 * Add the entry to the attribute indexes, then add it to
188 * the id2children index, dn2id index, and the id2entry index.
191 /* attribute indexes */
192 if ( index_add_entry( be, e ) != 0 ) {
193 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
195 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
201 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
202 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
204 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
210 if ( id2entry_add( be, e ) != 0 ) {
211 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
213 (void) dn2id_delete( be, e->e_ndn );
214 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
219 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
224 /* free parent and writer lock */
225 cache_return_entry_w( &li->li_cache, p );
229 /* release root lock */
230 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
234 /* free entry and writer lock */
235 cache_return_entry_w( &li->li_cache, e );