]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
41773376f099656bc2ffb438b2d3c65915a66e66
[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, *oe = op->ora_e;
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
95         /* check entry's schema */
96         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
97                 get_relax(op), 1, &rs->sr_text, textbuf, textlen );
98         if ( rs->sr_err != LDAP_SUCCESS ) {
99                 Debug( LDAP_DEBUG_TRACE,
100                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
101                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
102                 goto return_results;
103         }
104
105         /* add opattrs to shadow as well, only missing attrs will actually
106          * be added; helps compatibility with older OL versions */
107         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
108         if ( rs->sr_err != LDAP_SUCCESS ) {
109                 Debug( LDAP_DEBUG_TRACE,
110                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
111                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
112                 goto return_results;
113         }
114
115         subentry = is_entry_subentry( op->oq_add.rs_e );
116
117         /*
118          * acquire an ID outside of the operation transaction
119          * to avoid serializing adds.
120          */
121         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
122         if( rs->sr_err != 0 ) {
123                 Debug( LDAP_DEBUG_TRACE,
124                         LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
125                         rs->sr_err, 0, 0 );
126                 rs->sr_err = LDAP_OTHER;
127                 rs->sr_text = "internal error";
128                 goto return_results;
129         }
130
131         if( 0 ) {
132 retry:  /* transaction retry */
133                 if( p ) {
134                         /* free parent and reader lock */
135                         if ( p != (Entry *)&slap_entry_root ) {
136                                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
137                         }
138                         p = NULL;
139                 }
140                 rs->sr_err = TXN_ABORT( ltid );
141                 ltid = NULL;
142                 op->o_private = NULL;
143                 op->o_do_not_cache = opinfo.boi_acl_cache;
144                 if( rs->sr_err != 0 ) {
145                         rs->sr_err = LDAP_OTHER;
146                         rs->sr_text = "internal error";
147                         goto return_results;
148                 }
149                 if ( op->o_abandon ) {
150                         rs->sr_err = SLAPD_ABANDON;
151                         goto return_results;
152                 }
153                 bdb_trans_backoff( ++num_retries );
154         }
155
156         /* begin transaction */
157         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
158                 bdb->bi_db_opflags );
159         rs->sr_text = NULL;
160         if( rs->sr_err != 0 ) {
161                 Debug( LDAP_DEBUG_TRACE,
162                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
163                         db_strerror(rs->sr_err), rs->sr_err, 0 );
164                 rs->sr_err = LDAP_OTHER;
165                 rs->sr_text = "internal error";
166                 goto return_results;
167         }
168
169         locker = TXN_ID ( ltid );
170
171         opinfo.boi_bdb = op->o_bd;
172         opinfo.boi_txn = ltid;
173         opinfo.boi_locker = locker;
174         opinfo.boi_err = 0;
175         opinfo.boi_acl_cache = op->o_do_not_cache;
176         op->o_private = &opinfo;
177         
178         /*
179          * Get the parent dn and see if the corresponding entry exists.
180          */
181         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
182                 pdn = slap_empty_bv;
183         } else {
184                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
185         }
186
187         /* get entry or parent */
188         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
189                 1, locker, &lock );
190         switch( rs->sr_err ) {
191         case 0:
192                 rs->sr_err = LDAP_ALREADY_EXISTS;
193                 goto return_results;
194         case DB_NOTFOUND:
195                 break;
196         case DB_LOCK_DEADLOCK:
197         case DB_LOCK_NOTGRANTED:
198                 goto retry;
199         case LDAP_BUSY:
200                 rs->sr_text = "ldap server busy";
201                 goto return_results;
202         default:
203                 rs->sr_err = LDAP_OTHER;
204                 rs->sr_text = "internal error";
205                 goto return_results;
206         }
207
208         p = ei->bei_e;
209         if ( !p )
210                 p = (Entry *)&slap_entry_root;
211
212         if ( !bvmatch( &pdn, &p->e_nname ) ) {
213                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
214                         op->o_tmpmemctx );
215                 rs->sr_ref = is_entry_referral( p )
216                         ? get_entry_referrals( op, p )
217                         : NULL;
218                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
219                 p = NULL;
220                 Debug( LDAP_DEBUG_TRACE,
221                         LDAP_XSTRING(bdb_add) ": parent "
222                         "does not exist\n", 0, 0, 0 );
223
224                 rs->sr_err = LDAP_REFERRAL;
225                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
226                 goto return_results;
227         }
228
229         rs->sr_err = access_allowed( op, p,
230                 children, NULL, ACL_WADD, NULL );
231
232         if ( ! rs->sr_err ) {
233                 switch( opinfo.boi_err ) {
234                 case DB_LOCK_DEADLOCK:
235                 case DB_LOCK_NOTGRANTED:
236                         goto retry;
237                 }
238
239                 Debug( LDAP_DEBUG_TRACE,
240                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
241                         0, 0, 0 );
242                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
243                 rs->sr_text = "no write access to parent";
244                 goto return_results;;
245         }
246
247         if ( p != (Entry *)&slap_entry_root ) {
248                 if ( is_entry_subentry( p ) ) {
249                         /* parent is a subentry, don't allow add */
250                         Debug( LDAP_DEBUG_TRACE,
251                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
252                                 0, 0, 0 );
253                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
254                         rs->sr_text = "parent is a subentry";
255                         goto return_results;;
256                 }
257
258                 if ( is_entry_alias( p ) ) {
259                         /* parent is an alias, don't allow add */
260                         Debug( LDAP_DEBUG_TRACE,
261                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
262                                 0, 0, 0 );
263                         rs->sr_err = LDAP_ALIAS_PROBLEM;
264                         rs->sr_text = "parent is an alias";
265                         goto return_results;;
266                 }
267
268                 if ( is_entry_referral( p ) ) {
269                         /* parent is a referral, don't allow add */
270                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
271                                 op->o_tmpmemctx );
272                         rs->sr_ref = get_entry_referrals( op, p );
273                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
274                         p = NULL;
275                         Debug( LDAP_DEBUG_TRACE,
276                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
277                                 0, 0, 0 );
278
279                         rs->sr_err = LDAP_REFERRAL;
280                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
281                         goto return_results;
282                 }
283
284                 if ( subentry ) {
285                         /* FIXME: */
286                         /* parent must be an administrative point of the required kind */
287                 }
288         }
289
290         /* free parent and reader lock */
291         if ( p != (Entry *)&slap_entry_root ) {
292                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
293         }
294         p = NULL;
295
296         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
297                 entry, NULL, ACL_WADD, NULL );
298
299         if ( ! rs->sr_err ) {
300                 switch( opinfo.boi_err ) {
301                 case DB_LOCK_DEADLOCK:
302                 case DB_LOCK_NOTGRANTED:
303                         goto retry;
304                 }
305
306                 Debug( LDAP_DEBUG_TRACE,
307                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
308                         0, 0, 0 );
309                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
310                 rs->sr_text = "no write access to entry";
311                 goto return_results;;
312         }
313
314         /* nested transaction */
315         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
316                 bdb->bi_db_opflags );
317         rs->sr_text = NULL;
318         if( rs->sr_err != 0 ) {
319                 Debug( LDAP_DEBUG_TRACE,
320                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
321                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
322                 rs->sr_err = LDAP_OTHER;
323                 rs->sr_text = "internal error";
324                 goto return_results;
325         }
326
327         /* dn2id index */
328         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
329         if ( rs->sr_err != 0 ) {
330                 Debug( LDAP_DEBUG_TRACE,
331                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
332                         db_strerror(rs->sr_err), rs->sr_err, 0 );
333
334                 switch( rs->sr_err ) {
335                 case DB_LOCK_DEADLOCK:
336                 case DB_LOCK_NOTGRANTED:
337                         goto retry;
338                 case DB_KEYEXIST:
339                         rs->sr_err = LDAP_ALREADY_EXISTS;
340                         break;
341                 default:
342                         rs->sr_err = LDAP_OTHER;
343                 }
344                 goto return_results;
345         }
346
347         /* attribute indexes */
348         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
349         if ( rs->sr_err != LDAP_SUCCESS ) {
350                 Debug( LDAP_DEBUG_TRACE,
351                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
352                         0, 0, 0 );
353                 switch( rs->sr_err ) {
354                 case DB_LOCK_DEADLOCK:
355                 case DB_LOCK_NOTGRANTED:
356                         goto retry;
357                 default:
358                         rs->sr_err = LDAP_OTHER;
359                 }
360                 rs->sr_text = "index generation failed";
361                 goto return_results;
362         }
363
364         /* id2entry index */
365         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
366         if ( rs->sr_err != 0 ) {
367                 Debug( LDAP_DEBUG_TRACE,
368                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
369                         0, 0, 0 );
370                 switch( rs->sr_err ) {
371                 case DB_LOCK_DEADLOCK:
372                 case DB_LOCK_NOTGRANTED:
373                         goto retry;
374                 default:
375                         rs->sr_err = LDAP_OTHER;
376                 }
377                 rs->sr_text = "entry store failed";
378                 goto return_results;
379         }
380
381         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
382                 rs->sr_err = LDAP_OTHER;
383                 rs->sr_text = "txn_commit(2) failed";
384                 goto return_results;
385         }
386
387         /* post-read */
388         if( op->o_postread ) {
389                 if( postread_ctrl == NULL ) {
390                         postread_ctrl = &ctrls[num_ctrls++];
391                         ctrls[num_ctrls] = NULL;
392                 }
393                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
394                         &slap_post_read_bv, postread_ctrl ) )
395                 {
396                         Debug( LDAP_DEBUG_TRACE,
397                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
398                                 "failed!\n", 0, 0, 0 );
399                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
400                                 /* FIXME: is it correct to abort
401                                  * operation if control fails? */
402                                 goto return_results;
403                         }
404                 }
405         }
406
407         if ( op->o_noop ) {
408                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
409                         rs->sr_text = "txn_abort (no-op) failed";
410                 } else {
411                         rs->sr_err = LDAP_X_NO_OPERATION;
412                         ltid = NULL;
413                         goto return_results;
414                 }
415
416         } else {
417                 struct berval nrdn;
418
419                 /* pick the RDN if not suffix; otherwise pick the entire DN */
420                 if (pdn.bv_len) {
421                         nrdn.bv_val = op->ora_e->e_nname.bv_val;
422                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
423                 } else {
424                         nrdn = op->ora_e->e_nname;
425                 }
426
427                 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, locker );
428
429                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
430                         rs->sr_text = "txn_commit failed";
431                 } else {
432                         rs->sr_err = LDAP_SUCCESS;
433                 }
434         }
435
436         ltid = NULL;
437         op->o_private = NULL;
438
439         if ( rs->sr_err != LDAP_SUCCESS ) {
440                 Debug( LDAP_DEBUG_TRACE,
441                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
442                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
443                 rs->sr_err = LDAP_OTHER;
444                 goto return_results;
445         }
446
447         Debug(LDAP_DEBUG_TRACE,
448                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
449                 op->o_noop ? " (no-op)" : "",
450                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
451
452         rs->sr_text = NULL;
453         if( num_ctrls ) rs->sr_ctrls = ctrls;
454
455 return_results:
456         send_ldap_result( op, rs );
457         slap_graduate_commit_csn( op );
458
459         if( ltid != NULL ) {
460                 TXN_ABORT( ltid );
461         }
462         op->o_private = NULL;
463
464         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
465                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
466                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
467         }
468
469         if( rs->sr_err == LDAP_SUCCESS ) {
470                 EntryInfo *ei = oe->e_private;
471
472                 /* We own the entry now, and it can be purged at will
473                  * Check to make sure it's the same entry we entered with.
474                  * Possibly a callback may have mucked with it, although
475                  * in general callbacks should treat the entry as read-only.
476                  */
477                 if ( op->ora_e == oe )
478                         op->ora_e = NULL;
479                 ei->bei_state ^= CACHE_ENTRY_NOT_LINKED;
480
481                 if ( bdb->bi_txn_cp_kbyte ) {
482                         TXN_CHECKPOINT( bdb->bi_dbenv,
483                                 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
484                 }
485         }
486         return rs->sr_err;
487 }