1 /* modrdn.c - bdb backend modrdn routine */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
23 struct berval *newrdn,
24 struct berval *nnewrdn,
26 struct berval *newSuperior,
27 struct berval *nnewSuperior
30 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
31 AttributeDescription *children = slap_schema.si_ad_children;
32 struct berval p_dn, p_ndn;
33 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
39 char textbuf[SLAP_TEXT_BUFLEN];
40 size_t textlen = sizeof textbuf;
42 struct bdb_op_info opinfo;
46 LDAPRDN *new_rdn = NULL;
47 LDAPRDN *old_rdn = NULL;
49 Entry *np = NULL; /* newSuperior Entry */
50 struct berval *np_dn = NULL; /* newSuperior dn */
51 struct berval *np_ndn = NULL; /* newSuperior ndn */
52 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
54 /* Used to interface with bdb_modify_internal() */
55 Modifications *mod = NULL; /* Used to delete old rdn */
57 int manageDSAit = get_manageDSAit( op );
59 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
60 dn->bv_val, newrdn->bv_val,
61 newSuperior ? newSuperior->bv_val : "NULL" );
64 if( newSuperior != NULL ) {
65 rc = LDAP_UNWILLING_TO_PERFORM;
66 text = "newSuperior not implemented (yet)";
72 retry: /* transaction retry */
73 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
74 rc = txn_abort( ltid );
79 text = "internal error";
85 /* begin transaction */
86 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
90 Debug( LDAP_DEBUG_TRACE,
91 "bdb_delete: txn_begin failed: %s (%d)\n",
92 db_strerror(rc), rc, 0 );
94 text = "internal error";
100 opinfo.boi_txn = ltid;
102 op->o_private = &opinfo;
105 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
111 case DB_LOCK_DEADLOCK:
112 case DB_LOCK_NOTGRANTED:
116 text = "internal error";
121 char* matched_dn = NULL;
124 if( matched != NULL ) {
125 matched_dn = strdup( matched->e_dn );
126 refs = is_entry_referral( matched )
127 ? get_entry_referrals( be, conn, op, matched )
129 bdb_entry_return( be, matched );
133 refs = referral_rewrite( default_referral,
134 NULL, dn, LDAP_SCOPE_DEFAULT );
137 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
138 matched_dn, NULL, refs, NULL );
140 ber_bvarray_free( refs );
146 if (!manageDSAit && is_entry_referral( e ) ) {
147 /* parent is a referral, don't allow add */
148 /* parent is an alias, don't allow add */
149 BerVarray refs = get_entry_referrals( be,
152 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
155 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
156 e->e_dn, NULL, refs, NULL );
158 ber_bvarray_free( refs );
162 if ( be_issuffix( be, e->e_nname.bv_val ) ) {
166 rc = dnParent( e->e_nname.bv_val, (const char **)&p_ndn.bv_val );
167 if ( rc != LDAP_SUCCESS ) {
168 text = "internal error";
171 p_ndn.bv_len = e->e_nname.bv_len - (p_ndn.bv_val - e->e_nname.bv_val);
174 if ( p_ndn.bv_len != 0 ) {
175 /* Make sure parent entry exist and we can write its
178 rc = bdb_dn2entry( be, ltid, &p_ndn, &p, NULL, 0 );
184 case DB_LOCK_DEADLOCK:
185 case DB_LOCK_NOTGRANTED:
189 text = "internal error";
194 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
200 /* check parent for "children" acl */
201 if ( ! access_allowed( be, conn, op, p,
202 children, NULL, ACL_WRITE ) )
204 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
206 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
207 NULL, NULL, NULL, NULL );
211 Debug( LDAP_DEBUG_TRACE,
212 "bdb_modrdn: wr to children of entry %s OK\n",
213 p_ndn.bv_val, 0, 0 );
215 if ( be_issuffix( be, e->e_name.bv_val ) ) {
219 rc = dnParent( e->e_name.bv_val, &p_dn.bv_val );
220 if ( rc != LDAP_SUCCESS ) {
221 text = "internal error";
224 p_dn.bv_len = e->e_name.bv_len - (p_dn.bv_val - e->e_name.bv_val);
227 Debug( LDAP_DEBUG_TRACE,
228 "bdb_modrdn: parent dn=%s\n",
232 /* no parent, modrdn entry directly under root */
233 isroot = be_isroot( be, &op->o_ndn );
235 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
237 p = (Entry *)&slap_entry_root;
239 /* check parent for "children" acl */
240 rc = access_allowed( be, conn, op, p,
241 children, NULL, ACL_WRITE );
247 Debug( LDAP_DEBUG_TRACE,
248 "no access to parent\n",
250 send_ldap_result( conn, op,
251 LDAP_INSUFFICIENT_ACCESS,
252 NULL, NULL, NULL, NULL );
256 Debug( LDAP_DEBUG_TRACE,
257 "bdb_modrdn: wr to children of entry \"\" OK\n",
263 Debug( LDAP_DEBUG_TRACE,
264 "bdb_modrdn: parent dn=\"\"\n",
268 Debug( LDAP_DEBUG_TRACE,
269 "bdb_modrdn: no parent, not root "
270 "& \"\" is not suffix\n",
272 rc = LDAP_INSUFFICIENT_ACCESS;
278 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
280 if ( newSuperior != NULL ) {
281 Debug( LDAP_DEBUG_TRACE,
282 "bdb_modrdn: new parent \"%s\" requested...\n",
283 newSuperior->bv_val, 0, 0 );
285 if ( newSuperior->bv_len ) {
287 np_ndn = nnewSuperior;
289 /* newSuperior == oldParent?, if so ==> ERROR */
290 /* newSuperior == entry being moved?, if so ==> ERROR */
291 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
293 rc = bdb_dn2entry( be, ltid, nnewSuperior, &np, NULL, 0 );
299 case DB_LOCK_DEADLOCK:
300 case DB_LOCK_NOTGRANTED:
304 text = "internal error";
309 Debug( LDAP_DEBUG_TRACE,
310 "bdb_modrdn: newSup(ndn=%s) not here!\n",
311 np_ndn->bv_val, 0, 0);
316 Debug( LDAP_DEBUG_TRACE,
317 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
318 np, (long) np->e_id, 0 );
320 /* check newSuperior for "children" acl */
321 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
322 Debug( LDAP_DEBUG_TRACE,
323 "bdb_modrdn: no wr to newSup children\n",
325 rc = LDAP_INSUFFICIENT_ACCESS;
329 if ( is_entry_alias( np ) ) {
330 /* parent is an alias, don't allow add */
331 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
334 rc = LDAP_ALIAS_PROBLEM;
338 if ( is_entry_referral( np ) ) {
339 /* parent is a referral, don't allow add */
340 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
343 rc = LDAP_OPERATIONS_ERROR;
348 if ( isroot == -1 ) {
349 isroot = be_isroot( be, &op->o_ndn );
354 /* no parent, modrdn entry directly under root */
356 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
357 np = (Entry *)&slap_entry_root;
359 /* check parent for "children" acl */
360 rc = access_allowed( be, conn, op, np,
361 children, NULL, ACL_WRITE );
367 Debug( LDAP_DEBUG_TRACE,
368 "no access to new superior\n",
370 send_ldap_result( conn, op,
371 LDAP_INSUFFICIENT_ACCESS,
372 NULL, NULL, NULL, NULL );
376 Debug( LDAP_DEBUG_TRACE,
377 "bdb_modrdn: wr to children of entry \"\" OK\n",
381 Debug( LDAP_DEBUG_TRACE,
382 "bdb_modrdn: new superior=\"\", not root "
383 "& \"\" is not suffix\n",
385 rc = LDAP_INSUFFICIENT_ACCESS;
390 Debug( LDAP_DEBUG_TRACE,
391 "bdb_modrdn: new superior=\"\"\n",
395 Debug( LDAP_DEBUG_TRACE,
396 "bdb_modrdn: wr to new parent's children OK\n",
399 new_parent_dn = np_dn;
402 /* Build target dn and make sure target entry doesn't exist already. */
403 build_new_dn( &new_dn, new_parent_dn, newrdn );
405 dnNormalize2( NULL, &new_dn, &new_ndn );
407 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
408 new_ndn.bv_val, 0, 0 );
410 rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
412 case DB_LOCK_DEADLOCK:
413 case DB_LOCK_NOTGRANTED:
418 rc = LDAP_ALREADY_EXISTS;
422 text = "internal error";
426 Debug( LDAP_DEBUG_TRACE,
427 "bdb_modrdn: new ndn=%s does not exist\n",
428 new_ndn.bv_val, 0, 0 );
430 /* Get attribute type and attribute value of our new rdn, we will
431 * need to add that to our new entry
433 if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text,
434 LDAP_DN_FORMAT_LDAP ) )
436 Debug( LDAP_DEBUG_TRACE,
437 "bdb_modrdn: can't figure out type(s)/values(s) "
438 "of newrdn\n", 0, 0, 0 );
439 rc = LDAP_OPERATIONS_ERROR;
440 text = "unknown type(s) used in RDN";
444 Debug( LDAP_DEBUG_TRACE,
445 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
446 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
448 if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text,
449 LDAP_DN_FORMAT_LDAP ) )
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_back_modrdn: can't figure out the old_rdn "
453 "type(s)/value(s)\n", 0, 0, 0 );
455 text = "cannot parse RDN from old DN";
460 if ( newSuperior == NULL
461 && charray_strcasecmp( ( const char ** )old_rdn_types,
462 ( const char ** )new_rdn_types ) != 0 ) {
463 /* Not a big deal but we may say something */
464 Debug( LDAP_DEBUG_TRACE,
465 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
467 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
471 /* Add new attribute values to the entry */
472 for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
474 AttributeDescription *desc = NULL;
475 Modifications *mod_tmp;
477 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
479 if ( rc != LDAP_SUCCESS ) {
480 Debug( LDAP_DEBUG_TRACE,
481 "bdb_modrdn: %s: %s (new)\n",
482 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
486 /* ACL check of newly added attrs */
487 if ( !access_allowed( be, conn, op, e, desc,
488 &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE ) ) {
489 Debug( LDAP_DEBUG_TRACE,
490 "bdb_modrdn: access to attr \"%s\" "
491 "(new) not allowed\n",
492 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
493 rc = LDAP_INSUFFICIENT_ACCESS;
497 /* Apply modification */
498 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
499 + 2 * sizeof( struct berval ) );
500 mod_tmp->sml_desc = desc;
501 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
502 mod_tmp->sml_bvalues[ 0 ] = new_rdn[0][ a_cnt ]->la_value;
503 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
504 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
505 mod_tmp->sml_next = mod;
509 /* Remove old rdn value if required */
510 if ( deleteoldrdn ) {
511 /* Get value of old rdn */
512 if ( old_rdn == NULL) {
513 Debug( LDAP_DEBUG_TRACE,
514 "bdb_modrdn: can't figure out old RDN value(s) "
515 "from old RDN\n", 0, 0, 0 );
517 text = "could not parse value(s) from old RDN";
521 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
523 AttributeDescription *desc = NULL;
524 Modifications *mod_tmp;
526 rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
529 if ( rc != LDAP_SUCCESS ) {
530 Debug( LDAP_DEBUG_TRACE,
531 "bdb_modrdn: %s: %s (old)\n",
532 text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
536 /* ACL check of newly added attrs */
537 if ( !access_allowed( be, conn, op, e, desc,
538 &old_rdn[0][d_cnt]->la_value, ACL_WRITE ) ) {
539 Debug( LDAP_DEBUG_TRACE,
540 "bdb_modrdn: access to attr \"%s\" "
541 "(old) not allowed\n",
542 old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
543 rc = LDAP_INSUFFICIENT_ACCESS;
547 /* Apply modification */
548 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
549 + 2 * sizeof ( struct berval ) );
550 mod_tmp->sml_desc = desc;
551 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
552 mod_tmp->sml_bvalues[ 0 ] = old_rdn[0][ d_cnt ]->la_value;
553 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
554 mod_tmp->sml_op = LDAP_MOD_DELETE;
555 mod_tmp->sml_next = mod;
561 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
564 case DB_LOCK_DEADLOCK:
565 case DB_LOCK_NOTGRANTED:
569 text = "DN index delete fail";
573 /* Binary format uses a single contiguous block, cannot
574 * free individual fields. Leave new_dn/new_ndn set so
575 * they can be individually freed later.
578 e->e_nname = new_ndn;
581 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
584 case DB_LOCK_DEADLOCK:
585 case DB_LOCK_NOTGRANTED:
589 text = "DN index add failed";
594 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
595 &text, textbuf, textlen );
597 if( rc != LDAP_SUCCESS ) {
599 case DB_LOCK_DEADLOCK:
600 case DB_LOCK_NOTGRANTED:
607 rc = bdb_id2entry_update( be, ltid, e );
610 case DB_LOCK_DEADLOCK:
611 case DB_LOCK_NOTGRANTED:
615 text = "entry update failed";
620 rc = txn_commit( ltid, 0 );
623 op->o_private = NULL;
626 Debug( LDAP_DEBUG_TRACE,
627 "bdb_modrdn: txn_commit failed: %s (%d)\n",
628 db_strerror(rc), rc, 0 );
630 text = "commit failed";
632 Debug( LDAP_DEBUG_TRACE,
633 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
634 e->e_id, e->e_dn, 0 );
640 send_ldap_result( conn, op, rc,
641 NULL, text, NULL, NULL );
643 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
644 ldap_pvt_thread_yield();
645 TXN_CHECKPOINT( bdb->bi_dbenv,
646 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
650 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
651 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
653 /* LDAP v2 supporting correct attribute handling. */
654 if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
655 if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
658 for (; mod; mod=tmp ) {
664 /* LDAP v3 Support */
666 /* free new parent and writer lock */
667 bdb_entry_return( be, np );
671 /* free parent and writer lock */
672 bdb_entry_return( be, p );
677 bdb_entry_return( be, e );
682 op->o_private = NULL;