1 /* modrdn.c - ldbm backend modrdn routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>.
16 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
17 * This software is not subject to any license of Silicon Graphics
18 * Inc. or Purdue University.
20 * Redistribution and use in source and binary forms are permitted
21 * without restriction or fee of any kind as long as this notice
29 #include <ac/string.h>
30 #include <ac/socket.h>
33 #include "back-ldbm.h"
34 #include "proto-back-ldbm.h"
41 AttributeDescription *children = slap_schema.si_ad_children;
42 AttributeDescription *entry = slap_schema.si_ad_entry;
43 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
44 struct berval p_dn, p_ndn;
45 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
46 struct berval old_ndn = BER_BVNULL;
49 /* LDAP v2 supporting correct attribute handling. */
50 LDAPRDN new_rdn = NULL;
51 LDAPRDN old_rdn = NULL;
55 const char *text = NULL;
56 char textbuf[SLAP_TEXT_BUFLEN];
57 size_t textlen = sizeof textbuf;
58 /* Added to support newSuperior */
59 Entry *np = NULL; /* newSuperior Entry */
60 struct berval *np_ndn = NULL; /* newSuperior ndn */
61 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
62 /* Used to interface with ldbm_modify_internal() */
63 Modifications *mod = NULL; /* Used to delete old/add new rdn */
64 int manageDSAit = get_manageDSAit( op );
67 LDAP_LOG( BACK_LDBM, ENTRY,
68 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
69 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
70 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
72 Debug( LDAP_DEBUG_TRACE,
73 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n",
74 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
75 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
76 ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
79 /* grab giant lock for writing */
80 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
82 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
84 /* get entry with writer lock */
85 /* FIXME: dn2entry() should return non-glue entry */
86 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
87 if ( matched != NULL ) {
88 rs->sr_matched = strdup( matched->e_dn );
89 rs->sr_ref = is_entry_referral( matched )
90 ? get_entry_referrals( op, matched )
92 cache_return_entry_r( &li->li_cache, matched );
94 BerVarray deref = NULL;
95 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
97 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
99 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
100 ber_bvarray_add( &deref, &tmpbv );
103 deref = default_referral;
105 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
106 LDAP_SCOPE_DEFAULT );
109 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
111 rs->sr_err = LDAP_REFERRAL;
112 send_ldap_result( op, rs );
114 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
115 free( (char *)rs->sr_matched );
117 rs->sr_matched = NULL;
121 /* check entry for "entry" acl */
122 if ( ! access_allowed( op, e,
123 entry, NULL, ACL_WRITE, NULL ) )
126 LDAP_LOG( BACK_LDBM, ERR,
127 "ldbm_back_modrdn: no write access to entry of (%s)\n",
128 op->o_req_dn.bv_val, 0, 0 );
130 Debug( LDAP_DEBUG_TRACE,
131 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
135 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
136 "no write access to entry" );
141 if (!manageDSAit && is_entry_referral( e ) ) {
142 /* parent is a referral, don't allow add */
143 /* parent is an alias, don't allow add */
144 rs->sr_ref = get_entry_referrals( op, e );
147 LDAP_LOG( BACK_LDBM, INFO,
148 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
150 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
154 rs->sr_err = LDAP_REFERRAL;
155 rs->sr_matched = e->e_name.bv_val;
156 send_ldap_result( op, rs );
158 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
160 rs->sr_matched = NULL;
164 if ( has_children( op->o_bd, e ) ) {
166 LDAP_LOG( BACK_LDBM, INFO,
167 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
169 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
173 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
174 "subtree rename not supported" );
178 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
179 p_ndn = slap_empty_bv ;
181 dnParent( &e->e_nname, &p_ndn );
184 if ( p_ndn.bv_len != 0 ) {
185 /* Make sure parent entry exist and we can write its
189 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
191 LDAP_LOG( BACK_LDBM, INFO,
192 "ldbm_back_modrdn: parent of %s does not exist\n",
195 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
199 send_ldap_error( op, rs, LDAP_OTHER,
200 "parent entry does not exist" );
205 /* check parent for "children" acl */
206 if ( ! access_allowed( op, p,
207 children, NULL, ACL_WRITE, NULL ) )
210 LDAP_LOG( BACK_LDBM, INFO,
211 "ldbm_back_modrdn: no access to parent of (%s)\n",
214 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
218 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
224 LDAP_LOG( BACK_LDBM, DETAIL1,
225 "ldbm_back_modrdn: wr to children of entry %s OK\n",
226 p_ndn.bv_val, 0, 0 );
228 Debug( LDAP_DEBUG_TRACE,
229 "ldbm_back_modrdn: wr to children of entry %s OK\n",
230 p_ndn.bv_val, 0, 0 );
233 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
234 p_dn = slap_empty_bv;
236 dnParent( &e->e_name, &p_dn );
240 LDAP_LOG( BACK_LDBM, DETAIL1,
241 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
248 /* no parent, must be root to modify rdn */
249 isroot = be_isroot( op );
251 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
252 || be_isupdate( op ) || syncrepl_isupdate( op ) ) {
254 p = (Entry *)&slap_entry_root;
256 can_access = access_allowed( op, p,
257 children, NULL, ACL_WRITE, NULL );
260 /* check parent for "children" acl */
261 if ( ! can_access ) {
263 LDAP_LOG( BACK_LDBM, ERR,
264 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
266 Debug( LDAP_DEBUG_TRACE,
267 "<=- ldbm_back_modrdn: no "
268 "access to parent\n", 0, 0, 0 );
271 send_ldap_error( op, rs,
272 LDAP_INSUFFICIENT_ACCESS,
279 LDAP_LOG( BACK_LDBM, ERR,
280 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
283 Debug( LDAP_DEBUG_TRACE,
284 "<=- ldbm_back_modrdn: no parent & "
285 "not root\n", 0, 0, 0);
288 send_ldap_error( op, rs,
289 LDAP_INSUFFICIENT_ACCESS,
296 LDAP_LOG( BACK_LDBM, INFO,
297 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
299 Debug( LDAP_DEBUG_TRACE,
300 "ldbm_back_modrdn: no parent, locked root\n",
305 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
307 if ( op->oq_modrdn.rs_newSup != NULL ) {
309 LDAP_LOG( BACK_LDBM, DETAIL1,
310 "ldbm_back_modrdn: new parent \"%s\" requested\n",
311 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
313 Debug( LDAP_DEBUG_TRACE,
314 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
315 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
318 np_ndn = op->oq_modrdn.rs_nnewSup;
320 /* newSuperior == oldParent? */
321 if ( dn_match( &p_ndn, np_ndn ) ) {
323 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
324 "new parent\"%s\" seems to be the same as the "
325 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
327 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
328 "new parent\"%s\" seems to be the same as the "
329 "old parent \"%s\"\n",
330 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
333 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
337 if ( op->oq_modrdn.rs_newSup != NULL ) {
338 /* newSuperior == entry being moved?, if so ==> ERROR */
339 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
341 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
342 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
344 LDAP_LOG( BACK_LDBM, ERR,
345 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
346 np_ndn->bv_val, 0, 0 );
348 Debug( LDAP_DEBUG_TRACE,
349 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
350 np_ndn->bv_val, 0, 0);
353 send_ldap_error( op, rs, LDAP_OTHER,
354 "newSuperior not found" );
359 LDAP_LOG( BACK_LDBM, DETAIL1,
360 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
361 (void *) np, np->e_id, 0 );
363 Debug( LDAP_DEBUG_TRACE,
364 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
365 (void *) np, np->e_id, 0 );
368 /* check newSuperior for "children" acl */
369 if ( !access_allowed( op, np, children, NULL,
373 LDAP_LOG( BACK_LDBM, INFO,
374 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
376 Debug( LDAP_DEBUG_TRACE,
377 "ldbm_back_modrdn: no wr to newSup children\n",
381 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
385 if ( is_entry_alias( np ) ) {
386 /* parent is an alias, don't allow add */
388 LDAP_LOG( BACK_LDBM, INFO,
389 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
391 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
395 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
396 "newSuperior is an alias" );
401 if ( is_entry_referral( np ) ) {
402 /* parent is a referral, don't allow add */
404 LDAP_LOG( BACK_LDBM, INFO,
405 "ldbm_back_modrdn: entry (%s) is a referral\n",
408 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
412 send_ldap_error( op, rs, LDAP_OTHER,
413 "newSuperior is a referral" );
420 /* no parent, must be root to modify newSuperior */
421 if ( isroot == -1 ) {
422 isroot = be_isroot( op );
426 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
427 || be_isupdate( op ) || syncrepl_isupdate( op )) {
429 np = (Entry *)&slap_entry_root;
431 can_access = access_allowed( op, np,
432 children, NULL, ACL_WRITE, NULL );
435 /* check parent for "children" acl */
436 if ( ! can_access ) {
438 LDAP_LOG( BACK_LDBM, ERR,
439 "ldbm_back_modrdn: no access "
440 "to new superior \"\"\n", 0, 0, 0 );
442 Debug( LDAP_DEBUG_TRACE,
443 "<=- ldbm_back_modrdn: no "
444 "access to new superior\n", 0, 0, 0 );
447 send_ldap_error( op, rs,
448 LDAP_INSUFFICIENT_ACCESS,
455 LDAP_LOG( BACK_LDBM, ERR,
456 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
459 Debug( LDAP_DEBUG_TRACE,
460 "<=- ldbm_back_modrdn: \"\" "
461 "not allowed as new superior\n",
465 send_ldap_error( op, rs,
466 LDAP_INSUFFICIENT_ACCESS,
474 LDAP_LOG( BACK_LDBM, DETAIL1,
475 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
477 Debug( LDAP_DEBUG_TRACE,
478 "ldbm_back_modrdn: wr to new parent's children OK\n",
482 new_parent_dn = op->oq_modrdn.rs_newSup;
485 /* Build target dn and make sure target entry doesn't exist already. */
486 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL );
487 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
490 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
491 new_ndn.bv_val, 0, 0 );
493 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
494 new_ndn.bv_val, 0, 0 );
497 /* check for abandon */
498 if ( op->o_abandon ) {
502 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
503 /* if (rc_id) something bad happened to ldbm cache */
504 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
505 send_ldap_result( op, rs );
510 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
511 new_ndn.bv_val, 0, 0 );
513 Debug( LDAP_DEBUG_TRACE,
514 "ldbm_back_modrdn: new ndn=%s does not exist\n",
515 new_ndn.bv_val, 0, 0 );
518 /* Get attribute type and attribute value of our new rdn, we will
519 * need to add that to our new entry
521 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
522 LDAP_DN_FORMAT_LDAP ) )
525 LDAP_LOG ( OPERATION, ERR,
526 "ldbm_back_modrdn: can't figure out "
527 "type(s)/values(s) of newrdn\n",
530 Debug( LDAP_DEBUG_TRACE,
531 "ldbm_back_modrdn: can't figure out "
532 "type(s)/values(s) of newrdn\n",
535 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
536 "unknown type(s) used in RDN" );
541 LDAP_LOG ( OPERATION, RESULTS,
542 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
543 "new_rdn_val=\"%s\"\n",
544 new_rdn[ 0 ]->la_attr.bv_val,
545 new_rdn[ 0 ]->la_value.bv_val, 0 );
547 Debug( LDAP_DEBUG_TRACE,
548 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
549 "new_rdn_val=\"%s\"\n",
550 new_rdn[ 0 ]->la_attr.bv_val,
551 new_rdn[ 0 ]->la_value.bv_val, 0 );
554 if ( op->oq_modrdn.rs_deleteoldrdn ) {
555 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
556 LDAP_DN_FORMAT_LDAP ) )
559 LDAP_LOG ( OPERATION, ERR,
560 "ldbm_back_modrdn: can't figure out "
561 "type(s)/values(s) of old_rdn\n",
564 Debug( LDAP_DEBUG_TRACE,
565 "ldbm_back_modrdn: can't figure out "
566 "the old_rdn type(s)/value(s)\n",
569 send_ldap_error( op, rs, LDAP_OTHER,
570 "cannot parse RDN from old DN" );
576 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
578 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
582 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
583 send_ldap_result( op, rs );
587 /* check for abandon */
588 if ( op->o_abandon ) {
592 (void) cache_delete_entry( &li->li_cache, e );
595 old_ndn = e->e_nname;
597 e->e_nname = new_ndn;
598 new_dn.bv_val = NULL;
599 new_ndn.bv_val = NULL;
601 /* NOTE: after this you must not free new_dn or new_ndn!
602 * They are used by cache.
605 /* modify memory copy of entry */
606 rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
607 &rs->sr_text, textbuf, textlen );
608 switch ( rs->sr_err ) {
613 send_ldap_result( op, rs );
620 * NOTE: the backend MUST delete then add the entry,
621 * otherwise indexing may get hosed
622 * FIXME: if a new ID was used, the add could be done first.
623 * that would be safer.
627 if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
628 send_ldap_error( op, rs, LDAP_OTHER,
629 "DN index delete fail" );
634 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
635 /* try to repair old entry - probably hopeless */
636 if( dn2id_add( op->o_bd, &old_ndn, e->e_id) != 0 ) {
637 send_ldap_error( op, rs, LDAP_OTHER,
638 "DN index add and repair failed" );
640 send_ldap_error( op, rs, LDAP_OTHER,
641 "DN index add failed" );
647 if ( id2entry_add( op->o_bd, e ) != 0 ) {
650 rc = dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
651 rc |= dn2id_add( op->o_bd, &old_ndn, e->e_id );
653 send_ldap_error( op, rs, LDAP_OTHER,
654 "entry update and repair failed" );
656 send_ldap_error( op, rs, LDAP_OTHER,
657 "entry update failed" );
662 (void) cache_update_entry( &li->li_cache, e );
664 rs->sr_err = LDAP_SUCCESS;
666 send_ldap_result( op, rs );
667 cache_entry_commit( e );
670 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
671 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
672 if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
674 /* LDAP v2 supporting correct attribute handling. */
675 if ( new_rdn != NULL ) {
676 ldap_rdnfree( new_rdn );
678 if ( old_rdn != NULL ) {
679 ldap_rdnfree( old_rdn );
683 for (; mod; mod = tmp ) {
684 /* slap_modrdn2mods does things one way,
685 * slap_mods_opattrs does it differently
687 if ( mod->sml_op != SLAP_MOD_SOFTADD &&
688 mod->sml_op != LDAP_MOD_DELETE ) break;
689 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
693 slap_mods_free( mod );
696 /* LDAP v3 Support */
698 /* free new parent and writer lock */
699 cache_return_entry_w( &li->li_cache, np );
703 /* free parent and writer lock */
704 cache_return_entry_w( &li->li_cache, p );
707 /* free entry and writer lock */
708 cache_return_entry_w( &li->li_cache, e );
709 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
711 return( rs->sr_err );