Back to index

lightning-sunbird  0.9+nobinonly
rename.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1990 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  rename.c
00043  */
00044 
00045 #if 0
00046 #ifndef lint 
00047 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00048 #endif
00049 #endif
00050 
00051 #include "ldap-int.h"
00052 
00053 /*
00054  * ldap_rename - initiate an ldap modifyDN operation. Parameters:
00055  *
00056  *     ld            LDAP descriptor
00057  *     dn            DN of the object to modify
00058  *     newrdn        RDN that will form leftmost component of entry's new name
00059  *      newparent       if present, this is the Distinguished Name of the entry
00060  *                      which becomes the immediate parent of the existing entry
00061  *     deleteoldrdn  nonzero means to delete old rdn values from the entry
00062  *                      while zero means to retain them as attributes of the entry
00063  *      serverctrls     list of LDAP server controls
00064  *      clientctrls     list of client controls
00065  *      msgidp          this result parameter will be set to the message id of the
00066  *                      request if the ldap_rename() call succeeds
00067  *
00068  * Example:
00069  *      int rc;
00070  *     rc = ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid );
00071  */
00072 int
00073 LDAP_CALL
00074 ldap_rename( 
00075           LDAP *ld, 
00076           const char *dn, 
00077           const char *newrdn, 
00078           const char *newparent,
00079           int deleteoldrdn, 
00080           LDAPControl       **serverctrls,
00081           LDAPControl       **clientctrls,  /* not used for anything yet */
00082           int *msgidp
00083 )
00084 {
00085        BerElement    *ber;
00086        int           rc, err;
00087 
00088        /*
00089         * A modify dn request looks like this:
00090         *     ModifyDNRequest ::= SEQUENCE {
00091         *            entry         LDAPDN,
00092         *            newrdn        RelativeLDAPDN,
00093         *              newparent       [0] LDAPDN OPTIONAL,
00094         *            deleteoldrdn  BOOLEAN
00095         *     }
00096         */
00097 
00098        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
00099 
00100        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00101               return( LDAP_PARAM_ERROR );
00102        }
00103        if ( NULL == newrdn) {
00104               LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
00105               return( LDAP_PARAM_ERROR );
00106        }
00107 
00108        /* only ldapv3 or higher can do a proper rename
00109         * (i.e. with non-NULL newparent and/or controls)
00110         */
00111 
00112        if (( NSLDAPI_LDAP_VERSION( ld ) < LDAP_VERSION3 )
00113            && ((newparent != NULL) || (serverctrls != NULL)
00114            || (clientctrls != NULL))) {
00115               LDAP_SET_LDERRNO( ld, LDAP_NOT_SUPPORTED, NULL, NULL );
00116               return( LDAP_NOT_SUPPORTED );
00117        }
00118 
00119        if ( msgidp == NULL ) {
00120               LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
00121                 return( LDAP_PARAM_ERROR );
00122         }
00123 
00124        LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00125        *msgidp = ++ld->ld_msgid;
00126         LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00127 
00128        /* see if modRDN or modDN is handled by the cache */
00129        if ( ld->ld_cache_on ) {
00130               if ( newparent == NULL && ld->ld_cache_modrdn != NULL ) {
00131                      LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
00132                      if ( (rc = (ld->ld_cache_modrdn)( ld, *msgidp,
00133                          LDAP_REQ_MODRDN, dn, newrdn, deleteoldrdn ))
00134                          != 0 ) {   
00135                             *msgidp = rc;
00136                             LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00137                             return( LDAP_SUCCESS );
00138                      }
00139                      LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00140 #if 0
00141               } else if ( ld->ld_cache_rename != NULL ) {
00142                      LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
00143                      if ( (rc = (ld->ld_cache_rename)( ld, *msgidp,
00144                          LDAP_REQ_MODDN, dn, newrdn, newparent,
00145                          deleteoldrdn )) != 0 ) {   
00146                             *msgidp = rc;
00147                             return( LDAP_SUCCESS );
00148                      }
00149                      LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00150 #endif
00151               }
00152        }
00153 
00154        /* create a message to send */
00155        if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
00156            != LDAP_SUCCESS ) {
00157               return( err );
00158        }
00159 
00160        /* fill it in */
00161        if ( ber_printf( ber, "{it{ssb", *msgidp, LDAP_REQ_MODDN, dn,
00162            newrdn, deleteoldrdn ) == -1 ) {
00163               LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
00164               ber_free( ber, 1 );
00165               return( LDAP_ENCODING_ERROR );
00166        }
00167 
00168        if ( newparent == NULL ) {
00169               if ( ber_printf( ber, "}" ) == -1 ) {
00170                      LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
00171                      ber_free( ber, 1 );
00172                      return( LDAP_ENCODING_ERROR );
00173               }
00174        } else {
00175               if ( ber_printf( ber, "ts}", LDAP_TAG_NEWSUPERIOR, newparent )
00176                   == -1 ) {
00177                      LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
00178                      ber_free( ber, 1 );
00179                      return( LDAP_ENCODING_ERROR );
00180               }
00181        }
00182 
00183        if (( rc = nsldapi_put_controls( ld, serverctrls, 1, ber ))
00184            != LDAP_SUCCESS ) {
00185               ber_free( ber, 1 );
00186               return( rc );
00187        }
00188 
00189        /* send the message */
00190        rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_MODDN,
00191               (char *) dn, ber );
00192        *msgidp = rc;
00193        return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
00194 }
00195 
00196 int
00197 LDAP_CALL
00198 ldap_modrdn2( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
00199 {
00200        int             msgid;
00201 
00202        if ( ldap_rename( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
00203               return( msgid );
00204        } else {
00205               return( -1 ); /* error is in ld handle */
00206        }
00207 }
00208 
00209 int
00210 LDAP_CALL
00211 ldap_modrdn( LDAP *ld, const char *dn, const char *newrdn )
00212 {
00213        return( ldap_modrdn2( ld, dn, newrdn, 1 ) );
00214 }
00215 
00216 int
00217 LDAP_CALL
00218 ldap_rename_s( 
00219           LDAP *ld, 
00220           const char *dn, 
00221           const char *newrdn, 
00222           const char *newparent,
00223           int deleteoldrdn, 
00224           LDAPControl       **serverctrls,
00225           LDAPControl       **clientctrls  /* not used for anything yet */
00226 )
00227 {
00228        int           msgid;
00229        LDAPMessage   *res;
00230 
00231        if ( ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid ) != LDAP_SUCCESS ) {
00232               return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00233        }
00234 
00235        if ( msgid == -1 ) 
00236               return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00237 
00238        if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
00239               return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00240 
00241        return( ldap_result2error( ld, res, 1 ) );
00242 }
00243 
00244 int
00245 LDAP_CALL
00246 ldap_modrdn2_s( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
00247 {
00248         int             msgid;
00249         LDAPMessage     *res;
00250  
00251         if ( (msgid = ldap_modrdn2( ld, dn, newrdn, deleteoldrdn )) == -1 )
00252                 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00253  
00254         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
00255                 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00256  
00257         return( ldap_result2error( ld, res, 1 ) );
00258 }
00259 
00260 int
00261 LDAP_CALL
00262 ldap_modrdn_s( LDAP *ld, const char *dn, const char *newrdn )
00263 {
00264        return( ldap_modrdn2_s( ld, dn, newrdn, 1 ) );
00265 }