1 /* modrdn.c - ldbm backend modrdn routine */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * LDAP v3 newSuperior support. Add new rdn as an attribute.
9 * (Full support for v2 also used software/ideas contributed
10 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
13 * Copyright 1999, Juan C. Gomez, All rights reserved.
14 * This software is not subject to any license of Silicon Graphics
15 * Inc. or Purdue University.
17 * Redistribution and use in source and binary forms are permitted
18 * without restriction or fee of any kind as long as this notice
27 #include <ac/string.h>
28 #include <ac/socket.h>
31 #include "back-ldbm.h"
32 #include "proto-back-ldbm.h"
45 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
46 char *p_dn = NULL, *p_ndn = NULL;
47 char *new_dn = NULL, *new_ndn = NULL;
49 Entry *matched = NULL;
52 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
53 char *new_rdn_val = NULL; /* Val of new rdn */
54 char *new_rdn_type = NULL; /* Type of new rdn */
55 char *old_rdn; /* Old rdn's attr type & val */
56 char *old_rdn_type = NULL; /* Type of old rdn attr. */
57 char *old_rdn_val = NULL; /* Old rdn attribute value */
58 /* Added to support newSuperior */
59 Entry *np = NULL; /* newSuperior Entry */
60 char *np_dn = NULL; /* newSuperior dn */
61 char *np_ndn = NULL; /* newSuperior ndn */
62 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 /* Used to interface with ldbm_modify_internal() */
64 struct berval add_bv; /* Stores new rdn att */
65 struct berval *add_bvals[2]; /* Stores new rdn att */
66 struct berval del_bv; /* Stores old rdn att */
67 struct berval *del_bvals[2]; /* Stores old rdn att */
68 LDAPModList mod[2]; /* Used to delete old rdn */
69 int manageDSAit = get_manageDSAit( op );
71 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
72 (newSuperior ? newSuperior : "NULL"),
75 /* get entry with writer lock */
76 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
77 char* matched_dn = NULL;
78 struct berval** refs = NULL;
80 if( matched != NULL ) {
81 matched_dn = strdup( matched->e_dn );
82 refs = is_entry_referral( matched )
83 ? get_entry_referrals( be, conn, op, matched )
85 cache_return_entry_r( &li->li_cache, matched );
87 refs = default_referral;
90 send_ldap_result( conn, op, LDAP_REFERRAL,
91 matched_dn, NULL, refs, NULL );
93 if ( matched != NULL ) {
101 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
102 if ( ! access_allowed( be, conn, op, e,
103 "entry", NULL, ACL_WRITE ) )
105 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
107 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
108 NULL, NULL, NULL, NULL );
113 if (!manageDSAit && is_entry_referral( e ) ) {
114 /* parent is a referral, don't allow add */
115 /* parent is an alias, don't allow add */
116 struct berval **refs = get_entry_referrals( be,
119 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
122 send_ldap_result( conn, op, LDAP_REFERRAL,
123 e->e_dn, NULL, refs, NULL );
125 ber_bvecfree( refs );
130 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
132 /* Make sure parent entry exist and we can write its
136 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
137 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
139 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
140 NULL, NULL, NULL, NULL );
144 /* check parent for "children" acl */
145 if ( ! access_allowed( be, conn, op, p,
146 "children", NULL, ACL_WRITE ) )
148 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
150 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
151 NULL, NULL, NULL, NULL );
155 Debug( LDAP_DEBUG_TRACE,
156 "ldbm_back_modrdn: wr to children of entry %s OK\n",
159 p_dn = dn_parent( be, e->e_dn );
162 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
166 /* no parent, modrdn entry directly under root */
167 if( ! be_isroot( be, op->o_ndn ) ) {
168 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
170 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
171 NULL, NULL, NULL, NULL );
175 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
178 Debug( LDAP_DEBUG_TRACE,
179 "ldbm_back_modrdn: no parent, locked root\n",
184 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
186 if ( (np_dn = newSuperior) != NULL) {
187 Debug( LDAP_DEBUG_TRACE,
188 "ldbm_back_modrdn: new parent requested...\n",
191 np_ndn = ch_strdup( np_dn );
192 (void) dn_normalize_case( np_ndn );
194 /* newSuperior == oldParent?, if so ==> ERROR */
195 /* newSuperior == entry being moved?, if so ==> ERROR */
196 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
198 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
199 Debug( LDAP_DEBUG_TRACE,
200 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
202 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
203 NULL, NULL, NULL, NULL );
207 Debug( LDAP_DEBUG_TRACE,
208 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
211 /* check newSuperior for "children" acl */
212 if ( !access_allowed( be, conn, op, np, "children", NULL,
215 Debug( LDAP_DEBUG_TRACE,
216 "ldbm_back_modrdn: no wr to newSup children\n",
218 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
219 NULL, NULL, NULL, NULL );
223 if ( is_entry_alias( np ) ) {
224 /* entry is an alias, don't allow bind */
225 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
228 send_ldap_result( conn, op, LDAP_ALIAS_PROBLEM,
229 NULL, NULL, NULL, NULL );
234 if ( is_entry_referral( np ) ) {
235 /* parent is a referral, don't allow add */
236 /* parent is an alias, don't allow add */
237 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
240 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
241 NULL, NULL, NULL, NULL );
246 Debug( LDAP_DEBUG_TRACE,
247 "ldbm_back_modrdn: wr to new parent's children OK\n",
250 new_parent_dn = np_dn;
253 /* Build target dn and make sure target entry doesn't exist already. */
255 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
258 new_ndn = ch_strdup(new_dn);
259 (void) dn_normalize_case( new_ndn );
261 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
264 if (dn2id ( be, new_ndn ) != NOID) {
265 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS,
266 NULL, NULL, NULL, NULL );
270 Debug( LDAP_DEBUG_TRACE,
271 "ldbm_back_modrdn: new ndn=%s does not exist\n",
274 /* check for abandon */
275 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
276 if ( op->o_abandon ) {
277 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
280 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
282 /* Get attribute type and attribute value of our new rdn, we will
283 * need to add that to our new entry
286 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
288 Debug( LDAP_DEBUG_TRACE,
289 "ldbm_back_modrdn: can't figure out type of newrdn\n",
291 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
292 NULL, NULL, NULL, NULL );
297 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
299 Debug( LDAP_DEBUG_TRACE,
300 "ldbm_back_modrdn: can't figure out val of newrdn\n",
302 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
303 NULL, NULL, NULL, NULL );
308 Debug( LDAP_DEBUG_TRACE,
309 "ldbm_back_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
310 new_rdn_val, new_rdn_type, 0 );
312 /* Retrieve the old rdn from the entry's dn */
314 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
316 Debug( LDAP_DEBUG_TRACE,
317 "ldbm_back_modrdn: can't figure out old_rdn from dn\n",
319 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
320 NULL, NULL, NULL, NULL );
325 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
327 Debug( LDAP_DEBUG_TRACE,
328 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
330 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
331 NULL, NULL, NULL, NULL );
336 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
338 /* Not a big deal but we may say something */
339 Debug( LDAP_DEBUG_TRACE,
340 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
341 old_rdn_type, new_rdn_type, 0 );
346 if ( dn_type( old_rdn ) == DN_X500 ) {
349 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
352 /* Add new attribute value to the entry.
355 add_bvals[0] = &add_bv; /* Array of bervals */
358 add_bv.bv_val = new_rdn_val;
359 add_bv.bv_len = strlen(new_rdn_val);
361 mod[0].ml_type = new_rdn_type;
362 mod[0].ml_bvalues = add_bvals;
363 mod[0].ml_op = LDAP_MOD_SOFTADD;
364 mod[0].ml_next = NULL;
366 /* Remove old rdn value if required */
369 /* Get value of old rdn */
371 if ((old_rdn_val = rdn_attr_value( old_rdn ))
374 Debug( LDAP_DEBUG_TRACE,
375 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
377 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
378 NULL, NULL, NULL, NULL );
382 del_bvals[0] = &del_bv; /* Array of bervals */
385 /* Remove old value of rdn as an attribute. */
387 del_bv.bv_val = old_rdn_val;
388 del_bv.bv_len = strlen(old_rdn_val);
390 /* No need to normalize old_rdn_type, delete_values()
393 mod[0].ml_next = &mod[1];
394 mod[1].ml_type = old_rdn_type;
395 mod[1].ml_bvalues = del_bvals;
396 mod[1].ml_op = LDAP_MOD_DELETE;
397 mod[1].ml_next = NULL;
399 Debug( LDAP_DEBUG_TRACE,
400 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
406 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
408 /* XXXV3: not sure of what to do here */
409 Debug( LDAP_DEBUG_TRACE,
410 "ldbm_back_modrdn: not fully implemented...\n",
413 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
414 NULL, NULL, NULL, NULL );
421 if ( dn2id_delete( be, e->e_ndn, e->e_id ) != 0 ) {
422 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
423 NULL, NULL, NULL, NULL );
427 (void) cache_delete_entry( &li->li_cache, e );
434 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
435 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
436 NULL, NULL, NULL, NULL );
441 /* modify memory copy of entry */
442 if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
448 (void) cache_update_entry( &li->li_cache, e );
450 /* NOTE: after this you must not free new_dn or new_ndn!
451 * They are used by cache.
455 if ( id2entry_add( be, e ) != 0 ) {
457 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
458 NULL, NULL, NULL, NULL );
459 goto return_results_after;
462 send_ldap_result( conn, op, LDAP_SUCCESS,
463 NULL, NULL, NULL, NULL );
465 goto return_results_after;
468 if( new_dn != NULL ) free( new_dn );
469 if( new_ndn != NULL ) free( new_ndn );
471 return_results_after:
473 * new_dn and new_ndn are not deallocated because they are used by
476 if( p_dn != NULL ) free( p_dn );
477 if( p_ndn != NULL ) free( p_ndn );
479 if( matched != NULL ) free( matched );
481 /* LDAP v2 supporting correct attribute handling. */
482 if( new_rdn_type != NULL ) free(new_rdn_type);
483 if( new_rdn_val != NULL ) free(new_rdn_val);
484 if( old_rdn != NULL ) free(old_rdn);
485 if( old_rdn_type != NULL ) free(old_rdn_type);
486 if( old_rdn_val != NULL ) free(old_rdn_val);
489 /* LDAP v3 Support */
490 if ( np_dn != NULL ) free( np_dn );
491 if ( np_ndn != NULL ) free( np_ndn );
494 /* free new parent and writer lock */
495 cache_return_entry_w( &li->li_cache, np );
499 /* free parent and writer lock */
500 cache_return_entry_w( &li->li_cache, p );
504 /* release root writer lock */
505 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
508 /* free entry and writer lock */
509 cache_return_entry_w( &li->li_cache, e );