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;
33 #ifdef SLAPD_SCHEMA_NOT_COMPAT
34 static AttributeDescription *children = NULL;
36 static const char *children = "children";
40 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
42 /* nobody else can add until we lock our parent */
43 ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
45 if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
46 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
47 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
48 NULL, NULL, NULL, NULL );
52 rc = entry_schema_check( e, NULL, &text );
54 if ( rc != LDAP_SUCCESS ) {
55 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
57 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
60 send_ldap_result( conn, op, rc,
61 NULL, text, NULL, NULL );
66 * Get the parent dn and see if the corresponding entry exists.
67 * If the parent does not exist, only allow the "root" user to
71 pdn = dn_parent( be, e->e_ndn );
73 if( pdn != NULL && *pdn != '\0' ) {
74 Entry *matched = NULL;
76 assert( *pdn != '\0' );
78 /* get parent with writer lock */
79 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
83 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
85 if ( matched != NULL ) {
86 matched_dn = ch_strdup( matched->e_dn );
87 refs = is_entry_referral( matched )
88 ? get_entry_referrals( be, conn, op, matched )
90 cache_return_entry_r( &li->li_cache, matched );
94 refs = default_referral;
97 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
100 send_ldap_result( conn, op, LDAP_REFERRAL,
101 matched_dn, NULL, refs, NULL );
103 if( matched != NULL ) {
104 ber_bvecfree( refs );
112 /* don't need the add lock anymore */
113 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
117 if ( ! access_allowed( be, conn, op, p,
118 children, NULL, ACL_WRITE ) )
120 /* free parent and writer lock */
121 cache_return_entry_w( &li->li_cache, p );
123 Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
125 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
126 NULL, "no write access to parent", NULL, NULL );
132 if ( is_entry_alias( p ) ) {
133 /* parent is an alias, don't allow add */
135 /* free parent and writer lock */
136 cache_return_entry_w( &li->li_cache, p );
138 Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
141 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
142 NULL, "parent is an alias", NULL, NULL );
147 if ( is_entry_referral( p ) ) {
148 /* parent is a referral, don't allow add */
149 char *matched_dn = ch_strdup( p->e_dn );
150 struct berval **refs = is_entry_referral( p )
151 ? get_entry_referrals( be, conn, op, p )
154 /* free parent and writer lock */
155 cache_return_entry_w( &li->li_cache, p );
157 Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
159 send_ldap_result( conn, op, LDAP_REFERRAL,
160 matched_dn, NULL, refs, NULL );
162 ber_bvecfree( refs );
169 assert( *pdn == '\0' );
173 /* no parent, must be adding entry to root */
174 if ( !be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
175 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
177 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
178 pdn == NULL ? "suffix" : "entry at root",
181 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
182 NULL, NULL, NULL, NULL );
188 * no parent, acquire the root write lock
189 * and release the add lock.
191 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
193 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
196 e->e_id = next_id( be );
199 * Try to add the entry to the cache, assign it a new dnid.
201 rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
205 /* free parent and writer lock */
206 cache_return_entry_w( &li->li_cache, p );
210 /* release root lock */
211 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
214 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
217 send_ldap_result( conn, op,
218 rc > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER,
219 NULL, rc > 0 ? NULL : "cache add failed", NULL, NULL );
227 * Add the entry to the attribute indexes, then add it to
228 * the id2children index, dn2id index, and the id2entry index.
231 /* attribute indexes */
232 if ( index_add_entry( be, e ) != 0 ) {
233 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
235 send_ldap_result( conn, op, LDAP_OTHER,
236 NULL, "index generation failed", NULL, NULL );
242 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
243 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
245 send_ldap_result( conn, op, LDAP_OTHER,
246 NULL, "DN index generation failed", NULL, NULL );
252 if ( id2entry_add( be, e ) != 0 ) {
253 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
255 (void) dn2id_delete( be, e->e_ndn, e->e_id );
256 send_ldap_result( conn, op, LDAP_OTHER,
257 NULL, "entry store failed", NULL, NULL );
262 send_ldap_result( conn, op, LDAP_SUCCESS,
263 NULL, NULL, NULL, NULL );
268 /* free parent and writer lock */
269 cache_return_entry_w( &li->li_cache, p );
273 /* release root lock */
274 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
278 /* free entry and writer lock */
279 cache_return_entry_w( &li->li_cache, e );