]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Import minor trace output cleanup
[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            *dn = NULL, *pdn;
24         Entry           *p = NULL;
25         int                     rootlock = 0;
26         int                     rc = -1; 
27
28         dn = dn_normalize( ch_strdup( e->e_dn ) );
29
30         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
31
32         /* nobody else can add until we lock our parent */
33         pthread_mutex_lock(&li->li_add_mutex);
34
35         if ( ( dn2id( be, dn ) ) != NOID ) {
36                 pthread_mutex_unlock(&li->li_add_mutex);
37                 entry_free( e );
38                 free( dn );
39                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
40                 return( -1 );
41         }
42
43         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
44                 pthread_mutex_unlock(&li->li_add_mutex);
45
46                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
47                         0, 0, 0 );
48
49                 entry_free( e );
50                 free( dn );
51                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
52                     "" );
53                 return( -1 );
54         }
55
56         /*
57          * Get the parent dn and see if the corresponding entry exists.
58          * If the parent does not exist, only allow the "root" user to
59          * add the entry.
60          */
61
62         if ( (pdn = dn_parent( be, dn )) != NULL ) {
63                 char *matched = NULL;
64
65                 /* get parent with writer lock */
66                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
67                         pthread_mutex_unlock(&li->li_add_mutex);
68                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
69                             0, 0 );
70                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
71                             matched, "" );
72
73                         if ( matched != NULL ) {
74                                 free( matched );
75                         }
76
77                         entry_free( e );
78                         free( dn );
79                         free( pdn );
80                         return -1;
81                 }
82
83                 /* don't need the add lock anymore */
84                 pthread_mutex_unlock(&li->li_add_mutex);
85
86                 free(pdn);
87
88                 if ( matched != NULL ) {
89                         free( matched );
90                 }
91
92                 if ( ! access_allowed( be, conn, op, p, "children", NULL,
93                     op->o_dn, ACL_WRITE ) )
94                 {
95                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
96                             0, 0 );
97                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
98                             "", "" );
99
100                         /* free parent and writer lock */
101                         cache_return_entry_w( &li->li_cache, p ); 
102
103                         entry_free( e );
104                         free( dn );
105                         return -1;
106                 }
107
108         } else {
109                 /* no parent, must be adding entry to root */
110                 if ( ! be_isroot( be, op->o_dn ) ) {
111                         pthread_mutex_unlock(&li->li_add_mutex);
112                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
113                             0, 0 );
114                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
115                             "", "" );
116
117                         entry_free( e );
118                         free( dn );
119                         return -1;
120                 }
121
122                 /*
123                  * no parent, acquire the root write lock
124                  * and release the add lock.
125                  */
126                 pthread_mutex_lock(&li->li_root_mutex);
127                 pthread_mutex_unlock(&li->li_add_mutex);
128                 rootlock=1;
129         }
130
131         /*
132          * Try to add the entry to the cache, assign it a new dnid
133          * and mark it locked.  This should only fail if the entry
134          * already exists.
135          */
136
137         e->e_id = next_id( be );
138         if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING ) != 0 ) {
139                 if( p != NULL) {
140                         /* free parent and writer lock */
141                         cache_return_entry_w( &li->li_cache, p ); 
142                 } else if ( rootlock ) {
143                         /* release root lock */
144                         pthread_mutex_unlock(&li->li_root_mutex);
145                 }
146
147                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
148                     0 );
149                 next_id_return( be, e->e_id );
150                 
151                 /* XXX this should be ok, no other thread should have access
152                  * because e hasn't been added to the cache yet
153                  */
154                 entry_free( e );
155                 free( dn );
156                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
157                 return( -1 );
158         }
159
160         /* acquire writer lock */
161         entry_rdwr_lock(e, 1);
162
163         /*
164          * add it to the id2children index for the parent
165          */
166
167         if ( id2children_add( be, p, e ) != 0 ) {
168                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
169                     0, 0 );
170                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
171
172                 goto return_results;
173         }
174
175         /*
176          * Add the entry to the attribute indexes, then add it to
177          * the id2children index, dn2id index, and the id2entry index.
178          */
179
180         /* attribute indexes */
181         if ( index_add_entry( be, e ) != 0 ) {
182                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
183                     0, 0 );
184                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
185
186                 goto return_results;
187         }
188
189         /* dn2id index */
190         if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
191                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
192                     0, 0 );
193                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
194
195                 goto return_results;
196         }
197
198         /* id2entry index */
199         if ( id2entry_add( be, e ) != 0 ) {
200                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
201                     0, 0 );
202                 (void) dn2id_delete( be, dn );
203                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
204
205                 goto return_results;
206         }
207
208         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
209         rc = 0;
210
211 return_results:;
212         if ( dn != NULL )
213                 free( dn );
214
215         cache_set_state( &li->li_cache, e, 0 );
216
217         if (p != NULL) {
218                 /* free parent and writer lock */
219                 cache_return_entry_w( &li->li_cache, p ); 
220
221         } else if ( rootlock ) {
222                 /* release root lock */
223                 pthread_mutex_unlock(&li->li_root_mutex);
224         }
225
226         /* free entry and writer lock */
227         cache_return_entry_w( &li->li_cache, e ); 
228
229         return( rc );
230 }