Back to index

openldap  2.4.31
modrdn.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1999-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2001-2003 Pierangelo Masarati.
00006  * Portions Copyright 1999-2003 Howard Chu.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by the Howard Chu for inclusion
00019  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00020  * Masarati.
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #include <stdio.h>
00026 
00027 #include <ac/socket.h>
00028 #include <ac/string.h>
00029 
00030 #include "slap.h"
00031 #include "../back-ldap/back-ldap.h"
00032 #include "back-meta.h"
00033 
00034 int
00035 meta_back_modrdn( Operation *op, SlapReply *rs )
00036 {
00037        metainfo_t    *mi = ( metainfo_t * )op->o_bd->be_private;
00038        metatarget_t  *mt;
00039        metaconn_t    *mc;
00040        int           candidate = -1;
00041        struct berval mdn = BER_BVNULL,
00042                      mnewSuperior = BER_BVNULL;
00043        dncookie      dc;
00044        int           msgid;
00045        ldap_back_send_t     retrying = LDAP_BACK_RETRYING;
00046        LDAPControl   **ctrls = NULL;
00047        struct berval newrdn = BER_BVNULL;
00048 
00049        mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
00050        if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
00051               return rs->sr_err;
00052        }
00053 
00054        assert( mc->mc_conns[ candidate ].msc_ld != NULL );
00055 
00056        mt = mi->mi_targets[ candidate ];
00057        dc.target = mt;
00058        dc.conn = op->o_conn;
00059        dc.rs = rs;
00060 
00061        if ( op->orr_newSup ) {
00062 
00063               /*
00064                * NOTE: the newParent, if defined, must be on the 
00065                * same target as the entry to be renamed.  This check
00066                * has been anticipated in meta_back_getconn()
00067                */
00068               /*
00069                * FIXME: one possibility is to delete the entry
00070                * from one target and add it to the other;
00071                * unfortunately we'd need write access to both,
00072                * which is nearly impossible; for administration
00073                * needs, the rootdn of the metadirectory could
00074                * be mapped to an administrative account on each
00075                * target (the binddn?); we'll see.
00076                */
00077               /*
00078                * NOTE: we need to port the identity assertion
00079                * feature from back-ldap
00080                */
00081 
00082               /* needs LDAPv3 */
00083               switch ( mt->mt_version ) {
00084               case LDAP_VERSION3:
00085                      break;
00086 
00087               case 0:
00088                      if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
00089                             break;
00090                      }
00091                      /* fall thru */
00092 
00093               default:
00094                      /* op->o_protocol cannot be anything but LDAPv3,
00095                       * otherwise wouldn't be here */
00096                      rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
00097                      send_ldap_result( op, rs );
00098                      goto cleanup;
00099               }
00100               
00101               /*
00102                * Rewrite the new superior, if defined and required
00103                */
00104               dc.ctx = "newSuperiorDN";
00105               if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
00106                      rs->sr_err = LDAP_OTHER;
00107                      send_ldap_result( op, rs );
00108                      goto cleanup;
00109               }
00110        }
00111 
00112        /*
00113         * Rewrite the modrdn dn, if required
00114         */
00115        dc.ctx = "modrDN";
00116        if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
00117               rs->sr_err = LDAP_OTHER;
00118               send_ldap_result( op, rs );
00119               goto cleanup;
00120        }
00121 
00122        /* NOTE: we need to copy the newRDN in case it was formed
00123         * from a DN by simply changing the length (ITS#5397) */
00124        newrdn = op->orr_newrdn;
00125        if ( newrdn.bv_val[ newrdn.bv_len ] != '\0' ) {
00126               ber_dupbv_x( &newrdn, &op->orr_newrdn, op->o_tmpmemctx );
00127        }
00128 
00129 retry:;
00130        ctrls = op->o_ctrls;
00131        if ( meta_back_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
00132        {
00133               send_ldap_result( op, rs );
00134               goto cleanup;
00135        }
00136 
00137        rs->sr_err = ldap_rename( mc->mc_conns[ candidate ].msc_ld,
00138                      mdn.bv_val, newrdn.bv_val,
00139                      mnewSuperior.bv_val, op->orr_deleteoldrdn,
00140                      ctrls, NULL, &msgid );
00141        rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
00142               mt->mt_timeout[ SLAP_OP_MODRDN ], ( LDAP_BACK_SENDRESULT | retrying ) );
00143        if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
00144               retrying &= ~LDAP_BACK_RETRYING;
00145               if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
00146                      /* if the identity changed, there might be need to re-authz */
00147                      (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00148                      goto retry;
00149               }
00150        }
00151 
00152 cleanup:;
00153        (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00154 
00155        if ( mdn.bv_val != op->o_req_dn.bv_val ) {
00156               free( mdn.bv_val );
00157               BER_BVZERO( &mdn );
00158        }
00159        
00160        if ( !BER_BVISNULL( &mnewSuperior )
00161                      && mnewSuperior.bv_val != op->orr_newSup->bv_val )
00162        {
00163               free( mnewSuperior.bv_val );
00164               BER_BVZERO( &mnewSuperior );
00165        }
00166 
00167        if ( newrdn.bv_val != op->orr_newrdn.bv_val ) {
00168               op->o_tmpfree( newrdn.bv_val, op->o_tmpmemctx );
00169        }
00170 
00171        if ( mc ) {
00172               meta_back_release_conn( mi, mc );
00173        }
00174 
00175        return rs->sr_err;
00176 }
00177