]> 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                         if ( p != (Entry *)&slap_entry_root ) {
129                                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
130                         }
131                         p = NULL;
132                 }
133                 rs->sr_err = TXN_ABORT( ltid );
134                 ltid = NULL;
135                 op->o_private = NULL;
136                 op->o_do_not_cache = opinfo.boi_acl_cache;
137                 if( rs->sr_err != 0 ) {
138                         rs->sr_err = LDAP_OTHER;
139                         rs->sr_text = "internal error";
140                         goto return_results;
141                 }
142                 if ( op->o_abandon ) {
143                         rs->sr_err = SLAPD_ABANDON;
144                         goto return_results;
145                 }
146                 bdb_trans_backoff( ++num_retries );
147         }
148
149         /* begin transaction */
150         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
151                 bdb->bi_db_opflags );
152         rs->sr_text = NULL;
153         if( rs->sr_err != 0 ) {
154                 Debug( LDAP_DEBUG_TRACE,
155                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
156                         db_strerror(rs->sr_err), rs->sr_err, 0 );
157                 rs->sr_err = LDAP_OTHER;
158                 rs->sr_text = "internal error";
159                 goto return_results;
160         }
161
162         locker = TXN_ID ( ltid );
163
164         opinfo.boi_bdb = op->o_bd;
165         opinfo.boi_txn = ltid;
166         opinfo.boi_locker = locker;
167         opinfo.boi_err = 0;
168         opinfo.boi_acl_cache = op->o_do_not_cache;
169         op->o_private = &opinfo;
170         
171         /*
172          * Get the parent dn and see if the corresponding entry exists.
173          */
174         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
175                 pdn = slap_empty_bv;
176         } else {
177                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
178         }
179
180         /* get entry or parent */
181         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
182                 1, locker, &lock );
183         switch( rs->sr_err ) {
184         case 0:
185                 rs->sr_err = LDAP_ALREADY_EXISTS;
186                 goto return_results;
187         case DB_NOTFOUND:
188                 break;
189         case DB_LOCK_DEADLOCK:
190         case DB_LOCK_NOTGRANTED:
191                 goto retry;
192         case LDAP_BUSY:
193                 rs->sr_text = "ldap server busy";
194                 goto return_results;
195         default:
196                 rs->sr_err = LDAP_OTHER;
197                 rs->sr_text = "internal error";
198                 goto return_results;
199         }
200
201         p = ei->bei_e;
202         if ( !p )
203                 p = (Entry *)&slap_entry_root;
204
205         if ( !bvmatch( &pdn, &p->e_nname ) ) {
206                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
207                         op->o_tmpmemctx );
208                 rs->sr_ref = is_entry_referral( p )
209                         ? get_entry_referrals( op, p )
210                         : NULL;
211                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
212                 p = NULL;
213                 Debug( LDAP_DEBUG_TRACE,
214                         LDAP_XSTRING(bdb_add) ": parent "
215                         "does not exist\n", 0, 0, 0 );
216
217                 rs->sr_err = LDAP_REFERRAL;
218                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
219                 goto return_results;
220         }
221
222         rs->sr_err = access_allowed( op, p,
223                 children, NULL, ACL_WADD, NULL );
224
225         if ( ! rs->sr_err ) {
226                 switch( opinfo.boi_err ) {
227                 case DB_LOCK_DEADLOCK:
228                 case DB_LOCK_NOTGRANTED:
229                         goto retry;
230                 }
231
232                 Debug( LDAP_DEBUG_TRACE,
233                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
234                         0, 0, 0 );
235                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
236                 rs->sr_text = "no write access to parent";
237                 goto return_results;;
238         }
239
240         if ( is_entry_subentry( p ) ) {
241                 /* parent is a subentry, don't allow add */
242                 Debug( LDAP_DEBUG_TRACE,
243                         LDAP_XSTRING(bdb_add) ": parent is subentry\n",
244                         0, 0, 0 );
245                 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
246                 rs->sr_text = "parent is a subentry";
247                 goto return_results;;
248         }
249         if ( is_entry_alias( p ) ) {
250                 /* parent is an alias, don't allow add */
251                 Debug( LDAP_DEBUG_TRACE,
252                         LDAP_XSTRING(bdb_add) ": parent is alias\n",
253                         0, 0, 0 );
254                 rs->sr_err = LDAP_ALIAS_PROBLEM;
255                 rs->sr_text = "parent is an alias";
256                 goto return_results;;
257         }
258
259         if ( is_entry_referral( p ) ) {
260                 /* parent is a referral, don't allow add */
261                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
262                         op->o_tmpmemctx );
263                 rs->sr_ref = get_entry_referrals( op, p );
264                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
265                 p = NULL;
266                 Debug( LDAP_DEBUG_TRACE,
267                         LDAP_XSTRING(bdb_add) ": parent is referral\n",
268                         0, 0, 0 );
269
270                 rs->sr_err = LDAP_REFERRAL;
271                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
272                 goto return_results;
273         }
274
275         if ( subentry ) {
276                 /* FIXME: */
277                 /* parent must be an administrative point of the required kind */
278         }
279
280         /* free parent and reader lock */
281         if ( p != (Entry *)&slap_entry_root ) {
282                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
283         }
284         p = NULL;
285
286         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
287                 entry, NULL, ACL_WADD, NULL );
288
289         if ( ! rs->sr_err ) {
290                 switch( opinfo.boi_err ) {
291                 case DB_LOCK_DEADLOCK:
292                 case DB_LOCK_NOTGRANTED:
293                         goto retry;
294                 }
295
296                 Debug( LDAP_DEBUG_TRACE,
297                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
298                         0, 0, 0 );
299                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
300                 rs->sr_text = "no write access to entry";
301                 goto return_results;;
302         }
303
304         /* nested transaction */
305         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
306                 bdb->bi_db_opflags );
307         rs->sr_text = NULL;
308         if( rs->sr_err != 0 ) {
309                 Debug( LDAP_DEBUG_TRACE,
310                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
311                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
312                 rs->sr_err = LDAP_OTHER;
313                 rs->sr_text = "internal error";
314                 goto return_results;
315         }
316
317         /* dn2id index */
318         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
319         if ( rs->sr_err != 0 ) {
320                 Debug( LDAP_DEBUG_TRACE,
321                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
322                         db_strerror(rs->sr_err), rs->sr_err, 0 );
323
324                 switch( rs->sr_err ) {
325                 case DB_LOCK_DEADLOCK:
326                 case DB_LOCK_NOTGRANTED:
327                         goto retry;
328                 case DB_KEYEXIST:
329                         rs->sr_err = LDAP_ALREADY_EXISTS;
330                         break;
331                 default:
332                         rs->sr_err = LDAP_OTHER;
333                 }
334                 goto return_results;
335         }
336
337         /* attribute indexes */
338         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
339         if ( rs->sr_err != LDAP_SUCCESS ) {
340                 Debug( LDAP_DEBUG_TRACE,
341                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
342                         0, 0, 0 );
343                 switch( rs->sr_err ) {
344                 case DB_LOCK_DEADLOCK:
345                 case DB_LOCK_NOTGRANTED:
346                         goto retry;
347                 default:
348                         rs->sr_err = LDAP_OTHER;
349                 }
350                 rs->sr_text = "index generation failed";
351                 goto return_results;
352         }
353
354         /* id2entry index */
355         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
356         if ( rs->sr_err != 0 ) {
357                 Debug( LDAP_DEBUG_TRACE,
358                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
359                         0, 0, 0 );
360                 switch( rs->sr_err ) {
361                 case DB_LOCK_DEADLOCK:
362                 case DB_LOCK_NOTGRANTED:
363                         goto retry;
364                 default:
365                         rs->sr_err = LDAP_OTHER;
366                 }
367                 rs->sr_text = "entry store failed";
368                 goto return_results;
369         }
370
371         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
372                 rs->sr_err = LDAP_OTHER;
373                 rs->sr_text = "txn_commit(2) failed";
374                 goto return_results;
375         }
376
377         /* post-read */
378         if( op->o_postread ) {
379                 if( postread_ctrl == NULL ) {
380                         postread_ctrl = &ctrls[num_ctrls++];
381                         ctrls[num_ctrls] = NULL;
382                 }
383                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
384                         &slap_post_read_bv, postread_ctrl ) )
385                 {
386                         Debug( LDAP_DEBUG_TRACE,
387                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
388                                 "failed!\n", 0, 0, 0 );
389                         goto return_results;
390                 }
391         }
392
393         if ( op->o_noop ) {
394                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
395                         rs->sr_text = "txn_abort (no-op) failed";
396                 } else {
397                         rs->sr_err = LDAP_X_NO_OPERATION;
398                         ltid = NULL;
399                         goto return_results;
400                 }
401
402         } else {
403                 struct berval nrdn;
404                 Entry *e = entry_dup( op->ora_e );
405
406                 /* pick the RDN if not suffix; otherwise pick the entire DN */
407                 if (pdn.bv_len) {
408                         nrdn.bv_val = e->e_nname.bv_val;
409                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
410                 } else {
411                         nrdn = e->e_nname;
412                 }
413
414                 bdb_cache_add( bdb, ei, e, &nrdn, locker );
415
416                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
417                         rs->sr_text = "txn_commit failed";
418                 } else {
419                         rs->sr_err = LDAP_SUCCESS;
420                 }
421         }
422
423         ltid = NULL;
424         op->o_private = NULL;
425
426         if ( rs->sr_err != LDAP_SUCCESS ) {
427                 Debug( LDAP_DEBUG_TRACE,
428                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
429                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
430                 rs->sr_err = LDAP_OTHER;
431                 goto return_results;
432         }
433
434         Debug(LDAP_DEBUG_TRACE,
435                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
436                 op->o_noop ? " (no-op)" : "",
437                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
438
439         rs->sr_text = NULL;
440         if( num_ctrls ) rs->sr_ctrls = ctrls;
441
442 return_results:
443         send_ldap_result( op, rs );
444         slap_graduate_commit_csn( op );
445
446         if( ltid != NULL ) {
447                 TXN_ABORT( ltid );
448         }
449         op->o_private = NULL;
450
451         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
452                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
453                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
454         }
455
456         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
457                 TXN_CHECKPOINT( bdb->bi_dbenv,
458                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
459         }
460         return rs->sr_err;
461 }