Home | History | Annotate | Line # | Download | only in tools
ldapmodrdn.c revision 1.1
      1  1.1  lukem /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
      2  1.1  lukem /* $OpenLDAP: pkg/ldap/clients/tools/ldapmodrdn.c,v 1.116.2.4 2008/02/11 23:26:38 kurt Exp $ */
      3  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  1.1  lukem  *
      5  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      6  1.1  lukem  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
      7  1.1  lukem  * Portions Copyright 1998-2001 Net Boolean Incorporated.
      8  1.1  lukem  * Portions Copyright 2001-2003 IBM Corporation.
      9  1.1  lukem  * All rights reserved.
     10  1.1  lukem  *
     11  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     12  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     13  1.1  lukem  * Public License.
     14  1.1  lukem  *
     15  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     16  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     17  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     18  1.1  lukem  */
     19  1.1  lukem /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
     20  1.1  lukem  * This software is not subject to any license of Silicon Graphics
     21  1.1  lukem  * Inc. or Purdue University.
     22  1.1  lukem  *
     23  1.1  lukem  * Redistribution and use in source and binary forms are permitted
     24  1.1  lukem  * without restriction or fee of any kind as long as this notice
     25  1.1  lukem  * is preserved.
     26  1.1  lukem  */
     27  1.1  lukem /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
     28  1.1  lukem  * All rights reserved.
     29  1.1  lukem  *
     30  1.1  lukem  * Redistribution and use in source and binary forms are permitted
     31  1.1  lukem  * provided that this notice is preserved and that due credit is given
     32  1.1  lukem  * to the University of Michigan at Ann Arbor.  The name of the
     33  1.1  lukem  * University may not be used to endorse or promote products derived
     34  1.1  lukem  * from this software without specific prior written permission.  This
     35  1.1  lukem  * software is provided ``as is'' without express or implied warranty.
     36  1.1  lukem  */
     37  1.1  lukem /* ACKNOWLEDGEMENTS:
     38  1.1  lukem  * This work was originally developed by the University of Michigan
     39  1.1  lukem  * (as part of U-MICH LDAP).  Additional significant contributors
     40  1.1  lukem  * include:
     41  1.1  lukem  *    Kurt D. Zeilenga
     42  1.1  lukem  *    Juan C Gomez
     43  1.1  lukem  */
     44  1.1  lukem 
     45  1.1  lukem 
     46  1.1  lukem #include "portable.h"
     47  1.1  lukem 
     48  1.1  lukem #include <stdio.h>
     49  1.1  lukem 
     50  1.1  lukem #include <ac/stdlib.h>
     51  1.1  lukem 
     52  1.1  lukem #include <ac/ctype.h>
     53  1.1  lukem #include <ac/string.h>
     54  1.1  lukem #include <ac/unistd.h>
     55  1.1  lukem #include <ac/socket.h>
     56  1.1  lukem #include <ac/time.h>
     57  1.1  lukem 
     58  1.1  lukem #include <ldap.h>
     59  1.1  lukem #include "lutil.h"
     60  1.1  lukem #include "lutil_ldap.h"
     61  1.1  lukem #include "ldap_defaults.h"
     62  1.1  lukem 
     63  1.1  lukem #include "common.h"
     64  1.1  lukem 
     65  1.1  lukem 
     66  1.1  lukem static char	*newSuperior = NULL;
     67  1.1  lukem static int   remove_old_RDN = 0;
     68  1.1  lukem 
     69  1.1  lukem 
     70  1.1  lukem static int domodrdn(
     71  1.1  lukem     LDAP	*ld,
     72  1.1  lukem     char	*dn,
     73  1.1  lukem     char	*rdn,
     74  1.1  lukem     char	*newSuperior,
     75  1.1  lukem     int		remove );	/* flag: remove old RDN */
     76  1.1  lukem 
     77  1.1  lukem void
     78  1.1  lukem usage( void )
     79  1.1  lukem {
     80  1.1  lukem 	fprintf( stderr, _("Rename LDAP entries\n\n"));
     81  1.1  lukem 	fprintf( stderr, _("usage: %s [options] [dn rdn]\n"), prog);
     82  1.1  lukem 	fprintf( stderr, _("	dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"));
     83  1.1  lukem 	fprintf( stderr, _("		If not given, the list of modifications is read from stdin or\n"));
     84  1.1  lukem 	fprintf( stderr, _("		from the file specified by \"-f file\" (see man page).\n"));
     85  1.1  lukem 	fprintf( stderr, _("Rename options:\n"));
     86  1.1  lukem 	fprintf( stderr, _("  -r         remove old RDN\n"));
     87  1.1  lukem 	fprintf( stderr, _("  -s newsup  new superior entry\n"));
     88  1.1  lukem 	tool_common_usage();
     89  1.1  lukem 	exit( EXIT_FAILURE );
     90  1.1  lukem }
     91  1.1  lukem 
     92  1.1  lukem 
     93  1.1  lukem const char options[] = "rs:"
     94  1.1  lukem 	"cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
     95  1.1  lukem 
     96  1.1  lukem int
     97  1.1  lukem handle_private_option( int i )
     98  1.1  lukem {
     99  1.1  lukem 	switch ( i ) {
    100  1.1  lukem #if 0
    101  1.1  lukem 		int crit;
    102  1.1  lukem 		char *control, *cvalue;
    103  1.1  lukem 	case 'E': /* modrdn extensions */
    104  1.1  lukem 		if( protocol == LDAP_VERSION2 ) {
    105  1.1  lukem 			fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
    106  1.1  lukem 				prog, version );
    107  1.1  lukem 			exit( EXIT_FAILURE );
    108  1.1  lukem 		}
    109  1.1  lukem 
    110  1.1  lukem 		/* should be extended to support comma separated list of
    111  1.1  lukem 		 *	[!]key[=value] parameters, e.g.  -E !foo,bar=567
    112  1.1  lukem 		 */
    113  1.1  lukem 
    114  1.1  lukem 		crit = 0;
    115  1.1  lukem 		cvalue = NULL;
    116  1.1  lukem 		if( optarg[0] == '!' ) {
    117  1.1  lukem 			crit = 1;
    118  1.1  lukem 			optarg++;
    119  1.1  lukem 		}
    120  1.1  lukem 
    121  1.1  lukem 		control = strdup( optarg );
    122  1.1  lukem 		if ( (cvalue = strchr( control, '=' )) != NULL ) {
    123  1.1  lukem 			*cvalue++ = '\0';
    124  1.1  lukem 		}
    125  1.1  lukem 		fprintf( stderr, _("Invalid modrdn extension name: %s\n"), control );
    126  1.1  lukem 		usage();
    127  1.1  lukem #endif
    128  1.1  lukem 
    129  1.1  lukem 	case 'r':	/* remove old RDN */
    130  1.1  lukem 	    remove_old_RDN++;
    131  1.1  lukem 	    break;
    132  1.1  lukem 
    133  1.1  lukem 	case 's':	/* newSuperior */
    134  1.1  lukem 		if( protocol == LDAP_VERSION2 ) {
    135  1.1  lukem 			fprintf( stderr, _("%s: -X incompatible with LDAPv%d\n"),
    136  1.1  lukem 				prog, protocol );
    137  1.1  lukem 			exit( EXIT_FAILURE );
    138  1.1  lukem 		}
    139  1.1  lukem 	    newSuperior = strdup( optarg );
    140  1.1  lukem 	    protocol = LDAP_VERSION3;
    141  1.1  lukem 	    break;
    142  1.1  lukem 
    143  1.1  lukem 	default:
    144  1.1  lukem 		return 0;
    145  1.1  lukem 	}
    146  1.1  lukem 	return 1;
    147  1.1  lukem }
    148  1.1  lukem 
    149  1.1  lukem 
    150  1.1  lukem int
    151  1.1  lukem main(int argc, char **argv)
    152  1.1  lukem {
    153  1.1  lukem     char		*entrydn = NULL, *rdn = NULL, buf[ 4096 ];
    154  1.1  lukem     FILE		*fp;
    155  1.1  lukem     LDAP		*ld;
    156  1.1  lukem 	int		rc, retval, havedn;
    157  1.1  lukem 
    158  1.1  lukem     tool_init( TOOL_MODRDN );
    159  1.1  lukem     prog = lutil_progname( "ldapmodrdn", argc, argv );
    160  1.1  lukem 
    161  1.1  lukem 	tool_args( argc, argv );
    162  1.1  lukem 
    163  1.1  lukem     havedn = 0;
    164  1.1  lukem     if (argc - optind == 2) {
    165  1.1  lukem 	if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
    166  1.1  lukem 	    perror( "strdup" );
    167  1.1  lukem 	    return( EXIT_FAILURE );
    168  1.1  lukem 	}
    169  1.1  lukem         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
    170  1.1  lukem 	    perror( "strdup" );
    171  1.1  lukem 	    return( EXIT_FAILURE );
    172  1.1  lukem         }
    173  1.1  lukem 	++havedn;
    174  1.1  lukem     } else if ( argc - optind != 0 ) {
    175  1.1  lukem 	fprintf( stderr, _("%s: invalid number of arguments (%d), only two allowed\n"), prog, argc-optind );
    176  1.1  lukem 	usage();
    177  1.1  lukem     }
    178  1.1  lukem 
    179  1.1  lukem     if ( infile != NULL ) {
    180  1.1  lukem 	if (( fp = fopen( infile, "r" )) == NULL ) {
    181  1.1  lukem 	    perror( infile );
    182  1.1  lukem 	    return( EXIT_FAILURE );
    183  1.1  lukem 	}
    184  1.1  lukem     } else {
    185  1.1  lukem 	fp = stdin;
    186  1.1  lukem     }
    187  1.1  lukem 
    188  1.1  lukem 	ld = tool_conn_setup( 0, 0 );
    189  1.1  lukem 
    190  1.1  lukem 	if ( pw_file || want_bindpw ) {
    191  1.1  lukem 		if ( pw_file ) {
    192  1.1  lukem 			rc = lutil_get_filed_password( pw_file, &passwd );
    193  1.1  lukem 			if( rc ) return EXIT_FAILURE;
    194  1.1  lukem 		} else {
    195  1.1  lukem 			passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
    196  1.1  lukem 			passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
    197  1.1  lukem 		}
    198  1.1  lukem 	}
    199  1.1  lukem 
    200  1.1  lukem 	tool_bind( ld );
    201  1.1  lukem 
    202  1.1  lukem 	tool_server_controls( ld, NULL, 0 );
    203  1.1  lukem 
    204  1.1  lukem     retval = rc = 0;
    205  1.1  lukem     if (havedn)
    206  1.1  lukem 	retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
    207  1.1  lukem     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
    208  1.1  lukem 	if ( *buf != '\n' ) {	/* blank lines optional, skip */
    209  1.1  lukem 	    buf[ strlen( buf ) - 1 ] = '\0';	/* remove nl */
    210  1.1  lukem 
    211  1.1  lukem 	    if ( havedn ) {	/* have DN, get RDN */
    212  1.1  lukem 		if (( rdn = strdup( buf )) == NULL ) {
    213  1.1  lukem                     perror( "strdup" );
    214  1.1  lukem                     return( EXIT_FAILURE );
    215  1.1  lukem 		}
    216  1.1  lukem 		rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
    217  1.1  lukem 		if ( rc != 0 )
    218  1.1  lukem 			retval = rc;
    219  1.1  lukem 		havedn = 0;
    220  1.1  lukem 	    } else if ( !havedn ) {	/* don't have DN yet */
    221  1.1  lukem 	        if (( entrydn = strdup( buf )) == NULL ) {
    222  1.1  lukem 		    perror( "strdup" );
    223  1.1  lukem 		    return( EXIT_FAILURE );
    224  1.1  lukem 	        }
    225  1.1  lukem 		++havedn;
    226  1.1  lukem 	    }
    227  1.1  lukem 	}
    228  1.1  lukem     }
    229  1.1  lukem 
    230  1.1  lukem 	tool_unbind( ld );
    231  1.1  lukem 	tool_destroy();
    232  1.1  lukem     return( retval );
    233  1.1  lukem }
    234  1.1  lukem 
    235  1.1  lukem static int domodrdn(
    236  1.1  lukem     LDAP	*ld,
    237  1.1  lukem     char	*dn,
    238  1.1  lukem     char	*rdn,
    239  1.1  lukem     char	*newSuperior,
    240  1.1  lukem     int		remove ) /* flag: remove old RDN */
    241  1.1  lukem {
    242  1.1  lukem 	int rc, code, id;
    243  1.1  lukem 	char *matcheddn=NULL, *text=NULL, **refs=NULL;
    244  1.1  lukem 	LDAPControl **ctrls = NULL;
    245  1.1  lukem 	LDAPMessage *res;
    246  1.1  lukem 
    247  1.1  lukem     if ( verbose ) {
    248  1.1  lukem 		printf( _("Renaming \"%s\"\n"), dn );
    249  1.1  lukem 		printf( _("\tnew rdn=\"%s\" (%s old rdn)\n"),
    250  1.1  lukem 			rdn, remove ? _("delete") : _("keep") );
    251  1.1  lukem 		if( newSuperior != NULL ) {
    252  1.1  lukem 			printf(_("\tnew parent=\"%s\"\n"), newSuperior);
    253  1.1  lukem 		}
    254  1.1  lukem 	}
    255  1.1  lukem 
    256  1.1  lukem 	if( dont ) return LDAP_SUCCESS;
    257  1.1  lukem 
    258  1.1  lukem 	rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
    259  1.1  lukem 		NULL, NULL, &id );
    260  1.1  lukem 
    261  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
    262  1.1  lukem 		fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
    263  1.1  lukem 			prog, ldap_err2string( rc ), rc );
    264  1.1  lukem 		return rc;
    265  1.1  lukem 	}
    266  1.1  lukem 
    267  1.1  lukem 	for ( ; ; ) {
    268  1.1  lukem 		struct timeval	tv = { 0, 0 };
    269  1.1  lukem 
    270  1.1  lukem 		if ( tool_check_abandon( ld, id ) ) {
    271  1.1  lukem 			return LDAP_CANCELLED;
    272  1.1  lukem 		}
    273  1.1  lukem 
    274  1.1  lukem 		tv.tv_sec = 0;
    275  1.1  lukem 		tv.tv_usec = 100000;
    276  1.1  lukem 
    277  1.1  lukem 		rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
    278  1.1  lukem 		if ( rc < 0 ) {
    279  1.1  lukem 			tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
    280  1.1  lukem 			return rc;
    281  1.1  lukem 		}
    282  1.1  lukem 
    283  1.1  lukem 		if ( rc != 0 ) {
    284  1.1  lukem 			break;
    285  1.1  lukem 		}
    286  1.1  lukem 	}
    287  1.1  lukem 
    288  1.1  lukem 	rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
    289  1.1  lukem 
    290  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
    291  1.1  lukem 		fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
    292  1.1  lukem 			prog, ldap_err2string( rc ), rc );
    293  1.1  lukem 		return rc;
    294  1.1  lukem 	}
    295  1.1  lukem 
    296  1.1  lukem 	if( verbose || code != LDAP_SUCCESS ||
    297  1.1  lukem 		(matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
    298  1.1  lukem 	{
    299  1.1  lukem 		printf( _("Rename Result: %s (%d)\n"),
    300  1.1  lukem 			ldap_err2string( code ), code );
    301  1.1  lukem 
    302  1.1  lukem 		if( text && *text ) {
    303  1.1  lukem 			printf( _("Additional info: %s\n"), text );
    304  1.1  lukem 		}
    305  1.1  lukem 
    306  1.1  lukem 		if( matcheddn && *matcheddn ) {
    307  1.1  lukem 			printf( _("Matched DN: %s\n"), matcheddn );
    308  1.1  lukem 		}
    309  1.1  lukem 
    310  1.1  lukem 		if( refs ) {
    311  1.1  lukem 			int i;
    312  1.1  lukem 			for( i=0; refs[i]; i++ ) {
    313  1.1  lukem 				printf(_("Referral: %s\n"), refs[i] );
    314  1.1  lukem 			}
    315  1.1  lukem 		}
    316  1.1  lukem 	}
    317  1.1  lukem 
    318  1.1  lukem 	if (ctrls) {
    319  1.1  lukem 		tool_print_ctrls( ld, ctrls );
    320  1.1  lukem 		ldap_controls_free( ctrls );
    321  1.1  lukem     }
    322  1.1  lukem 
    323  1.1  lukem 	ber_memfree( text );
    324  1.1  lukem 	ber_memfree( matcheddn );
    325  1.1  lukem 	ber_memvfree( (void **) refs );
    326  1.1  lukem 
    327  1.1  lukem 	return code;
    328  1.1  lukem }
    329