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 #define CAN_ROLLBACK -1
55 #define MUST_DESTROY 1
56 int rc = CAN_ROLLBACK;
59 const char *text = NULL;
60 char textbuf[SLAP_TEXT_BUFLEN];
61 size_t textlen = sizeof textbuf;
62 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
63 char **new_rdn_vals = NULL; /* Vals of new rdn */
64 char **new_rdn_types = NULL; /* Types of new rdn */
66 char *old_rdn = NULL; /* Old rdn's attr type & val */
67 char **old_rdn_types = NULL; /* Types of old rdn attrs. */
68 char **old_rdn_vals = NULL; /* Old rdn attribute values */
69 /* Added to support newSuperior */
70 Entry *np = NULL; /* newSuperior Entry */
71 char *np_dn = NULL; /* newSuperior dn */
72 char *np_ndn = NULL; /* newSuperior ndn */
73 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
74 /* Used to interface with ldbm_modify_internal() */
75 Modifications *mod = NULL; /* Used to delete old/add new rdn */
76 int manageDSAit = get_manageDSAit( op );
78 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
79 (newSuperior ? newSuperior : "NULL"),
82 /* get entry with writer lock */
83 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
84 char* matched_dn = NULL;
85 struct berval** refs = NULL;
87 if( matched != NULL ) {
88 matched_dn = strdup( matched->e_dn );
89 refs = is_entry_referral( matched )
90 ? get_entry_referrals( be, conn, op, matched )
92 cache_return_entry_r( &li->li_cache, matched );
94 refs = default_referral;
97 send_ldap_result( conn, op, LDAP_REFERRAL,
98 matched_dn, NULL, refs, NULL );
100 if ( matched != NULL ) {
101 ber_bvecfree( refs );
108 if (!manageDSAit && is_entry_referral( e ) ) {
109 /* parent is a referral, don't allow add */
110 /* parent is an alias, don't allow add */
111 struct berval **refs = get_entry_referrals( be,
114 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
117 send_ldap_result( conn, op, LDAP_REFERRAL,
118 e->e_dn, NULL, refs, NULL );
120 ber_bvecfree( refs );
124 if ( has_children( be, e ) ) {
125 Debug( LDAP_DEBUG_TRACE, "entry %s referral\n", e->e_dn,
128 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
129 NULL, "subtree rename not supported", NULL, NULL );
133 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL && p_ndn[0] != '\0' ) {
134 /* Make sure parent entry exist and we can write its
138 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
139 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
142 send_ldap_result( conn, op, LDAP_OTHER,
143 NULL, "parent entry does not exist", NULL, NULL );
148 /* check parent for "children" acl */
149 if ( ! access_allowed( be, conn, op, p,
150 children, NULL, ACL_WRITE ) )
152 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
155 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
156 NULL, NULL, NULL, NULL );
160 Debug( LDAP_DEBUG_TRACE,
161 "ldbm_back_modrdn: wr to children of entry %s OK\n",
164 p_dn = dn_parent( be, e->e_dn );
166 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
170 /* no parent, modrdn entry directly under root */
171 if( ! be_isroot( be, op->o_ndn ) && ! be_issuffix( be, "" ) ) {
172 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
175 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
176 NULL, NULL, NULL, NULL );
180 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
183 Debug( LDAP_DEBUG_TRACE,
184 "ldbm_back_modrdn: no parent, locked root\n",
188 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
190 if ( newSuperior != NULL ) {
191 Debug( LDAP_DEBUG_TRACE,
192 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
195 np_dn = ch_strdup( newSuperior );
196 np_ndn = ch_strdup( np_dn );
197 (void) dn_normalize( np_ndn );
199 /* newSuperior == oldParent? */
200 if ( strcmp( p_ndn, np_ndn ) == 0 ) {
201 Debug( LDAP_DEBUG_TRACE,
202 "ldbm_back_modrdn: new parent \"%s\" seems to be the same as old parent \"%s\"...\n",
203 newSuperior, p_dn, 0 );
205 newSuperior = NULL; /* ignore newSuperior */
209 if ( newSuperior != NULL ) {
210 /* newSuperior == entry being moved?, if so ==> ERROR */
211 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
213 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
214 Debug( LDAP_DEBUG_TRACE,
215 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
218 send_ldap_result( conn, op, LDAP_OTHER,
219 NULL, "newSuperior not found", NULL, NULL );
223 Debug( LDAP_DEBUG_TRACE,
224 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
227 /* check newSuperior for "children" acl */
228 if ( !access_allowed( be, conn, op, np, children, NULL,
231 Debug( LDAP_DEBUG_TRACE,
232 "ldbm_back_modrdn: no wr to newSup children\n",
235 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
236 NULL, NULL, NULL, NULL );
240 if ( is_entry_alias( np ) ) {
241 /* entry is an alias, don't allow bind */
242 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
246 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
247 NULL, "newSuperior is an alias", NULL, NULL );
252 if ( is_entry_referral( np ) ) {
253 /* parent is a referral, don't allow add */
254 /* parent is an alias, don't allow add */
255 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
258 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
259 NULL, "newSuperior is a referral", NULL, NULL );
264 Debug( LDAP_DEBUG_TRACE,
265 "ldbm_back_modrdn: wr to new parent's children OK\n",
268 new_parent_dn = np_dn;
271 /* Build target dn and make sure target entry doesn't exist already. */
272 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
274 new_ndn = ch_strdup(new_dn);
275 (void) dn_normalize( new_ndn );
277 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
280 /* check for abandon */
281 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
282 if ( op->o_abandon ) {
283 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
287 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
288 if ( ( rc_id = dn2id ( be, new_ndn, &id ) ) || id != NOID ) {
289 /* if (rc_id) something bad happened to ldbm cache */
290 send_ldap_result( conn, op,
291 rc_id ? LDAP_OPERATIONS_ERROR : LDAP_ALREADY_EXISTS,
292 NULL, NULL, NULL, NULL );
296 Debug( LDAP_DEBUG_TRACE,
297 "ldbm_back_modrdn: new ndn=%s does not exist\n",
301 /* Get attribute types and values of our new rdn, we will
302 * need to add that to our new entry
304 if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
305 Debug( LDAP_DEBUG_TRACE,
306 "ldbm_back_modrdn: can't figure out type(s)/value(s) of newrdn\n",
309 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310 NULL, "unable to parse type(s)/value(s) used in RDN", NULL, NULL );
314 Debug( LDAP_DEBUG_TRACE,
315 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
316 new_rdn_vals[0], new_rdn_types[0], 0 );
318 /* Retrieve the old rdn from the entry's dn */
319 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
320 Debug( LDAP_DEBUG_TRACE,
321 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
324 send_ldap_result( conn, op, LDAP_OTHER,
325 NULL, "could not parse old DN", NULL, NULL );
329 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
330 Debug( LDAP_DEBUG_TRACE,
331 "ldbm_back_modrdn: can't figure out the old_rdn type(s)/value(s)\n",
334 send_ldap_result( conn, op, LDAP_OTHER,
335 NULL, "unable to parse type(s)/value(s) used in RDN from old DN", NULL, NULL );
339 if ( newSuperior == NULL
340 && charray_strcasecmp( (const char **)old_rdn_types, (const char **)new_rdn_types ) != 0 )
342 /* Not a big deal but we may say something */
343 Debug( LDAP_DEBUG_TRACE,
344 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
345 old_rdn_types[0], new_rdn_types[0], 0 );
348 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
352 for ( a_cnt = 0; new_rdn_types[a_cnt]; a_cnt++ ) {
354 Modifications *mod_tmp;
357 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
359 mod_tmp->sml_desc = NULL;
360 rc = slap_str2ad( new_rdn_types[a_cnt],
361 &mod_tmp->sml_desc, &text );
363 if ( rc != LDAP_SUCCESS ) {
364 Debug( LDAP_DEBUG_TRACE,
365 "ldbm_back_modrdn: %s: %s (new)\n",
366 text, new_rdn_types[a_cnt], 0 );
368 send_ldap_result( conn, op, rc,
369 NULL, text, NULL, NULL );
374 val.bv_val = new_rdn_vals[a_cnt];
375 val.bv_len = strlen( val.bv_val );
376 if ( ! access_allowed( be, conn, op, e,
377 mod_tmp->sml_desc, &val, ACL_WRITE ) ) {
378 Debug( LDAP_DEBUG_TRACE,
379 "ldbm_back_modrdn: access not allowed "
380 "to attr \"%s\"\n%s%s",
381 new_rdn_types[a_cnt], "", "" );
382 send_ldap_result( conn, op,
383 LDAP_INSUFFICIENT_ACCESS,
384 NULL, NULL, NULL, NULL );
389 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
390 mod_tmp->sml_bvalues[0] = ber_bvstrdup( new_rdn_vals[a_cnt] );
391 mod_tmp->sml_bvalues[1] = NULL;
392 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
393 mod_tmp->sml_next = mod;
397 /* Remove old rdn value if required */
398 if ( deleteoldrdn ) {
399 /* Get value of old rdn */
400 if ( old_rdn_vals == NULL ) {
401 Debug( LDAP_DEBUG_TRACE,
402 "ldbm_back_modrdn: can't figure out oldRDN value(s) from old RDN\n",
405 send_ldap_result( conn, op, LDAP_OTHER,
406 NULL, "could not parse value(s) from old RDN", NULL, NULL );
410 for ( d_cnt = 0; old_rdn_types[d_cnt]; d_cnt++ ) {
412 Modifications *mod_tmp;
415 mod_tmp = (Modifications *)ch_malloc( sizeof( Modifications ) );
417 mod_tmp->sml_desc = NULL;
418 rc = slap_str2ad( old_rdn_types[d_cnt],
419 &mod_tmp->sml_desc, &text );
421 if ( rc != LDAP_SUCCESS ) {
422 Debug( LDAP_DEBUG_TRACE,
423 "ldbm_back_modrdn: %s: %s (old)\n",
424 text, old_rdn_types[d_cnt], 0 );
426 send_ldap_result( conn, op, rc,
427 NULL, text, NULL, NULL );
432 val.bv_val = old_rdn_vals[d_cnt];
433 val.bv_len = strlen( val.bv_val );
434 if ( ! access_allowed( be, conn, op, e,
435 mod_tmp->sml_desc, &val, ACL_WRITE ) ) {
436 Debug( LDAP_DEBUG_TRACE,
437 "ldbm_back_modrdn: access not allowed "
438 "to attr \"%s\"\n%s%s",
439 old_rdn_types[d_cnt], "", "" );
440 send_ldap_result( conn, op,
441 LDAP_INSUFFICIENT_ACCESS,
442 NULL, NULL, NULL, NULL );
447 /* Remove old value of rdn as an attribute. */
448 mod_tmp->sml_bvalues = (struct berval **)ch_malloc( 2 * sizeof(struct berval *) );
449 mod_tmp->sml_bvalues[0] = ber_bvstrdup( old_rdn_vals[d_cnt] );
450 mod_tmp->sml_bvalues[1] = NULL;
451 mod_tmp->sml_op = LDAP_MOD_DELETE;
452 mod_tmp->sml_next = mod;
455 Debug( LDAP_DEBUG_TRACE,
456 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
457 old_rdn_vals[0], 0, 0 );
462 /* check for abandon */
463 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
464 if ( op->o_abandon ) {
465 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
468 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
471 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
472 send_ldap_result( conn, op, LDAP_OTHER,
473 NULL, "DN index delete fail", NULL, NULL );
477 (void) cache_delete_entry( &li->li_cache, e );
480 /* XXX: there is no going back! */
490 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
491 send_ldap_result( conn, op, LDAP_OTHER,
492 NULL, "DN index add failed", NULL, NULL );
496 /* modify memory copy of entry */
497 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e,
498 &text, textbuf, textlen );
500 if( rc != LDAP_SUCCESS ) {
501 if( rc != SLAPD_ABANDON ) {
502 send_ldap_result( conn, op, rc,
503 NULL, text, NULL, NULL );
506 /* here we may try to delete the newly added dn */
507 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
508 /* we already are in trouble ... */
515 (void) cache_update_entry( &li->li_cache, e );
517 /* NOTE: after this you must not free new_dn or new_ndn!
518 * They are used by cache.
522 if ( id2entry_add( be, e ) != 0 ) {
523 send_ldap_result( conn, op, LDAP_OTHER,
524 NULL, "entry update failed", NULL, NULL );
528 send_ldap_result( conn, op, LDAP_SUCCESS,
529 NULL, NULL, NULL, NULL );
531 cache_entry_commit( e );
534 if( new_dn != NULL ) free( new_dn );
535 if( new_ndn != NULL ) free( new_ndn );
537 if( p_dn != NULL ) free( p_dn );
538 if( p_ndn != NULL ) free( p_ndn );
540 /* LDAP v2 supporting correct attribute handling. */
541 if( new_rdn_types != NULL ) charray_free( new_rdn_types );
542 if( new_rdn_vals != NULL ) charray_free( new_rdn_vals );
543 if( old_rdn != NULL ) free(old_rdn);
544 if( old_rdn_types != NULL ) charray_free( old_rdn_types );
545 if( old_rdn_vals != NULL ) charray_free( old_rdn_vals );
548 slap_mods_free( mod );
551 /* LDAP v3 Support */
552 if ( np_dn != NULL ) free( np_dn );
553 if ( np_ndn != NULL ) free( np_ndn );
556 /* free new parent and writer lock */
557 cache_return_entry_w( &li->li_cache, np );
561 /* free parent and writer lock */
562 cache_return_entry_w( &li->li_cache, p );
566 /* release root writer lock */
567 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
570 /* free entry and writer lock */
571 cache_return_entry_w( &li->li_cache, e );
572 if ( rc == MUST_DESTROY ) {
573 /* if rc == MUST_DESTROY the entry is uncached
574 * and its private data is destroyed;
575 * the entry must be freed */