1 /* add.c - ldap ldbm back-end add routine */
6 #include <sys/socket.h>
9 #include "proto-back-ldbm.h"
11 extern int global_schemacheck;
12 extern char *dn_parent();
13 extern char *dn_normalize();
23 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25 char *dn = NULL, *pdn = NULL;
28 dn = dn_normalize( strdup( e->e_dn ) );
30 if ( (p = dn2entry_r( be, dn, &matched )) != NULL ) {
31 cache_return_entry_r( &li->li_cache, p );
34 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
37 if ( matched != NULL ) {
40 /* 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", 0, 0,
46 /* release writer lock */
47 pthread_rdwr_wunlock_np(&e->e_rdwr);
49 /* XXX this should be ok, no other thread should have access
50 * because e hasn't been added to the cache yet
54 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
60 * Try to add the entry to the cache, assign it a new dnid
61 * and mark it locked. This should only fail if the entry
65 e->e_id = next_id( be );
66 if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
68 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
70 next_id_return( be, e->e_id );
71 /* release writer lock */
72 pthread_rdwr_wunlock_np(&e->e_rdwr);
74 /* XXX this should be ok, no other thread should have access
75 * because e hasn't been added to the cache yet
79 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
84 * Get the parent dn and see if the corresponding entry exists.
85 * If the parent does not exist, only allow the "root" user to
89 if ( (pdn = dn_parent( be, dn )) != NULL ) {
92 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
93 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
95 if ( matched != NULL ) {
98 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
102 if ( matched != NULL ) {
106 if ( ! access_allowed( be, conn, op, p, "children", NULL,
107 op->o_dn, ACL_WRITE ) ) {
108 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
110 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
115 if ( ! be_isroot( be, op->o_dn ) ) {
116 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
118 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
126 * add it to the id2children index for the parent
129 if ( id2children_add( be, p, e ) != 0 ) {
130 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
132 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
137 * Add the entry to the attribute indexes, then add it to
138 * the id2children index, dn2id index, and the id2entry index.
141 /* attribute indexes */
142 if ( index_add_entry( be, e ) != 0 ) {
143 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
145 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
150 if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
151 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
153 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
158 if ( id2entry_add( be, e ) != 0 ) {
159 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
161 (void) dn2id_delete( be, dn );
162 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
166 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
171 cache_set_state( &li->li_cache, e, 0 );
172 cache_return_entry_w( &li->li_cache, e );
173 cache_return_entry_r( &li->li_cache, p );
177 /* release writer lock */
178 pthread_rdwr_wunlock_np(&e->e_rdwr);
184 next_id_return( be, e->e_id );
185 cache_delete_entry( &li->li_cache, e );
186 cache_return_entry_r( &li->li_cache, p );