1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
25 bdb_add(Operation *op, SlapReply *rs )
27 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
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};
43 LDAPControl **postread_ctrl = NULL;
44 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
47 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
48 op->oq_add.rs_e->e_name.bv_val, 0, 0);
52 /* add opattrs to shadow as well, only missing attrs will actually
53 * be added; helps compatibility with older OL versions */
54 slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
56 /* check entry's schema */
57 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
58 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
59 if ( rs->sr_err != LDAP_SUCCESS ) {
60 Debug( LDAP_DEBUG_TRACE,
61 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
62 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
66 subentry = is_entry_subentry( op->oq_add.rs_e );
69 * acquire an ID outside of the operation transaction
70 * to avoid serializing adds.
72 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
73 if( rs->sr_err != 0 ) {
74 Debug( LDAP_DEBUG_TRACE,
75 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
77 rs->sr_err = LDAP_OTHER;
78 rs->sr_text = "internal error";
83 retry: /* transaction retry */
85 /* free parent and reader lock */
86 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
89 rs->sr_err = TXN_ABORT( ltid );
92 op->o_do_not_cache = opinfo.boi_acl_cache;
93 if( rs->sr_err != 0 ) {
94 rs->sr_err = LDAP_OTHER;
95 rs->sr_text = "internal error";
98 if ( op->o_abandon ) {
99 rs->sr_err = SLAPD_ABANDON;
102 bdb_trans_backoff( ++num_retries );
105 /* begin transaction */
106 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
107 bdb->bi_db_opflags );
109 if( rs->sr_err != 0 ) {
110 Debug( LDAP_DEBUG_TRACE,
111 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
112 db_strerror(rs->sr_err), rs->sr_err, 0 );
113 rs->sr_err = LDAP_OTHER;
114 rs->sr_text = "internal error";
118 locker = TXN_ID ( ltid );
120 opinfo.boi_bdb = op->o_bd;
121 opinfo.boi_txn = ltid;
122 opinfo.boi_locker = locker;
124 opinfo.boi_acl_cache = op->o_do_not_cache;
125 op->o_private = &opinfo;
128 * Get the parent dn and see if the corresponding entry exists.
130 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
133 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
136 /* get entry or parent */
137 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
139 switch( rs->sr_err ) {
141 rs->sr_err = LDAP_ALREADY_EXISTS;
145 case DB_LOCK_DEADLOCK:
146 case DB_LOCK_NOTGRANTED:
149 rs->sr_text = "ldap server busy";
152 rs->sr_err = LDAP_OTHER;
153 rs->sr_text = "internal error";
159 if ( !bvmatch( &pdn, &p->e_nname ) ) {
160 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
162 rs->sr_ref = is_entry_referral( p )
163 ? get_entry_referrals( op, p )
165 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
167 Debug( LDAP_DEBUG_TRACE,
168 LDAP_XSTRING(bdb_add) ": parent "
169 "does not exist\n", 0, 0, 0 );
171 rs->sr_err = LDAP_REFERRAL;
172 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
176 rs->sr_err = access_allowed( op, p,
177 children, NULL, ACL_WADD, NULL );
179 if ( ! rs->sr_err ) {
180 switch( opinfo.boi_err ) {
181 case DB_LOCK_DEADLOCK:
182 case DB_LOCK_NOTGRANTED:
186 Debug( LDAP_DEBUG_TRACE,
187 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
189 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
190 rs->sr_text = "no write access to parent";
191 goto return_results;;
194 if ( is_entry_subentry( p ) ) {
195 /* parent is a subentry, don't allow add */
196 Debug( LDAP_DEBUG_TRACE,
197 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
199 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
200 rs->sr_text = "parent is a subentry";
201 goto return_results;;
203 if ( is_entry_alias( p ) ) {
204 /* parent is an alias, don't allow add */
205 Debug( LDAP_DEBUG_TRACE,
206 LDAP_XSTRING(bdb_add) ": parent is alias\n",
208 rs->sr_err = LDAP_ALIAS_PROBLEM;
209 rs->sr_text = "parent is an alias";
210 goto return_results;;
213 if ( is_entry_referral( p ) ) {
214 /* parent is a referral, don't allow add */
215 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
217 rs->sr_ref = get_entry_referrals( op, p );
218 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
220 Debug( LDAP_DEBUG_TRACE,
221 LDAP_XSTRING(bdb_add) ": parent is referral\n",
224 rs->sr_err = LDAP_REFERRAL;
225 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
231 /* parent must be an administrative point of the required kind */
234 /* free parent and reader lock */
235 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
241 * if not attempting to add entry at suffix or with parent ""
243 if ((( !be_isroot( op ) && !be_shadow_update(op) )
244 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
246 Debug( LDAP_DEBUG_TRACE,
247 LDAP_XSTRING(bdb_add) ": %s denied\n",
248 pdn.bv_len == 0 ? "suffix" : "entry at root",
250 rs->sr_err = LDAP_NO_SUCH_OBJECT;
255 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
256 entry, NULL, ACL_WADD, NULL );
258 if ( ! rs->sr_err ) {
259 switch( opinfo.boi_err ) {
260 case DB_LOCK_DEADLOCK:
261 case DB_LOCK_NOTGRANTED:
265 Debug( LDAP_DEBUG_TRACE,
266 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
268 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
269 rs->sr_text = "no write access to entry";
270 goto return_results;;
273 /* nested transaction */
274 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
275 bdb->bi_db_opflags );
277 if( rs->sr_err != 0 ) {
278 Debug( LDAP_DEBUG_TRACE,
279 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
280 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
281 rs->sr_err = LDAP_OTHER;
282 rs->sr_text = "internal error";
287 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
288 if ( rs->sr_err != 0 ) {
289 Debug( LDAP_DEBUG_TRACE,
290 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
291 db_strerror(rs->sr_err), rs->sr_err, 0 );
293 switch( rs->sr_err ) {
294 case DB_LOCK_DEADLOCK:
295 case DB_LOCK_NOTGRANTED:
298 rs->sr_err = LDAP_ALREADY_EXISTS;
301 rs->sr_err = LDAP_OTHER;
306 /* attribute indexes */
307 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
308 if ( rs->sr_err != LDAP_SUCCESS ) {
309 Debug( LDAP_DEBUG_TRACE,
310 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
312 switch( rs->sr_err ) {
313 case DB_LOCK_DEADLOCK:
314 case DB_LOCK_NOTGRANTED:
317 rs->sr_err = LDAP_OTHER;
319 rs->sr_text = "index generation failed";
324 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
325 if ( rs->sr_err != 0 ) {
326 Debug( LDAP_DEBUG_TRACE,
327 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
329 switch( rs->sr_err ) {
330 case DB_LOCK_DEADLOCK:
331 case DB_LOCK_NOTGRANTED:
334 rs->sr_err = LDAP_OTHER;
336 rs->sr_text = "entry store failed";
340 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
341 rs->sr_err = LDAP_OTHER;
342 rs->sr_text = "txn_commit(2) failed";
347 if( op->o_postread ) {
348 if( postread_ctrl == NULL ) {
349 postread_ctrl = &ctrls[num_ctrls++];
350 ctrls[num_ctrls] = NULL;
352 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
353 &slap_post_read_bv, postread_ctrl ) )
355 Debug( LDAP_DEBUG_TRACE,
356 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
357 "failed!\n", 0, 0, 0 );
363 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
364 rs->sr_text = "txn_abort (no-op) failed";
366 rs->sr_err = LDAP_X_NO_OPERATION;
373 Entry *e = entry_dup( op->ora_e );
375 /* pick the RDN if not suffix; otherwise pick the entire DN */
377 nrdn.bv_val = e->e_nname.bv_val;
378 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
383 bdb_cache_add( bdb, ei, e, &nrdn, locker );
385 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
386 rs->sr_text = "txn_commit failed";
388 rs->sr_err = LDAP_SUCCESS;
393 op->o_private = NULL;
395 if ( rs->sr_err != LDAP_SUCCESS ) {
396 Debug( LDAP_DEBUG_TRACE,
397 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
398 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
399 rs->sr_err = LDAP_OTHER;
403 Debug(LDAP_DEBUG_TRACE,
404 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
405 op->o_noop ? " (no-op)" : "",
406 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
409 if( num_ctrls ) rs->sr_ctrls = ctrls;
412 send_ldap_result( op, rs );
413 if ( !SLAP_SHADOW( op->o_bd ))
414 slap_graduate_commit_csn( op );
419 op->o_private = NULL;
421 if( postread_ctrl != NULL ) {
422 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
423 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
426 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
427 TXN_CHECKPOINT( bdb->bi_dbenv,
428 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );