1 /* add.c - ldap ldbm back-end add routine */
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
14 extern int global_schemacheck;
15 extern char *dn_parent();
16 extern char *dn_normalize();
26 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
27 char *dn = NULL, *pdn = NULL;
31 dn = dn_normalize( strdup( e->e_dn ) );
33 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
35 if ( ( dn2id( be, dn ) ) != NOID ) {
38 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
42 /* XXX race condition here til we cache_add_entry_lock below XXX */
44 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
45 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
48 /* XXX this should be ok, no other thread should have access
49 * because e hasn't been added to the cache yet
53 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
59 * Try to add the entry to the cache, assign it a new dnid
60 * and mark it locked. This should only fail if the entry
64 e->e_id = next_id( be );
65 if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
67 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
69 next_id_return( be, e->e_id );
71 /* XXX this should be ok, no other thread should have access
72 * because e hasn't been added to the cache yet
76 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
81 * Get the parent dn and see if the corresponding entry exists.
82 * If the parent does not exist, only allow the "root" user to
86 if ( (pdn = dn_parent( be, dn )) != NULL ) {
91 /* get entry with reader lock */
92 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
93 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
95 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
97 if ( matched != NULL ) {
104 if ( matched != NULL ) {
108 if ( ! access_allowed( be, conn, op, p, "children", NULL,
109 op->o_dn, ACL_WRITE ) ) {
110 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
112 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
119 if ( ! be_isroot( be, op->o_dn ) ) {
120 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
122 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
131 * add it to the id2children index for the parent
134 if ( id2children_add( be, p, e ) != 0 ) {
135 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
137 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
144 * Add the entry to the attribute indexes, then add it to
145 * the id2children index, dn2id index, and the id2entry index.
148 /* attribute indexes */
149 if ( index_add_entry( be, e ) != 0 ) {
150 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
152 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
159 if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
160 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
162 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
168 /* acquire writer lock */
169 entry_rdwr_lock(e, 1);
172 if ( id2entry_add( be, e ) != 0 ) {
173 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
175 (void) dn2id_delete( be, dn );
176 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
182 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
192 cache_set_state( &li->li_cache, e, 0 );
194 /* free entry and writer lock */
195 cache_return_entry_w( &li->li_cache, e );
197 /* free entry and reader lock */
199 cache_return_entry_r( &li->li_cache, p );