1 /* modrdn.c - ldbm backend modrdn routine */
4 * Copyright 1998-2000 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"
44 const char *newSuperior
47 AttributeDescription *children = slap_schema.si_ad_children;
48 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
49 char *p_dn = NULL, *p_ndn = NULL;
50 char *new_dn = NULL, *new_ndn = NULL;
54 int rc = -1, rc_id = 0;
56 const char *text = NULL;
57 char textbuf[SLAP_TEXT_BUFLEN];
58 size_t textlen = sizeof textbuf;
59 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
60 char **new_rdn_vals = NULL; /* Vals of new rdn */
61 char **new_rdn_types = NULL; /* Types of new rdn */
63 char *old_rdn = NULL; /* Old rdn's attr type & val */
64 char **old_rdn_types = NULL; /* Types of old rdn attrs. */
65 char **old_rdn_vals = NULL; /* Old rdn attribute values */
66 /* Added to support newSuperior */
67 Entry *np = NULL; /* newSuperior Entry */
68 char *np_dn = NULL; /* newSuperior dn */
69 char *np_ndn = NULL; /* newSuperior ndn */
70 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
71 /* Used to interface with ldbm_modify_internal() */
72 Modifications *mod = NULL; /* Used to delete old/add new rdn */
73 int manageDSAit = get_manageDSAit( op );
76 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
77 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
78 dn ? dn : "NULL", newSuperior ? newSuperior : "NULL" ));
80 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
81 (newSuperior ? newSuperior : "NULL"),
85 /* get entry with writer lock */
86 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
87 char* matched_dn = NULL;
88 struct berval** refs = NULL;
90 if( matched != NULL ) {
91 matched_dn = strdup( matched->e_dn );
92 refs = is_entry_referral( matched )
93 ? get_entry_referrals( be, conn, op, matched )
95 cache_return_entry_r( &li->li_cache, matched );
97 refs = default_referral;
100 send_ldap_result( conn, op, LDAP_REFERRAL,
101 matched_dn, NULL, refs, NULL );
103 if ( matched != NULL ) {
104 ber_bvecfree( refs );
111 if (!manageDSAit && is_entry_referral( e ) ) {
112 /* parent is a referral, don't allow add */
113 /* parent is an alias, don't allow add */
114 struct berval **refs = get_entry_referrals( be,
118 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
119 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn ));
121 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
125 send_ldap_result( conn, op, LDAP_REFERRAL,
126 e->e_dn, NULL, refs, NULL );
128 ber_bvecfree( refs );
132 if ( has_children( be, e ) ) {
134 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
135 "ldbm_back_modrdn: entry %s has children\n", e->e_dn ));
137 Debug( LDAP_DEBUG_TRACE, "entry %s referral\n", e->e_dn,
141 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
142 NULL, "subtree rename not supported", NULL, NULL );
146 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL && p_ndn[0] != '\0' ) {
147 /* Make sure parent entry exist and we can write its
151 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
153 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
154 "ldbm_back_modrdn: parent of %s does not exist\n", e->e_ndn ));
156 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
160 send_ldap_result( conn, op, LDAP_OTHER,
161 NULL, "parent entry does not exist", NULL, NULL );
166 /* check parent for "children" acl */
167 if ( ! access_allowed( be, conn, op, p,
168 children, NULL, ACL_WRITE ) )
171 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
172 "ldbm_back_modrdn: no access to parent of (%s)\n", e->e_dn ));
174 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
178 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
179 NULL, NULL, NULL, NULL );
184 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
185 "ldbm_back_modrdn: wr to children of entry %s OK\n",
188 Debug( LDAP_DEBUG_TRACE,
189 "ldbm_back_modrdn: wr to children of entry %s OK\n",
193 p_dn = dn_parent( be, e->e_dn );
196 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
197 "ldbm_back_modrdn: parent dn=%s\n", p_dn ));
199 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
204 /* no parent, modrdn entry directly under root */
205 if( ! be_isroot( be, op->o_ndn ) && ! be_issuffix( be, "" ) ) {
207 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
208 "ldbm_back_modrdn: (%s) no parent & not a root.\n",
211 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
215 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
216 NULL, NULL, NULL, NULL );
220 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
224 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
225 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
227 Debug( LDAP_DEBUG_TRACE,
228 "ldbm_back_modrdn: no parent, locked root\n",
233 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
235 if ( newSuperior != NULL ) {
237 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
238 "ldbm_back_modrdn: new parent \"%s\" requested\n", newSuperior ));
240 Debug( LDAP_DEBUG_TRACE,
241 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
245 np_dn = ch_strdup( newSuperior );
246 np_ndn = ch_strdup( np_dn );
247 (void) dn_normalize( np_ndn );
249 /* newSuperior == oldParent? */
250 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
252 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
253 "ldbm_back_modrdn: new parent\"%s\" seems to be the same as the old parent \"%s\"\n",
254 newSuperior, p_dn ));
256 Debug( LDAP_DEBUG_TRACE,
257 "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
258 newSuperior, p_dn, 0 );
261 newSuperior = NULL; /* ignore newSuperior */
265 if ( newSuperior != NULL ) {
266 /* newSuperior == entry being moved?, if so ==> ERROR */
267 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
269 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
271 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
272 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", np_ndn ));
274 Debug( LDAP_DEBUG_TRACE,
275 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
279 send_ldap_result( conn, op, LDAP_OTHER,
280 NULL, "newSuperior not found", NULL, NULL );
285 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
286 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
289 Debug( LDAP_DEBUG_TRACE,
290 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
294 /* check newSuperior for "children" acl */
295 if ( !access_allowed( be, conn, op, np, children, NULL,
299 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
300 "ldbm_back_modrdn: no wr to newSup children.\n" ));
302 Debug( LDAP_DEBUG_TRACE,
303 "ldbm_back_modrdn: no wr to newSup children\n",
307 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
308 NULL, NULL, NULL, NULL );
312 if ( is_entry_alias( np ) ) {
313 /* entry is an alias, don't allow bind */
315 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
316 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn ));
318 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
323 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
324 NULL, "newSuperior is an alias", NULL, NULL );
329 if ( is_entry_referral( np ) ) {
330 /* parent is a referral, don't allow add */
331 /* parent is an alias, don't allow add */
333 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
334 "ldbm_back_modrdn: entry (%s) is a referral\n",
337 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
341 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
342 NULL, "newSuperior is a referral", NULL, NULL );
348 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
349 "ldbm_back_modrdn: wr to new parent's children OK.\n" ));
351 Debug( LDAP_DEBUG_TRACE,
352 "ldbm_back_modrdn: wr to new parent's children OK\n",
356 new_parent_dn = np_dn;
359 /* Build target dn and make sure target entry doesn't exist already. */
360 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
362 new_ndn = ch_strdup(new_dn);
363 (void) dn_normalize( new_ndn );
366 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
367 "ldbm_back_modrdn: new ndn=%s\n", new_ndn ));
369 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
373 /* check for abandon */
374 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
375 if ( op->o_abandon ) {
376 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
380 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
381 if ( ( rc_id = dn2id ( be, new_ndn, &id ) ) || id != NOID ) {
382 /* if (rc_id) something bad happened to ldbm cache */
383 send_ldap_result( conn, op,
384 rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
385 NULL, NULL, NULL, NULL );
390 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
391 "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn ));
393 Debug( LDAP_DEBUG_TRACE,
394 "ldbm_back_modrdn: new ndn=%s does not exist\n",
399 /* Get attribute types and values of our new rdn, we will
400 * need to add that to our new entry
402 if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
404 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
405 "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n" ));
407 Debug( LDAP_DEBUG_TRACE,
408 "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n",
412 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
413 NULL, "unable to parse type(s)/value(s) used in RDN", NULL, NULL );
418 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
419 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
420 new_rdn_vals[0], new_rdn_types[0] ));
422 Debug( LDAP_DEBUG_TRACE,
423 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
424 new_rdn_vals[0], new_rdn_types[0], 0 );
427 /* Retrieve the old rdn from the entry's dn */
428 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
430 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
431 "ldbm_back_modrdn: can't figure out old_rdn from dn (%s)\n",
434 Debug( LDAP_DEBUG_TRACE,
435 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
439 send_ldap_result( conn, op, LDAP_OTHER,
440 NULL, "could not parse old DN", NULL, NULL );
444 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
446 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
447 "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s).\n" ));
449 Debug( LDAP_DEBUG_TRACE,
450 "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s)\n",
454 send_ldap_result( conn, op, LDAP_OTHER,
455 NULL, "unable to parse type(s)/value(s) used in RDN from old DN", NULL, NULL );
459 if ( newSuperior == NULL
460 && charray_strcasecmp( (const char **)old_rdn_types, (const char **)new_rdn_types ) != 0 )
462 /* Not a big deal but we may say something */
464 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
465 "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type=%s\n",
466 old_rdn_types[0], new_rdn_types[0] ));
468 Debug( LDAP_DEBUG_TRACE,
469 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
470 old_rdn_types[0], new_rdn_types[0], 0 );
475 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
476 "ldbm_back_modrdn: DN_X500\n" ));
478 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
483 for ( a_cnt = 0; new_rdn_types[a_cnt]; a_cnt++ ) {
485 Modifications *mod_tmp;
487 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
489 mod_tmp->sml_desc = NULL;
490 rc = slap_str2ad( new_rdn_types[a_cnt],
491 &mod_tmp->sml_desc, &text );
493 if ( rc != LDAP_SUCCESS ) {
495 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
496 "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
497 text, new_rdn_types[a_cnt] ));
499 Debug( LDAP_DEBUG_TRACE,
500 "ldbm_back_modrdn: %s: %s (new)\n",
501 text, new_rdn_types[a_cnt], 0 );
504 send_ldap_result( conn, op, rc,
505 NULL, text, NULL, NULL );
510 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
511 mod_tmp->sml_bvalues[0] = ber_bvstrdup( new_rdn_vals[a_cnt] );
512 mod_tmp->sml_bvalues[1] = NULL;
513 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
514 mod_tmp->sml_next = mod;
518 /* Remove old rdn value if required */
519 if ( deleteoldrdn ) {
520 /* Get value of old rdn */
521 if ( old_rdn_vals == NULL ) {
523 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
524 "ldbm_back_modrdn: can't figure out old RDN value(s) from old RDN\n" ));
526 Debug( LDAP_DEBUG_TRACE,
527 "ldbm_back_modrdn: can't figure out oldRDN value(s) from old RDN\n",
531 send_ldap_result( conn, op, LDAP_OTHER,
532 NULL, "could not parse value(s) from old RDN", NULL, NULL );
536 for ( d_cnt = 0; old_rdn_types[d_cnt]; d_cnt++ ) {
538 Modifications *mod_tmp;
540 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
543 mod_tmp->sml_desc = NULL;
544 rc = slap_str2ad( old_rdn_types[d_cnt],
545 &mod_tmp->sml_desc, &text );
547 if ( rc != LDAP_SUCCESS ) {
549 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
550 "ldbm_back_modrdn: %s: %s (old)\n",
551 text, old_rdn_types[d_cnt] ));
553 Debug( LDAP_DEBUG_TRACE,
554 "ldbm_back_modrdn: %s: %s (old)\n",
555 text, old_rdn_types[d_cnt], 0 );
558 send_ldap_result( conn, op, rc,
559 NULL, text, NULL, NULL );
564 /* Remove old value of rdn as an attribute. */
565 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
566 mod_tmp->sml_bvalues[0] = ber_bvstrdup( old_rdn_vals[d_cnt] );
567 mod_tmp->sml_bvalues[1] = NULL;
568 mod_tmp->sml_op = LDAP_MOD_DELETE;
569 mod_tmp->sml_next = mod;
573 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
574 "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_vals[0] ));
576 Debug( LDAP_DEBUG_TRACE,
577 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
578 old_rdn_vals[0], 0, 0 );
584 /* check for abandon */
585 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
586 if ( op->o_abandon ) {
587 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
590 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
593 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
594 send_ldap_result( conn, op, LDAP_OTHER,
595 NULL, "DN index delete fail", NULL, NULL );
599 (void) cache_delete_entry( &li->li_cache, e );
601 /* XXX: there is no going back! */
611 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
612 send_ldap_result( conn, op, LDAP_OTHER,
613 NULL, "DN index add failed", NULL, NULL );
617 /* modify memory copy of entry */
618 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e,
619 &text, textbuf, textlen );
621 if( rc != LDAP_SUCCESS ) {
622 if( rc != SLAPD_ABANDON ) {
623 send_ldap_result( conn, op, rc,
624 NULL, text, NULL, NULL );
631 (void) cache_update_entry( &li->li_cache, e );
633 /* NOTE: after this you must not free new_dn or new_ndn!
634 * They are used by cache.
638 if ( id2entry_add( be, e ) != 0 ) {
639 send_ldap_result( conn, op, LDAP_OTHER,
640 NULL, "entry update failed", NULL, NULL );
644 send_ldap_result( conn, op, LDAP_SUCCESS,
645 NULL, NULL, NULL, NULL );
647 cache_entry_commit( e );
650 if( new_dn != NULL ) free( new_dn );
651 if( new_ndn != NULL ) free( new_ndn );
653 if( p_dn != NULL ) free( p_dn );
654 if( p_ndn != NULL ) free( p_ndn );
656 /* LDAP v2 supporting correct attribute handling. */
657 if( new_rdn_types != NULL ) charray_free( new_rdn_types );
658 if( new_rdn_vals != NULL ) charray_free( new_rdn_vals );
659 if( old_rdn != NULL ) free(old_rdn);
660 if( old_rdn_types != NULL ) charray_free( old_rdn_types );
661 if( old_rdn_vals != NULL ) charray_free( old_rdn_vals );
664 slap_mods_free( mod );
667 /* LDAP v3 Support */
668 if ( np_dn != NULL ) free( np_dn );
669 if ( np_ndn != NULL ) free( np_ndn );
672 /* free new parent and writer lock */
673 cache_return_entry_w( &li->li_cache, np );
677 /* free parent and writer lock */
678 cache_return_entry_w( &li->li_cache, p );
682 /* release root writer lock */
683 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
686 /* free entry and writer lock */
687 cache_return_entry_w( &li->li_cache, e );
689 /* if rc != 0 the entry is uncached and its private data
690 * is destroyed; the entry must be freed */