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"
47 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48 static AttributeDescription *children = NULL;
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 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
60 char *new_rdn_val = NULL; /* Val of new rdn */
61 char *new_rdn_type = NULL; /* Type of new rdn */
62 char *old_rdn = NULL; /* Old rdn's attr type & val */
63 char *old_rdn_type = NULL; /* Type of old rdn attr. */
64 char *old_rdn_val = NULL; /* Old rdn attribute value */
65 /* Added to support newSuperior */
66 Entry *np = NULL; /* newSuperior Entry */
67 char *np_dn = NULL; /* newSuperior dn */
68 char *np_ndn = NULL; /* newSuperior ndn */
69 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
70 /* Used to interface with ldbm_modify_internal() */
71 struct berval add_bv; /* Stores new rdn att */
72 struct berval *add_bvals[2]; /* Stores new rdn att */
73 struct berval del_bv; /* Stores old rdn att */
74 struct berval *del_bvals[2]; /* Stores old rdn att */
75 Modifications mod[2]; /* Used to delete old 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 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
109 if ( ! access_allowed( be, conn, op, e,
110 "entry", NULL, ACL_WRITE ) )
112 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
114 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
115 NULL, NULL, NULL, NULL );
120 if (!manageDSAit && is_entry_referral( e ) ) {
121 /* parent is a referral, don't allow add */
122 /* parent is an alias, don't allow add */
123 struct berval **refs = get_entry_referrals( be,
126 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
129 send_ldap_result( conn, op, LDAP_REFERRAL,
130 e->e_dn, NULL, refs, NULL );
132 ber_bvecfree( refs );
137 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
139 /* Make sure parent entry exist and we can write its
143 if( (p = dn2entry_w( be, p_ndn, NULL )) == NULL) {
144 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
146 send_ldap_result( conn, op, LDAP_OTHER,
147 NULL, NULL, NULL, NULL );
151 /* check parent for "children" acl */
152 if ( ! access_allowed( be, conn, op, p,
153 children, NULL, ACL_WRITE ) )
155 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
157 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
158 NULL, NULL, NULL, NULL );
162 Debug( LDAP_DEBUG_TRACE,
163 "ldbm_back_modrdn: wr to children of entry %s OK\n",
166 p_dn = dn_parent( be, e->e_dn );
169 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
173 /* no parent, modrdn entry directly under root */
174 if( ! be_isroot( be, op->o_ndn ) ) {
175 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
177 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
178 NULL, NULL, NULL, NULL );
182 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
185 Debug( LDAP_DEBUG_TRACE,
186 "ldbm_back_modrdn: no parent, locked root\n",
191 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
193 if ( (np_dn = newSuperior) != NULL) {
194 Debug( LDAP_DEBUG_TRACE,
195 "ldbm_back_modrdn: new parent requested...\n",
198 np_ndn = ch_strdup( np_dn );
199 (void) dn_normalize( np_ndn );
201 /* newSuperior == oldParent?, if so ==> ERROR */
202 /* newSuperior == entry being moved?, if so ==> ERROR */
203 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
205 if( (np = dn2entry_w( be, np_ndn, NULL )) == NULL) {
206 Debug( LDAP_DEBUG_TRACE,
207 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
209 send_ldap_result( conn, op, LDAP_OTHER,
210 NULL, NULL, NULL, NULL );
214 Debug( LDAP_DEBUG_TRACE,
215 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
218 /* check newSuperior for "children" acl */
219 if ( !access_allowed( be, conn, op, np, children, NULL,
222 Debug( LDAP_DEBUG_TRACE,
223 "ldbm_back_modrdn: no wr to newSup children\n",
225 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
226 NULL, NULL, NULL, NULL );
230 if ( is_entry_alias( np ) ) {
231 /* entry is an alias, don't allow bind */
232 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
235 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
236 NULL, NULL, NULL, NULL );
241 if ( is_entry_referral( np ) ) {
242 /* parent is a referral, don't allow add */
243 /* parent is an alias, don't allow add */
244 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
247 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
248 NULL, NULL, NULL, NULL );
253 Debug( LDAP_DEBUG_TRACE,
254 "ldbm_back_modrdn: wr to new parent's children OK\n",
257 new_parent_dn = np_dn;
260 /* Build target dn and make sure target entry doesn't exist already. */
262 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
265 new_ndn = ch_strdup(new_dn);
266 (void) dn_normalize( new_ndn );
268 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
271 /* check for abandon */
272 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
273 if ( op->o_abandon ) {
274 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
278 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
279 if (dn2id ( be, new_ndn ) != NOID) {
280 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
281 NULL, NULL, NULL, NULL );
285 Debug( LDAP_DEBUG_TRACE,
286 "ldbm_back_modrdn: new ndn=%s does not exist\n",
289 /* Get attribute type and attribute value of our new rdn, we will
290 * need to add that to our new entry
293 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
295 Debug( LDAP_DEBUG_TRACE,
296 "ldbm_back_modrdn: can't figure out type of newrdn\n",
298 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
299 NULL, "unknown type used in RDN", NULL, NULL );
304 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
306 Debug( LDAP_DEBUG_TRACE,
307 "ldbm_back_modrdn: can't figure out val of newrdn\n",
309 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
310 NULL, "could not parse RDN value", NULL, NULL );
315 Debug( LDAP_DEBUG_TRACE,
316 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
317 new_rdn_val, new_rdn_type, 0 );
319 /* Retrieve the old rdn from the entry's dn */
321 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
323 Debug( LDAP_DEBUG_TRACE,
324 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
326 send_ldap_result( conn, op, LDAP_OTHER,
327 NULL, "could not parse old DN", NULL, NULL );
332 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
334 Debug( LDAP_DEBUG_TRACE,
335 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
337 send_ldap_result( conn, op, LDAP_OTHER,
338 NULL, "count parse RDN from old DN", NULL, NULL );
343 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
345 /* Not a big deal but we may say something */
346 Debug( LDAP_DEBUG_TRACE,
347 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
348 old_rdn_type, new_rdn_type, 0 );
352 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
355 /* Add new attribute value to the entry.
358 add_bvals[0] = &add_bv; /* Array of bervals */
361 add_bv.bv_val = new_rdn_val;
362 add_bv.bv_len = strlen(new_rdn_val);
364 #ifdef SLAPD_SCHEMA_NOT_COMPAT
365 /* not yet implemented */
367 mod[0].ml_type = new_rdn_type;
368 mod[0].ml_bvalues = add_bvals;
369 mod[0].ml_op = SLAP_MOD_SOFTADD;
370 mod[0].ml_next = NULL;
373 /* Remove old rdn value if required */
376 /* Get value of old rdn */
378 if ((old_rdn_val = rdn_attr_value( old_rdn ))
381 Debug( LDAP_DEBUG_TRACE,
382 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
384 send_ldap_result( conn, op, LDAP_OTHER,
385 NULL, "cound not parse value from old RDN", NULL, NULL );
389 del_bvals[0] = &del_bv; /* Array of bervals */
392 /* Remove old value of rdn as an attribute. */
394 del_bv.bv_val = old_rdn_val;
395 del_bv.bv_len = strlen(old_rdn_val);
397 #ifdef SLAPD_SCHEMA_NOT_COMPAT
398 /* not yet implemented */
400 /* No need to normalize old_rdn_type, delete_values()
403 mod[0].ml_next = &mod[1];
404 mod[1].ml_type = old_rdn_type;
405 mod[1].ml_bvalues = del_bvals;
406 mod[1].ml_op = LDAP_MOD_DELETE;
407 mod[1].ml_next = NULL;
410 Debug( LDAP_DEBUG_TRACE,
411 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
415 /* check for abandon */
416 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
417 if ( op->o_abandon ) {
418 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
421 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
424 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
425 send_ldap_result( conn, op, LDAP_OTHER,
426 NULL, "DN index delete fail", NULL, NULL );
430 (void) cache_delete_entry( &li->li_cache, e );
432 /* XXX: there is no going back! */
442 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
443 send_ldap_result( conn, op, LDAP_OTHER,
444 NULL, "DN index add failed", NULL, NULL );
448 /* modify memory copy of entry */
449 rc = ldbm_modify_internal( be, conn, op, dn, &mod[0], e );
451 if( rc != LDAP_SUCCESS ) {
452 if( rc != SLAPD_ABANDON ) {
453 send_ldap_result( conn, op, rc,
454 NULL, NULL, NULL, NULL );
460 (void) cache_update_entry( &li->li_cache, e );
462 /* NOTE: after this you must not free new_dn or new_ndn!
463 * They are used by cache.
467 if ( id2entry_add( be, e ) != 0 ) {
469 send_ldap_result( conn, op, LDAP_OTHER,
470 NULL, "entry update failed", NULL, NULL );
474 send_ldap_result( conn, op, LDAP_SUCCESS,
475 NULL, NULL, NULL, NULL );
479 if( new_dn != NULL ) free( new_dn );
480 if( new_ndn != NULL ) free( new_ndn );
482 if( p_dn != NULL ) free( p_dn );
483 if( p_ndn != NULL ) free( p_ndn );
485 /* LDAP v2 supporting correct attribute handling. */
486 if( new_rdn_type != NULL ) free(new_rdn_type);
487 if( new_rdn_val != NULL ) free(new_rdn_val);
488 if( old_rdn != NULL ) free(old_rdn);
489 if( old_rdn_type != NULL ) free(old_rdn_type);
490 if( old_rdn_val != NULL ) free(old_rdn_val);
493 /* LDAP v3 Support */
494 if ( np_dn != NULL ) free( np_dn );
495 if ( np_ndn != NULL ) free( np_ndn );
498 /* free new parent and writer lock */
499 cache_return_entry_w( &li->li_cache, np );
503 /* free parent and writer lock */
504 cache_return_entry_w( &li->li_cache, p );
508 /* release root writer lock */
509 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
512 /* free entry and writer lock */
513 cache_return_entry_w( &li->li_cache, e );