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 /* get entry with writer lock */
84 if ( (e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
85 if( matched != NULL ) {
86 rs->sr_matched = strdup( matched->e_dn );
87 rs->sr_ref = is_entry_referral( matched )
88 ? get_entry_referrals( op, matched )
90 cache_return_entry_r( &li->li_cache, matched );
92 rs->sr_ref = referral_rewrite( default_referral,
93 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
96 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
98 rs->sr_err = LDAP_REFERRAL;
99 send_ldap_result( op, rs );
101 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
102 free( (char *)rs->sr_matched );
107 /* check entry for "entry" acl */
108 if ( ! access_allowed( op, e,
109 entry, NULL, ACL_WRITE, NULL ) )
112 LDAP_LOG( BACK_LDBM, ERR,
113 "ldbm_back_modrdn: no write access to entry of (%s)\n",
116 Debug( LDAP_DEBUG_TRACE,
117 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
121 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
122 "no write access to entry" );
127 if (!manageDSAit && is_entry_referral( e ) ) {
128 /* parent is a referral, don't allow add */
129 /* parent is an alias, don't allow add */
130 rs->sr_ref = get_entry_referrals( op, e );
133 LDAP_LOG( BACK_LDBM, INFO,
134 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
136 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
140 rs->sr_err = LDAP_REFERRAL;
141 rs->sr_matched = e->e_name.bv_val;
142 send_ldap_result( op, rs );
144 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
148 if ( has_children( op->o_bd, e ) ) {
150 LDAP_LOG( BACK_LDBM, INFO,
151 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
153 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
157 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
158 "subtree rename not supported" );
162 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
163 p_ndn = slap_empty_bv ;
165 dnParent( &e->e_nname, &p_ndn );
168 if ( p_ndn.bv_len != 0 ) {
169 /* Make sure parent entry exist and we can write its
173 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
175 LDAP_LOG( BACK_LDBM, INFO,
176 "ldbm_back_modrdn: parent of %s does not exist\n",
179 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
183 send_ldap_error( op, rs, LDAP_OTHER,
184 "parent entry does not exist" );
189 /* check parent for "children" acl */
190 if ( ! access_allowed( op, p,
191 children, NULL, ACL_WRITE, NULL ) )
194 LDAP_LOG( BACK_LDBM, INFO,
195 "ldbm_back_modrdn: no access to parent of (%s)\n",
198 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
202 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
208 LDAP_LOG( BACK_LDBM, DETAIL1,
209 "ldbm_back_modrdn: wr to children of entry %s OK\n",
210 p_ndn.bv_val, 0, 0 );
212 Debug( LDAP_DEBUG_TRACE,
213 "ldbm_back_modrdn: wr to children of entry %s OK\n",
214 p_ndn.bv_val, 0, 0 );
217 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
218 p_dn = slap_empty_bv;
220 dnParent( &e->e_name, &p_dn );
224 LDAP_LOG( BACK_LDBM, DETAIL1,
225 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
227 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
232 /* no parent, must be root to modify rdn */
233 isroot = be_isroot( op->o_bd, &op->o_ndn );
235 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
237 p = (Entry *)&slap_entry_root;
239 can_access = access_allowed( op, p,
240 children, NULL, ACL_WRITE, NULL );
243 /* check parent for "children" acl */
244 if ( ! can_access ) {
246 LDAP_LOG( BACK_LDBM, ERR,
247 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
249 Debug( LDAP_DEBUG_TRACE,
250 "<=- ldbm_back_modrdn: no "
251 "access to parent\n", 0, 0, 0 );
254 send_ldap_error( op, rs,
255 LDAP_INSUFFICIENT_ACCESS,
262 LDAP_LOG( BACK_LDBM, ERR,
263 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
266 Debug( LDAP_DEBUG_TRACE,
267 "<=- ldbm_back_modrdn: no parent & "
268 "not root\n", 0, 0, 0);
271 send_ldap_error( op, rs,
272 LDAP_INSUFFICIENT_ACCESS,
279 LDAP_LOG( BACK_LDBM, INFO,
280 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
282 Debug( LDAP_DEBUG_TRACE,
283 "ldbm_back_modrdn: no parent, locked root\n",
288 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
290 if ( op->oq_modrdn.rs_newSup != NULL ) {
292 LDAP_LOG( BACK_LDBM, DETAIL1,
293 "ldbm_back_modrdn: new parent \"%s\" requested\n",
294 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
296 Debug( LDAP_DEBUG_TRACE,
297 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
298 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
301 np_ndn = op->oq_modrdn.rs_nnewSup;
303 /* newSuperior == oldParent? */
304 if ( dn_match( &p_ndn, np_ndn ) ) {
306 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
307 "new parent\"%s\" seems to be the same as the "
308 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
310 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
311 "new parent\"%s\" seems to be the same as the "
312 "old parent \"%s\"\n",
313 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
316 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
320 if ( op->oq_modrdn.rs_newSup != NULL ) {
321 /* newSuperior == entry being moved?, if so ==> ERROR */
322 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
324 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
325 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
327 LDAP_LOG( BACK_LDBM, ERR,
328 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
329 np_ndn->bv_val, 0, 0 );
331 Debug( LDAP_DEBUG_TRACE,
332 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
333 np_ndn->bv_val, 0, 0);
336 send_ldap_error( op, rs, LDAP_OTHER,
337 "newSuperior not found" );
342 LDAP_LOG( BACK_LDBM, DETAIL1,
343 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
346 Debug( LDAP_DEBUG_TRACE,
347 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
351 /* check newSuperior for "children" acl */
352 if ( !access_allowed( op, np, children, NULL,
356 LDAP_LOG( BACK_LDBM, INFO,
357 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE,
360 "ldbm_back_modrdn: no wr to newSup children\n",
364 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
368 if ( is_entry_alias( np ) ) {
369 /* parent is an alias, don't allow add */
371 LDAP_LOG( BACK_LDBM, INFO,
372 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
374 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
378 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
379 "newSuperior is an alias" );
384 if ( is_entry_referral( np ) ) {
385 /* parent is a referral, don't allow add */
387 LDAP_LOG( BACK_LDBM, INFO,
388 "ldbm_back_modrdn: entry (%s) is a referral\n",
391 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
395 send_ldap_error( op, rs, LDAP_OTHER,
396 "newSuperior is a referral" );
403 /* no parent, must be root to modify newSuperior */
404 if ( isroot == -1 ) {
405 isroot = be_isroot( op->o_bd, &op->o_ndn );
409 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
411 np = (Entry *)&slap_entry_root;
413 can_access = access_allowed( op, np,
414 children, NULL, ACL_WRITE, NULL );
417 /* check parent for "children" acl */
418 if ( ! can_access ) {
420 LDAP_LOG( BACK_LDBM, ERR,
421 "ldbm_back_modrdn: no access "
422 "to new superior \"\"\n", 0, 0, 0 );
424 Debug( LDAP_DEBUG_TRACE,
425 "<=- ldbm_back_modrdn: no "
426 "access to new superior\n", 0, 0, 0 );
429 send_ldap_error( op, rs,
430 LDAP_INSUFFICIENT_ACCESS,
437 LDAP_LOG( BACK_LDBM, ERR,
438 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
441 Debug( LDAP_DEBUG_TRACE,
442 "<=- ldbm_back_modrdn: \"\" "
443 "not allowed as new superior\n",
447 send_ldap_error( op, rs,
448 LDAP_INSUFFICIENT_ACCESS,
456 LDAP_LOG( BACK_LDBM, DETAIL1,
457 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
459 Debug( LDAP_DEBUG_TRACE,
460 "ldbm_back_modrdn: wr to new parent's children OK\n",
464 new_parent_dn = op->oq_modrdn.rs_newSup;
467 /* Build target dn and make sure target entry doesn't exist already. */
468 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn );
469 dnNormalize2( NULL, &new_dn, &new_ndn );
472 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
473 new_ndn.bv_val, 0, 0 );
475 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
476 new_ndn.bv_val, 0, 0 );
479 /* check for abandon */
480 if ( op->o_abandon ) {
484 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
485 /* if (rc_id) something bad happened to ldbm cache */
486 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
487 send_ldap_result( op, rs );
492 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
493 new_ndn.bv_val, 0, 0 );
495 Debug( LDAP_DEBUG_TRACE,
496 "ldbm_back_modrdn: new ndn=%s does not exist\n",
497 new_ndn.bv_val, 0, 0 );
500 /* Get attribute type and attribute value of our new rdn, we will
501 * need to add that to our new entry
503 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
504 LDAP_DN_FORMAT_LDAP ) )
507 LDAP_LOG ( OPERATION, ERR,
508 "ldbm_back_modrdn: can't figure out "
509 "type(s)/values(s) of newrdn\n",
512 Debug( LDAP_DEBUG_TRACE,
513 "ldbm_back_modrdn: can't figure out "
514 "type(s)/values(s) of newrdn\n",
521 LDAP_LOG ( OPERATION, RESULTS,
522 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
523 "new_rdn_val=\"%s\"\n",
524 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
525 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
527 Debug( LDAP_DEBUG_TRACE,
528 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
529 "new_rdn_val=\"%s\"\n",
530 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
531 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
534 if ( op->oq_modrdn.rs_deleteoldrdn ) {
535 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
536 LDAP_DN_FORMAT_LDAP ) )
539 LDAP_LOG ( OPERATION, ERR,
540 "ldbm_back_modrdn: can't figure out "
541 "type(s)/values(s) of old_rdn\n",
544 Debug( LDAP_DEBUG_TRACE,
545 "ldbm_back_modrdn: can't figure out "
546 "the old_rdn type(s)/value(s)\n",
554 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
556 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
560 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
565 /* check for abandon */
566 if ( op->o_abandon ) {
571 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
572 send_ldap_error( op, rs, LDAP_OTHER,
573 "DN index delete fail" );
577 (void) cache_delete_entry( &li->li_cache, e );
580 /* XXX: there is no going back! */
585 e->e_nname = new_ndn;
586 new_dn.bv_val = NULL;
587 new_ndn.bv_val = NULL;
589 /* NOTE: after this you must not free new_dn or new_ndn!
590 * They are used by cache.
594 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
595 send_ldap_error( op, rs, LDAP_OTHER,
596 "DN index add failed" );
600 /* modify memory copy of entry */
601 rc_id = ldbm_modify_internal( op, &mod[0], e,
602 &rs->sr_text, textbuf, textlen );
610 send_ldap_result( op, rs );
614 /* here we may try to delete the newly added dn */
615 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
616 /* we already are in trouble ... */
622 (void) cache_update_entry( &li->li_cache, e );
625 if ( id2entry_add( op->o_bd, e ) != 0 ) {
626 send_ldap_error( op, rs, LDAP_OTHER,
627 "entry update failed" );
631 rs->sr_err = LDAP_SUCCESS;
632 send_ldap_result( op, rs );
634 cache_entry_commit( e );
637 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
638 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
640 /* LDAP v2 supporting correct attribute handling. */
641 if ( new_rdn != NULL ) {
642 ldap_rdnfree( new_rdn );
644 if ( old_rdn != NULL ) {
645 ldap_rdnfree( old_rdn );
649 for (; mod; mod = tmp ) {
651 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
658 /* LDAP v3 Support */
660 /* free new parent and writer lock */
661 cache_return_entry_w( &li->li_cache, np );
665 /* free parent and writer lock */
666 cache_return_entry_w( &li->li_cache, p );
669 /* free entry and writer lock */
670 cache_return_entry_w( &li->li_cache, e );
671 if ( rc == MUST_DESTROY ) {
672 /* if rc == MUST_DESTROY the entry is uncached
673 * and its private data is destroyed;
674 * the entry must be freed */
677 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);