]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Code clean-up.
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 int
15 ldbm_back_add(
16     Backend     *be,
17     Connection  *conn,
18     Operation   *op,
19     Entry       *e
20 )
21 {
22         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23         char            *pdn;
24         Entry           *p = NULL;
25         int                     rootlock = 0;
26         int                     rc; 
27
28         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", e->e_dn, 0, 0);
29
30         /* nobody else can add until we lock our parent */
31         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
32
33         if ( ( dn2id( be, e->e_ndn ) ) != NOID ) {
34                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
35                 entry_free( e );
36                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
37                 return( -1 );
38         }
39
40         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
41                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
42
43                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
44                         0, 0, 0 );
45
46                 entry_free( e );
47                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
48                     "" );
49                 return( -1 );
50         }
51
52         /*
53          * Get the parent dn and see if the corresponding entry exists.
54          * If the parent does not exist, only allow the "root" user to
55          * add the entry.
56          */
57
58         if ( (pdn = dn_parent( be, e->e_ndn )) != NULL ) {
59                 char *matched = NULL;
60
61                 /* get parent with writer lock */
62                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
63                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
64                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
65                             0, 0 );
66                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
67                             matched, "" );
68
69                         if ( matched != NULL ) {
70                                 free( matched );
71                         }
72
73                         entry_free( e );
74                         free( pdn );
75                         return -1;
76                 }
77
78                 /* don't need the add lock anymore */
79                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
80
81                 free(pdn);
82
83                 if ( matched != NULL ) {
84                         free( matched );
85                 }
86
87                 if ( ! access_allowed( be, conn, op, p,
88                         "children", NULL, ACL_WRITE ) )
89                 {
90                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
91                             0, 0 );
92                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
93                             "", "" );
94
95                         /* free parent and writer lock */
96                         cache_return_entry_w( &li->li_cache, p ); 
97
98                         entry_free( e );
99                         return -1;
100                 }
101
102         } else {
103                 /* no parent, must be adding entry to root */
104                 if ( ! be_isroot( be, op->o_ndn ) ) {
105                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
106                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
107                             0, 0 );
108                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
109                             "", "" );
110
111                         entry_free( e );
112                         return -1;
113                 }
114
115                 /*
116                  * no parent, acquire the root write lock
117                  * and release the add lock.
118                  */
119                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
120                 rootlock = 1;
121                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
122         }
123
124         e->e_id = next_id( be );
125
126         /*
127          * Try to add the entry to the cache, assign it a new dnid.
128          */
129         rc = cache_add_entry_rw(&li->li_cache, e, CACHE_WRITE_LOCK);
130
131         if ( rc != 0 ) {
132                 if( p != NULL) {
133                         /* free parent and writer lock */
134                         cache_return_entry_w( &li->li_cache, p ); 
135                 }
136
137                 if ( rootlock ) {
138                         /* release root lock */
139                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
140                 }
141
142                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
143                     0 );
144
145                 /* return the id */
146                 next_id_return( be, e->e_id );
147
148                 /* free the entry */
149                 entry_free( e );
150
151                 if(rc > 0) {
152                         send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
153                 } else {
154                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
155                 }
156
157                 return( -1 );
158         }
159
160         rc = -1;
161
162         /*
163          * add it to the id2children index for the parent
164          */
165
166         if ( id2children_add( be, p, e ) != 0 ) {
167                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
168                     0, 0 );
169                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
170
171                 goto return_results;
172         }
173
174         /*
175          * Add the entry to the attribute indexes, then add it to
176          * the id2children index, dn2id index, and the id2entry index.
177          */
178
179         /* attribute indexes */
180         if ( index_add_entry( be, e ) != 0 ) {
181                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
182                     0, 0 );
183                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
184
185                 goto return_results;
186         }
187
188         /* dn2id index */
189         if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
190                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
191                     0, 0 );
192                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
193
194                 goto return_results;
195         }
196
197         /* id2entry index */
198         if ( id2entry_add( be, e ) != 0 ) {
199                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
200                     0, 0 );
201                 (void) dn2id_delete( be, e->e_ndn );
202                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
203
204                 goto return_results;
205         }
206
207         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
208         rc = 0;
209
210 return_results:;
211         if (p != NULL) {
212                 /* free parent and writer lock */
213                 cache_return_entry_w( &li->li_cache, p ); 
214         }
215
216         if ( rootlock ) {
217                 /* release root lock */
218                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
219         }
220
221         /* free entry and writer lock */
222         cache_return_entry_w( &li->li_cache, e ); 
223
224         return( rc );
225 }