]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Sync with HEAD
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27
28 int
29 ldbm_back_add(
30     Operation   *op,
31     SlapReply   *rs )
32 {
33         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
34         struct berval   pdn;
35         Entry           *p = NULL;
36         ID               id = NOID;
37         AttributeDescription *children = slap_schema.si_ad_children;
38         AttributeDescription *entry = slap_schema.si_ad_entry;
39         char textbuf[SLAP_TEXT_BUFLEN];
40         size_t textlen = sizeof textbuf;
41 #ifdef LDBM_SUBENTRIES
42         int subentry;
43 #endif
44
45 #ifdef NEW_LOGGING
46         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
47 #else
48         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
49 #endif
50         
51         rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
52
53         if ( rs->sr_err != LDAP_SUCCESS ) {
54 #ifdef NEW_LOGGING
55                 LDAP_LOG( BACK_LDBM, ERR, 
56                         "ldbm_back_add: entry (%s) failed schema check.\n", op->o_req_dn.bv_val, 0, 0 );
57 #else
58                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
59                         rs->sr_text, 0, 0 );
60 #endif
61
62                 send_ldap_result( op, rs );
63                 return rs->sr_err;
64         }
65
66 #ifdef LDBM_SUBENTRIES
67         subentry = is_entry_subentry( op->oq_add.rs_e );
68 #endif
69
70         if ( !access_allowed( op, op->oq_add.rs_e,
71                                 entry, NULL, ACL_WRITE, NULL ) )
72         {
73 #ifdef NEW_LOGGING
74                 LDAP_LOG( BACK_LDBM, ERR, 
75                         "ldbm_back_add: No write access to entry (%s).\n", 
76                         op->o_req_dn.bv_val, 0, 0 );
77 #else
78                 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
79                     0, 0 );
80 #endif
81
82                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
83                     "no write access to entry" );
84
85                 return LDAP_INSUFFICIENT_ACCESS;
86         }
87
88         /* grab giant lock for writing */
89         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
90
91         if ( ( rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id ) ) || id != NOID ) {
92                 /* if (rs->sr_err) something bad happened to ldbm cache */
93                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
94                 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
95                 send_ldap_result( op, rs );
96                 return rs->sr_err;
97         }
98
99         /*
100          * Get the parent dn and see if the corresponding entry exists.
101          * If the parent does not exist, only allow the "root" user to
102          * add the entry.
103          */
104
105         if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
106                 pdn = slap_empty_bv;
107         } else {
108                 dnParent( &op->o_req_ndn, &pdn );
109         }
110
111         if( pdn.bv_len ) {
112                 Entry *matched = NULL;
113
114                 /* get parent with writer lock */
115                 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
116                         if ( matched != NULL ) {
117                                 rs->sr_matched = ch_strdup( matched->e_dn );
118                                 rs->sr_ref = is_entry_referral( matched )
119                                         ? get_entry_referrals( op, matched )
120                                         : NULL;
121                                 cache_return_entry_r( &li->li_cache, matched );
122
123                         } else {
124                                 rs->sr_ref = referral_rewrite( default_referral,
125                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
126                         }
127
128                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
129
130 #ifdef NEW_LOGGING
131                         LDAP_LOG( BACK_LDBM, ERR, 
132                                 "ldbm_back_add: Parent of (%s) does not exist.\n", 
133                                 op->o_req_dn.bv_val, 0, 0 );
134 #else
135                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
136                                 0, 0, 0 );
137 #endif
138
139                         rs->sr_text = rs->sr_ref ? "parent is referral" : "parent does not exist";
140                         rs->sr_err = LDAP_REFERRAL;
141                         send_ldap_result( op, rs );
142
143                         ber_bvarray_free( rs->sr_ref );
144                         free( (char *)rs->sr_matched );
145                         rs->sr_ref = NULL;
146                         rs->sr_matched = NULL;
147                         return rs->sr_err;
148                 }
149
150                 if ( ! access_allowed( op, p,
151                         children, NULL, ACL_WRITE, NULL ) )
152                 {
153                         /* free parent and writer lock */
154                         cache_return_entry_w( &li->li_cache, p ); 
155                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
156
157 #ifdef NEW_LOGGING
158                         LDAP_LOG( BACK_LDBM, ERR, 
159                                 "ldbm_back_add: No write access to parent (%s).\n", 
160                                 op->o_req_dn.bv_val, 0, 0 );
161 #else
162                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
163                             0, 0 );
164 #endif
165
166                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
167                             "no write access to parent" );
168
169                         return LDAP_INSUFFICIENT_ACCESS;
170                 }
171
172 #ifdef LDBM_SUBENTRIES
173                 if ( is_entry_subentry( p )) {
174 #ifdef NEW_LOGGING
175                         LDAP_LOG( OPERATION, DETAIL1,
176                                 "bdb_add: parent is subentry\n", 0, 0, 0 );
177 #else
178                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
179                                 0, 0, 0 );
180 #endif
181                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
182                         rs->sr_text = "parent is a subentry";
183                         goto return_results;
184                 }
185 #endif
186
187                 if ( is_entry_alias( p ) ) {
188                         /* parent is an alias, don't allow add */
189
190                         /* free parent and writer lock */
191                         cache_return_entry_w( &li->li_cache, p );
192                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
193
194 #ifdef NEW_LOGGING
195                         LDAP_LOG(BACK_LDBM, ERR, 
196                                 "ldbm_back_add:  Parent is an alias.\n", 0, 0, 0 );
197 #else
198                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
199                             0, 0 );
200 #endif
201
202                         send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
203                             "parent is an alias" );
204
205                         return LDAP_ALIAS_PROBLEM;
206                 }
207
208                 if ( is_entry_referral( p ) ) {
209                         /* parent is a referral, don't allow add */
210                         rs->sr_matched = ch_strdup( p->e_dn );
211                         rs->sr_ref = is_entry_referral( p )
212                                 ? get_entry_referrals( op, p )
213                                 : NULL;
214
215                         /* free parent and writer lock */
216                         cache_return_entry_w( &li->li_cache, p );
217                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
218
219 #ifdef NEW_LOGGING
220                         LDAP_LOG( BACK_LDBM, ERR,
221                                    "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
222 #else
223                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
224                             0, 0 );
225 #endif
226                         rs->sr_err = LDAP_REFERRAL;
227                         send_ldap_result( op, rs );
228
229                         ber_bvarray_free( rs->sr_ref );
230                         free( (char *)rs->sr_matched );
231                         rs->sr_ref = NULL;
232                         rs->sr_matched = NULL;
233                         return rs->sr_err;
234                 }
235
236 #ifdef LDBM_SUBENTRIES
237                 if ( subentry ) {
238                         /* FIXME: */
239                         /* parent must be an administrative point of the required kind */
240                 }
241 #endif
242
243         } else {
244                 assert( pdn.bv_val == NULL || *pdn.bv_val == '\0' );
245
246                 if ( !be_isroot( op->o_bd, &op->o_ndn )
247                         && !is_entry_glue( op->oq_add.rs_e ))
248                 {
249                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
250
251 #ifdef NEW_LOGGING
252                         LDAP_LOG( BACK_LDBM, ERR,
253                            "ldbm_back_add: %s add denied.\n",
254                            pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
255 #else
256                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
257                                 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
258 #endif
259
260                         send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
261                         return LDAP_NO_SUCH_OBJECT;
262                 }
263         }
264
265         if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
266                 if( p != NULL) {
267                         /* free parent and writer lock */
268                         cache_return_entry_w( &li->li_cache, p ); 
269                 }
270
271                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
272
273 #ifdef NEW_LOGGING
274                 LDAP_LOG( BACK_LDBM, ERR,
275                         "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
276 #else
277                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
278                         0, 0, 0 );
279 #endif
280
281                 send_ldap_error( op, rs, LDAP_OTHER,
282                         "next_id add failed" );
283
284                 return LDAP_OTHER;
285         }
286
287         /*
288          * Try to add the entry to the cache, assign it a new dnid.
289          */
290         rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
291
292         if ( rs->sr_err != 0 ) {
293                 if( p != NULL) {
294                         /* free parent and writer lock */
295                         cache_return_entry_w( &li->li_cache, p ); 
296                 }
297
298                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
299
300 #ifdef NEW_LOGGING
301                 LDAP_LOG( BACK_LDBM, ERR,
302                         "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
303 #else
304                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
305                     0 );
306 #endif
307
308                 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
309                 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
310                 send_ldap_result( op, rs );
311
312                 return rs->sr_err;
313         }
314
315         rs->sr_err = -1;
316
317         /* attribute indexes */
318         if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
319 #ifdef NEW_LOGGING
320                 LDAP_LOG( BACK_LDBM, ERR,
321                         "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
322 #else
323                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
324                     0, 0 );
325 #endif
326                 
327                 send_ldap_error( op, rs, LDAP_OTHER,
328                         "index generation failed" );
329
330                 goto return_results;
331         }
332
333         /* dn2id index */
334         if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
335 #ifdef NEW_LOGGING
336                 LDAP_LOG( BACK_LDBM, ERR,
337                         "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
338 #else
339                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
340                     0, 0 );
341 #endif
342                 /* FIXME: delete attr indices? */
343
344                 send_ldap_error( op, rs, LDAP_OTHER,
345                         "DN index generation failed" );
346
347                 goto return_results;
348         }
349
350         /* id2entry index */
351         if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
352 #ifdef NEW_LOGGING
353                 LDAP_LOG( BACK_LDBM, ERR,
354                            "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
355 #else
356                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
357                     0, 0 );
358 #endif
359
360                 /* FIXME: delete attr indices? */
361                 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
362                 
363                 send_ldap_error( op, rs, LDAP_OTHER,
364                         "entry store failed" );
365
366                 goto return_results;
367         }
368
369         rs->sr_err = LDAP_SUCCESS;
370         rs->sr_text = NULL;
371         send_ldap_result( op, rs );
372
373         /* marks the entry as committed, so it is added to the cache;
374          * otherwise it is removed from the cache, but not destroyed;
375          * it will be destroyed by the caller */
376         cache_entry_commit( op->oq_add.rs_e );
377
378 return_results:;
379         if (p != NULL) {
380                 /* free parent and writer lock */
381                 cache_return_entry_w( &li->li_cache, p ); 
382         }
383
384         if ( rs->sr_err ) {
385                 /*
386                  * in case of error, writer lock is freed 
387                  * and entry's private data is destroyed.
388                  * otherwise, this is done when entry is released
389                  */
390                 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
391                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
392         }
393
394         return( rs->sr_err );
395 }