1 /* modrdn.c - bdb backend modrdn routine */
4 * Copyright 1998-2000 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 = NULL;
39 char textbuf[SLAP_TEXT_BUFLEN];
40 size_t textlen = sizeof textbuf;
42 struct bdb_op_info opinfo;
45 char **new_rdn_vals = NULL; /* Vals of new rdn */
46 char **new_rdn_types = NULL; /* Types of new rdn */
48 char *old_rdn = NULL; /* Old rdn's attr type & val */
49 char **old_rdn_types = NULL; /* Types of old rdn attr. */
50 char **old_rdn_vals = NULL; /* Old rdn attribute values */
52 Entry *np = NULL; /* newSuperior Entry */
53 struct berval *np_dn = NULL; /* newSuperior dn */
54 struct berval *np_ndn = NULL; /* newSuperior ndn */
55 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
57 /* Used to interface with bdb_modify_internal() */
58 Modifications *mod = NULL; /* Used to delete old rdn */
60 int manageDSAit = get_manageDSAit( op );
62 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
63 dn->bv_val, newrdn->bv_val,
64 newSuperior ? newSuperior->bv_val : "NULL" );
67 if( newSuperior != NULL ) {
68 rc = LDAP_UNWILLING_TO_PERFORM;
69 text = "newSuperior not implemented (yet)";
75 retry: /* transaction retry */
76 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
77 rc = txn_abort( ltid );
82 text = "internal error";
88 /* begin transaction */
89 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
93 Debug( LDAP_DEBUG_TRACE,
94 "bdb_delete: txn_begin failed: %s (%d)\n",
95 db_strerror(rc), rc, 0 );
97 text = "internal error";
103 opinfo.boi_txn = ltid;
105 op->o_private = &opinfo;
108 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
114 case DB_LOCK_DEADLOCK:
115 case DB_LOCK_NOTGRANTED:
119 text = "internal error";
124 char* matched_dn = NULL;
125 struct berval** refs;
127 if( matched != NULL ) {
128 matched_dn = strdup( matched->e_dn );
129 refs = is_entry_referral( matched )
130 ? get_entry_referrals( be, conn, op, matched )
132 bdb_entry_return( be, matched );
136 refs = referral_rewrite( default_referral,
137 NULL, dn, LDAP_SCOPE_DEFAULT );
140 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
141 matched_dn, NULL, refs, NULL );
143 ber_bvecfree( refs );
149 if (!manageDSAit && is_entry_referral( e ) ) {
150 /* parent is a referral, don't allow add */
151 /* parent is an alias, don't allow add */
152 struct berval **refs = get_entry_referrals( be,
155 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
158 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
159 e->e_dn, NULL, refs, NULL );
161 ber_bvecfree( refs );
165 p_ndn.bv_val = dn_parent( be, e->e_ndn );
167 p_ndn.bv_len = e->e_nname.bv_len - (p_ndn.bv_val - e->e_ndn);
171 if ( p_ndn.bv_len != 0 ) {
172 /* Make sure parent entry exist and we can write its
175 rc = bdb_dn2entry( be, ltid, &p_ndn, &p, NULL, 0 );
181 case DB_LOCK_DEADLOCK:
182 case DB_LOCK_NOTGRANTED:
186 text = "internal error";
191 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
197 /* check parent for "children" acl */
198 if ( ! access_allowed( be, conn, op, p,
199 children, NULL, ACL_WRITE ) )
201 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
203 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
204 NULL, NULL, NULL, NULL );
208 Debug( LDAP_DEBUG_TRACE,
209 "bdb_modrdn: wr to children of entry %s OK\n",
210 p_ndn.bv_val, 0, 0 );
212 p_dn.bv_val = dn_parent( be, e->e_dn );
214 p_dn.bv_len = e->e_name.bv_len - (p_dn.bv_val - e->e_dn);
218 Debug( LDAP_DEBUG_TRACE,
219 "bdb_modrdn: parent dn=%s\n",
223 /* no parent, modrdn entry directly under root */
224 isroot = be_isroot( be, &op->o_ndn );
226 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
228 p = (Entry *)&slap_entry_root;
230 /* check parent for "children" acl */
231 rc = access_allowed( be, conn, op, p,
232 children, NULL, ACL_WRITE );
238 Debug( LDAP_DEBUG_TRACE,
239 "no access to parent\n",
241 send_ldap_result( conn, op,
242 LDAP_INSUFFICIENT_ACCESS,
243 NULL, NULL, NULL, NULL );
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_modrdn: wr to children of entry \"\" OK\n",
254 Debug( LDAP_DEBUG_TRACE,
255 "bdb_modrdn: parent dn=\"\"\n",
259 Debug( LDAP_DEBUG_TRACE,
260 "bdb_modrdn: no parent, not root "
261 "& \"\" is not suffix\n",
263 rc = LDAP_INSUFFICIENT_ACCESS;
269 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
271 if ( newSuperior != NULL ) {
272 Debug( LDAP_DEBUG_TRACE,
273 "bdb_modrdn: new parent \"%s\" requested...\n",
274 newSuperior->bv_val, 0, 0 );
276 if ( newSuperior->bv_len ) {
278 np_ndn = nnewSuperior;
280 /* newSuperior == oldParent?, if so ==> ERROR */
281 /* newSuperior == entry being moved?, if so ==> ERROR */
282 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
284 rc = bdb_dn2entry( be, ltid, nnewSuperior, &np, NULL, 0 );
290 case DB_LOCK_DEADLOCK:
291 case DB_LOCK_NOTGRANTED:
295 text = "internal error";
300 Debug( LDAP_DEBUG_TRACE,
301 "bdb_modrdn: newSup(ndn=%s) not here!\n",
302 np_ndn->bv_val, 0, 0);
307 Debug( LDAP_DEBUG_TRACE,
308 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
309 np, (long) np->e_id, 0 );
311 /* check newSuperior for "children" acl */
312 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
313 Debug( LDAP_DEBUG_TRACE,
314 "bdb_modrdn: no wr to newSup children\n",
316 rc = LDAP_INSUFFICIENT_ACCESS;
320 if ( is_entry_alias( np ) ) {
321 /* parent is an alias, don't allow add */
322 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
325 rc = LDAP_ALIAS_PROBLEM;
329 if ( is_entry_referral( np ) ) {
330 /* parent is a referral, don't allow add */
331 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
334 rc = LDAP_OPERATIONS_ERROR;
339 if ( isroot == -1 ) {
340 isroot = be_isroot( be, &op->o_ndn );
345 /* no parent, modrdn entry directly under root */
347 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
348 np = (Entry *)&slap_entry_root;
350 /* check parent for "children" acl */
351 rc = access_allowed( be, conn, op, np,
352 children, NULL, ACL_WRITE );
358 Debug( LDAP_DEBUG_TRACE,
359 "no access to new superior\n",
361 send_ldap_result( conn, op,
362 LDAP_INSUFFICIENT_ACCESS,
363 NULL, NULL, NULL, NULL );
367 Debug( LDAP_DEBUG_TRACE,
368 "bdb_modrdn: wr to children of entry \"\" OK\n",
372 Debug( LDAP_DEBUG_TRACE,
373 "bdb_modrdn: new superior=\"\", not root "
374 "& \"\" is not suffix\n",
376 rc = LDAP_INSUFFICIENT_ACCESS;
381 Debug( LDAP_DEBUG_TRACE,
382 "bdb_modrdn: new superior=\"\"\n",
386 Debug( LDAP_DEBUG_TRACE,
387 "bdb_modrdn: wr to new parent's children OK\n",
390 new_parent_dn = np_dn;
393 /* Build target dn and make sure target entry doesn't exist already. */
394 build_new_dn( &new_dn, new_parent_dn, newrdn );
396 dnNormalize( NULL, &new_dn, &new_ndn );
398 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
401 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
403 case DB_LOCK_DEADLOCK:
404 case DB_LOCK_NOTGRANTED:
409 rc = LDAP_ALREADY_EXISTS;
413 text = "internal error";
417 Debug( LDAP_DEBUG_TRACE,
418 "bdb_modrdn: new ndn=%s does not exist\n",
421 /* Get attribute type and attribute value of our new rdn, we will
422 * need to add that to our new entry
425 if ( rdn_attrs( newrdn->bv_val, &new_rdn_types, &new_rdn_vals ) ) {
426 Debug( LDAP_DEBUG_TRACE,
427 "bdb_modrdn: can't figure out type(s)/values(s) "
428 "of newrdn\n", 0, 0, 0 );
429 rc = LDAP_OPERATIONS_ERROR;
430 text = "unknown type(s) used in RDN";
434 Debug( LDAP_DEBUG_TRACE,
435 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
436 new_rdn_vals[0], new_rdn_types[0], 0 );
438 /* Retrieve the old rdn from the entry's dn */
439 if ( ( old_rdn = dn_rdn( be, dn->bv_val ) ) == NULL ) {
440 Debug( LDAP_DEBUG_TRACE,
441 "bdb_modrdn: can't figure out old_rdn from dn\n",
444 text = "could not parse old DN";
448 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
449 Debug( LDAP_DEBUG_TRACE,
450 "bdb_back_modrdn: can't figure out the old_rdn "
451 "type(s)/value(s)\n", 0, 0, 0 );
453 text = "cannot parse RDN from old DN";
457 if ( newSuperior == NULL
458 && charray_strcasecmp( ( const char ** )old_rdn_types,
459 ( const char ** )new_rdn_types ) != 0 ) {
460 /* Not a big deal but we may say something */
461 Debug( LDAP_DEBUG_TRACE,
462 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
464 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
467 /* Add new attribute values to the entry */
468 for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
470 AttributeDescription *desc = NULL;
471 Modifications *mod_tmp;
474 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
476 if ( rc != LDAP_SUCCESS ) {
477 Debug( LDAP_DEBUG_TRACE,
478 "bdb_modrdn: %s: %s (new)\n",
479 text, new_rdn_types[ a_cnt ], 0 );
483 /* ACL check of newly added attrs */
484 val.bv_val = new_rdn_vals[ a_cnt ];
485 val.bv_len = strlen( val.bv_val );
486 if ( !access_allowed( be, conn, op, e,
487 desc, &val, ACL_WRITE ) ) {
488 Debug( LDAP_DEBUG_TRACE,
489 "bdb_modrdn: access to attr \"%s\" "
490 "(new) not allowed\n",
491 new_rdn_types[ a_cnt ], 0, 0 );
492 rc = LDAP_INSUFFICIENT_ACCESS;
496 /* Apply modification */
497 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
498 mod_tmp->sml_desc = desc;
499 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
500 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
501 mod_tmp->sml_bvalues[ 1 ] = NULL;
502 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
503 mod_tmp->sml_next = mod;
507 /* Remove old rdn value if required */
508 if ( deleteoldrdn ) {
509 /* Get value of old rdn */
510 if ( old_rdn_vals == NULL) {
511 Debug( LDAP_DEBUG_TRACE,
512 "bdb_modrdn: can't figure out old RDN value(s) "
513 "from old RDN\n", 0, 0, 0 );
515 text = "could not parse value(s) from old RDN";
519 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
521 AttributeDescription *desc = NULL;
522 Modifications *mod_tmp;
525 rc = slap_str2ad( old_rdn_types[ d_cnt ],
528 if ( rc != LDAP_SUCCESS ) {
529 Debug( LDAP_DEBUG_TRACE,
530 "bdb_modrdn: %s: %s (old)\n",
531 text, old_rdn_types[ d_cnt ], 0 );
535 /* ACL check of newly added attrs */
536 val.bv_val = new_rdn_vals[ d_cnt ];
537 val.bv_len = strlen( val.bv_val );
538 if ( !access_allowed( be, conn, op, e,
539 desc, &val, ACL_WRITE ) ) {
540 Debug( LDAP_DEBUG_TRACE,
541 "bdb_modrdn: access to attr \"%s\" "
542 "(old) not allowed\n",
543 old_rdn_types[ d_cnt ], 0, 0 );
544 rc = LDAP_INSUFFICIENT_ACCESS;
548 /* Apply modification */
549 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
550 mod_tmp->sml_desc = desc;
551 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
552 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
553 mod_tmp->sml_bvalues[ 1 ] = 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 != NULL ) ber_bvfree( new_ndn );
653 /* LDAP v2 supporting correct attribute handling. */
654 if( new_rdn_types != NULL ) charray_free(new_rdn_types);
655 if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
656 if( old_rdn != NULL ) free(old_rdn);
657 if( old_rdn_types != NULL ) charray_free(old_rdn_types);
658 if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
659 if( mod != NULL ) slap_mods_free(mod);
661 /* LDAP v3 Support */
663 /* free new parent and writer lock */
664 bdb_entry_return( be, np );
668 /* free parent and writer lock */
669 bdb_entry_return( be, p );
674 bdb_entry_return( be, e );
679 op->o_private = NULL;