xref: /titanic_51/usr/src/cmd/ldap/common/ldapmodrdn.c (revision a506a34ceb0e9dcc6c61bf0560202f8538928650)
17c478bd9Sstevel@tonic-gate /*
2*a506a34cSth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include <stdio.h>
117c478bd9Sstevel@tonic-gate #include <string.h>
127c478bd9Sstevel@tonic-gate #include <stdlib.h>
137c478bd9Sstevel@tonic-gate #include <locale.h>
147c478bd9Sstevel@tonic-gate #include <ctype.h>
157c478bd9Sstevel@tonic-gate #include <lber.h>
167c478bd9Sstevel@tonic-gate #include <ldap.h>
177c478bd9Sstevel@tonic-gate #include <locale.h>
187c478bd9Sstevel@tonic-gate #include "ldaptool.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate static int	contoper, remove_oldrdn;
217c478bd9Sstevel@tonic-gate static LDAP	*ld;
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate static int domodrdn( LDAP *ld, char *dn, char *rdn, char *newsuperior,
247c478bd9Sstevel@tonic-gate 			int remove_oldrdn);
257c478bd9Sstevel@tonic-gate static void options_callback( int option, char *optarg );
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate static void usage( void )
287c478bd9Sstevel@tonic-gate {
297c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("usage: %s [options] [dn newrdn [newsuperior]]\n"), ldaptool_progname);
307c478bd9Sstevel@tonic-gate 	fprintf( stderr, gettext("options:\n"));
317c478bd9Sstevel@tonic-gate 	ldaptool_common_usage( 0 );
327c478bd9Sstevel@tonic-gate 	fprintf( stderr, gettext("    -c\t\tcontinuous mode\n") );
337c478bd9Sstevel@tonic-gate 	fprintf( stderr, gettext("    -r\t\tremove old RDN from the entries\n"));
347c478bd9Sstevel@tonic-gate 	fprintf( stderr, gettext("    -f file\tread changes from `file'\n") );
357c478bd9Sstevel@tonic-gate 	exit(LDAP_PARAM_ERROR );
367c478bd9Sstevel@tonic-gate }
377c478bd9Sstevel@tonic-gate 
38*a506a34cSth160488 int
39*a506a34cSth160488 main(int argc, char **argv )
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	char *myname, *entrydn, *rdn, buf[ 4096 ];
427c478bd9Sstevel@tonic-gate 	int rc, havedn, deref, optind;
437c478bd9Sstevel@tonic-gate 	char * L_newParent = NULL;
447c478bd9Sstevel@tonic-gate 	int haverdn = 0;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	int L_protoVersion = LDAP_VERSION3;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	char *locale = setlocale(LC_ALL, "");
497c478bd9Sstevel@tonic-gate 	textdomain(TEXT_DOMAIN);
507c478bd9Sstevel@tonic-gate 	ldaplogconfigf(NULL);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	contoper =  remove_oldrdn = 0;
547c478bd9Sstevel@tonic-gate 
55*a506a34cSth160488 	if ((myname = strrchr(argv[0], '/')) == NULL)
56*a506a34cSth160488 		myname = argv[0];
57*a506a34cSth160488 	else
58*a506a34cSth160488 		++myname;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	optind = ldaptool_process_args( argc, argv, "cr", 0, options_callback);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if ( optind == -1 ) {
637c478bd9Sstevel@tonic-gate 		usage();
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if ( ldaptool_fp == NULL ) {
677c478bd9Sstevel@tonic-gate 	ldaptool_fp = stdin;
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	havedn = 0;
717c478bd9Sstevel@tonic-gate 	if (argc - optind == 3) 		/* accept as arguments: dn rdn newsuperior */
727c478bd9Sstevel@tonic-gate 	{
737c478bd9Sstevel@tonic-gate 		if (( L_newParent = strdup( argv[argc - 1] )) == NULL )
747c478bd9Sstevel@tonic-gate 		{
757c478bd9Sstevel@tonic-gate 			perror( "strdup" );
767c478bd9Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		if (( rdn = strdup( argv[argc - 2] )) == NULL )
807c478bd9Sstevel@tonic-gate 		{
817c478bd9Sstevel@tonic-gate 			perror( "strdup" );
827c478bd9Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		if (( entrydn = strdup( argv[argc - 3] )) == NULL )
867c478bd9Sstevel@tonic-gate 		{
877c478bd9Sstevel@tonic-gate 			perror( "strdup" );
887c478bd9Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 		++havedn;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	else if (argc - optind == 2) 		/* accept as arguments: dn rdn */
937c478bd9Sstevel@tonic-gate 	{
947c478bd9Sstevel@tonic-gate 		if (( rdn = strdup( argv[argc - 1] )) == NULL )
957c478bd9Sstevel@tonic-gate 		{
967c478bd9Sstevel@tonic-gate 			perror( "strdup" );
977c478bd9Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		if (( entrydn = strdup( argv[argc - 2] )) == NULL )
1017c478bd9Sstevel@tonic-gate 		{
1027c478bd9Sstevel@tonic-gate 			perror( "strdup" );
1037c478bd9Sstevel@tonic-gate 			exit( 1 );
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 		++havedn;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	else if ( argc - optind != 0 )
1087c478bd9Sstevel@tonic-gate 	{
1097c478bd9Sstevel@tonic-gate 		fprintf( stderr, gettext("%s: invalid number of arguments, only two or three allowed\n"), myname);
1107c478bd9Sstevel@tonic-gate 		usage();
1117c478bd9Sstevel@tonic-gate 		exit( 1 );
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	ld = ldaptool_ldap_init (0);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ( !ldaptool_not ) {
1177c478bd9Sstevel@tonic-gate 		deref = LDAP_DEREF_NEVER;	/* this seems prudent */
1187c478bd9Sstevel@tonic-gate 		ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	ldaptool_bind( ld );
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	rc = 0;
1247c478bd9Sstevel@tonic-gate 	if (havedn)
1257c478bd9Sstevel@tonic-gate 	{
1267c478bd9Sstevel@tonic-gate 		rc = domodrdn(ld, entrydn, rdn, L_newParent, remove_oldrdn);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	else while (	(rc == 0 || contoper) &&
1297c478bd9Sstevel@tonic-gate 					(fgets(buf, sizeof(buf), ldaptool_fp) != NULL) )
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	{
1327c478bd9Sstevel@tonic-gate 		/*
1337c478bd9Sstevel@tonic-gate 		 * The format of the file is one of the following:
1347c478bd9Sstevel@tonic-gate 		 * 	dn
1357c478bd9Sstevel@tonic-gate 		 * 	rdn
1367c478bd9Sstevel@tonic-gate 		 * 	newsuperior
1377c478bd9Sstevel@tonic-gate 		 * 	<blank lines...>
1387c478bd9Sstevel@tonic-gate 		 * OR
1397c478bd9Sstevel@tonic-gate 		 * 	dn
1407c478bd9Sstevel@tonic-gate 		 * 	rdn
1417c478bd9Sstevel@tonic-gate 		 * 	<blank lines...>
1427c478bd9Sstevel@tonic-gate 		 * both types of sequences can be found in the file
1437c478bd9Sstevel@tonic-gate 		 */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		if ( (strlen(buf) == 1) && (ldaptool_fp == stdin) )
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		buf[ strlen( buf ) - 1 ] = '\0';	/* remove nl */
1497c478bd9Sstevel@tonic-gate 		if ( *buf != '\0' ) 		/* blank lines optional, skip */
1507c478bd9Sstevel@tonic-gate 		{
1517c478bd9Sstevel@tonic-gate 			if ( haverdn )		/* first type of sequence */
1527c478bd9Sstevel@tonic-gate 			{
1537c478bd9Sstevel@tonic-gate 				if (( L_newParent = strdup( buf )) == NULL )
1547c478bd9Sstevel@tonic-gate 				{
1557c478bd9Sstevel@tonic-gate 					perror( "strdup" );
1567c478bd9Sstevel@tonic-gate 					exit( LDAP_NO_MEMORY );
1577c478bd9Sstevel@tonic-gate 				}
1587c478bd9Sstevel@tonic-gate 				if ( L_newParent && (L_protoVersion == LDAP_VERSION) )
1597c478bd9Sstevel@tonic-gate 				{
1607c478bd9Sstevel@tonic-gate 					printf( gettext("LDAP Server is V2: <newsuperior> argument is ignored...\n") );
1617c478bd9Sstevel@tonic-gate 					L_newParent = NULL;
1627c478bd9Sstevel@tonic-gate 				}
1637c478bd9Sstevel@tonic-gate 				rc = domodrdn(ld, entrydn, rdn, L_newParent, remove_oldrdn);
1647c478bd9Sstevel@tonic-gate 				haverdn = 0;
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 			else if ( havedn ) 		/* have DN, get RDN */
1677c478bd9Sstevel@tonic-gate 			{
1687c478bd9Sstevel@tonic-gate 				if (( rdn = strdup( buf )) == NULL )
1697c478bd9Sstevel@tonic-gate 				{
1707c478bd9Sstevel@tonic-gate 					perror( "strdup" );
1717c478bd9Sstevel@tonic-gate 					exit( LDAP_NO_MEMORY );
1727c478bd9Sstevel@tonic-gate 				}
1737c478bd9Sstevel@tonic-gate 				havedn = 0;
1747c478bd9Sstevel@tonic-gate 				++haverdn;
1757c478bd9Sstevel@tonic-gate 			}
1767c478bd9Sstevel@tonic-gate 			else if ( !havedn ) 		/* don't have DN yet */
1777c478bd9Sstevel@tonic-gate 			{
1787c478bd9Sstevel@tonic-gate 				if (( entrydn = strdup( buf )) == NULL)
1797c478bd9Sstevel@tonic-gate 				{
1807c478bd9Sstevel@tonic-gate 					perror( "strdup" );
1817c478bd9Sstevel@tonic-gate 					exit( LDAP_NO_MEMORY );
1827c478bd9Sstevel@tonic-gate 				}
1837c478bd9Sstevel@tonic-gate 				++havedn;
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		else
1877c478bd9Sstevel@tonic-gate 		{
1887c478bd9Sstevel@tonic-gate 			printf(gettext("kex: new line %d\n"), rc);
1897c478bd9Sstevel@tonic-gate 			if ( haverdn )		/* second type of sequence */
1907c478bd9Sstevel@tonic-gate 			{
1917c478bd9Sstevel@tonic-gate 				rc = domodrdn(ld, entrydn, rdn, NULL, remove_oldrdn);
1927c478bd9Sstevel@tonic-gate 				haverdn = 0;
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	if ( (rc == 0 || contoper) && haverdn )		/* second type of sequence */
1977c478bd9Sstevel@tonic-gate 	{
1987c478bd9Sstevel@tonic-gate 		rc = domodrdn(ld, entrydn, rdn, NULL, remove_oldrdn);
1997c478bd9Sstevel@tonic-gate 		haverdn = 0;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	ldaptool_cleanup( ld );
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	exit( rc );
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate static void
2087c478bd9Sstevel@tonic-gate options_callback( int option, char *optarg )
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	switch( option ) {
2117c478bd9Sstevel@tonic-gate 		case 'c':	/* continuous operation mode */
2127c478bd9Sstevel@tonic-gate 			++contoper;
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 		case 'r':	/* remove old RDN */
2157c478bd9Sstevel@tonic-gate 			++remove_oldrdn;
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 		default:
2187c478bd9Sstevel@tonic-gate 		usage();
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static int
2237c478bd9Sstevel@tonic-gate domodrdn( LDAP *ld, char *dn, char *rdn, char *newsuperior, int remove_oldrdn )
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	int	rc = LDAP_SUCCESS;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if ( ldaptool_verbose )
2287c478bd9Sstevel@tonic-gate 		printf( gettext("new RDN: %1$s (%2$skeep existing values)\n"),
229*a506a34cSth160488 						rdn, remove_oldrdn ? "do not " : "" );
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	printf( gettext("%1$srenaming entry %2$s\n"),
2327c478bd9Sstevel@tonic-gate 			ldaptool_not ? "!" : "", dn );
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if ( !ldaptool_not )
2357c478bd9Sstevel@tonic-gate 	{
2367c478bd9Sstevel@tonic-gate 		rc = ldap_rename_s( ld, dn, rdn, newsuperior, remove_oldrdn, NULL, NULL );
2377c478bd9Sstevel@tonic-gate 		if ( rc != LDAP_SUCCESS )
2387c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("ldap_rename_s: %s\n"), ldap_err2string(rc));
2397c478bd9Sstevel@tonic-gate 		else if ( ldaptool_verbose )
2407c478bd9Sstevel@tonic-gate 			printf( gettext("rename completed\n") );
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	putchar('\n');
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return( rc );
2467c478bd9Sstevel@tonic-gate }
247