1 /* add.c - ldap ldbm back-end add routine */
4 * Copyright 1998-2000 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;
32 const char *text = NULL;
33 AttributeDescription *children = slap_schema.si_ad_children;
36 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
38 /* nobody else can add until we lock our parent */
39 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
41 if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
42 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
43 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
44 NULL, NULL, NULL, NULL );
48 rc = entry_schema_check( e, NULL, &text );
50 if ( rc != LDAP_SUCCESS ) {
51 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
53 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
56 send_ldap_result( conn, op, rc,
57 NULL, text, NULL, NULL );
62 * Get the parent dn and see if the corresponding entry exists.
63 * If the parent does not exist, only allow the "root" user to
67 pdn = dn_parent( be, e->e_ndn );
69 if( pdn != NULL && *pdn != '\0' ) {
70 Entry *matched = NULL;
72 assert( *pdn != '\0' );
74 /* get parent with writer lock */
75 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
79 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
81 if ( matched != NULL ) {
82 matched_dn = ch_strdup( matched->e_dn );
83 refs = is_entry_referral( matched )
84 ? get_entry_referrals( be, conn, op, matched )
86 cache_return_entry_r( &li->li_cache, matched );
90 refs = default_referral;
93 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
96 send_ldap_result( conn, op, LDAP_REFERRAL,
97 matched_dn, NULL, refs, NULL );
99 if( matched != NULL ) {
100 ber_bvecfree( refs );
108 /* don't need the add lock anymore */
109 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
113 if ( ! access_allowed( be, conn, op, p,
114 children, NULL, ACL_WRITE ) )
116 /* free parent and writer lock */
117 cache_return_entry_w( &li->li_cache, p );
119 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
121 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
122 NULL, "no write access to parent", NULL, NULL );
128 if ( is_entry_alias( p ) ) {
129 /* parent is an alias, don't allow add */
131 /* free parent and writer lock */
132 cache_return_entry_w( &li->li_cache, p );
134 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
137 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
138 NULL, "parent is an alias", 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 );
165 assert( *pdn == '\0' );
169 /* no parent, must be adding entry to root */
170 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
171 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
173 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
174 pdn == NULL ? "suffix" : "entry at root",
177 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
178 NULL, NULL, NULL, NULL );
184 * no parent, acquire the root write lock
185 * and release the add lock.
187 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
189 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
192 e->e_id = next_id( be );
195 * Try to add the entry to the cache, assign it a new dnid.
197 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
201 /* free parent and writer lock */
202 cache_return_entry_w( &li->li_cache, p );
206 /* release root lock */
207 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
210 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
213 send_ldap_result( conn, op,
214 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
215 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
222 /* attribute indexes */
223 if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
224 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
226 send_ldap_result( conn, op, LDAP_OTHER,
227 NULL, "index generation failed", NULL, NULL );
233 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
234 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
236 send_ldap_result( conn, op, LDAP_OTHER,
237 NULL, "DN index generation failed", NULL, NULL );
243 if ( id2entry_add( be, e ) != 0 ) {
244 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
246 (void) dn2id_delete( be, e->e_ndn, e->e_id );
247 send_ldap_result( conn, op, LDAP_OTHER,
248 NULL, "entry store failed", NULL, NULL );
253 send_ldap_result( conn, op, LDAP_SUCCESS,
254 NULL, NULL, NULL, NULL );
259 /* free parent and writer lock */
260 cache_return_entry_w( &li->li_cache, p );
264 /* release root lock */
265 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
269 /* free entry and writer lock */
270 cache_return_entry_w( &li->li_cache, e );