int
main( int argc, char **argv )
{
    char	buf[ 4096 ];
    int		rc = 0;
    int		deref, optind;
    LDAPControl	*ldctrl;

#ifdef notdef
#ifdef HPUX11
#ifndef __LP64__
	_main( argc, argv);
#endif /* __LP64_ */
#endif /* HPUX11 */
#endif

    contoper = 0;

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

    if ( ldaptool_fp == NULL && optind >= argc ) {
	ldaptool_fp = stdin;
    }

    ld = ldaptool_ldap_init( 0 );

    deref = LDAP_DEREF_NEVER;	/* prudent, but probably unnecessary */
    ldap_set_option( ld, LDAP_OPT_DEREF, &deref );

    ldaptool_bind( ld );

    if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
    } 

    if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
	ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
    }

    if ( ldaptool_fp == NULL ) {
	for ( ; optind < argc; ++optind ) {
            char *conv;

            conv = ldaptool_local2UTF8( argv[ optind ], "DN" );
	    rc = dodelete( ld, conv, ldaptool_request_ctrls );
            if( conv != NULL )
                free( conv );
	}
    } else {
	while ((rc == 0 || contoper) &&
		fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
	    buf[ strlen( buf ) - 1 ] = '\0';	/* remove trailing newline */
	    if ( *buf != '\0' ) {
		rc = dodelete( ld, buf, ldaptool_request_ctrls );
	    }
	}
    }

    ldaptool_reset_control_array( ldaptool_request_ctrls );
    ldaptool_cleanup( ld );

    /* check for and report output error */
    fflush( stdout );
    rc = ldaptool_check_ferror( stdout, rc, "output error (output might be incomplete)" );
    return( rc );
}
int
main( int argc, char **argv )
{
	int	optind;
	int	rc = LDAP_SUCCESS; /* being superoptimistic for -n */
	LDAP	*ld;
	LDAPControl *ldctrl;
		
#ifdef notdef
#ifdef HPUX11
#ifndef __LP64__
	_main( argc, argv);
#endif /* __LP64_ */
#endif /* HPUX11 */
#endif

	optind = ldaptool_process_args( argc, argv, "ASa:t:s:T:", 0, options_callback );
	
	if ( (optind == -1) || (argc <= 1) ) {
		usage();
	}
	if ( (argc - optind) >= 1 ) {
		if ( argv[ optind ] ) {
			if ( (userid.bv_val = ldaptool_local2UTF8(argv[ optind ], "userid") ) == NULL ) {
				fprintf( stderr, "%s: not enough memory\n", ldaptool_progname );
				exit( LDAP_NO_MEMORY );
			}
			userid.bv_len = strlen( userid.bv_val );
			++optind;
		}
	}
	
	ld = ldaptool_ldap_init( 0 );
	ldaptool_bind( ld );
	
	if ( ldaptool_nobind && (userid.bv_val == NULL) && (userid.bv_len == 0) ) {
		usage();
	}

	if ((ldctrl = ldaptool_create_manage_dsait_control()) != NULL) {
		ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
	}

	if ((ldctrl = ldaptool_create_proxyauth_control(ld)) != NULL) {
		ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
	}

	if ( !ldaptool_not ) {
		rc = ldap_passwd_s( ld, userid.bv_val ? &userid : NULL, 
								oldpasswd.bv_val ? &oldpasswd : NULL, 
								newpasswd.bv_val ? &newpasswd : NULL, 
								&genpasswd, ldaptool_request_ctrls, NULL );
		if ( rc != LDAP_SUCCESS ) {
			ldap_perror( ld, ldaptool_progname );
		} else {
			fprintf( stderr, "%s: password successfully changed\n", 
					 ldaptool_progname );
		}
	
		if ( (genpasswd.bv_val != NULL) && (genpasswd.bv_len != 0) ) {
			fprintf( stderr, "New password: %s\n", genpasswd.bv_val );
		}
	}

	ldaptool_cleanup( ld );

	/* check for and report output error */
	fflush( stdout );
	rc = ldaptool_check_ferror( stdout, rc, "output error (output might be incomplete)" );
	return( rc );
}
Beispiel #3
0
int
main( int argc, char **argv )
{
    char		*filtpattern, **attrs;
    int			rc, optind, i, first, free_filtpattern;
    LDAP		*ld;

#ifdef SOLARIS_LDAP_CMD
    char *locale = setlocale(LC_ALL, "");
    textdomain(TEXT_DOMAIN);
    ldaptool_require_binddn = 0;
#endif	/* SOLARIS_LDAP_CMD */

    free_filtpattern = 0;
    deref = LDAP_DEREF_NEVER;
    allow_binary = vals2tmp = attrsonly = 0;
    minimize_base64 = produce_file_urls = 0;
    ldif = 1;
    fold = 1;
    sizelimit = timelimit = 0;
    scope = LDAP_SCOPE_SUBTREE;
	server_sort = 0;


#ifdef notdef
#ifdef HPUX11
#ifndef __LP64__
	_main( argc, argv);
#endif /* __LP64_ */
#endif /* HPUX11 */
#endif


    ldaptool_reset_control_array( ldaptool_request_ctrls );
#ifdef HAVE_SASL_OPTIONS
#ifdef SOLARIS_LDAP_CMD
    optind = ldaptool_process_args( argc, argv, "ABLTU1etuxra:b:F:G:l:S:s:z:C:",
        0, options_callback );
#else
    optind = ldaptool_process_args( argc, argv, "ABLTU1etuxa:b:F:G:l:S:s:z:C:c:",
        0, options_callback );
#endif	/* SOLARIS_LDAP_CMD */
#else
    optind = ldaptool_process_args( argc, argv, "ABLTU1eotuxa:b:F:G:l:S:s:z:C:c:",
        0, options_callback );
#endif	/* HAVE_SASL_OPTIONS */

    if ( optind == -1 ) {
	usage();
    }

    if ( base == NULL ) {
	if (( base = getenv( "LDAP_BASEDN" )) == NULL ) {
	    usage();
	}
    }    
    if ( sortattr ) {
	for ( sortsize = 0; sortattr[sortsize] != NULL; sortsize++ ) {
	    ;       /* NULL */
	}
	sortsize++;             /* add in the final NULL field */
	skipsortattr = (int *) malloc( sortsize * sizeof(int *) );
	if ( skipsortattr == NULL ) {
    		fprintf( stderr, gettext("Out of memory\n") );
		exit( LDAP_NO_MEMORY );
	}
	memset( (char *) skipsortattr, 0, sortsize * sizeof(int *) );
    } else if ( server_sort ) {
	server_sort = 0;   /* ignore this option if no sortattrs were given */
    }

    if ( argc - optind < 1 ) {
	if ( ldaptool_fp == NULL ) {
	    usage();
	}
	attrs = NULL;
	filtpattern = "%s";
    } else {	/* there are additional args (filter + attrs) */
	if ( ldaptool_fp == NULL || strstr( argv[ optind ], "%s" ) != NULL ) {
	    filtpattern = ldaptool_local2UTF8( argv[ optind ] );
	    /* since local2UTF8 always allocates something, we should free it */
	    free_filtpattern = 1;
	    ++optind;
	} else {
	    filtpattern = "%s";
	}

	if ( argv[ optind ] == NULL ) {
	    attrs = NULL;
	} else if ( sortattr == NULL || *sortattr == '\0' || server_sort) {
	    attrs = &argv[ optind ];
	} else {
	    attrs = ldap_charray_dup( &argv[ optind ] );
	    if ( attrs == NULL ) {
		fprintf( stderr, gettext("Out of memory\n") );
		exit( LDAP_NO_MEMORY );
	    }
	    for ( i = 0; i < (sortsize - 1); i++ ) {
                if ( !ldap_charray_inlist( attrs, sortattr[i] ) ) {
                    if ( ldap_charray_add( &attrs, sortattr[i] ) != 0 ) {
    			fprintf( stderr, gettext("Out of memory\n") );
			exit( LDAP_NO_MEMORY );
		    }
                    /*
                     * attribute in the search list only for the purpose of
                     * sorting
                     */
                    skipsortattr[i] = 1;
                }
	    }
        }
    }

    ld = ldaptool_ldap_init( 0 );

    if ( !ldaptool_not ) {
	ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
	ldap_set_option( ld, LDAP_OPT_TIMELIMIT, &timelimit );
	ldap_set_option( ld, LDAP_OPT_SIZELIMIT, &sizelimit );
    }

    ldaptool_bind( ld );

    if ( ldaptool_verbose ) {
	printf( gettext("filter pattern: %s\nreturning: "), filtpattern );
	if ( attrs == NULL ) {
	    printf( gettext("ALL") );
	} else {
	    for ( i = 0; attrs[ i ] != NULL; ++i ) {
		printf( "%s ", attrs[ i ] );
	    }
	}
	putchar( '\n' );
    }

    if ( ldaptool_fp == NULL ) {
	char *conv;

	conv = ldaptool_local2UTF8( base );
	rc = dosearch( ld, conv, scope, attrs, attrsonly, filtpattern, "" );
	if( conv != NULL )
            free( conv );
    } else {
	int done = 0;

	rc = LDAP_SUCCESS;
	first = 1;
	while ( rc == LDAP_SUCCESS && !done ) {
	    char *linep = NULL;
	    int   increment = 0;
	    int	  c, index;
	 
	    /* allocate initial block of memory */ 
	    if ((linep = (char *)malloc(BUFSIZ)) == NULL) {
	        fprintf( stderr, gettext("Out of memory\n") );
		exit( LDAP_NO_MEMORY );
	    }
	    increment++;
	    index = 0;
	    while ((c = fgetc( ldaptool_fp )) != '\n' && c != EOF) {

	        /* check if we will overflow the buffer */
	        if ((c != EOF) && (index == ((increment * BUFSIZ) -1))) {

		    /* if we did, add another BUFSIZ worth of bytes */
	   	    if ((linep = (char *)
			realloc(linep, (increment + 1) * BUFSIZ)) == NULL) {
			fprintf( stderr, gettext("Out of memory\n") );
			exit( LDAP_NO_MEMORY );
		    }
		    increment++;
		}
		linep[index++] = c;
	    }

	    if (c == EOF) {
		done = 1;
		break;
	    }
	    
	    linep[index] = '\0';

	    if ( !first ) {
		putchar( '\n' );
	    } else {
		first = 0;
	    }
	    rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
		    linep );
	    free (linep);
	}
    }

    ldaptool_cleanup( ld );
    if (free_filtpattern != 0 && filtpattern != NULL) {
	free (filtpattern);
    }

    /* check for and report output error */
    fflush( stdout );
    rc = ldaptool_check_ferror( stdout, rc,
		gettext("output error (output might be incomplete)") );
    return( rc );
}
static void
options_callback( int option, char *optarg )
{	
	char	*old_passwd = NULL;
	char	*new_passwd = NULL;
	char	*re_newpasswd = NULL;
	
    switch( option ) {
	case 'a':	/* old password */
		old_passwd = strdup( optarg );
		if (NULL == old_passwd)
		{
			perror("malloc");
			exit( LDAP_NO_MEMORY );
		}
		break;
	case 'A':	/* prompt old password */
		prompt_old_password = 1;
		break;
	case 't':	/* old password from file */
		if ((old_password_fp = fopen( optarg, "r" )) == NULL ) {
			fprintf(stderr, "%s: Unable to open '%s' file\n",
					ldaptool_progname, optarg);
			exit( LDAP_PARAM_ERROR );
		}
		is_file_old = 1;
		break;
	case 's':	/* new password */
		new_passwd = strdup( optarg );
		if (NULL == new_passwd)
		{
			perror("malloc");
			exit( LDAP_NO_MEMORY );
		}		
		break;
	case 'S':	/* prompt new password */
		prompt_new_password = 1;
		break;
	case 'T':	/* new password from file */
		if ((new_password_fp = fopen( optarg, "r" )) == NULL ) {
			fprintf(stderr, "%s: Unable to open '%s' file\n",
						ldaptool_progname, optarg);
			exit( LDAP_PARAM_ERROR );
		}
		is_file_new = 1;			
		break;
	default:
		usage();
		break;
    }

	if ( (oldpasswd.bv_val == NULL) && (oldpasswd.bv_len == 0)
		 && (prompt_old_password) ) {
		old_passwd = ldaptool_getpass( old_password_string );
	} else if ( (oldpasswd.bv_val == NULL) && (oldpasswd.bv_len == 0) 
				&& (is_file_old) ) {
		old_passwd = ldaptool_read_password( old_password_fp );
	}
	
	if ( old_passwd ) {
		if ( !ldaptool_noconv_passwd ) {
			oldpasswd.bv_val = ldaptool_local2UTF8( old_passwd, "old password" );
		} else {
			oldpasswd.bv_val = strdup( old_passwd );
		}
		if (NULL == oldpasswd.bv_val) 
		{
			perror("malloc");
			exit( LDAP_NO_MEMORY );
		} 				
		oldpasswd.bv_len = strlen( oldpasswd.bv_val );
	}
		
	if ( (newpasswd.bv_val == NULL) && (newpasswd.bv_len == 0)
		 && (prompt_new_password) ) {
try_again:
			new_passwd = ldaptool_getpass( new_password_string );
			re_newpasswd = ldaptool_getpass( re_new_password_string );
			if ( (NULL == new_passwd) || (NULL == re_newpasswd) ) 
			{
				perror("malloc");
				exit( LDAP_NO_MEMORY );
			}
			if ( (strncmp( new_passwd, re_newpasswd, 
						   strlen( new_passwd ) ) ) ) {
				fprintf( stderr, 
						 "%s: They don't match.\n\nPlease try again\n", 
						 ldaptool_progname );
				free( re_newpasswd );
				free( new_passwd );
				re_newpasswd = NULL;
				new_passwd = NULL;
				goto try_again;
			}
	} else if ( (newpasswd.bv_val == NULL) && (newpasswd.bv_len == 0) 
				&& (is_file_new) ) {
			new_passwd = ldaptool_read_password( new_password_fp );
	}

	if ( new_passwd ) {
		if ( !ldaptool_noconv_passwd ) {
			newpasswd.bv_val = ldaptool_local2UTF8( new_passwd, "new password" );
		} else {
			newpasswd.bv_val = strdup( new_passwd );
		}
		if (NULL == newpasswd.bv_val) {
			perror("malloc");
			exit( LDAP_NO_MEMORY );
		}				
		newpasswd.bv_len = strlen( newpasswd.bv_val );
	}
	
}