예제 #1
0
파일: ldapmodrdn.c 프로젝트: 1ack/Impala
int
main(int argc, char **argv)
{
	char		*entrydn = NULL, *rdn = NULL, buf[ 4096 ];
	FILE		*fp = NULL;
	LDAP		*ld;
	int		rc, retval, havedn;

	tool_init( TOOL_MODRDN );
	prog = lutil_progname( "ldapmodrdn", argc, argv );

	tool_args( argc, argv );

	havedn = 0;
	if (argc - optind == 2) {
		if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
			perror( "strdup" );
			retval = EXIT_FAILURE;
			goto fail;
		}
		if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
			perror( "strdup" );
			retval = EXIT_FAILURE;
			goto fail;
		}
		++havedn;
	} else if ( argc - optind != 0 ) {
		fprintf( stderr, _("%s: invalid number of arguments (%d), only two allowed\n"), prog, argc-optind );
		usage();
	}

	if ( infile != NULL ) {
		if (( fp = fopen( infile, "r" )) == NULL ) {
			perror( infile );
			retval = EXIT_FAILURE;
			goto fail;
		}
	} else {
		fp = stdin;
	}

	ld = tool_conn_setup( 0, 0 );

	tool_bind( ld );

	tool_server_controls( ld, NULL, 0 );

	retval = rc = 0;
	if (havedn)
		retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
	else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
		if ( *buf != '\n' ) {	/* blank lines optional, skip */
			buf[ strlen( buf ) - 1 ] = '\0';	/* remove nl */

			if ( havedn ) {	/* have DN, get RDN */
				if (( rdn = strdup( buf )) == NULL ) {
					perror( "strdup" );
					retval = EXIT_FAILURE;
					goto fail;
				}
				rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
				if ( rc != 0 )
					retval = rc;
				havedn = 0;
				free( rdn ); rdn = NULL;
				free( entrydn ); entrydn = NULL;
			} else if ( !havedn ) {	/* don't have DN yet */
				if (( entrydn = strdup( buf )) == NULL ) {
					retval = EXIT_FAILURE;
					goto fail;
				}
				++havedn;
			}
		}
	}

	tool_unbind( ld );
	tool_destroy();
fail:
	if ( fp && fp != stdin ) fclose( fp );
	if ( entrydn ) free( entrydn );
	if ( rdn ) free( rdn );
	return( retval );
}
예제 #2
0
int
main(int argc, char **argv)
{
    char		*entrydn = NULL, *rdn = NULL, buf[ 4096 ];
    FILE		*fp;
    LDAP		*ld;
    int		rc, retval, havedn;

    prog = lutil_progname( "ldapmodrdn", argc, argv );

    tool_args( argc, argv );

    havedn = 0;
    if (argc - optind == 2) {
        if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
            perror( "strdup" );
            return( EXIT_FAILURE );
        }
        if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
            perror( "strdup" );
            return( EXIT_FAILURE );
        }
        ++havedn;
    } else if ( argc - optind != 0 ) {
        fprintf( stderr, "%s: invalid number of arguments (%d), "
                 "only two allowed\n", prog, argc-optind );
        usage();
    }

    if ( infile != NULL ) {
        if (( fp = fopen( infile, "r" )) == NULL ) {
            perror( infile );
            return( EXIT_FAILURE );
        }
    } else {
        fp = stdin;
    }

    ld = tool_conn_setup( 0, 0 );

    if ( pw_file || want_bindpw ) {
        if ( pw_file ) {
            rc = lutil_get_filed_password( pw_file, &passwd );
            if( rc ) return EXIT_FAILURE;
        } else {
            passwd.bv_val = getpassphrase( "Enter LDAP Password: "******"strdup" );
                        return( EXIT_FAILURE );
                    }
                    rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
                    if ( rc != 0 )
                        retval = rc;
                    havedn = 0;
                } else if ( !havedn ) {	/* don't have DN yet */
                    if (( entrydn = strdup( buf )) == NULL ) {
                        perror( "strdup" );
                        return( EXIT_FAILURE );
                    }
                    ++havedn;
                }
            }
        }

    ldap_unbind( ld );

    return( retval );
}
예제 #3
0
int 
main(int argc, char **argv )
{
	char *myname, *entrydn, *rdn, buf[ 4096 ];
	int rc, havedn, deref, optind;
	char * L_newParent = NULL;
	int haverdn = 0;

	int L_protoVersion = LDAP_VERSION3;

	char *locale = setlocale(LC_ALL, "");
	textdomain(TEXT_DOMAIN);
	ldaplogconfigf(NULL); 


	contoper =  remove_oldrdn = 0;

	if ((myname = strrchr(argv[0], '/')) == NULL)
		myname = argv[0];
	else
		++myname;

	optind = ldaptool_process_args( argc, argv, "cr", 0, options_callback);
	
	if ( optind == -1 ) {
		usage();
	}

	if ( ldaptool_fp == NULL ) {
	ldaptool_fp = stdin;
	}

	havedn = 0;
	if (argc - optind == 3) 		/* accept as arguments: dn rdn newsuperior */
	{
		if (( L_newParent = strdup( argv[argc - 1] )) == NULL ) 
		{
			perror( "strdup" );
			exit( LDAP_NO_MEMORY );
		}

		if (( rdn = strdup( argv[argc - 2] )) == NULL ) 
		{
			perror( "strdup" );
			exit( LDAP_NO_MEMORY );
		}

		if (( entrydn = strdup( argv[argc - 3] )) == NULL ) 
		{
			perror( "strdup" );
			exit( LDAP_NO_MEMORY );
		}
		++havedn;
	} 
	else if (argc - optind == 2) 		/* accept as arguments: dn rdn */
	{
		if (( rdn = strdup( argv[argc - 1] )) == NULL ) 
		{
			perror( "strdup" );
			exit( LDAP_NO_MEMORY );
		}

		if (( entrydn = strdup( argv[argc - 2] )) == NULL ) 
		{
			perror( "strdup" );
			exit( 1 );
		}
		++havedn;
	} 
	else if ( argc - optind != 0 ) 
	{
		fprintf( stderr, gettext("%s: invalid number of arguments, only two or three allowed\n"), myname);
		usage();
		exit( 1 );
	}

	ld = ldaptool_ldap_init (0);

	if ( !ldaptool_not ) {
		deref = LDAP_DEREF_NEVER;	/* this seems prudent */
		ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
	}

	ldaptool_bind( ld );

	rc = 0;
	if (havedn)
	{
		rc = domodrdn(ld, entrydn, rdn, L_newParent, remove_oldrdn);
	}
	else while (	(rc == 0 || contoper) && 
					(fgets(buf, sizeof(buf), ldaptool_fp) != NULL) )

	{
		/* 
		 * The format of the file is one of the following:
		 * 	dn
		 * 	rdn
		 * 	newsuperior
		 * 	<blank lines...>
		 * OR
		 * 	dn
		 * 	rdn
		 * 	<blank lines...>
		 * both types of sequences can be found in the file
		 */
		
		if ( (strlen(buf) == 1) && (ldaptool_fp == stdin) )
			break;

		buf[ strlen( buf ) - 1 ] = '\0';	/* remove nl */
		if ( *buf != '\0' ) 		/* blank lines optional, skip */
		{
			if ( haverdn )		/* first type of sequence */
			{
				if (( L_newParent = strdup( buf )) == NULL ) 
				{
					perror( "strdup" );
					exit( LDAP_NO_MEMORY );
				}
				if ( L_newParent && (L_protoVersion == LDAP_VERSION) )
				{
					printf( gettext("LDAP Server is V2: <newsuperior> argument is ignored...\n") );
					L_newParent = NULL;
				}
				rc = domodrdn(ld, entrydn, rdn, L_newParent, remove_oldrdn);
				haverdn = 0;
			}
			else if ( havedn ) 		/* have DN, get RDN */
			{
				if (( rdn = strdup( buf )) == NULL ) 
				{
					perror( "strdup" );
					exit( LDAP_NO_MEMORY );
				}
				havedn = 0;
				++haverdn;
			}
			else if ( !havedn ) 		/* don't have DN yet */
			{
				if (( entrydn = strdup( buf )) == NULL)
				{
					perror( "strdup" );
					exit( LDAP_NO_MEMORY );
				}
				++havedn;
			}
		}
		else
		{
			printf(gettext("kex: new line %d\n"), rc);
			if ( haverdn )		/* second type of sequence */
			{
				rc = domodrdn(ld, entrydn, rdn, NULL, remove_oldrdn);
				haverdn = 0;
			}
		}
	}
	if ( (rc == 0 || contoper) && haverdn )		/* second type of sequence */
	{
		rc = domodrdn(ld, entrydn, rdn, NULL, remove_oldrdn);
		haverdn = 0;
	}

	ldaptool_cleanup( ld );

	exit( rc );
}