1 /* modrdn.c - ldbm backend modrdn routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 * LDAP v3 newSuperior support. Add new rdn as an attribute.
10 * (Full support for v2 also used software/ideas contributed
11 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
14 * Copyright 1999, Juan C. Gomez, All rights reserved.
15 * This software is not subject to any license of Silicon Graphics
16 * Inc. or Purdue University.
18 * Redistribution and use in source and binary forms are permitted
19 * without restriction or fee of any kind as long as this notice
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
40 AttributeDescription *children = slap_schema.si_ad_children;
41 AttributeDescription *entry = slap_schema.si_ad_entry;
42 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
43 struct berval p_dn, p_ndn;
44 struct berval new_dn = { 0, NULL}, new_ndn = { 0, NULL };
47 /* LDAP v2 supporting correct attribute handling. */
48 LDAPRDN new_rdn = NULL;
49 LDAPRDN old_rdn = NULL;
51 #define CAN_ROLLBACK -1
52 #define MUST_DESTROY 1
53 int rc = CAN_ROLLBACK;
56 const char *text = NULL;
57 char textbuf[SLAP_TEXT_BUFLEN];
58 size_t textlen = sizeof textbuf;
59 /* Added to support newSuperior */
60 Entry *np = NULL; /* newSuperior Entry */
61 struct berval *np_ndn = NULL; /* newSuperior ndn */
62 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 /* Used to interface with ldbm_modify_internal() */
64 Modifications *mod = NULL; /* Used to delete old/add new rdn */
65 int manageDSAit = get_manageDSAit( op );
68 LDAP_LOG( BACK_LDBM, ENTRY,
69 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
70 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
71 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
73 Debug( LDAP_DEBUG_TRACE,
74 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n",
75 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
76 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
77 ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
80 /* grab giant lock for writing */
81 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
83 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
85 /* get entry with writer lock */
86 /* FIXME: dn2entry() should return non-glue entry */
87 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
88 if ( matched != NULL ) {
89 rs->sr_matched = strdup( matched->e_dn );
90 rs->sr_ref = is_entry_referral( matched )
91 ? get_entry_referrals( op, matched )
93 cache_return_entry_r( &li->li_cache, matched );
95 BerVarray deref = op->o_bd->syncinfo ?
96 op->o_bd->syncinfo->provideruri_bv : default_referral;
97 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
100 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
102 rs->sr_err = LDAP_REFERRAL;
103 send_ldap_result( op, rs );
105 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
106 free( (char *)rs->sr_matched );
111 /* check entry for "entry" acl */
112 if ( ! access_allowed( op, e,
113 entry, NULL, ACL_WRITE, NULL ) )
116 LDAP_LOG( BACK_LDBM, ERR,
117 "ldbm_back_modrdn: no write access to entry of (%s)\n",
118 op->o_req_dn.bv_val, 0, 0 );
120 Debug( LDAP_DEBUG_TRACE,
121 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
125 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
126 "no write access to entry" );
131 if (!manageDSAit && is_entry_referral( e ) ) {
132 /* parent is a referral, don't allow add */
133 /* parent is an alias, don't allow add */
134 rs->sr_ref = get_entry_referrals( op, e );
137 LDAP_LOG( BACK_LDBM, INFO,
138 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
140 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
144 rs->sr_err = LDAP_REFERRAL;
145 rs->sr_matched = e->e_name.bv_val;
146 send_ldap_result( op, rs );
148 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
152 if ( has_children( op->o_bd, e ) ) {
154 LDAP_LOG( BACK_LDBM, INFO,
155 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
157 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
161 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
162 "subtree rename not supported" );
166 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
167 p_ndn = slap_empty_bv ;
169 dnParent( &e->e_nname, &p_ndn );
172 if ( p_ndn.bv_len != 0 ) {
173 /* Make sure parent entry exist and we can write its
177 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
179 LDAP_LOG( BACK_LDBM, INFO,
180 "ldbm_back_modrdn: parent of %s does not exist\n",
183 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
187 send_ldap_error( op, rs, LDAP_OTHER,
188 "parent entry does not exist" );
193 /* check parent for "children" acl */
194 if ( ! access_allowed( op, p,
195 children, NULL, ACL_WRITE, NULL ) )
198 LDAP_LOG( BACK_LDBM, INFO,
199 "ldbm_back_modrdn: no access to parent of (%s)\n",
202 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
206 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
212 LDAP_LOG( BACK_LDBM, DETAIL1,
213 "ldbm_back_modrdn: wr to children of entry %s OK\n",
214 p_ndn.bv_val, 0, 0 );
216 Debug( LDAP_DEBUG_TRACE,
217 "ldbm_back_modrdn: wr to children of entry %s OK\n",
218 p_ndn.bv_val, 0, 0 );
221 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
222 p_dn = slap_empty_bv;
224 dnParent( &e->e_name, &p_dn );
228 LDAP_LOG( BACK_LDBM, DETAIL1,
229 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
231 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
236 /* no parent, must be root to modify rdn */
237 isroot = be_isroot( op->o_bd, &op->o_ndn );
239 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
241 p = (Entry *)&slap_entry_root;
243 can_access = access_allowed( op, p,
244 children, NULL, ACL_WRITE, NULL );
247 /* check parent for "children" acl */
248 if ( ! can_access ) {
250 LDAP_LOG( BACK_LDBM, ERR,
251 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
253 Debug( LDAP_DEBUG_TRACE,
254 "<=- ldbm_back_modrdn: no "
255 "access to parent\n", 0, 0, 0 );
258 send_ldap_error( op, rs,
259 LDAP_INSUFFICIENT_ACCESS,
266 LDAP_LOG( BACK_LDBM, ERR,
267 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
270 Debug( LDAP_DEBUG_TRACE,
271 "<=- ldbm_back_modrdn: no parent & "
272 "not root\n", 0, 0, 0);
275 send_ldap_error( op, rs,
276 LDAP_INSUFFICIENT_ACCESS,
283 LDAP_LOG( BACK_LDBM, INFO,
284 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
286 Debug( LDAP_DEBUG_TRACE,
287 "ldbm_back_modrdn: no parent, locked root\n",
292 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
294 if ( op->oq_modrdn.rs_newSup != NULL ) {
296 LDAP_LOG( BACK_LDBM, DETAIL1,
297 "ldbm_back_modrdn: new parent \"%s\" requested\n",
298 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
300 Debug( LDAP_DEBUG_TRACE,
301 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
302 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
305 np_ndn = op->oq_modrdn.rs_nnewSup;
307 /* newSuperior == oldParent? */
308 if ( dn_match( &p_ndn, np_ndn ) ) {
310 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
311 "new parent\"%s\" seems to be the same as the "
312 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
314 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
315 "new parent\"%s\" seems to be the same as the "
316 "old parent \"%s\"\n",
317 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
320 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
324 if ( op->oq_modrdn.rs_newSup != NULL ) {
325 /* newSuperior == entry being moved?, if so ==> ERROR */
326 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
328 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
329 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
331 LDAP_LOG( BACK_LDBM, ERR,
332 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
333 np_ndn->bv_val, 0, 0 );
335 Debug( LDAP_DEBUG_TRACE,
336 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
337 np_ndn->bv_val, 0, 0);
340 send_ldap_error( op, rs, LDAP_OTHER,
341 "newSuperior not found" );
346 LDAP_LOG( BACK_LDBM, DETAIL1,
347 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
348 (void *) np, np->e_id, 0 );
350 Debug( LDAP_DEBUG_TRACE,
351 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
352 (void *) np, np->e_id, 0 );
355 /* check newSuperior for "children" acl */
356 if ( !access_allowed( op, np, children, NULL,
360 LDAP_LOG( BACK_LDBM, INFO,
361 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
363 Debug( LDAP_DEBUG_TRACE,
364 "ldbm_back_modrdn: no wr to newSup children\n",
368 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
372 if ( is_entry_alias( np ) ) {
373 /* parent is an alias, don't allow add */
375 LDAP_LOG( BACK_LDBM, INFO,
376 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
378 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
382 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
383 "newSuperior is an alias" );
388 if ( is_entry_referral( np ) ) {
389 /* parent is a referral, don't allow add */
391 LDAP_LOG( BACK_LDBM, INFO,
392 "ldbm_back_modrdn: entry (%s) is a referral\n",
395 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
399 send_ldap_error( op, rs, LDAP_OTHER,
400 "newSuperior is a referral" );
407 /* no parent, must be root to modify newSuperior */
408 if ( isroot == -1 ) {
409 isroot = be_isroot( op->o_bd, &op->o_ndn );
413 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
415 np = (Entry *)&slap_entry_root;
417 can_access = access_allowed( op, np,
418 children, NULL, ACL_WRITE, NULL );
421 /* check parent for "children" acl */
422 if ( ! can_access ) {
424 LDAP_LOG( BACK_LDBM, ERR,
425 "ldbm_back_modrdn: no access "
426 "to new superior \"\"\n", 0, 0, 0 );
428 Debug( LDAP_DEBUG_TRACE,
429 "<=- ldbm_back_modrdn: no "
430 "access to new superior\n", 0, 0, 0 );
433 send_ldap_error( op, rs,
434 LDAP_INSUFFICIENT_ACCESS,
441 LDAP_LOG( BACK_LDBM, ERR,
442 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
445 Debug( LDAP_DEBUG_TRACE,
446 "<=- ldbm_back_modrdn: \"\" "
447 "not allowed as new superior\n",
451 send_ldap_error( op, rs,
452 LDAP_INSUFFICIENT_ACCESS,
460 LDAP_LOG( BACK_LDBM, DETAIL1,
461 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
463 Debug( LDAP_DEBUG_TRACE,
464 "ldbm_back_modrdn: wr to new parent's children OK\n",
468 new_parent_dn = op->oq_modrdn.rs_newSup;
471 /* Build target dn and make sure target entry doesn't exist already. */
472 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn );
473 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
476 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
477 new_ndn.bv_val, 0, 0 );
479 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
480 new_ndn.bv_val, 0, 0 );
483 /* check for abandon */
484 if ( op->o_abandon ) {
488 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
489 /* if (rc_id) something bad happened to ldbm cache */
490 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
491 send_ldap_result( op, rs );
496 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
497 new_ndn.bv_val, 0, 0 );
499 Debug( LDAP_DEBUG_TRACE,
500 "ldbm_back_modrdn: new ndn=%s does not exist\n",
501 new_ndn.bv_val, 0, 0 );
504 /* Get attribute type and attribute value of our new rdn, we will
505 * need to add that to our new entry
507 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
508 LDAP_DN_FORMAT_LDAP ) )
511 LDAP_LOG ( OPERATION, ERR,
512 "ldbm_back_modrdn: can't figure out "
513 "type(s)/values(s) of newrdn\n",
516 Debug( LDAP_DEBUG_TRACE,
517 "ldbm_back_modrdn: can't figure out "
518 "type(s)/values(s) of newrdn\n",
525 LDAP_LOG ( OPERATION, RESULTS,
526 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
527 "new_rdn_val=\"%s\"\n",
528 new_rdn[ 0 ]->la_attr.bv_val,
529 new_rdn[ 0 ]->la_value.bv_val, 0 );
531 Debug( LDAP_DEBUG_TRACE,
532 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
533 "new_rdn_val=\"%s\"\n",
534 new_rdn[ 0 ]->la_attr.bv_val,
535 new_rdn[ 0 ]->la_value.bv_val, 0 );
538 if ( op->oq_modrdn.rs_deleteoldrdn ) {
539 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
540 LDAP_DN_FORMAT_LDAP ) )
543 LDAP_LOG ( OPERATION, ERR,
544 "ldbm_back_modrdn: can't figure out "
545 "type(s)/values(s) of old_rdn\n",
548 Debug( LDAP_DEBUG_TRACE,
549 "ldbm_back_modrdn: can't figure out "
550 "the old_rdn type(s)/value(s)\n",
558 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
560 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
564 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
569 /* check for abandon */
570 if ( op->o_abandon ) {
575 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
576 send_ldap_error( op, rs, LDAP_OTHER,
577 "DN index delete fail" );
581 (void) cache_delete_entry( &li->li_cache, e );
584 /* XXX: there is no going back! */
589 e->e_nname = new_ndn;
590 new_dn.bv_val = NULL;
591 new_ndn.bv_val = NULL;
593 /* NOTE: after this you must not free new_dn or new_ndn!
594 * They are used by cache.
598 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
599 send_ldap_error( op, rs, LDAP_OTHER,
600 "DN index add failed" );
604 /* modify memory copy of entry */
605 rc_id = ldbm_modify_internal( op, &mod[0], e,
606 &rs->sr_text, textbuf, textlen );
614 send_ldap_result( op, rs );
618 /* here we may try to delete the newly added dn */
619 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
620 /* we already are in trouble ... */
626 (void) cache_update_entry( &li->li_cache, e );
629 if ( id2entry_add( op->o_bd, e ) != 0 ) {
630 send_ldap_error( op, rs, LDAP_OTHER,
631 "entry update failed" );
635 rs->sr_err = LDAP_SUCCESS;
636 send_ldap_result( op, rs );
638 cache_entry_commit( e );
641 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
642 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
644 /* LDAP v2 supporting correct attribute handling. */
645 if ( new_rdn != NULL ) {
646 ldap_rdnfree( new_rdn );
648 if ( old_rdn != NULL ) {
649 ldap_rdnfree( old_rdn );
653 for (; mod; mod = tmp ) {
654 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
660 /* LDAP v3 Support */
662 /* free new parent and writer lock */
663 cache_return_entry_w( &li->li_cache, np );
667 /* free parent and writer lock */
668 cache_return_entry_w( &li->li_cache, p );
671 /* free entry and writer lock */
672 cache_return_entry_w( &li->li_cache, e );
673 if ( rc == MUST_DESTROY ) {
674 /* if rc == MUST_DESTROY the entry is uncached
675 * and its private data is destroyed;
676 * the entry must be freed */
679 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);