]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Stuart Lynne's UNTESTED thread patch (p1)
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include "slap.h"
8 #include "back-ldbm.h"
9 #include "proto-back-ldbm.h"
10
11 extern int      global_schemacheck;
12 extern char     *dn_parent();
13 extern char     *dn_normalize();
14
15 int
16 ldbm_back_add(
17     Backend     *be,
18     Connection  *conn,
19     Operation   *op,
20     Entry       *e
21 )
22 {
23         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
24         char            *matched;
25         char            *dn = NULL, *pdn = NULL;
26         Entry           *p;
27
28         dn = dn_normalize( strdup( e->e_dn ) );
29         matched = NULL;
30         if ( (p = dn2entry_r( be, dn, &matched )) != NULL ) {
31                 cache_return_entry_r( &li->li_cache, p );
32                 entry_free( e );
33                 free( dn );
34                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
35                 return( -1 );
36         }
37         if ( matched != NULL ) {
38                 free( matched );
39         }
40         /* XXX race condition here til we cache_add_entry_lock below XXX */
41
42
43         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
44                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n", 0, 0,
45                     0 );
46                 /* release writer lock */
47                 pthread_rdwr_wunlock_np(&e->e_rdwr);
48                 
49                 /* XXX this should be ok, no other thread should have access
50                  * because e hasn't been added to the cache yet
51                  */
52                 entry_free( e );
53                 free( dn );
54                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
55                     "" );
56                 return( -1 );
57         }
58
59         /*
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
62          * already exists.
63          */
64
65         e->e_id = next_id( be );
66         if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
67             != 0 ) {
68                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
69                     0 );
70                 next_id_return( be, e->e_id );
71                 /* release writer lock */
72                 pthread_rdwr_wunlock_np(&e->e_rdwr);
73                 
74                 /* XXX this should be ok, no other thread should have access
75                  * because e hasn't been added to the cache yet
76                  */
77                 entry_free( e );
78                 free( dn );
79                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
80                 return( -1 );
81         }
82
83         /*
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
86          * add the entry.
87          */
88
89         if ( (pdn = dn_parent( be, dn )) != NULL ) {
90                 /* no parent */
91                 matched = NULL;
92                 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
93                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
94                             matched, "" );
95                         if ( matched != NULL ) {
96                                 free( matched );
97                         }
98                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
99                             0, 0 );
100                         goto error_return;
101                 }
102                 if ( matched != NULL ) {
103                         free( matched );
104                 }
105
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,
109                             0, 0 );
110                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
111                             "", "" );
112                         goto error_return;
113                 }
114         } else {
115                 if ( ! be_isroot( be, op->o_dn ) ) {
116                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
117                             0, 0 );
118                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
119                             "", "" );
120                         goto error_return;
121                 }
122                 p = NULL;
123         }
124
125         /*
126          * add it to the id2children index for the parent
127          */
128
129         if ( id2children_add( be, p, e ) != 0 ) {
130                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
131                     0, 0 );
132                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
133                 goto error_return;
134         }
135
136         /*
137          * Add the entry to the attribute indexes, then add it to
138          * the id2children index, dn2id index, and the id2entry index.
139          */
140
141         /* attribute indexes */
142         if ( index_add_entry( be, e ) != 0 ) {
143                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
144                     0, 0 );
145                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
146                 goto error_return;
147         }
148
149         /* dn2id index */
150         if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
151                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
152                     0, 0 );
153                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
154                 goto error_return;
155         }
156
157         /* id2entry index */
158         if ( id2entry_add( be, e ) != 0 ) {
159                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
160                     0, 0 );
161                 (void) dn2id_delete( be, dn );
162                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
163                 goto error_return;
164         }
165
166         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
167         if ( dn != NULL )
168                 free( dn );
169         if ( pdn != NULL )
170                 free( pdn );
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 );
174         return( 0 );
175
176 error_return:;
177         /* release writer lock */
178         pthread_rdwr_wunlock_np(&e->e_rdwr);
179
180         if ( dn != NULL )
181                 free( dn );
182         if ( pdn != NULL )
183                 free( pdn );
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 );
187
188         return( -1 );
189 }