xref: /illumos-gate/usr/src/cmd/ldap/common/ldaptest.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
223a1cceaSRoger A. Faulkner  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate #include <stdio.h>
67c478bd9Sstevel@tonic-gate #include <ctype.h>
77c478bd9Sstevel@tonic-gate #include <string.h>
87c478bd9Sstevel@tonic-gate #include <sys/types.h>
97c478bd9Sstevel@tonic-gate #include <sys/socket.h>
107c478bd9Sstevel@tonic-gate #include <sys/time.h>
117c478bd9Sstevel@tonic-gate #include <sys/stat.h>
127c478bd9Sstevel@tonic-gate #include <sys/file.h>
137c478bd9Sstevel@tonic-gate #include <fcntl.h>
147c478bd9Sstevel@tonic-gate #include <unistd.h>
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include "lber.h"
177c478bd9Sstevel@tonic-gate #include "ldap.h"
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #define MOD_USE_BVALS
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #ifdef NEEDPROTOS
227c478bd9Sstevel@tonic-gate static void handle_result( LDAP *ld, LDAPMessage *lm );
237c478bd9Sstevel@tonic-gate static void print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s );
247c478bd9Sstevel@tonic-gate static void print_search_entry( LDAP *ld, LDAPMessage *res );
257c478bd9Sstevel@tonic-gate static void free_list( char **list );
267c478bd9Sstevel@tonic-gate #else
277c478bd9Sstevel@tonic-gate static void handle_result();
287c478bd9Sstevel@tonic-gate static void print_ldap_result();
297c478bd9Sstevel@tonic-gate static void print_search_entry();
307c478bd9Sstevel@tonic-gate static void free_list();
317c478bd9Sstevel@tonic-gate #endif /* NEEDPROTOS */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define NOCACHEERRMSG	"don't compile with -DNO_CACHE if you desire local caching"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate char *dnsuffix;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static char *
getaline(char * line,int len,FILE * fp,char * prompt)3823a1cceaSRoger A. Faulkner getaline( char *line, int len, FILE *fp, char *prompt )
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	printf(prompt);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	if ( fgets( line, len, fp ) == NULL )
437c478bd9Sstevel@tonic-gate 		return( NULL );
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	line[ strlen( line ) - 1 ] = '\0';
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	return( line );
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static char **
get_list(char * prompt)517c478bd9Sstevel@tonic-gate get_list( char *prompt )
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	static char	buf[256];
547c478bd9Sstevel@tonic-gate 	int		num;
557c478bd9Sstevel@tonic-gate 	char		**result;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	num = 0;
587c478bd9Sstevel@tonic-gate 	result = (char **) 0;
597c478bd9Sstevel@tonic-gate 	while ( 1 ) {
6023a1cceaSRoger A. Faulkner 		getaline( buf, sizeof(buf), stdin, prompt );
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 		if ( *buf == '\0' )
637c478bd9Sstevel@tonic-gate 			break;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 		if ( result == (char **) 0 )
667c478bd9Sstevel@tonic-gate 			result = (char **) malloc( sizeof(char *) );
677c478bd9Sstevel@tonic-gate 		else
687c478bd9Sstevel@tonic-gate 			result = (char **) realloc( result,
697c478bd9Sstevel@tonic-gate 			    sizeof(char *) * (num + 1) );
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		result[num++] = (char *) strdup( buf );
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	if ( result == (char **) 0 )
747c478bd9Sstevel@tonic-gate 		return( NULL );
757c478bd9Sstevel@tonic-gate 	result = (char **) realloc( result, sizeof(char *) * (num + 1) );
767c478bd9Sstevel@tonic-gate 	result[num] = NULL;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return( result );
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static void
free_list(char ** list)837c478bd9Sstevel@tonic-gate free_list( char **list )
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	int	i;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if ( list != NULL ) {
887c478bd9Sstevel@tonic-gate 		for ( i = 0; list[ i ] != NULL; ++i ) {
897c478bd9Sstevel@tonic-gate 			free( list[ i ] );
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		free( (char *)list );
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #ifdef MOD_USE_BVALS
977c478bd9Sstevel@tonic-gate static int
file_read(char * path,struct berval * bv)987c478bd9Sstevel@tonic-gate file_read( char *path, struct berval *bv )
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	FILE		*fp;
1017c478bd9Sstevel@tonic-gate 	long		rlen;
1027c478bd9Sstevel@tonic-gate 	int		eof;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (( fp = fopen( path, "r" )) == NULL ) {
1057c478bd9Sstevel@tonic-gate 	    	perror( path );
1067c478bd9Sstevel@tonic-gate 		return( -1 );
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
1107c478bd9Sstevel@tonic-gate 		perror( path );
1117c478bd9Sstevel@tonic-gate 		fclose( fp );
1127c478bd9Sstevel@tonic-gate 		return( -1 );
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	bv->bv_len = ftell( fp );
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
1187c478bd9Sstevel@tonic-gate 		perror( "malloc" );
1197c478bd9Sstevel@tonic-gate 		fclose( fp );
1207c478bd9Sstevel@tonic-gate 		return( -1 );
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
1247c478bd9Sstevel@tonic-gate 		perror( path );
1257c478bd9Sstevel@tonic-gate 		fclose( fp );
1267c478bd9Sstevel@tonic-gate 		return( -1 );
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
1307c478bd9Sstevel@tonic-gate 	eof = feof( fp );
1317c478bd9Sstevel@tonic-gate 	fclose( fp );
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if ( rlen != bv->bv_len ) {
1347c478bd9Sstevel@tonic-gate 		perror( path );
1357c478bd9Sstevel@tonic-gate 		free( bv->bv_val );
1367c478bd9Sstevel@tonic-gate 		return( -1 );
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return( bv->bv_len );
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static LDAPMod **
get_modlist(char * prompt1,char * prompt2,char * prompt3)1457c478bd9Sstevel@tonic-gate get_modlist( char *prompt1, char *prompt2, char *prompt3 )
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	static char	buf[256];
1487c478bd9Sstevel@tonic-gate 	int		num;
1497c478bd9Sstevel@tonic-gate 	LDAPMod		tmp;
1507c478bd9Sstevel@tonic-gate 	LDAPMod		**result;
1517c478bd9Sstevel@tonic-gate #ifdef MOD_USE_BVALS
1527c478bd9Sstevel@tonic-gate 	struct berval	**bvals;
1537c478bd9Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	num = 0;
1567c478bd9Sstevel@tonic-gate 	result = NULL;
1577c478bd9Sstevel@tonic-gate 	while ( 1 ) {
1587c478bd9Sstevel@tonic-gate 		if ( prompt1 ) {
15923a1cceaSRoger A. Faulkner 			getaline( buf, sizeof(buf), stdin, prompt1 );
1607c478bd9Sstevel@tonic-gate 			tmp.mod_op = atoi( buf );
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			if ( tmp.mod_op == -1 || buf[0] == '\0' )
1637c478bd9Sstevel@tonic-gate 				break;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 
16623a1cceaSRoger A. Faulkner 		getaline( buf, sizeof(buf), stdin, prompt2 );
1677c478bd9Sstevel@tonic-gate 		if ( buf[0] == '\0' )
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		tmp.mod_type = strdup( buf );
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		tmp.mod_values = get_list( prompt3 );
1727c478bd9Sstevel@tonic-gate #ifdef MOD_USE_BVALS
1737c478bd9Sstevel@tonic-gate 		if ( tmp.mod_values != NULL ) {
1747c478bd9Sstevel@tonic-gate 			int	i;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 			for ( i = 0; tmp.mod_values[i] != NULL; ++i )
1777c478bd9Sstevel@tonic-gate 				;
1787c478bd9Sstevel@tonic-gate 			bvals = (struct berval **)calloc( i + 1,
1797c478bd9Sstevel@tonic-gate 			    sizeof( struct berval *));
1807c478bd9Sstevel@tonic-gate 			for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
1817c478bd9Sstevel@tonic-gate 				bvals[i] = (struct berval *)malloc(
1827c478bd9Sstevel@tonic-gate 				    sizeof( struct berval ));
1837c478bd9Sstevel@tonic-gate 				if ( strncmp( tmp.mod_values[i], "{FILE}",
1847c478bd9Sstevel@tonic-gate 				    6 ) == 0 ) {
1857c478bd9Sstevel@tonic-gate 					if ( file_read( tmp.mod_values[i] + 6,
1867c478bd9Sstevel@tonic-gate 					    bvals[i] ) < 0 ) {
1877c478bd9Sstevel@tonic-gate 						return( NULL );
1887c478bd9Sstevel@tonic-gate 					}
1897c478bd9Sstevel@tonic-gate 				} else {
1907c478bd9Sstevel@tonic-gate 					bvals[i]->bv_val = tmp.mod_values[i];
1917c478bd9Sstevel@tonic-gate 					bvals[i]->bv_len =
1927c478bd9Sstevel@tonic-gate 					    strlen( tmp.mod_values[i] );
1937c478bd9Sstevel@tonic-gate 				}
1947c478bd9Sstevel@tonic-gate 			}
1957c478bd9Sstevel@tonic-gate 			tmp.mod_bvalues = bvals;
1967c478bd9Sstevel@tonic-gate 			tmp.mod_op |= LDAP_MOD_BVALUES;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		if ( result == NULL )
2017c478bd9Sstevel@tonic-gate 			result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
2027c478bd9Sstevel@tonic-gate 		else
2037c478bd9Sstevel@tonic-gate 			result = (LDAPMod **) realloc( result,
2047c478bd9Sstevel@tonic-gate 			    sizeof(LDAPMod *) * (num + 1) );
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
2077c478bd9Sstevel@tonic-gate 		*(result[num]) = tmp;	/* struct copy */
2087c478bd9Sstevel@tonic-gate 		num++;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	if ( result == NULL )
2117c478bd9Sstevel@tonic-gate 		return( NULL );
2127c478bd9Sstevel@tonic-gate 	result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
2137c478bd9Sstevel@tonic-gate 	result[num] = NULL;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return( result );
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate int
bind_prompt(LDAP * ld,char ** dnp,char ** passwdp,int * authmethodp,int freeit)2207c478bd9Sstevel@tonic-gate bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
2217c478bd9Sstevel@tonic-gate 	int freeit )
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	static char	dn[256], passwd[256];
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if ( !freeit ) {
2267c478bd9Sstevel@tonic-gate #ifdef KERBEROS
22723a1cceaSRoger A. Faulkner 		getaline( dn, sizeof(dn), stdin,
2287c478bd9Sstevel@tonic-gate 		    "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
2297c478bd9Sstevel@tonic-gate 		if (( *authmethodp = atoi( dn )) == 3 ) {
2307c478bd9Sstevel@tonic-gate 			*authmethodp = LDAP_AUTH_KRBV4;
2317c478bd9Sstevel@tonic-gate 		} else {
2327c478bd9Sstevel@tonic-gate 			*authmethodp |= 0x80;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate #else /* KERBEROS */
2357c478bd9Sstevel@tonic-gate 		*authmethodp = LDAP_AUTH_SIMPLE;
2367c478bd9Sstevel@tonic-gate #endif /* KERBEROS */
2377c478bd9Sstevel@tonic-gate 
23823a1cceaSRoger A. Faulkner 		getaline( dn, sizeof(dn), stdin, "re-bind dn? " );
2397c478bd9Sstevel@tonic-gate 		strcat( dn, dnsuffix );
2407c478bd9Sstevel@tonic-gate 		*dnp = dn;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
24323a1cceaSRoger A. Faulkner 			getaline( passwd, sizeof(passwd), stdin,
2447c478bd9Sstevel@tonic-gate 			    "re-bind password? " );
2457c478bd9Sstevel@tonic-gate 		} else {
2467c478bd9Sstevel@tonic-gate 			passwd[0] = '\0';
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 		*passwdp = passwd;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)2567c478bd9Sstevel@tonic-gate main(int argc, char **argv )
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	LDAP	*ld;
259*55fea89dSDan Cross 	int		i, c, port, cldapflg, errflg, method, id,
2607c478bd9Sstevel@tonic-gate 		msgtype, delrdn, theInt, sizelimit, err;
2617c478bd9Sstevel@tonic-gate 	char	line[256], command1, command2, command3;
2627c478bd9Sstevel@tonic-gate 	char	passwd[64], dn[256], rdn[64], attr[64], value[256];
2637c478bd9Sstevel@tonic-gate 	char	filter[256], *host, **types;
2647c478bd9Sstevel@tonic-gate 	char 	*mechanism;
265*55fea89dSDan Cross 
2667c478bd9Sstevel@tonic-gate 	char	**exdn;
2677c478bd9Sstevel@tonic-gate 	char	*usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
2687c478bd9Sstevel@tonic-gate 	int		bound, all, scope, attrsonly;
2697c478bd9Sstevel@tonic-gate 	LDAPMessage	*res;
2707c478bd9Sstevel@tonic-gate 	LDAPMod	**mods, **attrs;
2717c478bd9Sstevel@tonic-gate 	struct timeval	timeout, timelimit;
2727c478bd9Sstevel@tonic-gate 	char	*copyfname = NULL;
2737c478bd9Sstevel@tonic-gate 	int		copyoptions = 0, resultusetimelimit = 0;
2747c478bd9Sstevel@tonic-gate 	LDAPURLDesc	*ludp;
2757c478bd9Sstevel@tonic-gate 	struct berval bv, cred, *srvcrds = NULL;
2767c478bd9Sstevel@tonic-gate 	extern char	*optarg;
2777c478bd9Sstevel@tonic-gate 	extern int	optind;
2787c478bd9Sstevel@tonic-gate 	LDAPControl *ctrls[2];
2797c478bd9Sstevel@tonic-gate 	LDAPControl aCtrl;
280*55fea89dSDan Cross 
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate #ifdef MACOS
2837c478bd9Sstevel@tonic-gate 	if (( argv = get_list( "cmd line arg?" )) == NULL ) {
2847c478bd9Sstevel@tonic-gate 		exit( 1 );
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
2877c478bd9Sstevel@tonic-gate 		;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate #endif /* MACOS */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	host = NULL;
2927c478bd9Sstevel@tonic-gate 	port = LDAP_PORT;
2937c478bd9Sstevel@tonic-gate 	dnsuffix = "";
2947c478bd9Sstevel@tonic-gate 	cldapflg = errflg = 0;
2957c478bd9Sstevel@tonic-gate 	ctrls[0] = &aCtrl;
2967c478bd9Sstevel@tonic-gate 	ctrls[1] = NULL;
297*55fea89dSDan Cross 
2987c478bd9Sstevel@tonic-gate 	while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
2997c478bd9Sstevel@tonic-gate 		switch( c ) {
3007c478bd9Sstevel@tonic-gate 		case 'u':
3017c478bd9Sstevel@tonic-gate #ifdef CLDAP
3027c478bd9Sstevel@tonic-gate 			cldapflg++;
3037c478bd9Sstevel@tonic-gate #else /* CLDAP */
3047c478bd9Sstevel@tonic-gate 			printf( "Compile with -DCLDAP for UDP support\n" );
3057c478bd9Sstevel@tonic-gate #endif /* CLDAP */
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		case 'd':
3097c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
3107c478bd9Sstevel@tonic-gate 			ldap_debug = atoi( optarg );
3117c478bd9Sstevel@tonic-gate 			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
3127c478bd9Sstevel@tonic-gate 				lber_debug = ldap_debug;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate #else
3157c478bd9Sstevel@tonic-gate 			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
3167c478bd9Sstevel@tonic-gate #endif
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		case 'h':
3207c478bd9Sstevel@tonic-gate 			host = optarg;
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		case 's':
3247c478bd9Sstevel@tonic-gate 			dnsuffix = optarg;
3257c478bd9Sstevel@tonic-gate 			break;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		case 'p':
3287c478bd9Sstevel@tonic-gate 			port = atoi( optarg );
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS)
3327c478bd9Sstevel@tonic-gate 		case 't':	/* copy ber's to given file */
3337c478bd9Sstevel@tonic-gate 			copyfname = strdup( optarg );
3347c478bd9Sstevel@tonic-gate 			copyoptions = LBER_TO_FILE;
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		case 'T':	/* only output ber's to given file */
3387c478bd9Sstevel@tonic-gate 			copyfname = strdup( optarg );
3397c478bd9Sstevel@tonic-gate 			copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		default:
3447c478bd9Sstevel@tonic-gate 		    ++errflg;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if ( host == NULL && optind == argc - 1 ) {
3497c478bd9Sstevel@tonic-gate 		host = argv[ optind ];
3507c478bd9Sstevel@tonic-gate 		++optind;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if ( errflg || optind < argc - 1 ) {
3547c478bd9Sstevel@tonic-gate 		fprintf( stderr, usage, argv[ 0 ] );
3557c478bd9Sstevel@tonic-gate 		exit( 1 );
3567c478bd9Sstevel@tonic-gate 	}
357*55fea89dSDan Cross 
3587c478bd9Sstevel@tonic-gate 	printf( "%s( %s, %d )\n", cldapflg ? "cldap_open" : "ldap_init",
3597c478bd9Sstevel@tonic-gate 		host == NULL ? "(null)" : host, port );
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if ( cldapflg ) {
3627c478bd9Sstevel@tonic-gate #ifdef CLDAP
3637c478bd9Sstevel@tonic-gate 		ld = cldap_open( host, port );
3647c478bd9Sstevel@tonic-gate #endif /* CLDAP */
3657c478bd9Sstevel@tonic-gate 	} else {
3667c478bd9Sstevel@tonic-gate 		ld = ldap_init( host, port );
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if ( ld == NULL ) {
3707c478bd9Sstevel@tonic-gate 		perror( "ldap_init" );
3717c478bd9Sstevel@tonic-gate 		exit(1);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS)
3757c478bd9Sstevel@tonic-gate 	if ( copyfname != NULL ) {
3767c478bd9Sstevel@tonic-gate 		if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
3777c478bd9Sstevel@tonic-gate 		    0600 ))  == -1 ) {
3787c478bd9Sstevel@tonic-gate 			perror( copyfname );
3797c478bd9Sstevel@tonic-gate 			exit ( 1 );
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		ld->ld_sb.sb_options = copyoptions;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate #endif
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	bound = 0;
3867c478bd9Sstevel@tonic-gate 	timeout.tv_sec = 0;
3877c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
3887c478bd9Sstevel@tonic-gate 	timelimit.tv_sec = 0;
3897c478bd9Sstevel@tonic-gate 	timelimit.tv_usec = 0;
390*55fea89dSDan Cross 
3917c478bd9Sstevel@tonic-gate 	(void) memset( line, '\0', sizeof(line) );
39223a1cceaSRoger A. Faulkner 	while ( getaline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
3937c478bd9Sstevel@tonic-gate 		command1 = line[0];
3947c478bd9Sstevel@tonic-gate 		command2 = line[1];
3957c478bd9Sstevel@tonic-gate 		command3 = line[2];
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		switch ( command1 ) {
3987c478bd9Sstevel@tonic-gate 		case 'a':	/* add or abandon */
3997c478bd9Sstevel@tonic-gate 			switch ( command2 ) {
4007c478bd9Sstevel@tonic-gate 			case 'd':	/* add */
40123a1cceaSRoger A. Faulkner 				getaline( dn, sizeof(dn), stdin, "dn? " );
4027c478bd9Sstevel@tonic-gate 				strcat( dn, dnsuffix );
4037c478bd9Sstevel@tonic-gate 				if ( (attrs = get_modlist( NULL, "attr? ",
4047c478bd9Sstevel@tonic-gate 				    "value? " )) == NULL )
4057c478bd9Sstevel@tonic-gate 					break;
4067c478bd9Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
4077c478bd9Sstevel@tonic-gate 					if ((err = ldap_add_ext( ld, dn, attrs, NULL, NULL, &id )) != LDAP_SUCCESS )
4087c478bd9Sstevel@tonic-gate 						printf( "Error in ldap_add_ext: %s\n", ldap_err2string(err) );
4097c478bd9Sstevel@tonic-gate 					else
4107c478bd9Sstevel@tonic-gate 						printf( "Add initiated with id %d\n", id );
4117c478bd9Sstevel@tonic-gate 				}
4127c478bd9Sstevel@tonic-gate 				else {
4137c478bd9Sstevel@tonic-gate 					if ( (id = ldap_add( ld, dn, attrs )) == -1 )
4147c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_add" );
4157c478bd9Sstevel@tonic-gate 					else
4167c478bd9Sstevel@tonic-gate 						printf( "Add initiated with id %d\n", id );
4177c478bd9Sstevel@tonic-gate 				}
418*55fea89dSDan Cross 
4197c478bd9Sstevel@tonic-gate 				break;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 			case 'b':	/* abandon */
42223a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin, "msgid? " );
4237c478bd9Sstevel@tonic-gate 				id = atoi( line );
4247c478bd9Sstevel@tonic-gate 				if ( ldap_abandon( ld, id ) != 0 )
4257c478bd9Sstevel@tonic-gate 					ldap_perror( ld, "ldap_abandon" );
4267c478bd9Sstevel@tonic-gate 				else
4277c478bd9Sstevel@tonic-gate 					printf( "Abandon successful\n" );
4287c478bd9Sstevel@tonic-gate 				break;
4297c478bd9Sstevel@tonic-gate 			default:
4307c478bd9Sstevel@tonic-gate 				printf( "Possibilities: [ad]d, [ab]ort\n" );
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		case 'b':	/* asynch bind */
4357c478bd9Sstevel@tonic-gate #ifdef KERBEROS
43623a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
4377c478bd9Sstevel@tonic-gate 			    "method (0->simple, 1->krbv41, 2->krbv42)? " );
4387c478bd9Sstevel@tonic-gate 			method = atoi( line ) | 0x80;
4397c478bd9Sstevel@tonic-gate #else /* KERBEROS */
4407c478bd9Sstevel@tonic-gate 			method = LDAP_AUTH_SIMPLE;
4417c478bd9Sstevel@tonic-gate #endif /* KERBEROS */
44223a1cceaSRoger A. Faulkner 			getaline( dn, sizeof(dn), stdin, "dn? " );
4437c478bd9Sstevel@tonic-gate 			strcat( dn, dnsuffix );
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 			if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
44623a1cceaSRoger A. Faulkner 				getaline( passwd, sizeof(passwd), stdin,
4477c478bd9Sstevel@tonic-gate 				    "password? " );
4487c478bd9Sstevel@tonic-gate 			else
4497c478bd9Sstevel@tonic-gate 				passwd[0] = '\0';
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
4527c478bd9Sstevel@tonic-gate 				fprintf( stderr, "ldap_bind failed\n" );
4537c478bd9Sstevel@tonic-gate 				ldap_perror( ld, "ldap_bind" );
4547c478bd9Sstevel@tonic-gate 			} else {
4557c478bd9Sstevel@tonic-gate 				printf( "Bind initiated\n" );
4567c478bd9Sstevel@tonic-gate 				bound = 1;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		case 'B':	/* synch bind */
4617c478bd9Sstevel@tonic-gate #ifdef KERBEROS
46223a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
4637c478bd9Sstevel@tonic-gate 			    "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
4647c478bd9Sstevel@tonic-gate 			method = atoi( line );
4657c478bd9Sstevel@tonic-gate 			if ( method == 3 )
4667c478bd9Sstevel@tonic-gate 				method = LDAP_AUTH_KRBV4;
4677c478bd9Sstevel@tonic-gate 			else
4687c478bd9Sstevel@tonic-gate 				method = method | 0x80;
4697c478bd9Sstevel@tonic-gate #else /* KERBEROS */
47023a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
4717c478bd9Sstevel@tonic-gate 					 "method 0->simple, 1->SASL? ");
4727c478bd9Sstevel@tonic-gate 			method = atoi (line);
4737c478bd9Sstevel@tonic-gate 			if (method == 1){
4747c478bd9Sstevel@tonic-gate 				method = LDAP_AUTH_SASL;
47523a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
4767c478bd9Sstevel@tonic-gate 						 "mechanism 0->CRAM_MD5, 1->TLS? ");
4777c478bd9Sstevel@tonic-gate 				theInt = atoi(line);
4787c478bd9Sstevel@tonic-gate 				if (theInt == 0){
4797c478bd9Sstevel@tonic-gate 					mechanism = LDAP_SASL_CRAM_MD5;
4807c478bd9Sstevel@tonic-gate 				}
4817c478bd9Sstevel@tonic-gate 				else{
4827c478bd9Sstevel@tonic-gate 					mechanism = LDAP_SASL_X511_STRONG;
4837c478bd9Sstevel@tonic-gate 				}
4847c478bd9Sstevel@tonic-gate 			} else {
4857c478bd9Sstevel@tonic-gate 				method = LDAP_AUTH_SIMPLE;
4867c478bd9Sstevel@tonic-gate 			}
487*55fea89dSDan Cross 
4887c478bd9Sstevel@tonic-gate #endif /* KERBEROS */
48923a1cceaSRoger A. Faulkner 			getaline( dn, sizeof(dn), stdin, "dn? " );
4907c478bd9Sstevel@tonic-gate 			strcat( dn, dnsuffix );
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 			if ( dn[0] != '\0' )
49323a1cceaSRoger A. Faulkner 				getaline( passwd, sizeof(passwd), stdin,
4947c478bd9Sstevel@tonic-gate 				    "password? " );
4957c478bd9Sstevel@tonic-gate 			else
4967c478bd9Sstevel@tonic-gate 				passwd[0] = '\0';
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 			if (method == LDAP_AUTH_SIMPLE) {
4997c478bd9Sstevel@tonic-gate 				if ( ldap_bind_s( ld, dn, passwd, method ) !=
5007c478bd9Sstevel@tonic-gate 					 LDAP_SUCCESS ) {
5017c478bd9Sstevel@tonic-gate 					fprintf( stderr, "ldap_bind_s failed\n" );
5027c478bd9Sstevel@tonic-gate 					ldap_perror( ld, "ldap_bind_s" );
5037c478bd9Sstevel@tonic-gate 				} else {
5047c478bd9Sstevel@tonic-gate 					printf( "Bind successful\n" );
5057c478bd9Sstevel@tonic-gate 					bound = 1;
5067c478bd9Sstevel@tonic-gate 				}
5077c478bd9Sstevel@tonic-gate 			} else {
5087c478bd9Sstevel@tonic-gate 				if (strcmp(mechanism, LDAP_SASL_CRAM_MD5) == 0){
5097c478bd9Sstevel@tonic-gate 					cred.bv_val = passwd;
5107c478bd9Sstevel@tonic-gate 					cred.bv_len = strlen(passwd);
511*55fea89dSDan Cross 
5127c478bd9Sstevel@tonic-gate 					if ( ldap_sasl_cram_md5_bind_s(ld, dn, &cred, NULL, NULL) != LDAP_SUCCESS ){
5137c478bd9Sstevel@tonic-gate 						fprintf( stderr, "ldap_sasl_cram_md5_bind_s failed\n" );
5147c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_sasl_cram_md5_bind_s" );
5157c478bd9Sstevel@tonic-gate 					} else {
5167c478bd9Sstevel@tonic-gate 						printf ( "Bind successful\n");
5177c478bd9Sstevel@tonic-gate 						bound = 1;
5187c478bd9Sstevel@tonic-gate 					}
5197c478bd9Sstevel@tonic-gate 				} else {
5207c478bd9Sstevel@tonic-gate 					if (ldap_sasl_bind_s(ld, dn, mechanism, &cred, NULL, NULL, &srvcrds ) != LDAP_SUCCESS){
5217c478bd9Sstevel@tonic-gate 						fprintf( stderr, "ldap_sasl_bind_s failed\n" );
5227c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_sasl_bind_s" );
5237c478bd9Sstevel@tonic-gate 					}
5247c478bd9Sstevel@tonic-gate 				}
5257c478bd9Sstevel@tonic-gate 			}
5267c478bd9Sstevel@tonic-gate 			break;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		case 'c':	/* compare */
52923a1cceaSRoger A. Faulkner 			getaline( dn, sizeof(dn), stdin, "dn? " );
5307c478bd9Sstevel@tonic-gate 			strcat( dn, dnsuffix );
53123a1cceaSRoger A. Faulkner 			getaline( attr, sizeof(attr), stdin, "attr? " );
53223a1cceaSRoger A. Faulkner 			getaline( value, sizeof(value), stdin, "value? " );
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 			if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
5357c478bd9Sstevel@tonic-gate 				bv.bv_val = value;
5367c478bd9Sstevel@tonic-gate 				bv.bv_len = strlen(value);
5377c478bd9Sstevel@tonic-gate 				if ((err = ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &id )) != LDAP_SUCCESS )
5387c478bd9Sstevel@tonic-gate 					printf( "Error in ldap_compare_ext: %s\n", ldap_err2string(err) );
5397c478bd9Sstevel@tonic-gate 				else
5407c478bd9Sstevel@tonic-gate 					printf( "Compare initiated with id %d\n", id );
5417c478bd9Sstevel@tonic-gate 			} else {
5427c478bd9Sstevel@tonic-gate 				if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
5437c478bd9Sstevel@tonic-gate 					ldap_perror( ld, "ldap_compare" );
5447c478bd9Sstevel@tonic-gate 				else
5457c478bd9Sstevel@tonic-gate 					printf( "Compare initiated with id %d\n", id );
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 			break;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		case 'd':	/* turn on debugging */
5507c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
55123a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "debug level? " );
5527c478bd9Sstevel@tonic-gate 			ldap_debug = atoi( line );
5537c478bd9Sstevel@tonic-gate 			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
5547c478bd9Sstevel@tonic-gate 				lber_debug = ldap_debug;
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate #else
5577c478bd9Sstevel@tonic-gate 			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
5587c478bd9Sstevel@tonic-gate #endif
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		case 'E':	/* explode a dn */
56223a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "dn? " );
5637c478bd9Sstevel@tonic-gate 			exdn = ldap_explode_dn( line, 0 );
5647c478bd9Sstevel@tonic-gate 			for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
5657c478bd9Sstevel@tonic-gate 				printf( "\t%s\n", exdn[i] );
5667c478bd9Sstevel@tonic-gate 			}
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		case 'g':	/* set next msgid */
57023a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "msgid? " );
5717c478bd9Sstevel@tonic-gate 			ld->ld_msgid = atoi( line );
5727c478bd9Sstevel@tonic-gate 			break;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		case 'v':	/* set version number */
57523a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "version? " );
5767c478bd9Sstevel@tonic-gate 			theInt = atoi(line);
5777c478bd9Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &theInt);
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		case 'm':	/* modify or modifyrdn */
5817c478bd9Sstevel@tonic-gate 			if ( strncmp( line, "modify", 4 ) == 0 ) {
58223a1cceaSRoger A. Faulkner 				getaline( dn, sizeof(dn), stdin, "dn? " );
5837c478bd9Sstevel@tonic-gate 				strcat( dn, dnsuffix );
5847c478bd9Sstevel@tonic-gate 				if ( (mods = get_modlist(
5857c478bd9Sstevel@tonic-gate 				    "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
5867c478bd9Sstevel@tonic-gate 				    "attribute type? ", "attribute value? " ))
5877c478bd9Sstevel@tonic-gate 				    == NULL )
5887c478bd9Sstevel@tonic-gate 					break;
5897c478bd9Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
5907c478bd9Sstevel@tonic-gate 					if ((err = ldap_modify_ext( ld, dn, mods, NULL, NULL, &id )) != LDAP_SUCCESS )
5917c478bd9Sstevel@tonic-gate 						printf( "Error in ldap_modify_ext: %s\n", ldap_err2string(err) );
5927c478bd9Sstevel@tonic-gate 					else
5937c478bd9Sstevel@tonic-gate 						printf( "Modify initiated with id %d\n", id );
5947c478bd9Sstevel@tonic-gate 				}
5957c478bd9Sstevel@tonic-gate 				else {
5967c478bd9Sstevel@tonic-gate 					if ( (id = ldap_modify( ld, dn, mods )) == -1 )
5977c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_modify" );
5987c478bd9Sstevel@tonic-gate 					else
5997c478bd9Sstevel@tonic-gate 						printf( "Modify initiated with id %d\n", id );
6007c478bd9Sstevel@tonic-gate 				}
6017c478bd9Sstevel@tonic-gate 			} else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
60223a1cceaSRoger A. Faulkner 				getaline( dn, sizeof(dn), stdin, "dn? " );
6037c478bd9Sstevel@tonic-gate 				strcat( dn, dnsuffix );
60423a1cceaSRoger A. Faulkner 				getaline( rdn, sizeof(rdn), stdin, "newrdn? " );
60523a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin, "delete old rdn (0=>no, 1=>yes)?");
6067c478bd9Sstevel@tonic-gate 				delrdn = atoi(line);
6077c478bd9Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
6087c478bd9Sstevel@tonic-gate 					if ((err = ldap_rename(ld, dn, rdn, NULL, delrdn, NULL,NULL, &id)) != LDAP_SUCCESS){
6097c478bd9Sstevel@tonic-gate 						printf( "Error in ldap_rename (modrdn): %s\n", ldap_err2string(err));
6107c478bd9Sstevel@tonic-gate 					}
6117c478bd9Sstevel@tonic-gate 					else
6127c478bd9Sstevel@tonic-gate 						printf( "Modrdn initiated with id %d\n", id );
6137c478bd9Sstevel@tonic-gate 				}
6147c478bd9Sstevel@tonic-gate 				else {
6157c478bd9Sstevel@tonic-gate 					if ( (id = ldap_modrdn( ld, dn, rdn, delrdn )) == -1 )
6167c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_modrdn" );
6177c478bd9Sstevel@tonic-gate 					else
6187c478bd9Sstevel@tonic-gate 						printf( "Modrdn initiated with id %d\n", id );
6197c478bd9Sstevel@tonic-gate 				}
6207c478bd9Sstevel@tonic-gate 			} else {
6217c478bd9Sstevel@tonic-gate 				printf( "Possibilities: [modi]fy, [modr]dn\n" );
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 			break;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		case 'q':	/* quit */
6267c478bd9Sstevel@tonic-gate #ifdef CLDAP
6277c478bd9Sstevel@tonic-gate 			if ( cldapflg )
6287c478bd9Sstevel@tonic-gate 				cldap_close( ld );
6297c478bd9Sstevel@tonic-gate #endif /* CLDAP */
6307c478bd9Sstevel@tonic-gate 			if ( !cldapflg )
6317c478bd9Sstevel@tonic-gate 				ldap_unbind( ld );
6327c478bd9Sstevel@tonic-gate 			exit( 0 );
6337c478bd9Sstevel@tonic-gate 			break;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		case 'r':	/* result or remove */
6367c478bd9Sstevel@tonic-gate 			switch ( command3 ) {
6377c478bd9Sstevel@tonic-gate 			case 's':	/* result */
63823a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
6397c478bd9Sstevel@tonic-gate 				    "msgid (-1=>any)? " );
6407c478bd9Sstevel@tonic-gate 				if ( line[0] == '\0' )
6417c478bd9Sstevel@tonic-gate 					id = -1;
6427c478bd9Sstevel@tonic-gate 				else
6437c478bd9Sstevel@tonic-gate 					id = atoi( line );
64423a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
6457c478bd9Sstevel@tonic-gate 				    "all (0=>any, 1=>all)? " );
6467c478bd9Sstevel@tonic-gate 				if ( line[0] == '\0' )
6477c478bd9Sstevel@tonic-gate 					all = 1;
6487c478bd9Sstevel@tonic-gate 				else
6497c478bd9Sstevel@tonic-gate 					all = atoi( line );
650*55fea89dSDan Cross 
6517c478bd9Sstevel@tonic-gate 				if (( msgtype = ldap_result( ld, id, all,
6527c478bd9Sstevel@tonic-gate 				    resultusetimelimit ? &timelimit : &timeout, &res )) < 1 ) {
6537c478bd9Sstevel@tonic-gate 					ldap_perror( ld, "ldap_result" );
6547c478bd9Sstevel@tonic-gate 					break;
6557c478bd9Sstevel@tonic-gate 				}
6567c478bd9Sstevel@tonic-gate 				printf( "\nresult: msgtype %d msgid %d\n",
6577c478bd9Sstevel@tonic-gate 				    msgtype, res->lm_msgid );
6587c478bd9Sstevel@tonic-gate 				handle_result( ld, res );
6597c478bd9Sstevel@tonic-gate 				if (all || msgtype == LDAP_RES_SEARCH_RESULT)
6607c478bd9Sstevel@tonic-gate 					resultusetimelimit = 0;
6617c478bd9Sstevel@tonic-gate 				res = NULLMSG;
6627c478bd9Sstevel@tonic-gate 				break;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 			case 'm':	/* remove */
66523a1cceaSRoger A. Faulkner 				getaline( dn, sizeof(dn), stdin, "dn? " );
6667c478bd9Sstevel@tonic-gate 				strcat( dn, dnsuffix );
6677c478bd9Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
6687c478bd9Sstevel@tonic-gate 					if ((err = ldap_delete_ext( ld, dn, NULL, NULL, &id )) != LDAP_SUCCESS )
6697c478bd9Sstevel@tonic-gate 						printf( "Error in ldap_delete_ext: %s\n", ldap_err2string(err) );
6707c478bd9Sstevel@tonic-gate 					else
6717c478bd9Sstevel@tonic-gate 						printf( "Remove initiated with id %d\n", id );
6727c478bd9Sstevel@tonic-gate 				} else {
6737c478bd9Sstevel@tonic-gate 					if ( (id = ldap_delete( ld, dn )) == -1 )
6747c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_delete" );
6757c478bd9Sstevel@tonic-gate 					else
6767c478bd9Sstevel@tonic-gate 						printf( "Remove initiated with id %d\n", id );
6777c478bd9Sstevel@tonic-gate 				}
6787c478bd9Sstevel@tonic-gate 				break;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			default:
6817c478bd9Sstevel@tonic-gate 				printf( "Possibilities: [rem]ove, [res]ult\n" );
6827c478bd9Sstevel@tonic-gate 				break;
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 			break;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		case 's':	/* search */
68723a1cceaSRoger A. Faulkner 			getaline( dn, sizeof(dn), stdin, "searchbase? " );
6887c478bd9Sstevel@tonic-gate 			strcat( dn, dnsuffix );
68923a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
6907c478bd9Sstevel@tonic-gate 			    "scope (0=Base, 1=One Level, 2=Subtree)? " );
6917c478bd9Sstevel@tonic-gate 			scope = atoi( line );
69223a1cceaSRoger A. Faulkner 			getaline( filter, sizeof(filter), stdin,
6937c478bd9Sstevel@tonic-gate 			    "search filter (e.g. sn=jones)? " );
6947c478bd9Sstevel@tonic-gate 			types = get_list( "attrs to return? " );
69523a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
6967c478bd9Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
6977c478bd9Sstevel@tonic-gate 			attrsonly = atoi( line );
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 			if ( cldapflg ) {
7007c478bd9Sstevel@tonic-gate #ifdef CLDAP
70123a1cceaSRoger A. Faulkner 			    getaline( line, sizeof(line), stdin,
7027c478bd9Sstevel@tonic-gate 				"Requestor DN (for logging)? " );
7037c478bd9Sstevel@tonic-gate 			    if ( cldap_search_s( ld, dn, scope, filter, types,
7047c478bd9Sstevel@tonic-gate 				    attrsonly, &res, line ) != 0 ) {
7057c478bd9Sstevel@tonic-gate 				ldap_perror( ld, "cldap_search_s" );
7067c478bd9Sstevel@tonic-gate 			    } else {
7077c478bd9Sstevel@tonic-gate 				printf( "\nresult: msgid %d\n",
7087c478bd9Sstevel@tonic-gate 				    res->lm_msgid );
7097c478bd9Sstevel@tonic-gate 				handle_result( ld, res );
7107c478bd9Sstevel@tonic-gate 				res = NULLMSG;
7117c478bd9Sstevel@tonic-gate 			    }
7127c478bd9Sstevel@tonic-gate #endif /* CLDAP */
7137c478bd9Sstevel@tonic-gate 			} else {
7147c478bd9Sstevel@tonic-gate 				theInt = 0;
7157c478bd9Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
7167c478bd9Sstevel@tonic-gate 					resultusetimelimit = 1;
71723a1cceaSRoger A. Faulkner 					getaline( line, sizeof(line), stdin,
7187c478bd9Sstevel@tonic-gate 							 "ldap_search_ext (0=>no, 1=>yes - default: yes)? " );
7197c478bd9Sstevel@tonic-gate 					if (line[0] == '\0')
7207c478bd9Sstevel@tonic-gate 						theInt = 1;
7217c478bd9Sstevel@tonic-gate 					else
7227c478bd9Sstevel@tonic-gate 						theInt = atoi( line );
7237c478bd9Sstevel@tonic-gate 				}
7247c478bd9Sstevel@tonic-gate 				if (theInt){
72523a1cceaSRoger A. Faulkner 					getaline(line, sizeof(line), stdin, "time limit?");
7267c478bd9Sstevel@tonic-gate 					timelimit.tv_sec = atoi(line);
7277c478bd9Sstevel@tonic-gate 					resultusetimelimit = 1;
72823a1cceaSRoger A. Faulkner 					getaline(line, sizeof(line), stdin, "size limit?");
7297c478bd9Sstevel@tonic-gate 					sizelimit = atoi(line);
7307c478bd9Sstevel@tonic-gate 					if (( err = ldap_search_ext(ld, dn, scope, filter, types, attrsonly, NULL, NULL,
7317c478bd9Sstevel@tonic-gate 												&timelimit, sizelimit, &id)) != LDAP_SUCCESS){
7327c478bd9Sstevel@tonic-gate 						printf( "Error in ldap_search_ext: %s\n", ldap_err2string(err));
7337c478bd9Sstevel@tonic-gate 					} else {
7347c478bd9Sstevel@tonic-gate 						printf( "Search initiated with id %d\n", id );
7357c478bd9Sstevel@tonic-gate 					}
7367c478bd9Sstevel@tonic-gate 				} else {
7377c478bd9Sstevel@tonic-gate 					if (( id = ldap_search( ld, dn, scope, filter,
7387c478bd9Sstevel@tonic-gate 											types, attrsonly  )) == -1 ) {
7397c478bd9Sstevel@tonic-gate 						ldap_perror( ld, "ldap_search" );
7407c478bd9Sstevel@tonic-gate 					} else {
7417c478bd9Sstevel@tonic-gate 						printf( "Search initiated with id %d\n", id );
7427c478bd9Sstevel@tonic-gate 					}
7437c478bd9Sstevel@tonic-gate 				}
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			free_list( types );
7467c478bd9Sstevel@tonic-gate 			break;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		case 't':	/* set timeout value */
74923a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "timeout? " );
7507c478bd9Sstevel@tonic-gate 			timeout.tv_sec = atoi( line );
7517c478bd9Sstevel@tonic-gate 			break;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		case 'U':	/* set ufn search prefix */
75423a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "ufn prefix? " );
7557c478bd9Sstevel@tonic-gate 			ldap_ufn_setprefix( ld, line );
7567c478bd9Sstevel@tonic-gate 			break;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		case 'u':	/* user friendly search w/optional timeout */
75923a1cceaSRoger A. Faulkner 			getaline( dn, sizeof(dn), stdin, "ufn? " );
7607c478bd9Sstevel@tonic-gate 			strcat( dn, dnsuffix );
7617c478bd9Sstevel@tonic-gate 			types = get_list( "attrs to return? " );
76223a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
7637c478bd9Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
7647c478bd9Sstevel@tonic-gate 			attrsonly = atoi( line );
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 			if ( command2 == 't' ) {
7677c478bd9Sstevel@tonic-gate 				id = ldap_ufn_search_c( ld, dn, types,
7687c478bd9Sstevel@tonic-gate 				    attrsonly, &res, ldap_ufn_timeout,
7697c478bd9Sstevel@tonic-gate 				    &timeout );
7707c478bd9Sstevel@tonic-gate 			} else {
7717c478bd9Sstevel@tonic-gate 				id = ldap_ufn_search_s( ld, dn, types,
7727c478bd9Sstevel@tonic-gate 				    attrsonly, &res );
7737c478bd9Sstevel@tonic-gate 			}
7747c478bd9Sstevel@tonic-gate 			if ( res == NULL )
7757c478bd9Sstevel@tonic-gate 				ldap_perror( ld, "ldap_ufn_search" );
7767c478bd9Sstevel@tonic-gate 			else {
7777c478bd9Sstevel@tonic-gate 				printf( "\nresult: err %d\n", id );
7787c478bd9Sstevel@tonic-gate 				handle_result( ld, res );
7797c478bd9Sstevel@tonic-gate 				res = NULLMSG;
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 			free_list( types );
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		case 'l':	/* URL search */
78523a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
7867c478bd9Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
7877c478bd9Sstevel@tonic-gate 			attrsonly = atoi( line );
78823a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "LDAP URL? " );
7897c478bd9Sstevel@tonic-gate 			if (( id = ldap_url_search( ld, line, attrsonly  ))
7907c478bd9Sstevel@tonic-gate 				== -1 ) {
7917c478bd9Sstevel@tonic-gate 			    ldap_perror( ld, "ldap_url_search" );
7927c478bd9Sstevel@tonic-gate 			} else {
7937c478bd9Sstevel@tonic-gate 			    printf( "URL search initiated with id %d\n", id );
7947c478bd9Sstevel@tonic-gate 			}
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		case 'p':	/* parse LDAP URL */
79823a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "LDAP URL? " );
7997c478bd9Sstevel@tonic-gate 			if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
8007c478bd9Sstevel@tonic-gate 			    fprintf( stderr, "ldap_url_parse: error %d\n", i );
8017c478bd9Sstevel@tonic-gate 			} else {
8027c478bd9Sstevel@tonic-gate 			    printf( "\t  host: " );
8037c478bd9Sstevel@tonic-gate 			    if ( ludp->lud_host == NULL ) {
8047c478bd9Sstevel@tonic-gate 				printf( "DEFAULT\n" );
8057c478bd9Sstevel@tonic-gate 			    } else {
8067c478bd9Sstevel@tonic-gate 				printf( "<%s>\n", ludp->lud_host );
8077c478bd9Sstevel@tonic-gate 			    }
8087c478bd9Sstevel@tonic-gate 			    printf( "\t  port: " );
8097c478bd9Sstevel@tonic-gate 			    if ( ludp->lud_port == 0 ) {
8107c478bd9Sstevel@tonic-gate 				printf( "DEFAULT\n" );
8117c478bd9Sstevel@tonic-gate 			    } else {
8127c478bd9Sstevel@tonic-gate 				printf( "%d\n", ludp->lud_port );
8137c478bd9Sstevel@tonic-gate 			    }
8147c478bd9Sstevel@tonic-gate 			    printf( "\t    dn: <%s>\n", ludp->lud_dn );
8157c478bd9Sstevel@tonic-gate 			    printf( "\t attrs:" );
8167c478bd9Sstevel@tonic-gate 			    if ( ludp->lud_attrs == NULL ) {
8177c478bd9Sstevel@tonic-gate 				printf( " ALL" );
8187c478bd9Sstevel@tonic-gate 			    } else {
8197c478bd9Sstevel@tonic-gate 				for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
8207c478bd9Sstevel@tonic-gate 				    printf( " <%s>", ludp->lud_attrs[ i ] );
8217c478bd9Sstevel@tonic-gate 				}
8227c478bd9Sstevel@tonic-gate 			    }
823*55fea89dSDan Cross 			    printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_UNKNOWN ? "DEFAULT (base)" :
824*55fea89dSDan Cross 						ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "ONE" :
8257c478bd9Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
8267c478bd9Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
8277c478bd9Sstevel@tonic-gate 			    printf( "\tfilter: <%s>\n", ludp->lud_filter ? ludp->lud_filter : "NONE");
8287c478bd9Sstevel@tonic-gate 				if (ludp->lud_extensions){
8297c478bd9Sstevel@tonic-gate 					printf("\textensions: \n");
8307c478bd9Sstevel@tonic-gate 					for (i = 0; ludp->lud_extensions[i] != NULL; i++)
831*55fea89dSDan Cross 						printf("\t\t%s (%s)\n", ludp->lud_extensions[i]->lue_type,
8327c478bd9Sstevel@tonic-gate 							   ludp->lud_extensions[i]->lue_iscritical ? "Critical" : "Non critical");
8337c478bd9Sstevel@tonic-gate 				}
834*55fea89dSDan Cross 
8357c478bd9Sstevel@tonic-gate 			    ldap_free_urldesc( ludp );
8367c478bd9Sstevel@tonic-gate 			}
8377c478bd9Sstevel@tonic-gate 			    break;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		case 'n':	/* set dn suffix, for convenience */
84023a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "DN suffix? " );
8417c478bd9Sstevel@tonic-gate 			strcpy( dnsuffix, line );
8427c478bd9Sstevel@tonic-gate 			break;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		case 'e':	/* enable cache */
8457c478bd9Sstevel@tonic-gate #ifdef NO_CACHE
8467c478bd9Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
8477c478bd9Sstevel@tonic-gate #else /* NO_CACHE */
84823a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
8497c478bd9Sstevel@tonic-gate 			i = atoi( line );
85023a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
8517c478bd9Sstevel@tonic-gate 			if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
852*55fea89dSDan Cross 				printf( "local cache is on\n" );
8537c478bd9Sstevel@tonic-gate 			} else {
854*55fea89dSDan Cross 				printf( "ldap_enable_cache failed\n" );
8557c478bd9Sstevel@tonic-gate 			}
8567c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		case 'x':	/* uncache entry */
8607c478bd9Sstevel@tonic-gate #ifdef NO_CACHE
8617c478bd9Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
8627c478bd9Sstevel@tonic-gate #else /* NO_CACHE */
86323a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "DN? " );
8647c478bd9Sstevel@tonic-gate 			ldap_uncache_entry( ld, line );
8657c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */
8667c478bd9Sstevel@tonic-gate 			break;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		case 'X':	/* uncache request */
8697c478bd9Sstevel@tonic-gate #ifdef NO_CACHE
8707c478bd9Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
8717c478bd9Sstevel@tonic-gate #else /* NO_CACHE */
87223a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "request msgid? " );
8737c478bd9Sstevel@tonic-gate 			ldap_uncache_request( ld, atoi( line ));
8747c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */
8757c478bd9Sstevel@tonic-gate 			break;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		case 'o':	/* set ldap options */
87823a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
8797c478bd9Sstevel@tonic-gate 			theInt = atoi(line);
8807c478bd9Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_DEREF, &theInt );
88123a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "timelimit?" );
8827c478bd9Sstevel@tonic-gate 			theInt = atoi(line);
8837c478bd9Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_TIMELIMIT,  &theInt);
88423a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "sizelimit?" );
8857c478bd9Sstevel@tonic-gate 			theInt = atoi(line);
8867c478bd9Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &theInt);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 			ld->ld_options = 0;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION
89123a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
8927c478bd9Sstevel@tonic-gate 				"Automatic translation of T.61 strings (0=no, 1=yes)?" );
8937c478bd9Sstevel@tonic-gate 			if ( atoi( line ) == 0 ) {
8947c478bd9Sstevel@tonic-gate 				ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
8957c478bd9Sstevel@tonic-gate 			} else {
8967c478bd9Sstevel@tonic-gate 				ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
8977c478bd9Sstevel@tonic-gate #ifdef LDAP_CHARSET_8859
89823a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
8997c478bd9Sstevel@tonic-gate 					"Translate to/from ISO-8859 (0=no, 1=yes?" );
9007c478bd9Sstevel@tonic-gate 				if ( atoi( line ) != 0 ) {
9017c478bd9Sstevel@tonic-gate 					ldap_set_string_translators( ld,
9027c478bd9Sstevel@tonic-gate 					    ldap_8859_to_t61,
9037c478bd9Sstevel@tonic-gate 					    ldap_t61_to_8859 );
9047c478bd9Sstevel@tonic-gate 				}
9057c478bd9Sstevel@tonic-gate #endif /* LDAP_CHARSET_8859 */
9067c478bd9Sstevel@tonic-gate 			}
9077c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate #ifdef LDAP_DNS
91023a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
9117c478bd9Sstevel@tonic-gate 				"Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
9127c478bd9Sstevel@tonic-gate 			if ( atoi( line ) != 0 ) {
9137c478bd9Sstevel@tonic-gate 				ld->ld_options |= LDAP_OPT_DNS;
9147c478bd9Sstevel@tonic-gate 			}
9157c478bd9Sstevel@tonic-gate #endif /* LDAP_DNS */
9167c478bd9Sstevel@tonic-gate 
91723a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin,
9187c478bd9Sstevel@tonic-gate 				"Recognize and chase referrals (0=no, 1=yes)?" );
9197c478bd9Sstevel@tonic-gate 			if ( atoi( line ) != 0 ) {
9207c478bd9Sstevel@tonic-gate 				theInt = LDAP_OPT_ON;
92123a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
9227c478bd9Sstevel@tonic-gate 						 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
9237c478bd9Sstevel@tonic-gate 				if ( atoi( line ) != 0 ) {
9247c478bd9Sstevel@tonic-gate 					ldap_set_option( ld, LDAP_OPT_REBIND_FN, bind_prompt );
9257c478bd9Sstevel@tonic-gate 				}
9267c478bd9Sstevel@tonic-gate 			} else {
9277c478bd9Sstevel@tonic-gate 				theInt = LDAP_OPT_OFF;
9287c478bd9Sstevel@tonic-gate 			}
9297c478bd9Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_REFERRALS, &theInt);
9307c478bd9Sstevel@tonic-gate 			break;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		case 'k': /* Set some controls */
933*55fea89dSDan Cross 			getaline( line, sizeof(line), stdin,
9347c478bd9Sstevel@tonic-gate 					 "Set control: (0 for none, 1 for ManageDSA, 2 for preferredLang, 3 for BAD)?");
9357c478bd9Sstevel@tonic-gate 			theInt = atoi(line);
9367c478bd9Sstevel@tonic-gate 			switch (theInt){
9377c478bd9Sstevel@tonic-gate 			case 0:
9387c478bd9Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, NULL);
9397c478bd9Sstevel@tonic-gate 				break;
9407c478bd9Sstevel@tonic-gate 			case 1:
9417c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_oid = "2.16.840.1.113730.3.4.2";
9427c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = 1;
9437c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_value = NULL;
9447c478bd9Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
9457c478bd9Sstevel@tonic-gate 				break;
9467c478bd9Sstevel@tonic-gate 			case 2:
94723a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
9487c478bd9Sstevel@tonic-gate 						 "Preferred Language Control : lang ?");
9497c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_oid = "1.3.6.1.4.1.1466.20035";
9507c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = 1;
9517c478bd9Sstevel@tonic-gate 				bv.bv_val = strdup(line);
9527c478bd9Sstevel@tonic-gate 				bv.bv_len = strlen(line);
9537c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_value = &bv;
9547c478bd9Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
9557c478bd9Sstevel@tonic-gate 				break;
9567c478bd9Sstevel@tonic-gate 			default:
95723a1cceaSRoger A. Faulkner 				getaline( line, sizeof(line), stdin,
9587c478bd9Sstevel@tonic-gate 						 "Bad Control is critical (0=false, 1=true)?");
9597c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_oid = "1.1.1.1.1.1";
9607c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = atoi(line);
9617c478bd9Sstevel@tonic-gate 				aCtrl.ldctl_value = NULL;
9627c478bd9Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
9637c478bd9Sstevel@tonic-gate 				break;
9647c478bd9Sstevel@tonic-gate 			}
9657c478bd9Sstevel@tonic-gate 			break;
966*55fea89dSDan Cross 
9677c478bd9Sstevel@tonic-gate 		case 'O':	/* set cache options */
9687c478bd9Sstevel@tonic-gate #ifdef NO_CACHE
9697c478bd9Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
9707c478bd9Sstevel@tonic-gate #else /* NO_CACHE */
97123a1cceaSRoger A. Faulkner 			getaline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
9727c478bd9Sstevel@tonic-gate 			switch( atoi( line )) {
9737c478bd9Sstevel@tonic-gate 			case 0:
9747c478bd9Sstevel@tonic-gate 				ldap_set_cache_options( ld, 0 );
9757c478bd9Sstevel@tonic-gate 				break;
9767c478bd9Sstevel@tonic-gate 			case 1:
9777c478bd9Sstevel@tonic-gate 				ldap_set_cache_options( ld,
9787c478bd9Sstevel@tonic-gate 					LDAP_CACHE_OPT_CACHENOERRS );
9797c478bd9Sstevel@tonic-gate 				break;
9807c478bd9Sstevel@tonic-gate 			case 2:
9817c478bd9Sstevel@tonic-gate 				ldap_set_cache_options( ld,
9827c478bd9Sstevel@tonic-gate 					LDAP_CACHE_OPT_CACHEALLERRS );
9837c478bd9Sstevel@tonic-gate 				break;
9847c478bd9Sstevel@tonic-gate 			default:
9857c478bd9Sstevel@tonic-gate 				printf( "not a valid cache option\n" );
9867c478bd9Sstevel@tonic-gate 			}
9877c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */
9887c478bd9Sstevel@tonic-gate 			break;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		case '?':	/* help */
9917c478bd9Sstevel@tonic-gate     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
9927c478bd9Sstevel@tonic-gate     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
9937c478bd9Sstevel@tonic-gate     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
9947c478bd9Sstevel@tonic-gate     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
9957c478bd9Sstevel@tonic-gate     printf( "          [u]fn search  [ut]fn search with timeout\n" );
9967c478bd9Sstevel@tonic-gate     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
9977c478bd9Sstevel@tonic-gate     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
9987c478bd9Sstevel@tonic-gate     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
9997c478bd9Sstevel@tonic-gate     printf( "          [?]help       [o]ptions         [O]cache options\n" );
10007c478bd9Sstevel@tonic-gate     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
10017c478bd9Sstevel@tonic-gate 			break;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		default:
10047c478bd9Sstevel@tonic-gate 			printf( "Invalid command.  Type ? for help.\n" );
10057c478bd9Sstevel@tonic-gate 			break;
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		(void) memset( line, '\0', sizeof(line) );
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	return( 0 );
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate static void
handle_result(LDAP * ld,LDAPMessage * lm)10157c478bd9Sstevel@tonic-gate handle_result( LDAP *ld, LDAPMessage *lm )
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	switch ( lm->lm_msgtype ) {
10187c478bd9Sstevel@tonic-gate 	case LDAP_RES_COMPARE:
10197c478bd9Sstevel@tonic-gate 		printf( "Compare result\n" );
10207c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "compare" );
10217c478bd9Sstevel@tonic-gate 		break;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	case LDAP_RES_SEARCH_RESULT:
10247c478bd9Sstevel@tonic-gate 		printf( "Search result\n" );
10257c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "search" );
10267c478bd9Sstevel@tonic-gate 		break;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	case LDAP_RES_SEARCH_REFERENCE:
10297c478bd9Sstevel@tonic-gate 		printf( "Search reference\n" );
10307c478bd9Sstevel@tonic-gate 		print_search_entry( ld, lm );
10317c478bd9Sstevel@tonic-gate 		break;
1032*55fea89dSDan Cross 
10337c478bd9Sstevel@tonic-gate 	case LDAP_RES_SEARCH_ENTRY:
10347c478bd9Sstevel@tonic-gate 		printf( "Search entry\n" );
10357c478bd9Sstevel@tonic-gate 		print_search_entry( ld, lm );
10367c478bd9Sstevel@tonic-gate 		break;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	case LDAP_RES_ADD:
10397c478bd9Sstevel@tonic-gate 		printf( "Add result\n" );
10407c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "add" );
10417c478bd9Sstevel@tonic-gate 		break;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	case LDAP_RES_DELETE:
10447c478bd9Sstevel@tonic-gate 		printf( "Delete result\n" );
10457c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "delete" );
10467c478bd9Sstevel@tonic-gate 		break;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	case LDAP_RES_MODIFY:
10497c478bd9Sstevel@tonic-gate 		printf( "Modify result\n" );
10507c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "modify" );
10517c478bd9Sstevel@tonic-gate 		break;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	case LDAP_RES_MODRDN:
10547c478bd9Sstevel@tonic-gate 		printf( "ModRDN result\n" );
10557c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "modrdn" );
10567c478bd9Sstevel@tonic-gate 		break;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	case LDAP_RES_BIND:
10597c478bd9Sstevel@tonic-gate 		printf( "Bind result\n" );
10607c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "bind" );
10617c478bd9Sstevel@tonic-gate 		break;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	default:
10647c478bd9Sstevel@tonic-gate 		printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
10657c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, lm, "unknown" );
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate static void
print_ldap_result(LDAP * ld,LDAPMessage * lm,char * s)10707c478bd9Sstevel@tonic-gate print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	int rc, i;
10737c478bd9Sstevel@tonic-gate 	int errCode;
10747c478bd9Sstevel@tonic-gate 	char *matched = NULL, *errMsg = NULL, **referrals = NULL;
10757c478bd9Sstevel@tonic-gate 	LDAPControl **srvctrls = NULL;
1076*55fea89dSDan Cross 
10777c478bd9Sstevel@tonic-gate 	if ((rc = ldap_parse_result(ld, lm, &errCode, &matched, &errMsg, &referrals, &srvctrls, 0)) != LDAP_SUCCESS){
10787c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: error while parsing result (%s)\n", s, ldap_err2string(rc));
10797c478bd9Sstevel@tonic-gate 		return;
10807c478bd9Sstevel@tonic-gate 	}
1081*55fea89dSDan Cross 
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s: %s\n", s, ldap_err2string(errCode));
10847c478bd9Sstevel@tonic-gate 	if (errCode == LDAP_REFERRAL){
10857c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\tReferrals returned: \n");
10867c478bd9Sstevel@tonic-gate 		for (i = 0; referrals[i] != NULL; i++)
10877c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\t\t%s\n", referrals[i]);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	if (errMsg && *errMsg)
10907c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\tAdditional info: %s\n", errMsg);
10917c478bd9Sstevel@tonic-gate 	free(errMsg);
10927c478bd9Sstevel@tonic-gate 	if (NAME_ERROR(errCode) && matched && *matched){
10937c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\tMatched DN: %s\n", matched);
10947c478bd9Sstevel@tonic-gate 		free(matched);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	if (srvctrls != NULL){
10977c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\tLDAPControls returned: \n");
10987c478bd9Sstevel@tonic-gate 		for (i=0;srvctrls[i] != NULL; i++)
10997c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\t\t%s (%s)\n", srvctrls[i]->ldctl_oid, srvctrls[i]->ldctl_iscritical ? "Critical" : "Not critical");
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 	return;
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate static void
print_search_entry(LDAP * ld,LDAPMessage * res)11057c478bd9Sstevel@tonic-gate print_search_entry( LDAP *ld, LDAPMessage *res )
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	BerElement	*ber;
11087c478bd9Sstevel@tonic-gate 	char		*a, *dn, *ufn;
11097c478bd9Sstevel@tonic-gate 	struct berval	**vals;
11107c478bd9Sstevel@tonic-gate 	int		i;
11117c478bd9Sstevel@tonic-gate 	LDAPMessage	*e;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	for ( e = ldap_first_message( ld, res ); e != NULLMSG;
11147c478bd9Sstevel@tonic-gate 	    e = ldap_next_message( ld, e ) ) {
11157c478bd9Sstevel@tonic-gate 		if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
11167c478bd9Sstevel@tonic-gate 			break;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		dn = ldap_get_dn( ld, e );
11197c478bd9Sstevel@tonic-gate 		printf( "\tDN: %s\n", dn );
1120*55fea89dSDan Cross 
11217c478bd9Sstevel@tonic-gate 		ufn = ldap_dn2ufn( dn );
11227c478bd9Sstevel@tonic-gate 		printf( "\tUFN: %s\n", ufn );
11237c478bd9Sstevel@tonic-gate 		free( dn );
11247c478bd9Sstevel@tonic-gate 		free( ufn );
1125*55fea89dSDan Cross 
11267c478bd9Sstevel@tonic-gate 		if ( e->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ){
11277c478bd9Sstevel@tonic-gate 			char **urls = ldap_get_reference_urls(ld, e);
11287c478bd9Sstevel@tonic-gate 			if (urls == NULL){
11297c478bd9Sstevel@tonic-gate 				printf("\t\tError with references: %s\n", ldap_err2string(ld->ld_errno));
11307c478bd9Sstevel@tonic-gate 			} else {
11317c478bd9Sstevel@tonic-gate 				for (i=0;urls[i] != NULL;i++)
11327c478bd9Sstevel@tonic-gate 					printf("\t\tURL: %s\n", urls[i]);
11337c478bd9Sstevel@tonic-gate 			}
11347c478bd9Sstevel@tonic-gate 		} else {
11357c478bd9Sstevel@tonic-gate 			for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
11367c478bd9Sstevel@tonic-gate 				  a = ldap_next_attribute( ld, e, ber ) ) {
11377c478bd9Sstevel@tonic-gate 				printf( "\t\tATTR: %s\n", a );
11387c478bd9Sstevel@tonic-gate 				if ( (vals = ldap_get_values_len( ld, e, a ))
11397c478bd9Sstevel@tonic-gate 					 == NULL ) {
11407c478bd9Sstevel@tonic-gate 					printf( "\t\t\t(no values)\n" );
11417c478bd9Sstevel@tonic-gate 				} else {
11427c478bd9Sstevel@tonic-gate 					for ( i = 0; vals[i] != NULL; i++ ) {
11437c478bd9Sstevel@tonic-gate 						int	j, nonascii;
1144*55fea89dSDan Cross 
11457c478bd9Sstevel@tonic-gate 						nonascii = 0;
11467c478bd9Sstevel@tonic-gate 						for ( j = 0; j < vals[i]->bv_len; j++ )
11477c478bd9Sstevel@tonic-gate 							if ( !isascii( vals[i]->bv_val[j] ) ) {
11487c478bd9Sstevel@tonic-gate 							nonascii = 1;
11497c478bd9Sstevel@tonic-gate 							break;
11507c478bd9Sstevel@tonic-gate 							}
1151*55fea89dSDan Cross 
11527c478bd9Sstevel@tonic-gate 						if ( nonascii ) {
11537c478bd9Sstevel@tonic-gate 							printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
11547c478bd9Sstevel@tonic-gate #ifdef BPRINT_NONASCII
11557c478bd9Sstevel@tonic-gate 							lber_bprint( vals[i]->bv_val,
11567c478bd9Sstevel@tonic-gate 										 vals[i]->bv_len );
11577c478bd9Sstevel@tonic-gate #endif /* BPRINT_NONASCII */
11587c478bd9Sstevel@tonic-gate 							continue;
11597c478bd9Sstevel@tonic-gate 						}
11607c478bd9Sstevel@tonic-gate 						printf( "\t\t\tlength (%ld) %s\n",
11617c478bd9Sstevel@tonic-gate 								vals[i]->bv_len, vals[i]->bv_val );
11627c478bd9Sstevel@tonic-gate 					}
11637c478bd9Sstevel@tonic-gate 					ber_bvecfree( vals );
11647c478bd9Sstevel@tonic-gate 				}
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 	}
1168*55fea89dSDan Cross 
11697c478bd9Sstevel@tonic-gate 	if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
11707c478bd9Sstevel@tonic-gate 	    || res->lm_chain != NULLMSG )
11717c478bd9Sstevel@tonic-gate 		print_ldap_result( ld, res, "search" );
11727c478bd9Sstevel@tonic-gate }
1173