Back to index

lightning-sunbird  0.9+nobinonly
modrdn.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 /*
00039  * Modify the RDN (relative distinguished name) of an entry.  In this
00040  * example, we change the dn "cn=Jacques Smith,ou=People,dc=example,dc=com
00041  * to "cn=Jacques M Smith,ou=People,dc=example,dc=com.
00042  *
00043  * Since it is an error to either (1) attempt to modrdn an entry which
00044  * does not exist, or (2) modrdn an entry where the destination name
00045  * already exists, we take some steps, for this example, to make sure
00046  * we'll succeed.  We (1) add "cn=Jacques Smith" (if the entry exists,
00047  * we just ignore the error, and (2) delete "cn=Jacques M Smith" (if the
00048  * entry doesn't exist, we ignore the error).
00049  *
00050  * We pass 0 for the "deleteoldrdn" argument to ldap_modrdn2_s().  This
00051  * means that after we change the RDN, the server will put the value
00052  * "Jacques Smith" into the cn attribute of the new entry, in addition to
00053  * "Jacques M Smith".
00054  */
00055 
00056 #include "examples.h"
00057 
00058 #define       NMODS  4
00059 
00060 unsigned long global_counter = 0;
00061 
00062 static void free_mods( LDAPMod **mods );
00063 
00064 int
00065 main( int argc, char **argv )
00066 {
00067     LDAP             *ld;
00068     char             *dn, *ndn, *nrdn;
00069     int                     i;
00070     int                     rc;
00071     LDAPMod          **mods;
00072 
00073     /* Values we'll use in creating the entry */
00074     char *objectclass_values[] = { "top", "person", "organizationalPerson",
00075                                "inetOrgPerson", NULL };
00076     char *cn_values[] = { "Jacques Smith", NULL };
00077     char *sn_values[] = { "Smith", NULL };
00078     char *givenname_values[] = { "Jacques", NULL };
00079 
00080     /* Specify the DN we're adding */
00081     dn = "cn=Jacques Smith, " PEOPLE_BASE;       /* see examples.h */
00082     /* the destination DN */
00083     ndn = "cn=Jacques M Smith, " PEOPLE_BASE;    /* see examples.h */
00084     /* the new RDN */
00085     nrdn = "cn=Jacques M Smith";
00086 
00087     /* get a handle to an LDAP connection */
00088     if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {
00089        perror( "ldap_init" );
00090        return( 1 );
00091     }
00092     /* authenticate to the directory as the Directory Manager */
00093     if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) {
00094        ldap_perror( ld, "ldap_simple_bind_s" );
00095        return( 1 );
00096     }
00097 
00098     if (( mods = ( LDAPMod ** ) malloc(( NMODS + 1 ) * sizeof( LDAPMod *)))
00099            == NULL ) {
00100        fprintf( stderr, "Cannot allocate memory for mods array\n" );
00101        return( 1 );
00102     }
00103     /* Construct the array of values to add */
00104     for ( i = 0; i < NMODS; i++ ) {
00105        if (( mods[ i ] = ( LDAPMod * ) malloc( sizeof( LDAPMod ))) == NULL ) {
00106            fprintf( stderr, "Cannot allocate memory for mods element\n" );
00107            return( 1 );
00108        }
00109     }
00110     mods[ 0 ]->mod_op = 0;
00111     mods[ 0 ]->mod_type = "objectclass";
00112     mods[ 0 ]->mod_values = objectclass_values;
00113     mods[ 1 ]->mod_op = 0;
00114     mods[ 1 ]->mod_type = "cn";
00115     mods[ 1 ]->mod_values = cn_values;
00116     mods[ 2 ]->mod_op = 0;
00117     mods[ 2 ]->mod_type = "sn";
00118     mods[ 2 ]->mod_values = sn_values;
00119     mods[ 3 ]->mod_op = 0;
00120     mods[ 3 ]->mod_type = "givenname";
00121     mods[ 3 ]->mod_values = givenname_values;
00122     mods[ 4 ] = NULL;
00123 
00124 
00125     /* Add the entry */
00126     if (( rc = ldap_add_s( ld, dn, mods )) != LDAP_SUCCESS ) {
00127        /* If entry exists already, fine.  Ignore this error. */
00128        if ( rc == LDAP_ALREADY_EXISTS ) {
00129            printf( "Entry \"%s is already in the directory.\n", dn );
00130        } else {
00131            ldap_perror( ld, "ldap_add_s" );
00132            free_mods( mods );
00133            return( 1 );
00134        }
00135     } else {
00136        printf( "Added entry \"%s\".\n", dn );
00137     }
00138     free_mods( mods );
00139 
00140     /* Delete the destination entry, for this example */
00141     if (( rc = ldap_delete_s( ld, ndn )) != LDAP_SUCCESS ) {
00142        /* If entry does not exist, fine.  Ignore this error. */
00143        if ( rc == LDAP_NO_SUCH_OBJECT ) {
00144            printf( "Entry \"%s\" is not in the directory.  "
00145                   "No need to delete.\n", ndn );
00146        } else {
00147            ldap_perror( ld, "ldap_delete_s" );
00148            return( 1 );
00149        }
00150     } else {
00151        printf( "Deleted entry \"%s\".\n", ndn );
00152     }
00153 
00154     /* Do the modrdn operation */
00155     if ( ldap_modrdn2_s( ld, dn, nrdn, 0 ) != LDAP_SUCCESS ) {
00156        ldap_perror( ld, "ldap_modrdn2_s" );
00157        return( 1 );
00158     }
00159 
00160     printf( "The modrdn operation was successful.  Entry\n"
00161            "\"%s\" has been changed to\n"
00162            "\"%s\".\n", dn, ndn );
00163 
00164     ldap_unbind( ld );
00165     return 0;
00166 }
00167 
00168 
00169 
00170 /*
00171  * Free a mods array.
00172  */
00173 static void
00174 free_mods( LDAPMod **mods )
00175 {
00176     int i;
00177  
00178     for ( i = 0; i < NMODS; i++ ) {
00179         free( mods[ i ] );
00180     }
00181     free( mods );
00182 }