Back to index

lightning-sunbird  0.9+nobinonly
ldapmodrdn.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 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
00039 
00040 #include "ldaptool.h"
00041 
00042 static int domodrdn( LDAP *ld, char *dn, char *rdn, int remove,
00043     LDAPControl **serverctrls);
00044 static void options_callback( int option, char *optarg );
00045 
00046 static int    contoper, remove_oldrdn;
00047 static LDAP   *ld;
00048 
00049 
00050 static void
00051 usage( void )
00052 {
00053     fprintf( stderr, "usage: %s [options] [dn rdn]\n", ldaptool_progname );
00054     fprintf( stderr, "options:\n" );
00055     ldaptool_common_usage( 0 );
00056     fprintf( stderr, "    -c\t\tcontinuous mode (do not stop on errors)\n" );
00057     fprintf( stderr, "    -r\t\tremove old RDN\n" );
00058     fprintf( stderr, "    -f file\tread changes from file\n" );
00059     exit( LDAP_PARAM_ERROR );
00060 }
00061 
00062 
00063 main( argc, argv )
00064     int              argc;
00065     char      **argv;
00066 {
00067     char      *entrydn, *rdn, buf[ 4096 ];
00068     int              rc, havedn, deref, optind;
00069     LDAPControl      *ctrls[2], **serverctrls;
00070 
00071     contoper = remove_oldrdn = 0;
00072 
00073     optind = ldaptool_process_args( argc, argv, "cr", 0, options_callback );
00074 
00075     if ( optind == -1 ) {
00076        usage();
00077     }
00078 
00079     if ( ldaptool_fp == NULL ) {
00080        ldaptool_fp = stdin;
00081     }
00082 
00083     havedn = 0;
00084     if (argc - optind == 2) {
00085        if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
00086            perror( "strdup" );
00087            exit( LDAP_NO_MEMORY );
00088        }
00089         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
00090            perror( "strdup" );
00091            exit( LDAP_NO_MEMORY );
00092         }
00093        ++havedn;
00094     } else if ( argc - optind != 0 ) {
00095        fprintf( stderr, "%s: invalid number of arguments, only two allowed\n",
00096            ldaptool_progname );
00097        usage();
00098     }
00099 
00100     ld = ldaptool_ldap_init( 0 );
00101 
00102     if ( !ldaptool_not ) {
00103        deref = LDAP_DEREF_NEVER;   /* this seems prudent */
00104        ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
00105     }
00106 
00107     ldaptool_bind( ld );
00108 
00109     if (( ctrls[0] = ldaptool_create_manage_dsait_control()) != NULL ) {
00110        ctrls[1] = NULL;
00111        serverctrls = ctrls;
00112     } else {
00113        serverctrls = NULL;
00114     }
00115 
00116     rc = 0;
00117     if (havedn) {
00118        rc = domodrdn(ld, entrydn, rdn, remove_oldrdn, serverctrls);
00119     } else while ((rc == 0 || contoper) &&
00120            fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
00121        if ( *buf != '\0' && *buf != '\n' ) {     /* skip blank lines */
00122            buf[ strlen( buf ) - 1 ] = '\0';      /* remove nl */
00123 
00124            if ( havedn ) {  /* have DN, get RDN */
00125               if (( rdn = strdup( buf )) == NULL ) {
00126                     perror( "strdup" );
00127                     exit( LDAP_NO_MEMORY );
00128               }
00129               rc = domodrdn(ld, entrydn, rdn, remove_oldrdn, serverctrls);
00130               havedn = 0;
00131            } else if ( !havedn ) { /* don't have DN yet */
00132                if (( entrydn = strdup( buf )) == NULL ) {
00133                   perror( "strdup" );
00134                   exit( LDAP_NO_MEMORY );
00135                }
00136               ++havedn;
00137            }
00138        }
00139     }
00140 
00141     ldaptool_cleanup( ld );
00142     exit( rc );
00143 }
00144 
00145 
00146 static void
00147 options_callback( int option, char *optarg )
00148 {
00149     switch( option ) {
00150     case 'c': /* continuous operation mode */
00151        ++contoper;
00152        break;
00153     case 'r': /* remove old RDN */
00154        ++remove_oldrdn;
00155        break;
00156     default:
00157        usage();
00158     }
00159 }
00160 
00161 
00162 static int
00163 domodrdn( LDAP *ld, char *dn, char *rdn, int remove_oldrdn,
00164        LDAPControl **serverctrls )
00165 {
00166     int       i;
00167 
00168     if ( ldaptool_verbose ) {
00169        printf( "modrdn %s:\n\t%s\n", dn, rdn );
00170        if (remove_oldrdn)
00171            printf("removing old RDN\n");
00172        else
00173            printf("keeping old RDN\n");
00174     }
00175 
00176     if ( !ldaptool_not ) {
00177        if (( i = ldaptool_rename_s( ld, dn, rdn, NULL, remove_oldrdn,
00178               serverctrls, NULL, "ldap_rename" )) == LDAP_SUCCESS
00179               && ldaptool_verbose ) {
00180            printf( "modrdn complete\n" );
00181        }
00182     } else {
00183        i = LDAP_SUCCESS;
00184     }
00185 
00186     return( i );
00187 }