1 /* add.c - ldap ldbm back-end add routine */
8 #include <sys/socket.h>
10 #include "back-ldbm.h"
11 #include "proto-back-ldbm.h"
13 extern int global_schemacheck;
14 extern char *dn_parent();
15 extern char *dn_normalize();
25 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
26 char *dn = NULL, *pdn = NULL;
30 dn = dn_normalize( strdup( e->e_dn ) );
32 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
34 if ( ( dn2id( be, dn ) ) != NOID ) {
37 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
41 /* XXX race condition here til we cache_add_entry_lock below XXX */
43 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
44 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
47 /* XXX this should be ok, no other thread should have access
48 * because e hasn't been added to the cache yet
52 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
58 * Try to add the entry to the cache, assign it a new dnid
59 * and mark it locked. This should only fail if the entry
63 e->e_id = next_id( be );
64 if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
66 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
68 next_id_return( be, e->e_id );
70 /* XXX this should be ok, no other thread should have access
71 * because e hasn't been added to the cache yet
75 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
80 * Get the parent dn and see if the corresponding entry exists.
81 * If the parent does not exist, only allow the "root" user to
85 if ( (pdn = dn_parent( be, dn )) != NULL ) {
90 /* get entry with reader lock */
91 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
92 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
94 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
96 if ( matched != NULL ) {
103 if ( matched != NULL ) {
107 if ( ! access_allowed( be, conn, op, p, "children", NULL,
108 op->o_dn, ACL_WRITE ) ) {
109 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
111 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
118 if ( ! be_isroot( be, op->o_dn ) ) {
119 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
121 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
130 * add it to the id2children index for the parent
133 if ( id2children_add( be, p, e ) != 0 ) {
134 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
136 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
143 * Add the entry to the attribute indexes, then add it to
144 * the id2children index, dn2id index, and the id2entry index.
147 /* attribute indexes */
148 if ( index_add_entry( be, e ) != 0 ) {
149 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
151 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
158 if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
159 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
161 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
167 /* acquire writer lock */
168 entry_rdwr_lock(e, 1);
171 if ( id2entry_add( be, e ) != 0 ) {
172 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
174 (void) dn2id_delete( be, dn );
175 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
181 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
191 cache_set_state( &li->li_cache, e, 0 );
193 /* free entry and writer lock */
194 cache_return_entry_w( &li->li_cache, e );
196 /* free entry and reader lock */
198 cache_return_entry_r( &li->li_cache, p );