1 /* modrdn.c - ldbm backend modrdn routine */
4 * LDAP v3 newSuperior support. Add new rdn as an attribute.
5 * (Full support for v2 also used software/ideas contributed
6 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
9 * Copyright 1999, Juan C. Gomez, All rights reserved.
10 * This software is not subject to any license of Silicon Graphics
11 * Inc. or Purdue University.
13 * Redistribution and use in source and binary forms are permitted
14 * without restriction or fee of any kind as long as this notice
23 #include <ac/string.h>
24 #include <ac/socket.h>
27 #include "back-ldbm.h"
28 #include "proto-back-ldbm.h"
41 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
43 char *p_dn = NULL, *p_ndn = NULL;
44 char *new_dn = NULL, *new_ndn = NULL;
48 /* Added to support LDAP v2 correctly (deleteoldrdn thing) */
49 char *new_rdn_val = NULL; /* Val of new rdn */
50 char *new_rdn_type = NULL; /* Type of new rdn */
51 char *old_rdn; /* Old rdn's attr type & val */
52 char *old_rdn_type = NULL; /* Type of old rdn attr. */
53 char *old_rdn_val = NULL; /* Old rdn attribute value */
54 /* Added to support newSuperior */
55 Entry *np = NULL; /* newSuperior Entry */
56 char *np_dn = NULL; /* newSuperior dn */
57 char *np_ndn = NULL; /* newSuperior ndn */
58 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
59 /* Used to interface with ldbm_modify_internal() */
60 struct berval add_bv; /* Stores new rdn att */
61 struct berval *add_bvals[2]; /* Stores new rdn att */
62 struct berval del_bv; /* Stores old rdn att */
63 struct berval *del_bvals[2]; /* Stores old rdn att */
64 LDAPModList mod[2]; /* Used to delete old rdn */
67 Debug( LDAP_DEBUG_TRACE, "==>ldbm_back_modrdn(newSuperior=%s)\n",
68 (newSuperior ? newSuperior : "NULL"),
71 /* get entry with writer lock */
72 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
73 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched, "" );
74 if ( matched != NULL ) {
80 #ifdef SLAPD_CHILD_MODIFICATION_WITH_ENTRY_ACL
81 /* check parent for "children" acl */
82 if ( ! access_allowed( be, conn, op, e,
83 "entry", NULL, ACL_WRITE ) )
85 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
87 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
93 if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) {
95 /* Make sure parent entry exist and we can write its
99 if( (p = dn2entry_w( be, p_ndn, &matched )) == NULL) {
100 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
102 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
107 /* check parent for "children" acl */
108 if ( ! access_allowed( be, conn, op, p,
109 "children", NULL, ACL_WRITE ) )
111 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
113 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
118 Debug( LDAP_DEBUG_TRACE,
119 "ldbm_back_modrdn: wr to children of entry %s OK\n",
122 p_dn = dn_parent( be, e->e_dn );
125 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
129 /* no parent, modrdn entry directly under root */
130 if( ! be_isroot( be, op->o_ndn ) ) {
131 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n",
133 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
138 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
141 Debug( LDAP_DEBUG_TRACE,
142 "ldbm_back_modrdn: no parent, locked root\n",
145 }/* if ( (p_ndn = dn_parent( be, e->e_ndn )) != NULL ) else */
147 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
149 if ( (np_dn = newSuperior) != NULL) {
152 Debug( LDAP_DEBUG_TRACE,
153 "ldbm_back_modrdn: new parent requested...\n",
156 np_ndn = dn_normalize_case( ch_strdup( np_dn ) );
158 /* newSuperior == oldParent?, if so ==> ERROR */
160 /* newSuperior == entry being moved?, if so ==> ERROR */
162 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
164 if( (np = dn2entry_w( be, np_ndn, &matched )) == NULL) {
166 Debug( LDAP_DEBUG_TRACE,
167 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
169 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
174 Debug( LDAP_DEBUG_TRACE,
175 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%d\n",
178 /* check newSuperior for "children" acl */
179 if ( !access_allowed( be, conn, op, np, "children", NULL,
182 Debug( LDAP_DEBUG_TRACE,
183 "ldbm_back_modrdn: no wr to newSup children\n",
185 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
190 Debug( LDAP_DEBUG_TRACE,
191 "ldbm_back_modrdn: wr to new parent's children OK\n",
195 new_parent_dn = np_dn;
199 /* Build target dn and make sure target entry doesn't exist already. */
201 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
204 new_ndn = dn_normalize_case( ch_strdup(new_dn) );
206 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
209 if (dn2id ( be, new_ndn ) != NOID) {
210 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, NULL, NULL );
214 Debug( LDAP_DEBUG_TRACE,
215 "ldbm_back_modrdn: new ndn=%s does not exist\n",
218 /* check for abandon */
219 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
220 if ( op->o_abandon ) {
221 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
224 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
227 if ( dn2id_delete( be, e->e_ndn ) != 0 ) {
228 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
232 (void) cache_delete_entry( &li->li_cache, e );
239 if ( dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
240 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
245 /* Get attribute type and attribute value of our new rdn, we will
246 * need to add that to our new entry
249 if ( (new_rdn_type = rdn_attr_type( newrdn )) == NULL ) {
251 Debug( LDAP_DEBUG_TRACE,
252 "ldbm_back_modrdn: can't figure out type of newrdn\n",
254 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
259 if ( (new_rdn_val = rdn_attr_value( newrdn )) == NULL ) {
261 Debug( LDAP_DEBUG_TRACE,
262 "ldbm_back_modrdn: can't figure out val of newrdn\n",
264 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
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, "", "" );
285 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
287 Debug( LDAP_DEBUG_TRACE,
288 "ldbm_back_modrdn: can't figure out the old_rdn type\n",
290 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
295 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
297 /* Not a big deal but we may say something */
298 Debug( LDAP_DEBUG_TRACE,
299 "ldbm_back_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
300 old_rdn_type, new_rdn_type, 0 );
304 if ( dn_type( old_rdn ) == DN_X500 ) {
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 = old_rdn_type;
319 mod[0].ml_bvalues = add_bvals;
320 mod[0].ml_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
321 mod[0].ml_next = NULL;
323 /* Remove old rdn value if required */
327 /* Get value of old rdn */
329 if ((old_rdn_val = rdn_attr_value( old_rdn ))
332 Debug( LDAP_DEBUG_TRACE,
333 "ldbm_back_modrdn: can't figure out old_rdn_val from old_rdn\n",
335 send_ldap_result( conn, op,
336 LDAP_OPERATIONS_ERROR,
343 del_bvals[0] = &del_bv; /* Array of bervals */
346 /* Remove old value of rdn as an attribute. */
348 del_bv.bv_val = old_rdn_val;
349 del_bv.bv_len = strlen(old_rdn_val);
351 /* No need to normalize old_rdn_type, delete_values()
354 mod[0].ml_next = &mod[1];
355 mod[1].ml_type = old_rdn_type;
356 mod[1].ml_bvalues = del_bvals;
357 mod[1].ml_op = LDAP_MOD_DELETE | LDAP_MOD_BVALUES;
358 mod[1].ml_next = NULL;
360 Debug( LDAP_DEBUG_TRACE,
361 "ldbm_back_modrdn: removing old_rdn_val=%s\n",
364 }/* if (deleteoldrdn) */
370 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DNS DN\n",
372 /* XXXV3: not sure of what to do here */
373 Debug( LDAP_DEBUG_TRACE,
374 "ldbm_back_modrdn: not fully implemented...\n",
377 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL,
383 /* modify memory copy of entry */
384 if ( ldbm_modify_internal( be, conn, op, dn, &mod[0], e )
391 (void) cache_update_entry( &li->li_cache, e );
393 /* NOTE: after this you must not free new_dn or new_ndn!
394 * They are used by cache.
398 if ( id2entry_add( be, e ) != 0 ) {
400 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
401 goto return_results_after;
404 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
406 goto return_results_after;
409 if( new_dn != NULL ) free( new_dn );
410 if( new_ndn != NULL ) free( new_ndn );
411 return_results_after:
413 * new_dn and new_ndn are not deallocated because they are used by
416 if( p_dn != NULL ) free( p_dn );
417 if( p_ndn != NULL ) free( p_ndn );
419 if( matched != NULL ) free( matched );
421 /* LDAP v2 supporting correct attribute handling. */
422 if( new_rdn_type != NULL ) free(new_rdn_type);
423 if( new_rdn_val != NULL ) free(new_rdn_val);
424 if( old_rdn != NULL ) free(old_rdn);
425 if( old_rdn_type != NULL ) free(old_rdn_type);
426 if( old_rdn_val != NULL ) free(old_rdn_val);
429 /* LDAP v3 Support */
430 if ( np_dn != NULL ) free( np_dn );
431 if ( np_ndn != NULL ) free( np_ndn );
434 /* free parent and writer lock */
435 cache_return_entry_w( &li->li_cache, p );
439 /* release root writer lock */
440 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
443 /* free entry and writer lock */
444 cache_return_entry_w( &li->li_cache, e );