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 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00016  * All rights reserved.
00017  */
00018 /* Copyright 1999, Juan C. Gomez, All rights reserved.
00019  * This software is not subject to any license of Silicon Graphics 
00020  * Inc. or Purdue University.
00021  *
00022  * Redistribution and use in source and binary forms are permitted
00023  * without restriction or fee of any kind as long as this notice
00024  * is preserved.
00025  */
00026 
00027 /* ACKNOWLEDGEMENTS:
00028  *     Juan C. Gomez
00029  */
00030 
00031 #include "portable.h"
00032 
00033 #include <stdio.h>
00034 
00035 #include <ac/socket.h>
00036 #include <ac/string.h>
00037 #include <ac/time.h>
00038 
00039 #include "ldap-int.h"
00040 
00041 /*
00042  * A modify rdn request looks like this:
00043  *     ModifyRDNRequest ::= SEQUENCE {
00044  *            entry         DistinguishedName,
00045  *            newrdn        RelativeDistinguishedName,
00046  *            deleteoldrdn  BOOLEAN
00047  *            newSuperior   [0] DistinguishedName       [v3 only]
00048  *     }
00049  */
00050 
00051 
00052 /*
00053  * ldap_rename - initiate an ldap extended modifyDN operation.
00054  *
00055  * Parameters:
00056  *     ld                          LDAP descriptor
00057  *     dn                          DN of the object to modify
00058  *     newrdn               RDN to give the object
00059  *     deleteoldrdn  nonzero means to delete old rdn values from the entry
00060  *     newSuperior          DN of the new parent if applicable
00061  *
00062  * Returns the LDAP error code.
00063  */
00064 
00065 int
00066 ldap_rename(
00067        LDAP *ld,
00068        LDAP_CONST char *dn,
00069        LDAP_CONST char *newrdn,
00070        LDAP_CONST char *newSuperior,
00071        int deleteoldrdn,
00072        LDAPControl **sctrls,
00073        LDAPControl **cctrls,
00074        int *msgidp )
00075 {
00076        BerElement    *ber;
00077        int rc;
00078        ber_int_t id;
00079 
00080        Debug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
00081 
00082        /* check client controls */
00083        rc = ldap_int_client_controls( ld, cctrls );
00084        if( rc != LDAP_SUCCESS ) return rc;
00085 
00086        /* create a message to send */
00087        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
00088               return( LDAP_NO_MEMORY );
00089        }
00090 
00091        LDAP_NEXT_MSGID( ld, id );
00092        if( newSuperior != NULL ) {
00093               /* must be version 3 (or greater) */
00094               if ( ld->ld_version < LDAP_VERSION3 ) {
00095                      ld->ld_errno = LDAP_NOT_SUPPORTED;
00096                      ber_free( ber, 1 );
00097                      return( ld->ld_errno );
00098               }
00099               rc = ber_printf( ber, "{it{ssbtsN}", /* '}' */ 
00100                      id, LDAP_REQ_MODDN,
00101                      dn, newrdn, (ber_int_t) deleteoldrdn,
00102                      LDAP_TAG_NEWSUPERIOR, newSuperior );
00103 
00104        } else {
00105               rc = ber_printf( ber, "{it{ssbN}", /* '}' */ 
00106                      id, LDAP_REQ_MODDN,
00107                      dn, newrdn, (ber_int_t) deleteoldrdn );
00108        }
00109 
00110        if ( rc < 0 ) {
00111               ld->ld_errno = LDAP_ENCODING_ERROR;
00112               ber_free( ber, 1 );
00113               return( ld->ld_errno );
00114        }
00115 
00116        /* Put Server Controls */
00117        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
00118               ber_free( ber, 1 );
00119               return ld->ld_errno;
00120        }
00121 
00122        rc = ber_printf( ber, /*{*/ "N}" );
00123        if ( rc < 0 ) {
00124               ld->ld_errno = LDAP_ENCODING_ERROR;
00125               ber_free( ber, 1 );
00126               return( ld->ld_errno );
00127        }
00128 
00129        /* send the message */
00130        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber, id );
00131        
00132        if( *msgidp < 0 ) {
00133               return( ld->ld_errno );
00134        }
00135 
00136        return LDAP_SUCCESS;
00137 }
00138 
00139 
00140 /*
00141  * ldap_rename2 - initiate an ldap (and X.500) modifyDN operation. Parameters:
00142  *     (LDAP V3 MODIFYDN REQUEST)
00143  *     ld            LDAP descriptor
00144  *     dn            DN of the object to modify
00145  *     newrdn        RDN to give the object
00146  *     deleteoldrdn  nonzero means to delete old rdn values from the entry
00147  *     newSuperior   DN of the new parent if applicable
00148  *
00149  * ldap_rename2 uses a U-Mich Style API.  It returns the msgid.
00150  */
00151 
00152 int
00153 ldap_rename2(
00154        LDAP *ld,
00155        LDAP_CONST char *dn,
00156        LDAP_CONST char *newrdn,
00157        LDAP_CONST char *newSuperior,
00158        int deleteoldrdn )
00159 {
00160        int msgid;
00161        int rc;
00162 
00163        Debug( LDAP_DEBUG_TRACE, "ldap_rename2\n", 0, 0, 0 );
00164 
00165        rc = ldap_rename( ld, dn, newrdn, newSuperior,
00166               deleteoldrdn, NULL, NULL, &msgid );
00167 
00168        return rc == LDAP_SUCCESS ? msgid : -1;
00169 }
00170 
00171 
00172 /*
00173  * ldap_modrdn2 - initiate an ldap modifyRDN operation. Parameters:
00174  *
00175  *     ld            LDAP descriptor
00176  *     dn            DN of the object to modify
00177  *     newrdn        RDN to give the object
00178  *     deleteoldrdn  nonzero means to delete old rdn values from the entry
00179  *
00180  * Example:
00181  *     msgid = ldap_modrdn( ld, dn, newrdn );
00182  */
00183 int
00184 ldap_modrdn2( LDAP *ld,
00185        LDAP_CONST char *dn,
00186        LDAP_CONST char *newrdn,
00187        int deleteoldrdn )
00188 {
00189        return ldap_rename2( ld, dn, newrdn, NULL, deleteoldrdn );
00190 }
00191 
00192 int
00193 ldap_modrdn( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
00194 {
00195        return( ldap_rename2( ld, dn, newrdn, NULL, 1 ) );
00196 }
00197 
00198 
00199 int
00200 ldap_rename_s(
00201        LDAP *ld,
00202        LDAP_CONST char *dn,
00203        LDAP_CONST char *newrdn,
00204        LDAP_CONST char *newSuperior,
00205        int deleteoldrdn,
00206        LDAPControl **sctrls,
00207        LDAPControl **cctrls )
00208 {
00209        int rc;
00210        int msgid;
00211        LDAPMessage *res;
00212 
00213        rc = ldap_rename( ld, dn, newrdn, newSuperior,
00214               deleteoldrdn, sctrls, cctrls, &msgid );
00215 
00216        if( rc != LDAP_SUCCESS ) {
00217               return rc;
00218        }
00219 
00220        rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res );
00221 
00222        if( rc == -1 || !res ) {
00223               return ld->ld_errno;
00224        }
00225 
00226        return ldap_result2error( ld, res, 1 );
00227 }
00228 
00229 int
00230 ldap_rename2_s(
00231        LDAP *ld,
00232        LDAP_CONST char *dn,
00233        LDAP_CONST char *newrdn,
00234        LDAP_CONST char *newSuperior,
00235        int deleteoldrdn )
00236 {
00237        return ldap_rename_s( ld, dn, newrdn, newSuperior,
00238               deleteoldrdn, NULL, NULL );
00239 }
00240 
00241 int
00242 ldap_modrdn2_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn, int deleteoldrdn )
00243 {
00244        return ldap_rename_s( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL );
00245 }
00246 
00247 int
00248 ldap_modrdn_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
00249 {
00250        return ldap_rename_s( ld, dn, newrdn, NULL, 1, NULL, NULL );
00251 }
00252