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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48 AttributeDescription *children = slap_schema.si_ad_children;
50 static const char *children = "children";
52 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
53 char *p_dn = NULL, *p_ndn = NULL;
54 char *new_dn = NULL, *new_ndn = NULL;
59 const char *text = NULL;
60 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
61 char *new_rdn_val = NULL; /* Val of new rdn */
62 char *new_rdn_type = NULL; /* Type of new rdn */
63 char *old_rdn = NULL; /* Old rdn's attr type & val */
64 char *old_rdn_type = NULL; /* Type of old rdn attr. */
65 char *old_rdn_val = NULL; /* Old rdn attribute value */
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 struct berval add_bv; /* Stores new rdn att */
73 struct berval *add_bvals[2]; /* Stores new rdn att */
74 struct berval del_bv; /* Stores old rdn att */
75 struct berval *del_bvals[2]; /* Stores old rdn att */
76 Modifications mod[2]; /* Used to delete old rdn */
77 int manageDSAit = get_manageDSAit( op );
79 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
80 (newSuperior ? newSuperior : "NULL"),
83 /* get entry with writer lock */
84 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
85 char* matched_dn = NULL;
86 struct berval** refs = NULL;
88 if( matched != NULL ) {
89 matched_dn = strdup( matched->e_dn );
90 refs = is_entry_referral( matched )
91 ? get_entry_referrals( be, conn, op, matched )
93 cache_return_entry_r( &li->li_cache, matched );
95 refs = default_referral;
98 send_ldap_result( conn, op, LDAP_REFERRAL,
99 matched_dn, NULL, refs, NULL );
101 if ( matched != NULL ) {
102 ber_bvecfree( refs );
109 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
110 if ( ! access_allowed( be, conn, op, e,
111 "entry", NULL, ACL_WRITE ) )
113 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
115 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
116 NULL, NULL, NULL, NULL );
121 if (!manageDSAit && is_entry_referral( e ) ) {
122 /* parent is a referral, don't allow add */
123 /* parent is an alias, don't allow add */
124 struct berval **refs = get_entry_referrals( be,
127 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
130 send_ldap_result( conn, op, LDAP_REFERRAL,
131 e->e_dn, NULL, refs, NULL );
133 ber_bvecfree( refs );
138 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
140 /* Make sure parent entry exist and we can write its
144 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
145 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
147 send_ldap_result( conn, op, LDAP_OTHER,
148 NULL, NULL, NULL, NULL );
152 /* check parent for "children" acl */
153 if ( ! access_allowed( be, conn, op, p,
154 children, NULL, ACL_WRITE ) )
156 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
158 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
159 NULL, NULL, NULL, NULL );
163 Debug( LDAP_DEBUG_TRACE,
164 "ldbm_back_modrdn: wr to children of entry %s OK\n",
167 p_dn = dn_parent( be, e->e_dn );
170 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
174 /* no parent, modrdn entry directly under root */
175 if( ! be_isroot( be, op->o_ndn ) ) {
176 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
178 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
179 NULL, NULL, NULL, NULL );
183 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
186 Debug( LDAP_DEBUG_TRACE,
187 "ldbm_back_modrdn: no parent, locked root\n",
192 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
194 if ( newSuperior != NULL ) {
195 Debug( LDAP_DEBUG_TRACE,
196 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
199 np_dn = ch_strdup( newSuperior );
200 np_ndn = ch_strdup( np_dn );
201 (void) dn_normalize( np_ndn );
203 /* newSuperior == oldParent?, if so ==> ERROR */
204 /* newSuperior == entry being moved?, if so ==> ERROR */
205 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
207 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
208 Debug( LDAP_DEBUG_TRACE,
209 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
211 send_ldap_result( conn, op, LDAP_OTHER,
212 NULL, NULL, NULL, NULL );
216 Debug( LDAP_DEBUG_TRACE,
217 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
220 /* check newSuperior for "children" acl */
221 if ( !access_allowed( be, conn, op, np, children, NULL,
224 Debug( LDAP_DEBUG_TRACE,
225 "ldbm_back_modrdn: no wr to newSup children\n",
227 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
228 NULL, NULL, NULL, NULL );
232 if ( is_entry_alias( np ) ) {
233 /* entry is an alias, don't allow bind */
234 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
237 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
238 NULL, NULL, NULL, NULL );
243 if ( is_entry_referral( np ) ) {
244 /* parent is a referral, don't allow add */
245 /* parent is an alias, don't allow add */
246 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
249 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
250 NULL, NULL, NULL, NULL );
255 Debug( LDAP_DEBUG_TRACE,
256 "ldbm_back_modrdn: wr to new parent's children OK\n",
259 new_parent_dn = np_dn;
262 /* Build target dn and make sure target entry doesn't exist already. */
264 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
267 new_ndn = ch_strdup(new_dn);
268 (void) dn_normalize( new_ndn );
270 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
273 /* check for abandon */
274 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
275 if ( op->o_abandon ) {
276 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
280 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
281 if (dn2id ( be, new_ndn ) != NOID) {
282 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
283 NULL, NULL, NULL, NULL );
287 Debug( LDAP_DEBUG_TRACE,
288 "ldbm_back_modrdn: new ndn=%s does not exist\n",
291 /* Get attribute type and attribute value of our new rdn, we will
292 * need to add that to our new entry
295 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
297 Debug( LDAP_DEBUG_TRACE,
298 "ldbm_back_modrdn: can't figure out type of newrdn\n",
300 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
301 NULL, "unknown type used in RDN", NULL, NULL );
306 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
308 Debug( LDAP_DEBUG_TRACE,
309 "ldbm_back_modrdn: can't figure out val of newrdn\n",
311 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
312 NULL, "could not parse RDN value", NULL, NULL );
317 Debug( LDAP_DEBUG_TRACE,
318 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
319 new_rdn_val, new_rdn_type, 0 );
321 /* Retrieve the old rdn from the entry's dn */
323 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
324 Debug( LDAP_DEBUG_TRACE,
325 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
327 send_ldap_result( conn, op, LDAP_OTHER,
328 NULL, "could not parse old DN", NULL, NULL );
332 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
333 Debug( LDAP_DEBUG_TRACE,
334 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
336 send_ldap_result( conn, op, LDAP_OTHER,
337 NULL, "count parse RDN from old DN", NULL, NULL );
341 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 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_type, new_rdn_type, 0 );
348 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
351 /* Add new attribute value to the entry.
354 add_bvals[0] = &add_bv; /* Array of bervals */
357 add_bv.bv_val = new_rdn_val;
358 add_bv.bv_len = strlen(new_rdn_val);
360 #ifdef SLAPD_SCHEMA_NOT_COMPAT
364 mod[0].sml_desc = NULL;
365 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
367 if( rc != LDAP_SUCCESS ) {
368 Debug( LDAP_DEBUG_TRACE,
369 "ldbm_back_modrdn: %s: %s (new)\n",
370 text, new_rdn_type, 0 );
371 send_ldap_result( conn, op, rc,
372 NULL, text, NULL, NULL );
377 mod[0].sml_type = new_rdn_type;
379 mod[0].sml_bvalues = add_bvals;
380 mod[0].sml_op = SLAP_MOD_SOFTADD;
381 mod[0].sml_next = NULL;
383 /* Remove old rdn value if required */
386 /* Get value of old rdn */
388 if ((old_rdn_val = rdn_attr_value( old_rdn ))
391 Debug( LDAP_DEBUG_TRACE,
392 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
394 send_ldap_result( conn, op, LDAP_OTHER,
395 NULL, "could not parse value from old RDN", NULL, NULL );
399 del_bvals[0] = &del_bv; /* Array of bervals */
402 /* Remove old value of rdn as an attribute. */
404 del_bv.bv_val = old_rdn_val;
405 del_bv.bv_len = strlen(old_rdn_val);
407 #ifdef SLAPD_SCHEMA_NOT_COMPAT
411 mod[1].sml_desc = NULL;
412 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
414 if( rc != LDAP_SUCCESS ) {
415 Debug( LDAP_DEBUG_TRACE,
416 "ldbm_back_modrdn: %s: %s (old)\n",
417 text, old_rdn_type, 0 );
418 send_ldap_result( conn, op, rc,
419 NULL, text, NULL, NULL );
424 mod[1].sml_type = old_rdn_type;
426 mod[0].sml_next = &mod[1];
427 mod[1].sml_bvalues = del_bvals;
428 mod[1].sml_op = LDAP_MOD_DELETE;
429 mod[1].sml_next = NULL;
431 Debug( LDAP_DEBUG_TRACE,
432 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
436 /* check for abandon */
437 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
438 if ( op->o_abandon ) {
439 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
442 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
445 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
446 send_ldap_result( conn, op, LDAP_OTHER,
447 NULL, "DN index delete fail", NULL, NULL );
451 (void) cache_delete_entry( &li->li_cache, e );
453 /* XXX: there is no going back! */
463 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
464 send_ldap_result( conn, op, LDAP_OTHER,
465 NULL, "DN index add failed", NULL, NULL );
469 /* modify memory copy of entry */
470 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e, &text );
472 if( rc != LDAP_SUCCESS ) {
473 if( rc != SLAPD_ABANDON ) {
474 send_ldap_result( conn, op, rc,
475 NULL, text, NULL, NULL );
481 (void) cache_update_entry( &li->li_cache, e );
483 /* NOTE: after this you must not free new_dn or new_ndn!
484 * They are used by cache.
488 if ( id2entry_add( be, e ) != 0 ) {
490 send_ldap_result( conn, op, LDAP_OTHER,
491 NULL, "entry update failed", NULL, NULL );
495 send_ldap_result( conn, op, LDAP_SUCCESS,
496 NULL, NULL, NULL, NULL );
500 if( new_dn != NULL ) free( new_dn );
501 if( new_ndn != NULL ) free( new_ndn );
503 if( p_dn != NULL ) free( p_dn );
504 if( p_ndn != NULL ) free( p_ndn );
506 /* LDAP v2 supporting correct attribute handling. */
507 if( new_rdn_type != NULL ) free(new_rdn_type);
508 if( new_rdn_val != NULL ) free(new_rdn_val);
509 if( old_rdn != NULL ) free(old_rdn);
510 if( old_rdn_type != NULL ) free(old_rdn_type);
511 if( old_rdn_val != NULL ) free(old_rdn_val);
514 /* LDAP v3 Support */
515 if ( np_dn != NULL ) free( np_dn );
516 if ( np_ndn != NULL ) free( np_ndn );
519 /* free new parent and writer lock */
520 cache_return_entry_w( &li->li_cache, np );
524 /* free parent and writer lock */
525 cache_return_entry_w( &li->li_cache, p );
529 /* release root writer lock */
530 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
533 /* free entry and writer lock */
534 cache_return_entry_w( &li->li_cache, e );