]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Sync with HEAD
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2006 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 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 int
25 bdb_add(Operation *op, SlapReply *rs )
26 {
27         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL;
30         EntryInfo       *ei;
31         char textbuf[SLAP_TEXT_BUFLEN];
32         size_t textlen = sizeof textbuf;
33         AttributeDescription *children = slap_schema.si_ad_children;
34         AttributeDescription *entry = slap_schema.si_ad_entry;
35         DB_TXN          *ltid = NULL, *lt2;
36         struct bdb_op_info opinfo = {0};
37         int subentry;
38         u_int32_t       locker = 0;
39         DB_LOCK         lock;
40
41         int             num_retries = 0;
42
43         LDAPControl **postread_ctrl = NULL;
44         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
45         int num_ctrls = 0;
46
47 #ifdef LDAP_X_TXN
48         int settle = 0;
49 #endif
50
51         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
52                 op->oq_add.rs_e->e_name.bv_val, 0, 0);
53
54 #ifdef LDAP_X_TXN
55         if( op->o_txnSpec ) {
56                 /* acquire connection lock */
57                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
58                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
59                         rs->sr_text = "invalid transaction identifier";
60                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
61                         goto txnReturn;
62                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
63                         settle=1;
64                         goto txnReturn;
65                 }
66
67                 if( op->o_conn->c_txn_backend == NULL ) {
68                         op->o_conn->c_txn_backend = op->o_bd;
69
70                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
71                         rs->sr_text = "transaction cannot span multiple database contexts";
72                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
73                         goto txnReturn;
74                 }
75
76                 /* insert operation into transaction */
77
78                 rs->sr_text = "transaction specified";
79                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
80
81 txnReturn:
82                 /* release connection lock */
83                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
84
85                 if( !settle ) {
86                         send_ldap_result( op, rs );
87                         return rs->sr_err;
88                 }
89         }
90 #endif
91
92         ctrls[num_ctrls] = 0;
93
94         /* add opattrs to shadow as well, only missing attrs will actually
95          * be added; helps compatibility with older OL versions */
96         slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
97
98         /* check entry's schema */
99         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
100                 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
101         if ( rs->sr_err != LDAP_SUCCESS ) {
102                 Debug( LDAP_DEBUG_TRACE,
103                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
104                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
105                 goto return_results;
106         }
107
108         subentry = is_entry_subentry( op->oq_add.rs_e );
109
110         /*
111          * acquire an ID outside of the operation transaction
112          * to avoid serializing adds.
113          */
114         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
115         if( rs->sr_err != 0 ) {
116                 Debug( LDAP_DEBUG_TRACE,
117                         LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
118                         rs->sr_err, 0, 0 );
119                 rs->sr_err = LDAP_OTHER;
120                 rs->sr_text = "internal error";
121                 goto return_results;
122         }
123
124         if( 0 ) {
125 retry:  /* transaction retry */
126                 if( p ) {
127                         /* free parent and reader lock */
128                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
129                         p = NULL;
130                 }
131                 rs->sr_err = TXN_ABORT( ltid );
132                 ltid = NULL;
133                 op->o_private = NULL;
134                 op->o_do_not_cache = opinfo.boi_acl_cache;
135                 if( rs->sr_err != 0 ) {
136                         rs->sr_err = LDAP_OTHER;
137                         rs->sr_text = "internal error";
138                         goto return_results;
139                 }
140                 if ( op->o_abandon ) {
141                         rs->sr_err = SLAPD_ABANDON;
142                         goto return_results;
143                 }
144                 bdb_trans_backoff( ++num_retries );
145         }
146
147         /* begin transaction */
148         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
149                 bdb->bi_db_opflags );
150         rs->sr_text = NULL;
151         if( rs->sr_err != 0 ) {
152                 Debug( LDAP_DEBUG_TRACE,
153                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
154                         db_strerror(rs->sr_err), rs->sr_err, 0 );
155                 rs->sr_err = LDAP_OTHER;
156                 rs->sr_text = "internal error";
157                 goto return_results;
158         }
159
160         locker = TXN_ID ( ltid );
161
162         opinfo.boi_bdb = op->o_bd;
163         opinfo.boi_txn = ltid;
164         opinfo.boi_locker = locker;
165         opinfo.boi_err = 0;
166         opinfo.boi_acl_cache = op->o_do_not_cache;
167         op->o_private = &opinfo;
168         
169         /*
170          * Get the parent dn and see if the corresponding entry exists.
171          */
172         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
173                 pdn = slap_empty_bv;
174         } else {
175                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
176         }
177
178         /* get entry or parent */
179         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
180                 1, locker, &lock );
181         switch( rs->sr_err ) {
182         case 0:
183                 rs->sr_err = LDAP_ALREADY_EXISTS;
184                 goto return_results;
185         case DB_NOTFOUND:
186                 break;
187         case DB_LOCK_DEADLOCK:
188         case DB_LOCK_NOTGRANTED:
189                 goto retry;
190         case LDAP_BUSY:
191                 rs->sr_text = "ldap server busy";
192                 goto return_results;
193         default:
194                 rs->sr_err = LDAP_OTHER;
195                 rs->sr_text = "internal error";
196                 goto return_results;
197         }
198
199         p = ei->bei_e;
200         if ( p ) {
201                 if ( !bvmatch( &pdn, &p->e_nname ) ) {
202                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
203                                 op->o_tmpmemctx );
204                         rs->sr_ref = is_entry_referral( p )
205                                 ? get_entry_referrals( op, p )
206                                 : NULL;
207                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
208                         p = NULL;
209                         Debug( LDAP_DEBUG_TRACE,
210                                 LDAP_XSTRING(bdb_add) ": parent "
211                                 "does not exist\n", 0, 0, 0 );
212
213                         rs->sr_err = LDAP_REFERRAL;
214                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
215                         goto return_results;
216                 }
217
218                 rs->sr_err = access_allowed( op, p,
219                         children, NULL, ACL_WADD, NULL );
220
221                 if ( ! rs->sr_err ) {
222                         switch( opinfo.boi_err ) {
223                         case DB_LOCK_DEADLOCK:
224                         case DB_LOCK_NOTGRANTED:
225                                 goto retry;
226                         }
227
228                         Debug( LDAP_DEBUG_TRACE,
229                                 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
230                                 0, 0, 0 );
231                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
232                         rs->sr_text = "no write access to parent";
233                         goto return_results;;
234                 }
235
236                 if ( is_entry_subentry( p ) ) {
237                         /* parent is a subentry, don't allow add */
238                         Debug( LDAP_DEBUG_TRACE,
239                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
240                                 0, 0, 0 );
241                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
242                         rs->sr_text = "parent is a subentry";
243                         goto return_results;;
244                 }
245                 if ( is_entry_alias( p ) ) {
246                         /* parent is an alias, don't allow add */
247                         Debug( LDAP_DEBUG_TRACE,
248                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
249                                 0, 0, 0 );
250                         rs->sr_err = LDAP_ALIAS_PROBLEM;
251                         rs->sr_text = "parent is an alias";
252                         goto return_results;;
253                 }
254
255                 if ( is_entry_referral( p ) ) {
256                         /* parent is a referral, don't allow add */
257                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
258                                 op->o_tmpmemctx );
259                         rs->sr_ref = get_entry_referrals( op, p );
260                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
261                         p = NULL;
262                         Debug( LDAP_DEBUG_TRACE,
263                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
264                                 0, 0, 0 );
265
266                         rs->sr_err = LDAP_REFERRAL;
267                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
268                         goto return_results;
269                 }
270
271                 if ( subentry ) {
272                         /* FIXME: */
273                         /* parent must be an administrative point of the required kind */
274                 }
275
276                 /* free parent and reader lock */
277                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
278                 p = NULL;
279
280         } else {
281                 /*
282                  * no parent!
283                  *  if not attempting to add entry at suffix or with parent ""
284                  */
285                 if ((( !be_isroot( op ) && !be_shadow_update(op) )
286                         || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
287                 {
288                         Debug( LDAP_DEBUG_TRACE,
289                                 LDAP_XSTRING(bdb_add) ": %s denied\n",
290                                 pdn.bv_len == 0 ? "suffix" : "entry at root",
291                                 0, 0 );
292                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
293                         goto return_results;
294                 }
295         }
296
297         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
298                 entry, NULL, ACL_WADD, NULL );
299
300         if ( ! rs->sr_err ) {
301                 switch( opinfo.boi_err ) {
302                 case DB_LOCK_DEADLOCK:
303                 case DB_LOCK_NOTGRANTED:
304                         goto retry;
305                 }
306
307                 Debug( LDAP_DEBUG_TRACE,
308                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
309                         0, 0, 0 );
310                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
311                 rs->sr_text = "no write access to entry";
312                 goto return_results;;
313         }
314
315         /* nested transaction */
316         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
317                 bdb->bi_db_opflags );
318         rs->sr_text = NULL;
319         if( rs->sr_err != 0 ) {
320                 Debug( LDAP_DEBUG_TRACE,
321                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
322                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
323                 rs->sr_err = LDAP_OTHER;
324                 rs->sr_text = "internal error";
325                 goto return_results;
326         }
327
328         /* dn2id index */
329         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
330         if ( rs->sr_err != 0 ) {
331                 Debug( LDAP_DEBUG_TRACE,
332                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
333                         db_strerror(rs->sr_err), rs->sr_err, 0 );
334
335                 switch( rs->sr_err ) {
336                 case DB_LOCK_DEADLOCK:
337                 case DB_LOCK_NOTGRANTED:
338                         goto retry;
339                 case DB_KEYEXIST:
340                         rs->sr_err = LDAP_ALREADY_EXISTS;
341                         break;
342                 default:
343                         rs->sr_err = LDAP_OTHER;
344                 }
345                 goto return_results;
346         }
347
348         /* attribute indexes */
349         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
350         if ( rs->sr_err != LDAP_SUCCESS ) {
351                 Debug( LDAP_DEBUG_TRACE,
352                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
353                         0, 0, 0 );
354                 switch( rs->sr_err ) {
355                 case DB_LOCK_DEADLOCK:
356                 case DB_LOCK_NOTGRANTED:
357                         goto retry;
358                 default:
359                         rs->sr_err = LDAP_OTHER;
360                 }
361                 rs->sr_text = "index generation failed";
362                 goto return_results;
363         }
364
365         /* id2entry index */
366         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
367         if ( rs->sr_err != 0 ) {
368                 Debug( LDAP_DEBUG_TRACE,
369                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
370                         0, 0, 0 );
371                 switch( rs->sr_err ) {
372                 case DB_LOCK_DEADLOCK:
373                 case DB_LOCK_NOTGRANTED:
374                         goto retry;
375                 default:
376                         rs->sr_err = LDAP_OTHER;
377                 }
378                 rs->sr_text = "entry store failed";
379                 goto return_results;
380         }
381
382         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
383                 rs->sr_err = LDAP_OTHER;
384                 rs->sr_text = "txn_commit(2) failed";
385                 goto return_results;
386         }
387
388         /* post-read */
389         if( op->o_postread ) {
390                 if( postread_ctrl == NULL ) {
391                         postread_ctrl = &ctrls[num_ctrls++];
392                         ctrls[num_ctrls] = NULL;
393                 }
394                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
395                         &slap_post_read_bv, postread_ctrl ) )
396                 {
397                         Debug( LDAP_DEBUG_TRACE,
398                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
399                                 "failed!\n", 0, 0, 0 );
400                         goto return_results;
401                 }
402         }
403
404         if ( op->o_noop ) {
405                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
406                         rs->sr_text = "txn_abort (no-op) failed";
407                 } else {
408                         rs->sr_err = LDAP_X_NO_OPERATION;
409                         ltid = NULL;
410                         goto return_results;
411                 }
412
413         } else {
414                 struct berval nrdn;
415                 Entry *e = entry_dup( op->ora_e );
416
417                 /* pick the RDN if not suffix; otherwise pick the entire DN */
418                 if (pdn.bv_len) {
419                         nrdn.bv_val = e->e_nname.bv_val;
420                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
421                 } else {
422                         nrdn = e->e_nname;
423                 }
424
425                 bdb_cache_add( bdb, ei, e, &nrdn, locker );
426
427                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
428                         rs->sr_text = "txn_commit failed";
429                 } else {
430                         rs->sr_err = LDAP_SUCCESS;
431                 }
432         }
433
434         ltid = NULL;
435         op->o_private = NULL;
436
437         if ( rs->sr_err != LDAP_SUCCESS ) {
438                 Debug( LDAP_DEBUG_TRACE,
439                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
440                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
441                 rs->sr_err = LDAP_OTHER;
442                 goto return_results;
443         }
444
445         Debug(LDAP_DEBUG_TRACE,
446                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
447                 op->o_noop ? " (no-op)" : "",
448                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
449
450         rs->sr_text = NULL;
451         if( num_ctrls ) rs->sr_ctrls = ctrls;
452
453 return_results:
454         send_ldap_result( op, rs );
455         slap_graduate_commit_csn( op );
456
457         if( ltid != NULL ) {
458                 TXN_ABORT( ltid );
459         }
460         op->o_private = NULL;
461
462         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
463                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
464                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
465         }
466
467         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
468                 TXN_CHECKPOINT( bdb->bi_dbenv,
469                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
470         }
471         return rs->sr_err;
472 }