Back to index

openldap  2.4.31
modrdn.c
Go to the documentation of this file.
00001 /* modrdn.c - ldap backend modrdn function */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1999-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 1999-2003 Howard Chu.
00007  * Portions Copyright 2000-2003 Pierangelo Masarati.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted only as authorized by the OpenLDAP
00012  * Public License.
00013  *
00014  * A copy of this license is available in the file LICENSE in the
00015  * top-level directory of the distribution or, alternatively, at
00016  * <http://www.OpenLDAP.org/license.html>.
00017  */
00018 /* ACKNOWLEDGEMENTS:
00019  * This work was initially developed by the Howard Chu for inclusion
00020  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00021  * Masarati.
00022  */
00023 
00024 #include "portable.h"
00025 
00026 #include <stdio.h>
00027 
00028 #include <ac/socket.h>
00029 #include <ac/string.h>
00030 
00031 #include "slap.h"
00032 #include "back-ldap.h"
00033 
00034 int
00035 ldap_back_modrdn(
00036               Operation     *op,
00037               SlapReply     *rs )
00038 {
00039        ldapinfo_t           *li = (ldapinfo_t *)op->o_bd->be_private;
00040 
00041        ldapconn_t           *lc = NULL;
00042        ber_int_t            msgid;
00043        LDAPControl          **ctrls = NULL;
00044        ldap_back_send_t     retrying = LDAP_BACK_RETRYING;
00045        int                  rc = LDAP_SUCCESS;
00046        char                 *newSup = NULL;
00047        struct berval        newrdn = BER_BVNULL;
00048 
00049        if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
00050               return rs->sr_err;
00051        }
00052 
00053        if ( op->orr_newSup ) {
00054               /* needs LDAPv3 */
00055               switch ( li->li_version ) {
00056               case LDAP_VERSION3:
00057                      break;
00058 
00059               case 0:
00060                      if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
00061                             break;
00062                      }
00063                      /* fall thru */
00064 
00065               default:
00066                      /* op->o_protocol cannot be anything but LDAPv3,
00067                       * otherwise wouldn't be here */
00068                      rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
00069                      send_ldap_result( op, rs );
00070                      goto cleanup;
00071               }
00072               
00073               newSup = op->orr_newSup->bv_val;
00074        }
00075 
00076        /* NOTE: we need to copy the newRDN in case it was formed
00077         * from a DN by simply changing the length (ITS#5397) */
00078        newrdn = op->orr_newrdn;
00079        if ( newrdn.bv_val[ newrdn.bv_len ] != '\0' ) {
00080               ber_dupbv_x( &newrdn, &op->orr_newrdn, op->o_tmpmemctx );
00081        }
00082 
00083 retry:
00084        ctrls = op->o_ctrls;
00085        rc = ldap_back_controls_add( op, rs, lc, &ctrls );
00086        if ( rc != LDAP_SUCCESS ) {
00087               send_ldap_result( op, rs );
00088               rc = -1;
00089               goto cleanup;
00090        }
00091 
00092        rs->sr_err = ldap_rename( lc->lc_ld, op->o_req_dn.bv_val,
00093                      newrdn.bv_val, newSup,
00094                      op->orr_deleteoldrdn, ctrls, NULL, &msgid );
00095        rc = ldap_back_op_result( lc, op, rs, msgid,
00096               li->li_timeout[ SLAP_OP_MODRDN ],
00097               ( LDAP_BACK_SENDRESULT | retrying ) );
00098        if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
00099               retrying &= ~LDAP_BACK_RETRYING;
00100               if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
00101                      /* if the identity changed, there might be need to re-authz */
00102                      (void)ldap_back_controls_free( op, rs, &ctrls );
00103                      goto retry;
00104               }
00105        }
00106 
00107 cleanup:
00108        (void)ldap_back_controls_free( op, rs, &ctrls );
00109 
00110        if ( newrdn.bv_val != op->orr_newrdn.bv_val ) {
00111               op->o_tmpfree( newrdn.bv_val, op->o_tmpmemctx );
00112        }
00113 
00114        if ( lc != NULL ) {
00115               ldap_back_release_conn( li, lc );
00116        }
00117 
00118        return rc;
00119 }
00120