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;
55 const char *text = NULL;
56 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
57 char *new_rdn_val = NULL; /* Val of new rdn */
58 char *new_rdn_type = NULL; /* Type of new rdn */
59 char *old_rdn = NULL; /* Old rdn's attr type & val */
60 char *old_rdn_type = NULL; /* Type of old rdn attr. */
61 char *old_rdn_val = NULL; /* Old rdn attribute value */
62 /* Added to support newSuperior */
63 Entry *np = NULL; /* newSuperior Entry */
64 char *np_dn = NULL; /* newSuperior dn */
65 char *np_ndn = NULL; /* newSuperior ndn */
66 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
67 /* Used to interface with ldbm_modify_internal() */
68 struct berval add_bv; /* Stores new rdn att */
69 struct berval *add_bvals[2]; /* Stores new rdn att */
70 struct berval del_bv; /* Stores old rdn att */
71 struct berval *del_bvals[2]; /* Stores old rdn att */
72 Modifications mod[2]; /* Used to delete old 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 ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
133 /* Make sure parent entry exist and we can write its
137 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
139 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
140 "ldbm_back_modrdn: parent of %s does not exist\n", e->e_ndn ));
142 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
146 send_ldap_result( conn, op, LDAP_OTHER,
147 NULL, NULL, NULL, NULL );
152 /* check parent for "children" acl */
153 if ( ! access_allowed( be, conn, op, p,
154 children, NULL, ACL_WRITE ) )
157 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
158 "ldbm_back_modrdn: no access to parent of (%s)\n", e->e_dn ));
160 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
164 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
165 NULL, NULL, NULL, NULL );
170 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
171 "ldbm_back_modrdn: wr to children of entry %s OK\n",
174 Debug( LDAP_DEBUG_TRACE,
175 "ldbm_back_modrdn: wr to children of entry %s OK\n",
179 p_dn = dn_parent( be, e->e_dn );
182 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
183 "ldbm_back_modrdn: parent dn=%s\n", p_dn ));
185 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
190 /* no parent, modrdn entry directly under root */
191 if( ! be_isroot( be, op->o_ndn ) ) {
193 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
194 "ldbm_back_modrdn: (%s) no parent & not a root.\n",
197 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
201 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
202 NULL, NULL, NULL, NULL );
206 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
210 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
211 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn ));
213 Debug( LDAP_DEBUG_TRACE,
214 "ldbm_back_modrdn: no parent, locked root\n",
219 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
221 if ( newSuperior != NULL ) {
223 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
224 "ldbm_back_modrdn: new parent \"%s\" requested\n", newSuperior ));
226 Debug( LDAP_DEBUG_TRACE,
227 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
231 np_dn = ch_strdup( newSuperior );
232 np_ndn = ch_strdup( np_dn );
233 (void) dn_normalize( np_ndn );
235 /* newSuperior == oldParent? */
236 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
238 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
239 "ldbm_back_modrdn: new parent\"%s\" seems to be the same as the old parent \"%s\"\n",
240 newSuperior, p_dn ));
242 Debug( LDAP_DEBUG_TRACE,
243 "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
244 newSuperior, p_dn, 0 );
247 newSuperior = NULL; /* ignore newSuperior */
251 if ( newSuperior != NULL ) {
252 /* newSuperior == entry being moved?, if so ==> ERROR */
253 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
255 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
257 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
258 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n", np_ndn ));
260 Debug( LDAP_DEBUG_TRACE,
261 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
265 send_ldap_result( conn, op, LDAP_OTHER,
266 NULL, NULL, NULL, NULL );
271 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
272 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
275 Debug( LDAP_DEBUG_TRACE,
276 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
280 /* check newSuperior for "children" acl */
281 if ( !access_allowed( be, conn, op, np, children, NULL,
285 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
286 "ldbm_back_modrdn: no wr to newSup children.\n" ));
288 Debug( LDAP_DEBUG_TRACE,
289 "ldbm_back_modrdn: no wr to newSup children\n",
293 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
294 NULL, NULL, NULL, NULL );
298 if ( is_entry_alias( np ) ) {
299 /* entry is an alias, don't allow bind */
301 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
302 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn ));
304 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
309 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
310 NULL, NULL, NULL, NULL );
315 if ( is_entry_referral( np ) ) {
316 /* parent is a referral, don't allow add */
317 /* parent is an alias, don't allow add */
319 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
320 "ldbm_back_modrdn: entry (%s) is a referral\n",
323 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
327 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
328 NULL, NULL, NULL, NULL );
334 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
335 "ldbm_back_modrdn: wr to new parent's children OK.\n" ));
337 Debug( LDAP_DEBUG_TRACE,
338 "ldbm_back_modrdn: wr to new parent's children OK\n",
342 new_parent_dn = np_dn;
345 /* Build target dn and make sure target entry doesn't exist already. */
346 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
348 new_ndn = ch_strdup(new_dn);
349 (void) dn_normalize( new_ndn );
352 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
353 "ldbm_back_modrdn: new ndn=%s\n", new_ndn ));
355 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
359 /* check for abandon */
360 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
361 if ( op->o_abandon ) {
362 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
366 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
367 if (dn2id ( be, new_ndn ) != NOID) {
368 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
369 NULL, NULL, NULL, NULL );
374 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
375 "ldbm_back_modrdn: new ndn (%s) does not exist\n", new_ndn ));
377 Debug( LDAP_DEBUG_TRACE,
378 "ldbm_back_modrdn: new ndn=%s does not exist\n",
382 /* Get attribute type and attribute value of our new rdn, we will
383 * need to add that to our new entry
385 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
387 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
388 "ldbm_back_modrdn: can't figure out type of newrdn\n" ));
390 Debug( LDAP_DEBUG_TRACE,
391 "ldbm_back_modrdn: can't figure out type of newrdn\n",
395 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
396 NULL, "unknown type used in RDN", NULL, NULL );
400 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
402 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
403 "ldbm_back_modrdn: can't figure out val of newrdn\n"));
405 Debug( LDAP_DEBUG_TRACE,
406 "ldbm_back_modrdn: can't figure out val of newrdn\n",
410 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
411 NULL, "could not parse RDN value", NULL, NULL );
416 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
417 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
418 new_rdn_val, new_rdn_type ));
420 Debug( LDAP_DEBUG_TRACE,
421 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
422 new_rdn_val, new_rdn_type, 0 );
425 /* Retrieve the old rdn from the entry's dn */
426 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
428 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
429 "ldbm_back_modrdn: can't figure out old_rdn from dn (%s)\n",
432 Debug( LDAP_DEBUG_TRACE,
433 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
437 send_ldap_result( conn, op, LDAP_OTHER,
438 NULL, "could not parse old DN", NULL, NULL );
442 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
444 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
445 "ldbm_back_modrdn: can't figure out the old_rdn type.\n" ));
447 Debug( LDAP_DEBUG_TRACE,
448 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
452 send_ldap_result( conn, op, LDAP_OTHER,
453 NULL, "count parse RDN from old DN", NULL, NULL );
457 if ( newSuperior == NULL
458 && strcasecmp( old_rdn_type, new_rdn_type ) != 0 )
460 /* Not a big deal but we may say something */
462 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
463 "ldbm_back_modrdn: old_rdn_type=%s new_rdn_type-%s\n",
464 old_rdn_type, new_rdn_type ));
466 Debug( LDAP_DEBUG_TRACE,
467 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
468 old_rdn_type, new_rdn_type, 0 );
473 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
474 "ldbm_back_modrdn: DN_X500\n" ));
476 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
480 /* Add new attribute value to the entry.
483 add_bvals[0] = &add_bv; /* Array of bervals */
486 add_bv.bv_val = new_rdn_val;
487 add_bv.bv_len = strlen(new_rdn_val);
492 mod[0].sml_desc = NULL;
493 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
495 if( rc != LDAP_SUCCESS ) {
497 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
498 "ldbm_back_modrdn: slap_str2ad error: %s (%s)\n",
499 text, new_rdn_type ));
501 Debug( LDAP_DEBUG_TRACE,
502 "ldbm_back_modrdn: %s: %s (new)\n",
503 text, new_rdn_type, 0 );
506 send_ldap_result( conn, op, rc,
507 NULL, text, NULL, NULL );
512 mod[0].sml_bvalues = add_bvals;
513 mod[0].sml_op = SLAP_MOD_SOFTADD;
514 mod[0].sml_next = NULL;
516 /* Remove old rdn value if required */
518 /* Get value of old rdn */
520 if ((old_rdn_val = rdn_attr_value( old_rdn )) == NULL) {
522 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
523 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n" ));
525 Debug( LDAP_DEBUG_TRACE,
526 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
530 send_ldap_result( conn, op, LDAP_OTHER,
531 NULL, "could not parse value from old RDN", NULL, NULL );
535 del_bvals[0] = &del_bv; /* Array of bervals */
538 /* Remove old value of rdn as an attribute. */
540 del_bv.bv_val = old_rdn_val;
541 del_bv.bv_len = strlen(old_rdn_val);
546 mod[1].sml_desc = NULL;
547 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
549 if( rc != LDAP_SUCCESS ) {
551 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
552 "ldbm_back_modrdn: %s: %s (old)\n",
553 text, old_rdn_type ));
555 Debug( LDAP_DEBUG_TRACE,
556 "ldbm_back_modrdn: %s: %s (old)\n",
557 text, old_rdn_type, 0 );
560 send_ldap_result( conn, op, rc,
561 NULL, text, NULL, NULL );
566 mod[0].sml_next = &mod[1];
567 mod[1].sml_bvalues = del_bvals;
568 mod[1].sml_op = LDAP_MOD_DELETE;
569 mod[1].sml_next = NULL;
572 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
573 "ldbm_back_modrdn: removing old_rdn_val=%s\n", old_rdn_val ));
575 Debug( LDAP_DEBUG_TRACE,
576 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
581 /* check for abandon */
582 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
583 if ( op->o_abandon ) {
584 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
587 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
590 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
591 send_ldap_result( conn, op, LDAP_OTHER,
592 NULL, "DN index delete fail", NULL, NULL );
596 (void) cache_delete_entry( &li->li_cache, e );
598 /* XXX: there is no going back! */
608 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
609 send_ldap_result( conn, op, LDAP_OTHER,
610 NULL, "DN index add failed", NULL, NULL );
614 /* modify memory copy of entry */
615 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e, &text );
617 if( rc != LDAP_SUCCESS ) {
618 if( rc != SLAPD_ABANDON ) {
619 send_ldap_result( conn, op, rc,
620 NULL, text, NULL, NULL );
626 (void) cache_update_entry( &li->li_cache, e );
628 /* NOTE: after this you must not free new_dn or new_ndn!
629 * They are used by cache.
633 if ( id2entry_add( be, e ) != 0 ) {
635 send_ldap_result( conn, op, LDAP_OTHER,
636 NULL, "entry update failed", NULL, NULL );
640 send_ldap_result( conn, op, LDAP_SUCCESS,
641 NULL, NULL, NULL, NULL );
645 if( new_dn != NULL ) free( new_dn );
646 if( new_ndn != NULL ) free( new_ndn );
648 if( p_dn != NULL ) free( p_dn );
649 if( p_ndn != NULL ) free( p_ndn );
651 /* LDAP v2 supporting correct attribute handling. */
652 if( new_rdn_type != NULL ) free(new_rdn_type);
653 if( new_rdn_val != NULL ) free(new_rdn_val);
654 if( old_rdn != NULL ) free(old_rdn);
655 if( old_rdn_type != NULL ) free(old_rdn_type);
656 if( old_rdn_val != NULL ) free(old_rdn_val);
658 /* LDAP v3 Support */
659 if ( np_dn != NULL ) free( np_dn );
660 if ( np_ndn != NULL ) free( np_ndn );
663 /* free new parent and writer lock */
664 cache_return_entry_w( &li->li_cache, np );
668 /* free parent and writer lock */
669 cache_return_entry_w( &li->li_cache, p );
673 /* release root writer lock */
674 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
677 /* free entry and writer lock */
678 cache_return_entry_w( &li->li_cache, e );