1 /* add.c - ldap ldbm back-end add routine */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
27 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
33 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
35 /* nobody else can add until we lock our parent */
36 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
38 if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
39 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
41 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
42 NULL, NULL, NULL, NULL );
46 if ( schema_check_entry( e ) != 0 ) {
47 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
49 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
53 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
54 NULL, NULL, NULL, NULL );
59 * Get the parent dn and see if the corresponding entry exists.
60 * If the parent does not exist, only allow the "root" user to
64 pdn = dn_parent( be, e->e_ndn );
66 if( pdn != NULL && *pdn != '\0' ) {
67 Entry *matched = NULL;
69 assert( *pdn != '\0' );
71 /* get parent with writer lock */
72 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
76 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
78 if ( matched != NULL ) {
79 matched_dn = ch_strdup( matched->e_dn );
80 refs = is_entry_referral( matched )
81 ? get_entry_referrals( be, conn, op, matched )
83 cache_return_entry_r( &li->li_cache, matched );
87 refs = default_referral;
90 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
93 send_ldap_result( conn, op, LDAP_REFERRAL,
94 matched_dn, NULL, refs, NULL );
96 if( matched != NULL ) {
106 /* don't need the add lock anymore */
107 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
111 if ( ! access_allowed( be, conn, op, p,
112 "children", NULL, ACL_WRITE ) )
114 /* free parent and writer lock */
115 cache_return_entry_w( &li->li_cache, p );
117 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
119 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
120 NULL, NULL, NULL, NULL );
127 if ( is_entry_alias( p ) ) {
128 /* parent is an alias, don't allow add */
130 /* free parent and writer lock */
131 cache_return_entry_w( &li->li_cache, p );
133 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
136 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
137 NULL, NULL, NULL, NULL );
143 if ( is_entry_referral( p ) ) {
144 /* parent is a referral, don't allow add */
145 char *matched_dn = ch_strdup( p->e_dn );
146 struct berval **refs = is_entry_referral( p )
147 ? get_entry_referrals( be, conn, op, p )
150 /* free parent and writer lock */
151 cache_return_entry_w( &li->li_cache, p );
153 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
155 send_ldap_result( conn, op, LDAP_REFERRAL,
156 matched_dn, NULL, refs, NULL );
158 ber_bvecfree( refs );
166 assert( *pdn == '\0' );
170 /* no parent, must be adding entry to root */
171 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
172 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
174 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
175 pdn == NULL ? "suffix" : "entry at root",
178 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
179 NULL, NULL, NULL, NULL );
186 * no parent, acquire the root write lock
187 * and release the add lock.
189 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
191 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
194 e->e_id = next_id( be );
197 * Try to add the entry to the cache, assign it a new dnid.
199 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
203 /* free parent and writer lock */
204 cache_return_entry_w( &li->li_cache, p );
208 /* release root lock */
209 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
212 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
218 send_ldap_result( conn, op,
219 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OPERATIONS_ERROR,
220 NULL, NULL, NULL, NULL );
228 * Add the entry to the attribute indexes, then add it to
229 * the id2children index, dn2id index, and the id2entry index.
232 /* attribute indexes */
233 if ( index_add_entry( be, e ) != 0 ) {
234 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
236 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
237 NULL, NULL, NULL, NULL );
243 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
244 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
246 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
247 NULL, NULL, NULL, NULL );
253 if ( id2entry_add( be, e ) != 0 ) {
254 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
256 (void) dn2id_delete( be, e->e_ndn, e->e_id );
257 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
258 NULL, NULL, NULL, NULL );
263 send_ldap_result( conn, op, LDAP_SUCCESS,
264 NULL, NULL, NULL, NULL );
269 /* free parent and writer lock */
270 cache_return_entry_w( &li->li_cache, p );
274 /* release root lock */
275 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
279 /* free entry and writer lock */
280 cache_return_entry_w( &li->li_cache, e );