1 /* modrdn.c - bdb2 backend modrdn routine */
5 * LDAP v3 newSuperior support.
7 * Copyright 1999, Juan C. Gomez, All rights reserved.
8 * This software is not subject to any license of Silicon Graphics
9 * Inc. or Purdue University.
11 * Redistribution and use in source and binary forms are permitted
12 * without restriction or fee of any kind as long as this notice
21 #include <ac/string.h>
22 #include <ac/socket.h>
25 #include "back-bdb2.h"
26 #include "proto-back-bdb2.h"
29 bdb2i_back_modrdn_internal(
39 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
40 Entry *matched = NULL;
41 char *p_dn = NULL, *p_ndn = NULL;
42 char *new_dn = NULL, *new_ndn = NULL;
45 int rc = -1, manageDSAit;
46 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
47 char *new_rdn_val = NULL; /* Val of new rdn */
48 char *new_rdn_type = NULL; /* Type of new rdn */
49 char *old_rdn = NULL; /* Old rdn's attr type & val */
50 char *old_rdn_type = NULL; /* Type of old rdn attr. */
51 char *old_rdn_val = NULL; /* Old rdn attribute value */
52 struct berval add_bv; /* Stores new rdn att */
53 struct berval *add_bvals[2]; /* Stores new rdn att */
54 struct berval del_bv; /* Stores old rdn att */
55 struct berval *del_bvals[2]; /* Stores old rdn att */
56 LDAPModList mod[2]; /* Used to delete old rdn */
57 /* Added to support newSuperior */
58 Entry *np = NULL; /* newSuperior Entry */
59 char *np_dn = NULL; /* newSuperior dn */
60 char *np_ndn = NULL; /* newSuperior ndn */
61 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
64 (newSuperior ? newSuperior : "NULL"),
67 /* get entry with writer lock */
68 if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
69 char *matched_dn = NULL;
70 struct berval **refs = NULL;
72 if ( matched != NULL ) {
73 matched_dn = ch_strdup( matched->e_dn );
74 refs = is_entry_referral( matched )
75 ? get_entry_referrals( be, conn, op, matched )
77 bdb2i_cache_return_entry_r( &li->li_cache, matched );
79 refs = default_referral;
82 send_ldap_result( conn, op, LDAP_REFERRAL,
83 matched_dn, NULL, refs, NULL );
85 if( matched != NULL ) {
93 if (!manageDSAit && is_entry_referral( e ) ) {
94 /* entry is a referral, don't allow add */
95 struct berval **refs = get_entry_referrals( be,
98 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
101 send_ldap_result( conn, op, LDAP_REFERRAL,
102 e->e_dn, NULL, refs, NULL );
104 ber_bvecfree( refs );
109 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
110 /* check parent for "children" acl */
111 if ( ! access_allowed( be, conn, op, e,
112 "entry", NULL, ACL_WRITE ) )
114 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
116 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
117 NULL, NULL, NULL, NULL );
122 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
123 /* parent + rdn + separator(s) + null */
124 if( (p = bdb2i_dn2entry_w( be, p_ndn, &matched )) == NULL) {
125 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
127 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
128 NULL, NULL, NULL, NULL );
132 /* check parent for "children" acl */
133 if ( ! access_allowed( be, conn, op, p,
134 "children", NULL, ACL_WRITE ) )
136 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
138 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
139 NULL, NULL, NULL, NULL );
143 p_dn = dn_parent( be, e->e_dn );
145 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
150 /* no parent, modrdn entry directly under root */
151 if( ! be_isroot( be, op->o_ndn ) ) {
152 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
154 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
155 NULL, NULL, NULL, NULL );
159 Debug( LDAP_DEBUG_TRACE,
160 "ldbm_back_modrdn: no parent!, working on root\n",
165 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
167 if ( (np_dn = newSuperior) != NULL) {
170 Debug( LDAP_DEBUG_TRACE,
171 "ldbm_back_modrdn: new parent requested...\n",
174 np_ndn = ch_strdup( np_dn );
175 (void) dn_normalize( np_ndn );
177 /* newSuperior == oldParent?, if so ==> ERROR */
179 /* newSuperior == entry being moved?, if so ==> ERROR */
181 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
183 if( (np = bdb2i_dn2entry_w( be, np_ndn, &matched )) == NULL) {
185 Debug( LDAP_DEBUG_TRACE,
186 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
188 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
189 NULL, NULL, NULL, NULL );
193 Debug( LDAP_DEBUG_TRACE,
194 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
197 /* check newSuperior for "children" acl */
198 if ( !access_allowed( be, conn, op, np, "children", NULL,
201 Debug( LDAP_DEBUG_TRACE,
202 "ldbm_back_modrdn: no wr to newSup children\n",
204 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
205 NULL, NULL, NULL, NULL );
209 Debug( LDAP_DEBUG_TRACE,
210 "ldbm_back_modrdn: wr to new parent's children OK\n",
214 new_parent_dn = np_dn;
218 /* Build target dn and make sure target entry doesn't exist already. */
220 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
223 new_ndn = ch_strdup( new_dn );
224 (void) dn_normalize( new_ndn );
226 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
229 /* check for abandon */
230 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
231 if ( op->o_abandon ) {
232 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
235 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
237 if ( (bdb2i_dn2id ( be, new_ndn ) ) != NOID ) {
238 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
239 NULL, NULL, NULL, NULL );
243 /* Get attribute type and attribute value of our new rdn, we will
244 * need to add that to our new entry
247 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
249 Debug( LDAP_DEBUG_TRACE,
250 "ldbm_back_modrdn: can't figure out type of newrdn\n",
252 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
253 NULL, NULL, NULL, NULL );
258 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
260 Debug( LDAP_DEBUG_TRACE,
261 "ldbm_back_modrdn: can't figure out val of newrdn\n",
263 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
264 NULL, NULL, NULL, NULL );
269 Debug( LDAP_DEBUG_TRACE,
270 "ldbm_back_modrdn: new_rdn_val=%s, new_rdn_type=%s\n",
271 new_rdn_val, new_rdn_type, 0 );
273 /* Retrieve the old rdn from the entry's dn */
275 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
277 Debug( LDAP_DEBUG_TRACE,
278 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
280 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
281 NULL, NULL, NULL, NULL );
286 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
288 Debug( LDAP_DEBUG_TRACE,
289 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
291 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
292 NULL, NULL, NULL, NULL );
297 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
299 /* Not a big deal but we may say something */
300 Debug( LDAP_DEBUG_TRACE,
301 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
302 old_rdn_type, new_rdn_type, 0 );
306 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
309 /* Add new attribute value to the entry.
312 add_bvals[0] = &add_bv; /* Array of bervals */
315 add_bv.bv_val = new_rdn_val;
316 add_bv.bv_len = strlen(new_rdn_val);
318 mod[0].ml_type = new_rdn_type;
319 mod[0].ml_bvalues = add_bvals;
320 mod[0].ml_op = LDAP_MOD_SOFTADD;
321 mod[0].ml_next = NULL;
323 Debug( LDAP_DEBUG_TRACE,
324 "ldbm_back_modrdn: adding new rdn attr val =%s\n",
327 /* Remove old rdn value if required */
331 del_bvals[0] = &del_bv; /* Array of bervals */
333 /* Get value of old rdn */
335 if ((old_rdn_val = rdn_attr_value( old_rdn ))
338 Debug( LDAP_DEBUG_TRACE,
339 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
341 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
342 NULL, NULL, NULL, NULL );
348 /* Remove old value of rdn as an attribute. */
350 del_bv.bv_val = old_rdn_val;
351 del_bv.bv_len = strlen(old_rdn_val);
353 mod[0].ml_next = &mod[1];
354 mod[1].ml_type = old_rdn_type;
355 mod[1].ml_bvalues = del_bvals;
356 mod[1].ml_op = LDAP_MOD_DELETE;
357 mod[1].ml_next = NULL;
360 Debug( LDAP_DEBUG_TRACE,
361 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
364 }/* if (deleteoldrdn) */
366 /* check for abandon */
367 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
368 if ( op->o_abandon ) {
369 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
372 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
375 if ( bdb2i_dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
376 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
377 NULL, NULL, NULL, NULL );
381 (void) bdb2i_cache_delete_entry( &li->li_cache, e );
391 if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
392 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
393 NULL, NULL, NULL, NULL );
397 /* modify memory copy of entry */
398 if ( bdb2i_back_modify_internal( be, conn, op, dn, &mod[0], e )
405 (void) bdb2i_cache_update_entry( &li->li_cache, e );
407 /* NOTE: after this you must not free new_dn or new_ndn!
408 * They are used by cache.
412 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
414 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
415 NULL, NULL, NULL, NULL );
419 send_ldap_result( conn, op, LDAP_SUCCESS,
420 NULL, NULL, NULL, NULL );
424 if( new_dn != NULL ) free( new_dn );
425 if( new_ndn != NULL ) free( new_ndn );
428 * new_dn and new_ndn are not deallocated because they are used by
431 if( p_dn != NULL ) free( p_dn );
432 if( p_ndn != NULL ) free( p_ndn );
434 /* LDAP v2 supporting correct attribute handling. */
435 if( new_rdn_type != NULL ) free(new_rdn_type);
436 if( new_rdn_val != NULL ) free(new_rdn_val);
437 if( old_rdn != NULL ) free(old_rdn);
438 if( old_rdn_type != NULL ) free(old_rdn_type);
439 if( old_rdn_val != NULL ) free(old_rdn_val);
441 /* LDAP v3 Support */
442 if ( np_dn != NULL ) free( np_dn );
443 if ( np_ndn != NULL ) free( np_ndn );
446 /* free parent and writer lock */
447 bdb2i_cache_return_entry_w( &li->li_cache, p );
451 /* free entry and writer lock */
452 bdb2i_cache_return_entry_w( &li->li_cache, e );
470 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
471 struct timeval time1;
474 bdb2i_start_timing( be->bd_info, &time1 );
476 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
477 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
478 NULL, NULL, NULL, NULL );
483 ret = bdb2i_back_modrdn_internal( be, conn, op, ndn,
484 newrdn, deleteoldrdn,
487 (void) bdb2i_leave_backend_w( lock );
488 bdb2i_stop_timing( be->bd_info, time1, "MODRDN", conn, op );