ldaptool_host2 = strdup( optarg );
fprintf( stderr, "%s: only one host (-h option) should be specified\n", ldaptool_progname ); return(-1);
ldaptool_process_args( int argc, char **argv, char *extra_opts,	int two_hosts, void (*extra_opt_callback)( int option, char *optarg )){    int		rc, i, hostnum;    char	*optstring, *common_opts;    extern char	*optarg;    extern int	optind;    LDAPAPIInfo	ldai;    char *ctrl_arg, *ctrl_oid=NULL, *ctrl_value=NULL;    int ctrl_criticality=0, vlen;    LDAPControl *ldctrl;    /*     * Set program name global based on argv[0].     */    if (( ldaptool_progname = strrchr( argv[ 0 ], '/' )) == NULL ) {        ldaptool_progname = argv[ 0 ];    } else {        ++ldaptool_progname;    }#ifdef LDAPTOOL_DEBUG_MEMORY    {	struct ldap_memalloc_fns mafns = {		ldaptool_debug_malloc,		ldaptool_debug_calloc,		ldaptool_debug_realloc,		ldaptool_debug_free	};	ldap_set_option( NULL, LDAP_OPT_MEMALLOC_FN_PTRS, &mafns );    }#endif	/* LDAPTOOL_DEBUG_MEMORY */#ifdef LDAP_DEBUG    i = LDAP_DEBUG_ANY;    ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, (void *) &i);#endif    /*     * Perform a sanity check on the revision of the LDAP API library to     * make sure it is at least as new as the one we were compiled against.     * If the API implementation is from the same vendor as we were compiled     * against, we also check to make sure the vendor version is at least     * as new as the library we were compiled against.     *     * Version differences are fatal unless the -0 option is passed on the     * tool command line (that's a zero, not an oh).  We check for the     * presence of -0 in a crude way to it must appear by itself in argv.     */    for ( i = 1; i < argc; ++i ) {	if ( strcmp( argv[i], "-0" ) == 0 ) {	    lib_version_mismatch_is_fatal = 0;	    break;	}    }    memset( &ldai, 0, sizeof(ldai));    ldai.ldapai_info_version = LDAP_API_INFO_VERSION;    if (( rc = ldap_get_option( NULL, LDAP_OPT_API_INFO, &ldai )) != 0 ) {	fprintf( stderr, "%s: unable to retrieve LDAP library version"		" information;\n\tthis program requires an LDAP library that"		" implements revision\n\t%d or greater of the LDAP API.\n",		ldaptool_progname, LDAP_API_VERSION );	if ( lib_version_mismatch_is_fatal ) {	    exit( LDAP_LOCAL_ERROR );	}    } else if ( ldai.ldapai_api_version < LDAP_API_VERSION ) {	fprintf( stderr, "%s: this program requires an LDAP library that"		" implements revision\n\t%d or greater of the LDAP API;"		" running with revision %d.\n",		ldaptool_progname, LDAP_API_VERSION, ldai.ldapai_api_version );	if ( lib_version_mismatch_is_fatal ) {	    exit( LDAP_LOCAL_ERROR );	}    } else if ( strcmp( ldai.ldapai_vendor_name, LDAP_VENDOR_NAME ) != 0 ) {	fprintf( stderr, "%s: this program requires %s's LDAP\n"		"\tlibrary version %2.2f or greater; running with\n"		"\t%s's version %2.2f.\n",		ldaptool_progname, LDAP_VENDOR_NAME,		(float)LDAP_VENDOR_VERSION / 100,		ldai.ldapai_vendor_name,		(float)ldai.ldapai_vendor_version / 100 );	if ( lib_version_mismatch_is_fatal ) {	    exit( LDAP_LOCAL_ERROR );	}    } else if ( ldai.ldapai_vendor_version < LDAP_VENDOR_VERSION ) {	fprintf( stderr, "%s: this program requires %s's LDAP\n"		"\tlibrary version %2.2f or greater; running with"		" version %2.2f.\n",		ldaptool_progname, LDAP_VENDOR_NAME,		(float)LDAP_VENDOR_VERSION / 100,		(float)ldai.ldapai_vendor_version / 100 );	if ( lib_version_mismatch_is_fatal ) {	    exit( LDAP_LOCAL_ERROR );	}    }    /*     * Process command line options.     */    if ( extra_opts == NULL ) {	extra_opts = "";    }    common_opts = "nvEMRHZ03d:D:f:h:j:I:K:N:O:P:p:Q:W:w:V:X:m:i:k:y:Y:J:";    /* note: optstring must include room for liblcache "C:" option */    if (( optstring = (char *) malloc( strlen( extra_opts ) + strlen( common_opts )	    + 3 )) == NULL ) {	perror( "malloc" );	exit( LDAP_NO_MEMORY );    }#ifdef NO_LIBLCACHE    sprintf( optstring, "%s%s", common_opts, extra_opts );#else    sprintf( optstring, "%s%sC:", common_opts, extra_opts );#endif    hostnum = 0;    while ( (i = getopt( argc, argv, optstring )) != EOF ) {	switch( i ) {	case 'n':	/* do Not do any LDAP operations */	    ++ldaptool_not;	    break;	case 'v':	/* verbose mode */	    ++ldaptool_verbose;	    break;	case 'd':#ifdef LDAP_DEBUG	    ldaptool_dbg_lvl = atoi( optarg );	/* */	    ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL,		    (void *)&ldaptool_dbg_lvl);	    ldaptool_dbg_lvl |= LDAP_DEBUG_ANY;	    ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL,		    (void *)&ldaptool_dbg_lvl);#else /* LDAP_DEBUG */	    fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );#endif /* LDAP_DEBUG */	    break;	case 'R':	/* don't automatically chase referrals */	    chase_referrals = 0;	    break;#ifndef NO_LIBLCACHE	case 'C':	/* search local database */	    cache_config_file = strdup( optarg );	    break;#endif	case 'f':	/* input file */	    if ( optarg[0] == '-' && optarg[1] == '\0' ) {		ldaptool_fp = stdin;	    } else if (( ldaptool_fp = ldaptool_open_file( optarg, "r" )) == NULL ) {			perror( optarg );			exit( LDAP_PARAM_ERROR );	    }	    break;	case 'h':	/* ldap host */	    if ( hostnum == 0 ) {		ldaptool_host = strdup( optarg );	    } else {		ldaptool_host2 = strdup( optarg );	    }	    ++hostnum;	    break;	case 'D':	/* bind DN */	    isD = 1;	    binddn = strdup( optarg );	    break;	case 'E':	/* expose bind identity via auth. response control */	    ++send_auth_response_ctrl;	    break;	case 'p':	/* ldap port */	    if ( !user_specified_port ) {		++user_specified_port;		ldaptool_port = atoi( optarg );	    } else {		++user_specified_port2;		ldaptool_port2 = atoi( optarg );	    }	    break;#if defined(NET_SSL)	case 'P':	/* path to security database */	    secure = 1; /* do SSL encryption */	    ssl_certdbpath = strdup( optarg );	    if (NULL == ssl_certdbpath)	    {		perror("malloc");		exit( LDAP_NO_MEMORY );	    }	    break;	case 'Z':	/* do SSL encryption */	    secure = 1;	    isZ = 1;	    break;	case 'N':	/* nickname of cert. to use for client auth. */	    ssl_certname = strdup( optarg );	    if (NULL == ssl_certname)	    {		perror("malloc");		exit( LDAP_NO_MEMORY );	    }	    isN = 1;	    break;	case 'K':	/* location of key database */	    ssl_keydbpath = strdup( optarg );	    if (NULL == ssl_keydbpath)	    {		perror("malloc");		exit( LDAP_NO_MEMORY );	    }	    break;	case 'W':	/* SSL key password */	    ssl_passwd = strdup( optarg );	    if (NULL == ssl_passwd)	    {		perror("malloc");		exit( LDAP_NO_MEMORY );	    }	    isW = 1;	    break;	case '3':	/* check hostnames in SSL certificates ("no third") */	    ssl_strength = LDAPSSL_AUTH_CNCHECK;	    break;#ifdef LDAP_TOOL_PKCS11	case 'm':	/* SSL secmod path */	    ssl_secmodpath = strdup( optarg);	    if (NULL == ssl_secmodpath)	    {		perror("malloc");		exit( LDAP_NO_MEMORY );	    }	    break;	case 'Q': 	/* FORTEZZA [card][:personality] */	    pkcs_token = strdup(optarg);	    if (NULL == pkcs_token)	    {		perror("malloc");		exit( LDAP_NO_MEMORY );	    }	    break;	case 'X':	/* path to FORTEZZA CKL file */	  /*	    fortezza_krlfile = strdup( optarg );	    */	    break;	case 'I':	/* FORTEZZA PIN (password file) */	    ssl_donglefile = strdup( optarg );	    	    break;#endif /* LDAP_TOOL_PKCS11 */#endif /* NET_SSL */	case 'w':	/* bind password */	    isw = 1;	    if ( optarg[0] == '-' && optarg[1] == '\0' )		prompt_password = 1;	    else		passwd = strdup( optarg );	    break;	case 'j':       /* bind password from file */	    isj = 1;	    if ((password_fp = fopen( optarg, "r" )) == NULL ) {		fprintf(stderr, "%s: Unable to open '%s' file\n",			ldaptool_progname, optarg);		exit( LDAP_PARAM_ERROR );	    }            break;	case 'O':	/* referral hop limit */	    refhoplim = atoi( optarg );	    break;	case 'V':	/* protocol version */	    ldversion = atoi (optarg);	    if ( ldversion != LDAP_VERSION2 && ldversion != LDAP_VERSION3 ) {		fprintf( stderr, "%s: LDAP protocol version %d is not "			"supported (use -V%d or -V%d)\n",			ldaptool_progname, ldversion, LDAP_VERSION2,			LDAP_VERSION3 );		exit( LDAP_PARAM_ERROR );	    }	    break;	case 'M':	/* send a manageDsaIT control */	    send_manage_dsait_ctrl = 1;	    break;	case 'i':   /* character set specified */	    ldaptool_charset = strdup( optarg );	    if (NULL == ldaptool_charset)	    {		perror( "malloc" );		exit( LDAP_NO_MEMORY );	    }			    break;	case 'k':   /* conversion directory */	    ldaptool_convdir = strdup( optarg );	    if (NULL == ldaptool_convdir)	    {		perror( "malloc" );		exit( LDAP_NO_MEMORY );	    }	    break;	case 'y':   /* old (version 1) proxied authorization control */		proxyauth_version = 1;	case 'Y':   /* new (version 2 ) proxied authorization control */		/*FALLTHRU*/	    proxyauth_id = strdup(optarg);	    if (NULL == proxyauth_id)	    {		perror( "malloc" );		exit( LDAP_NO_MEMORY );	    }	    break; 	case '0':	/* zero -- override LDAP library version check */	    break;	/* already handled above */	case 'J':	/* send an arbitrary control */	    if ( (ctrl_arg = strdup( optarg)) == NULL ) {		perror ("strdup");		exit (LDAP_NO_MEMORY);	    }	    if (ldaptool_parse_ctrl_arg(ctrl_arg, ':', &ctrl_oid,		    &ctrl_criticality, &ctrl_value, &vlen)) {		return (-1);	    }	    ldctrl = calloc(1,sizeof(LDAPControl));	    if (ctrl_value) {		rc = ldaptool_berval_from_ldif_value( ctrl_value, 			vlen, &(ldctrl->ldctl_value),			1 /* recognize file URLs */, 			0 /* always try file */,			1 /* report errors */ );		if ((rc = ldaptool_fileurlerr2ldaperr( rc )) != LDAP_SUCCESS) {		    fprintf( stderr, "Unable to parse %s\n", ctrl_value);		    return (-1);		}	    }	    ldctrl->ldctl_oid = ctrl_oid;	    ldctrl->ldctl_iscritical = ctrl_criticality;	    ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);	    break;	default:	    (*extra_opt_callback)( i, optarg );	}    }    /* If '-Z' is specified, check if '-P' is specified too. */    if ( isN || isW ) {	if ( !isZ ) {		fprintf( stderr, "%s: with -N, -W options, please specify -Z\n\n", ldaptool_progname ); 		return (-1);	}    }    /* if '-N' is specified, -W is needed too */    if ( isN && NULL == ssl_passwd ) {	fprintf( stderr, "%s: with the -N option, please specify -W also\n\n", ldaptool_progname ); 	return (-1);    }    if ( isj && isw ) {	fprintf(stderr, "%s: -j and -w options cannot be specified simultaneously\n\n", ldaptool_progname );	return (-1);    }    if ( (isj || isw) && !isD ) {	fprintf(stderr, "%s: with -j, -w options, please specify -D\n\n", ldaptool_progname );	return (-1);    }    /* use default key and cert DB paths if not set on the command line */    if ( NULL == ssl_keydbpath ) {	if ( NULL == ssl_certdbpath ) {	    ssl_keydbpath = LDAPTOOL_DEFKEYDBPATH;	} else {	    ssl_keydbpath = certpath2keypath( ssl_certdbpath );	}    }    if ( NULL == ssl_certdbpath ) {	ssl_certdbpath = LDAPTOOL_DEFCERTDBPATH;    }    if (prompt_password != 0) {	char *password_string = "Enter bind password: ";#if defined(_WIN32)	char pbuf[257];	fputs(password_string,stdout);	fflush(stdout);	if (fgets(pbuf,256,stdin) == NULL) {	    passwd = NULL;	} else {	    char *tmp;	    tmp = strchr(pbuf,'\n');	    if (tmp) *tmp = '\0';	    tmp = strchr(pbuf,'\r');	    if (tmp) *tmp = '\0';	    passwd = strdup(pbuf);	}#else#if defined(SOLARIS)	/* 256 characters on Solaris */	passwd = getpassphrase(password_string);#else	/* limited to 16 chars on Tru64, 32 on AIX */	passwd = getpass(password_string);#endif#endif    } else if (password_fp != NULL) {	char *linep = NULL;	int   increment = 0;	int   c, index;	/* allocate initial block of memory */	if ((linep = (char *)malloc(BUFSIZ)) == NULL) {	    fprintf( stderr, "%s: not enough memory to read password from file\n", ldaptool_progname );	    exit( LDAP_NO_MEMORY );	}	increment++;	index = 0;	while ((c = fgetc( password_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, "%s: not enough memory to read password from file\n", ldaptool_progname );			exit( LDAP_NO_MEMORY );		}	 	increment++;	    }	    linep[index++] = c;	}	linep[index] = '\0';	passwd = linep;    }    /*     * If verbose (-v) flag was passed in, display program name and start time.     * If the verbose flag was passed at least twice (-vv), also display     * information about the API library we are running with.     */    if ( ldaptool_verbose ) {	time_t	curtime;	curtime = time( NULL );	printf( "%s: started %s\n", ldaptool_progname, ctime( &curtime ));	if ( ldaptool_verbose > 1 ) {	    print_library_info( &ldai, stdout );	}    }#ifdef LDAP_TOOL_PKCS11    if ((NULL != pkcs_token) && (NULL != ssl_certname)) {	char *result;		if ( (result = buildTokenCertName( pkcs_token, ssl_certname)) != NULL){	    free( ssl_certname );	    ssl_certname = result;	}    }#endif /* LDAP_TOOL_PKCS11 */    free( optstring );    /*     * Clean up and return index of first non-option argument.     */    if ( ldai.ldapai_extensions != NULL ) {	ldap_value_free( ldai.ldapai_extensions );    }    if ( ldai.ldapai_vendor_name != NULL ) {	ldap_memfree( ldai.ldapai_vendor_name );    }    return( optind );}