xref: /onnv-gate/usr/src/cmd/ldap/common/ldaptest.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
2*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate  */
40Sstevel@tonic-gate 
50Sstevel@tonic-gate #include <stdio.h>
60Sstevel@tonic-gate #include <ctype.h>
70Sstevel@tonic-gate #include <string.h>
80Sstevel@tonic-gate #include <sys/types.h>
90Sstevel@tonic-gate #include <sys/socket.h>
100Sstevel@tonic-gate #include <sys/time.h>
110Sstevel@tonic-gate #include <sys/stat.h>
120Sstevel@tonic-gate #include <sys/file.h>
130Sstevel@tonic-gate #include <fcntl.h>
140Sstevel@tonic-gate #include <unistd.h>
150Sstevel@tonic-gate 
160Sstevel@tonic-gate #include "lber.h"
170Sstevel@tonic-gate #include "ldap.h"
180Sstevel@tonic-gate 
190Sstevel@tonic-gate #define MOD_USE_BVALS
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #ifdef NEEDPROTOS
220Sstevel@tonic-gate static void handle_result( LDAP *ld, LDAPMessage *lm );
230Sstevel@tonic-gate static void print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s );
240Sstevel@tonic-gate static void print_search_entry( LDAP *ld, LDAPMessage *res );
250Sstevel@tonic-gate static void free_list( char **list );
260Sstevel@tonic-gate #else
270Sstevel@tonic-gate static void handle_result();
280Sstevel@tonic-gate static void print_ldap_result();
290Sstevel@tonic-gate static void print_search_entry();
300Sstevel@tonic-gate static void free_list();
310Sstevel@tonic-gate #endif /* NEEDPROTOS */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #define NOCACHEERRMSG	"don't compile with -DNO_CACHE if you desire local caching"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate char *dnsuffix;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static char *
getaline(char * line,int len,FILE * fp,char * prompt)38*13093SRoger.Faulkner@Oracle.COM getaline( char *line, int len, FILE *fp, char *prompt )
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	printf(prompt);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	if ( fgets( line, len, fp ) == NULL )
430Sstevel@tonic-gate 		return( NULL );
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	line[ strlen( line ) - 1 ] = '\0';
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	return( line );
480Sstevel@tonic-gate }
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static char **
get_list(char * prompt)510Sstevel@tonic-gate get_list( char *prompt )
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	static char	buf[256];
540Sstevel@tonic-gate 	int		num;
550Sstevel@tonic-gate 	char		**result;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	num = 0;
580Sstevel@tonic-gate 	result = (char **) 0;
590Sstevel@tonic-gate 	while ( 1 ) {
60*13093SRoger.Faulkner@Oracle.COM 		getaline( buf, sizeof(buf), stdin, prompt );
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 		if ( *buf == '\0' )
630Sstevel@tonic-gate 			break;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		if ( result == (char **) 0 )
660Sstevel@tonic-gate 			result = (char **) malloc( sizeof(char *) );
670Sstevel@tonic-gate 		else
680Sstevel@tonic-gate 			result = (char **) realloc( result,
690Sstevel@tonic-gate 			    sizeof(char *) * (num + 1) );
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 		result[num++] = (char *) strdup( buf );
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	if ( result == (char **) 0 )
740Sstevel@tonic-gate 		return( NULL );
750Sstevel@tonic-gate 	result = (char **) realloc( result, sizeof(char *) * (num + 1) );
760Sstevel@tonic-gate 	result[num] = NULL;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	return( result );
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static void
free_list(char ** list)830Sstevel@tonic-gate free_list( char **list )
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	int	i;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if ( list != NULL ) {
880Sstevel@tonic-gate 		for ( i = 0; list[ i ] != NULL; ++i ) {
890Sstevel@tonic-gate 			free( list[ i ] );
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 		free( (char *)list );
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #ifdef MOD_USE_BVALS
970Sstevel@tonic-gate static int
file_read(char * path,struct berval * bv)980Sstevel@tonic-gate file_read( char *path, struct berval *bv )
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	FILE		*fp;
1010Sstevel@tonic-gate 	long		rlen;
1020Sstevel@tonic-gate 	int		eof;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (( fp = fopen( path, "r" )) == NULL ) {
1050Sstevel@tonic-gate 	    	perror( path );
1060Sstevel@tonic-gate 		return( -1 );
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
1100Sstevel@tonic-gate 		perror( path );
1110Sstevel@tonic-gate 		fclose( fp );
1120Sstevel@tonic-gate 		return( -1 );
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	bv->bv_len = ftell( fp );
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
1180Sstevel@tonic-gate 		perror( "malloc" );
1190Sstevel@tonic-gate 		fclose( fp );
1200Sstevel@tonic-gate 		return( -1 );
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
1240Sstevel@tonic-gate 		perror( path );
1250Sstevel@tonic-gate 		fclose( fp );
1260Sstevel@tonic-gate 		return( -1 );
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
1300Sstevel@tonic-gate 	eof = feof( fp );
1310Sstevel@tonic-gate 	fclose( fp );
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ( rlen != bv->bv_len ) {
1340Sstevel@tonic-gate 		perror( path );
1350Sstevel@tonic-gate 		free( bv->bv_val );
1360Sstevel@tonic-gate 		return( -1 );
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	return( bv->bv_len );
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static LDAPMod **
get_modlist(char * prompt1,char * prompt2,char * prompt3)1450Sstevel@tonic-gate get_modlist( char *prompt1, char *prompt2, char *prompt3 )
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	static char	buf[256];
1480Sstevel@tonic-gate 	int		num;
1490Sstevel@tonic-gate 	LDAPMod		tmp;
1500Sstevel@tonic-gate 	LDAPMod		**result;
1510Sstevel@tonic-gate #ifdef MOD_USE_BVALS
1520Sstevel@tonic-gate 	struct berval	**bvals;
1530Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	num = 0;
1560Sstevel@tonic-gate 	result = NULL;
1570Sstevel@tonic-gate 	while ( 1 ) {
1580Sstevel@tonic-gate 		if ( prompt1 ) {
159*13093SRoger.Faulkner@Oracle.COM 			getaline( buf, sizeof(buf), stdin, prompt1 );
1600Sstevel@tonic-gate 			tmp.mod_op = atoi( buf );
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 			if ( tmp.mod_op == -1 || buf[0] == '\0' )
1630Sstevel@tonic-gate 				break;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 
166*13093SRoger.Faulkner@Oracle.COM 		getaline( buf, sizeof(buf), stdin, prompt2 );
1670Sstevel@tonic-gate 		if ( buf[0] == '\0' )
1680Sstevel@tonic-gate 			break;
1690Sstevel@tonic-gate 		tmp.mod_type = strdup( buf );
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		tmp.mod_values = get_list( prompt3 );
1720Sstevel@tonic-gate #ifdef MOD_USE_BVALS
1730Sstevel@tonic-gate 		if ( tmp.mod_values != NULL ) {
1740Sstevel@tonic-gate 			int	i;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 			for ( i = 0; tmp.mod_values[i] != NULL; ++i )
1770Sstevel@tonic-gate 				;
1780Sstevel@tonic-gate 			bvals = (struct berval **)calloc( i + 1,
1790Sstevel@tonic-gate 			    sizeof( struct berval *));
1800Sstevel@tonic-gate 			for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
1810Sstevel@tonic-gate 				bvals[i] = (struct berval *)malloc(
1820Sstevel@tonic-gate 				    sizeof( struct berval ));
1830Sstevel@tonic-gate 				if ( strncmp( tmp.mod_values[i], "{FILE}",
1840Sstevel@tonic-gate 				    6 ) == 0 ) {
1850Sstevel@tonic-gate 					if ( file_read( tmp.mod_values[i] + 6,
1860Sstevel@tonic-gate 					    bvals[i] ) < 0 ) {
1870Sstevel@tonic-gate 						return( NULL );
1880Sstevel@tonic-gate 					}
1890Sstevel@tonic-gate 				} else {
1900Sstevel@tonic-gate 					bvals[i]->bv_val = tmp.mod_values[i];
1910Sstevel@tonic-gate 					bvals[i]->bv_len =
1920Sstevel@tonic-gate 					    strlen( tmp.mod_values[i] );
1930Sstevel@tonic-gate 				}
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 			tmp.mod_bvalues = bvals;
1960Sstevel@tonic-gate 			tmp.mod_op |= LDAP_MOD_BVALUES;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		if ( result == NULL )
2010Sstevel@tonic-gate 			result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
2020Sstevel@tonic-gate 		else
2030Sstevel@tonic-gate 			result = (LDAPMod **) realloc( result,
2040Sstevel@tonic-gate 			    sizeof(LDAPMod *) * (num + 1) );
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
2070Sstevel@tonic-gate 		*(result[num]) = tmp;	/* struct copy */
2080Sstevel@tonic-gate 		num++;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	if ( result == NULL )
2110Sstevel@tonic-gate 		return( NULL );
2120Sstevel@tonic-gate 	result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
2130Sstevel@tonic-gate 	result[num] = NULL;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	return( result );
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate int
bind_prompt(LDAP * ld,char ** dnp,char ** passwdp,int * authmethodp,int freeit)2200Sstevel@tonic-gate bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
2210Sstevel@tonic-gate 	int freeit )
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	static char	dn[256], passwd[256];
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if ( !freeit ) {
2260Sstevel@tonic-gate #ifdef KERBEROS
227*13093SRoger.Faulkner@Oracle.COM 		getaline( dn, sizeof(dn), stdin,
2280Sstevel@tonic-gate 		    "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
2290Sstevel@tonic-gate 		if (( *authmethodp = atoi( dn )) == 3 ) {
2300Sstevel@tonic-gate 			*authmethodp = LDAP_AUTH_KRBV4;
2310Sstevel@tonic-gate 		} else {
2320Sstevel@tonic-gate 			*authmethodp |= 0x80;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate #else /* KERBEROS */
2350Sstevel@tonic-gate 		*authmethodp = LDAP_AUTH_SIMPLE;
2360Sstevel@tonic-gate #endif /* KERBEROS */
2370Sstevel@tonic-gate 
238*13093SRoger.Faulkner@Oracle.COM 		getaline( dn, sizeof(dn), stdin, "re-bind dn? " );
2390Sstevel@tonic-gate 		strcat( dn, dnsuffix );
2400Sstevel@tonic-gate 		*dnp = dn;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
243*13093SRoger.Faulkner@Oracle.COM 			getaline( passwd, sizeof(passwd), stdin,
2440Sstevel@tonic-gate 			    "re-bind password? " );
2450Sstevel@tonic-gate 		} else {
2460Sstevel@tonic-gate 			passwd[0] = '\0';
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 		*passwdp = passwd;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	return( LDAP_SUCCESS );
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate int
main(int argc,char ** argv)2560Sstevel@tonic-gate main(int argc, char **argv )
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	LDAP	*ld;
2590Sstevel@tonic-gate 	int		i, c, port, cldapflg, errflg, method, id,
2600Sstevel@tonic-gate 		msgtype, delrdn, theInt, sizelimit, err;
2610Sstevel@tonic-gate 	char	line[256], command1, command2, command3;
2620Sstevel@tonic-gate 	char	passwd[64], dn[256], rdn[64], attr[64], value[256];
2630Sstevel@tonic-gate 	char	filter[256], *host, **types;
2640Sstevel@tonic-gate 	char 	*mechanism;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	char	**exdn;
2670Sstevel@tonic-gate 	char	*usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
2680Sstevel@tonic-gate 	int		bound, all, scope, attrsonly;
2690Sstevel@tonic-gate 	LDAPMessage	*res;
2700Sstevel@tonic-gate 	LDAPMod	**mods, **attrs;
2710Sstevel@tonic-gate 	struct timeval	timeout, timelimit;
2720Sstevel@tonic-gate 	char	*copyfname = NULL;
2730Sstevel@tonic-gate 	int		copyoptions = 0, resultusetimelimit = 0;
2740Sstevel@tonic-gate 	LDAPURLDesc	*ludp;
2750Sstevel@tonic-gate 	struct berval bv, cred, *srvcrds = NULL;
2760Sstevel@tonic-gate 	extern char	*optarg;
2770Sstevel@tonic-gate 	extern int	optind;
2780Sstevel@tonic-gate 	LDAPControl *ctrls[2];
2790Sstevel@tonic-gate 	LDAPControl aCtrl;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate #ifdef MACOS
2830Sstevel@tonic-gate 	if (( argv = get_list( "cmd line arg?" )) == NULL ) {
2840Sstevel@tonic-gate 		exit( 1 );
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 	for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
2870Sstevel@tonic-gate 		;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate #endif /* MACOS */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	host = NULL;
2920Sstevel@tonic-gate 	port = LDAP_PORT;
2930Sstevel@tonic-gate 	dnsuffix = "";
2940Sstevel@tonic-gate 	cldapflg = errflg = 0;
2950Sstevel@tonic-gate 	ctrls[0] = &aCtrl;
2960Sstevel@tonic-gate 	ctrls[1] = NULL;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
2990Sstevel@tonic-gate 		switch( c ) {
3000Sstevel@tonic-gate 		case 'u':
3010Sstevel@tonic-gate #ifdef CLDAP
3020Sstevel@tonic-gate 			cldapflg++;
3030Sstevel@tonic-gate #else /* CLDAP */
3040Sstevel@tonic-gate 			printf( "Compile with -DCLDAP for UDP support\n" );
3050Sstevel@tonic-gate #endif /* CLDAP */
3060Sstevel@tonic-gate 			break;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		case 'd':
3090Sstevel@tonic-gate #ifdef LDAP_DEBUG
3100Sstevel@tonic-gate 			ldap_debug = atoi( optarg );
3110Sstevel@tonic-gate 			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
3120Sstevel@tonic-gate 				lber_debug = ldap_debug;
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate #else
3150Sstevel@tonic-gate 			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
3160Sstevel@tonic-gate #endif
3170Sstevel@tonic-gate 			break;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 		case 'h':
3200Sstevel@tonic-gate 			host = optarg;
3210Sstevel@tonic-gate 			break;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		case 's':
3240Sstevel@tonic-gate 			dnsuffix = optarg;
3250Sstevel@tonic-gate 			break;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		case 'p':
3280Sstevel@tonic-gate 			port = atoi( optarg );
3290Sstevel@tonic-gate 			break;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS)
3320Sstevel@tonic-gate 		case 't':	/* copy ber's to given file */
3330Sstevel@tonic-gate 			copyfname = strdup( optarg );
3340Sstevel@tonic-gate 			copyoptions = LBER_TO_FILE;
3350Sstevel@tonic-gate 			break;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		case 'T':	/* only output ber's to given file */
3380Sstevel@tonic-gate 			copyfname = strdup( optarg );
3390Sstevel@tonic-gate 			copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
3400Sstevel@tonic-gate 			break;
3410Sstevel@tonic-gate #endif
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		default:
3440Sstevel@tonic-gate 		    ++errflg;
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if ( host == NULL && optind == argc - 1 ) {
3490Sstevel@tonic-gate 		host = argv[ optind ];
3500Sstevel@tonic-gate 		++optind;
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	if ( errflg || optind < argc - 1 ) {
3540Sstevel@tonic-gate 		fprintf( stderr, usage, argv[ 0 ] );
3550Sstevel@tonic-gate 		exit( 1 );
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	printf( "%s( %s, %d )\n", cldapflg ? "cldap_open" : "ldap_init",
3590Sstevel@tonic-gate 		host == NULL ? "(null)" : host, port );
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if ( cldapflg ) {
3620Sstevel@tonic-gate #ifdef CLDAP
3630Sstevel@tonic-gate 		ld = cldap_open( host, port );
3640Sstevel@tonic-gate #endif /* CLDAP */
3650Sstevel@tonic-gate 	} else {
3660Sstevel@tonic-gate 		ld = ldap_init( host, port );
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if ( ld == NULL ) {
3700Sstevel@tonic-gate 		perror( "ldap_init" );
3710Sstevel@tonic-gate 		exit(1);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS)
3750Sstevel@tonic-gate 	if ( copyfname != NULL ) {
3760Sstevel@tonic-gate 		if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
3770Sstevel@tonic-gate 		    0600 ))  == -1 ) {
3780Sstevel@tonic-gate 			perror( copyfname );
3790Sstevel@tonic-gate 			exit ( 1 );
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 		ld->ld_sb.sb_options = copyoptions;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate #endif
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	bound = 0;
3860Sstevel@tonic-gate 	timeout.tv_sec = 0;
3870Sstevel@tonic-gate 	timeout.tv_usec = 0;
3880Sstevel@tonic-gate 	timelimit.tv_sec = 0;
3890Sstevel@tonic-gate 	timelimit.tv_usec = 0;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	(void) memset( line, '\0', sizeof(line) );
392*13093SRoger.Faulkner@Oracle.COM 	while ( getaline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
3930Sstevel@tonic-gate 		command1 = line[0];
3940Sstevel@tonic-gate 		command2 = line[1];
3950Sstevel@tonic-gate 		command3 = line[2];
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		switch ( command1 ) {
3980Sstevel@tonic-gate 		case 'a':	/* add or abandon */
3990Sstevel@tonic-gate 			switch ( command2 ) {
4000Sstevel@tonic-gate 			case 'd':	/* add */
401*13093SRoger.Faulkner@Oracle.COM 				getaline( dn, sizeof(dn), stdin, "dn? " );
4020Sstevel@tonic-gate 				strcat( dn, dnsuffix );
4030Sstevel@tonic-gate 				if ( (attrs = get_modlist( NULL, "attr? ",
4040Sstevel@tonic-gate 				    "value? " )) == NULL )
4050Sstevel@tonic-gate 					break;
4060Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
4070Sstevel@tonic-gate 					if ((err = ldap_add_ext( ld, dn, attrs, NULL, NULL, &id )) != LDAP_SUCCESS )
4080Sstevel@tonic-gate 						printf( "Error in ldap_add_ext: %s\n", ldap_err2string(err) );
4090Sstevel@tonic-gate 					else
4100Sstevel@tonic-gate 						printf( "Add initiated with id %d\n", id );
4110Sstevel@tonic-gate 				}
4120Sstevel@tonic-gate 				else {
4130Sstevel@tonic-gate 					if ( (id = ldap_add( ld, dn, attrs )) == -1 )
4140Sstevel@tonic-gate 						ldap_perror( ld, "ldap_add" );
4150Sstevel@tonic-gate 					else
4160Sstevel@tonic-gate 						printf( "Add initiated with id %d\n", id );
4170Sstevel@tonic-gate 				}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 				break;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 			case 'b':	/* abandon */
422*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin, "msgid? " );
4230Sstevel@tonic-gate 				id = atoi( line );
4240Sstevel@tonic-gate 				if ( ldap_abandon( ld, id ) != 0 )
4250Sstevel@tonic-gate 					ldap_perror( ld, "ldap_abandon" );
4260Sstevel@tonic-gate 				else
4270Sstevel@tonic-gate 					printf( "Abandon successful\n" );
4280Sstevel@tonic-gate 				break;
4290Sstevel@tonic-gate 			default:
4300Sstevel@tonic-gate 				printf( "Possibilities: [ad]d, [ab]ort\n" );
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 			break;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		case 'b':	/* asynch bind */
4350Sstevel@tonic-gate #ifdef KERBEROS
436*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
4370Sstevel@tonic-gate 			    "method (0->simple, 1->krbv41, 2->krbv42)? " );
4380Sstevel@tonic-gate 			method = atoi( line ) | 0x80;
4390Sstevel@tonic-gate #else /* KERBEROS */
4400Sstevel@tonic-gate 			method = LDAP_AUTH_SIMPLE;
4410Sstevel@tonic-gate #endif /* KERBEROS */
442*13093SRoger.Faulkner@Oracle.COM 			getaline( dn, sizeof(dn), stdin, "dn? " );
4430Sstevel@tonic-gate 			strcat( dn, dnsuffix );
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 			if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
446*13093SRoger.Faulkner@Oracle.COM 				getaline( passwd, sizeof(passwd), stdin,
4470Sstevel@tonic-gate 				    "password? " );
4480Sstevel@tonic-gate 			else
4490Sstevel@tonic-gate 				passwd[0] = '\0';
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 			if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
4520Sstevel@tonic-gate 				fprintf( stderr, "ldap_bind failed\n" );
4530Sstevel@tonic-gate 				ldap_perror( ld, "ldap_bind" );
4540Sstevel@tonic-gate 			} else {
4550Sstevel@tonic-gate 				printf( "Bind initiated\n" );
4560Sstevel@tonic-gate 				bound = 1;
4570Sstevel@tonic-gate 			}
4580Sstevel@tonic-gate 			break;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		case 'B':	/* synch bind */
4610Sstevel@tonic-gate #ifdef KERBEROS
462*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
4630Sstevel@tonic-gate 			    "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
4640Sstevel@tonic-gate 			method = atoi( line );
4650Sstevel@tonic-gate 			if ( method == 3 )
4660Sstevel@tonic-gate 				method = LDAP_AUTH_KRBV4;
4670Sstevel@tonic-gate 			else
4680Sstevel@tonic-gate 				method = method | 0x80;
4690Sstevel@tonic-gate #else /* KERBEROS */
470*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
4710Sstevel@tonic-gate 					 "method 0->simple, 1->SASL? ");
4720Sstevel@tonic-gate 			method = atoi (line);
4730Sstevel@tonic-gate 			if (method == 1){
4740Sstevel@tonic-gate 				method = LDAP_AUTH_SASL;
475*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
4760Sstevel@tonic-gate 						 "mechanism 0->CRAM_MD5, 1->TLS? ");
4770Sstevel@tonic-gate 				theInt = atoi(line);
4780Sstevel@tonic-gate 				if (theInt == 0){
4790Sstevel@tonic-gate 					mechanism = LDAP_SASL_CRAM_MD5;
4800Sstevel@tonic-gate 				}
4810Sstevel@tonic-gate 				else{
4820Sstevel@tonic-gate 					mechanism = LDAP_SASL_X511_STRONG;
4830Sstevel@tonic-gate 				}
4840Sstevel@tonic-gate 			} else {
4850Sstevel@tonic-gate 				method = LDAP_AUTH_SIMPLE;
4860Sstevel@tonic-gate 			}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate #endif /* KERBEROS */
489*13093SRoger.Faulkner@Oracle.COM 			getaline( dn, sizeof(dn), stdin, "dn? " );
4900Sstevel@tonic-gate 			strcat( dn, dnsuffix );
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 			if ( dn[0] != '\0' )
493*13093SRoger.Faulkner@Oracle.COM 				getaline( passwd, sizeof(passwd), stdin,
4940Sstevel@tonic-gate 				    "password? " );
4950Sstevel@tonic-gate 			else
4960Sstevel@tonic-gate 				passwd[0] = '\0';
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 			if (method == LDAP_AUTH_SIMPLE) {
4990Sstevel@tonic-gate 				if ( ldap_bind_s( ld, dn, passwd, method ) !=
5000Sstevel@tonic-gate 					 LDAP_SUCCESS ) {
5010Sstevel@tonic-gate 					fprintf( stderr, "ldap_bind_s failed\n" );
5020Sstevel@tonic-gate 					ldap_perror( ld, "ldap_bind_s" );
5030Sstevel@tonic-gate 				} else {
5040Sstevel@tonic-gate 					printf( "Bind successful\n" );
5050Sstevel@tonic-gate 					bound = 1;
5060Sstevel@tonic-gate 				}
5070Sstevel@tonic-gate 			} else {
5080Sstevel@tonic-gate 				if (strcmp(mechanism, LDAP_SASL_CRAM_MD5) == 0){
5090Sstevel@tonic-gate 					cred.bv_val = passwd;
5100Sstevel@tonic-gate 					cred.bv_len = strlen(passwd);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 					if ( ldap_sasl_cram_md5_bind_s(ld, dn, &cred, NULL, NULL) != LDAP_SUCCESS ){
5130Sstevel@tonic-gate 						fprintf( stderr, "ldap_sasl_cram_md5_bind_s failed\n" );
5140Sstevel@tonic-gate 						ldap_perror( ld, "ldap_sasl_cram_md5_bind_s" );
5150Sstevel@tonic-gate 					} else {
5160Sstevel@tonic-gate 						printf ( "Bind successful\n");
5170Sstevel@tonic-gate 						bound = 1;
5180Sstevel@tonic-gate 					}
5190Sstevel@tonic-gate 				} else {
5200Sstevel@tonic-gate 					if (ldap_sasl_bind_s(ld, dn, mechanism, &cred, NULL, NULL, &srvcrds ) != LDAP_SUCCESS){
5210Sstevel@tonic-gate 						fprintf( stderr, "ldap_sasl_bind_s failed\n" );
5220Sstevel@tonic-gate 						ldap_perror( ld, "ldap_sasl_bind_s" );
5230Sstevel@tonic-gate 					}
5240Sstevel@tonic-gate 				}
5250Sstevel@tonic-gate 			}
5260Sstevel@tonic-gate 			break;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 		case 'c':	/* compare */
529*13093SRoger.Faulkner@Oracle.COM 			getaline( dn, sizeof(dn), stdin, "dn? " );
5300Sstevel@tonic-gate 			strcat( dn, dnsuffix );
531*13093SRoger.Faulkner@Oracle.COM 			getaline( attr, sizeof(attr), stdin, "attr? " );
532*13093SRoger.Faulkner@Oracle.COM 			getaline( value, sizeof(value), stdin, "value? " );
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 			if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
5350Sstevel@tonic-gate 				bv.bv_val = value;
5360Sstevel@tonic-gate 				bv.bv_len = strlen(value);
5370Sstevel@tonic-gate 				if ((err = ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &id )) != LDAP_SUCCESS )
5380Sstevel@tonic-gate 					printf( "Error in ldap_compare_ext: %s\n", ldap_err2string(err) );
5390Sstevel@tonic-gate 				else
5400Sstevel@tonic-gate 					printf( "Compare initiated with id %d\n", id );
5410Sstevel@tonic-gate 			} else {
5420Sstevel@tonic-gate 				if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
5430Sstevel@tonic-gate 					ldap_perror( ld, "ldap_compare" );
5440Sstevel@tonic-gate 				else
5450Sstevel@tonic-gate 					printf( "Compare initiated with id %d\n", id );
5460Sstevel@tonic-gate 			}
5470Sstevel@tonic-gate 			break;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		case 'd':	/* turn on debugging */
5500Sstevel@tonic-gate #ifdef LDAP_DEBUG
551*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "debug level? " );
5520Sstevel@tonic-gate 			ldap_debug = atoi( line );
5530Sstevel@tonic-gate 			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
5540Sstevel@tonic-gate 				lber_debug = ldap_debug;
5550Sstevel@tonic-gate 			}
5560Sstevel@tonic-gate #else
5570Sstevel@tonic-gate 			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
5580Sstevel@tonic-gate #endif
5590Sstevel@tonic-gate 			break;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		case 'E':	/* explode a dn */
562*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "dn? " );
5630Sstevel@tonic-gate 			exdn = ldap_explode_dn( line, 0 );
5640Sstevel@tonic-gate 			for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
5650Sstevel@tonic-gate 				printf( "\t%s\n", exdn[i] );
5660Sstevel@tonic-gate 			}
5670Sstevel@tonic-gate 			break;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		case 'g':	/* set next msgid */
570*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "msgid? " );
5710Sstevel@tonic-gate 			ld->ld_msgid = atoi( line );
5720Sstevel@tonic-gate 			break;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 		case 'v':	/* set version number */
575*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "version? " );
5760Sstevel@tonic-gate 			theInt = atoi(line);
5770Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &theInt);
5780Sstevel@tonic-gate 			break;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 		case 'm':	/* modify or modifyrdn */
5810Sstevel@tonic-gate 			if ( strncmp( line, "modify", 4 ) == 0 ) {
582*13093SRoger.Faulkner@Oracle.COM 				getaline( dn, sizeof(dn), stdin, "dn? " );
5830Sstevel@tonic-gate 				strcat( dn, dnsuffix );
5840Sstevel@tonic-gate 				if ( (mods = get_modlist(
5850Sstevel@tonic-gate 				    "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
5860Sstevel@tonic-gate 				    "attribute type? ", "attribute value? " ))
5870Sstevel@tonic-gate 				    == NULL )
5880Sstevel@tonic-gate 					break;
5890Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
5900Sstevel@tonic-gate 					if ((err = ldap_modify_ext( ld, dn, mods, NULL, NULL, &id )) != LDAP_SUCCESS )
5910Sstevel@tonic-gate 						printf( "Error in ldap_modify_ext: %s\n", ldap_err2string(err) );
5920Sstevel@tonic-gate 					else
5930Sstevel@tonic-gate 						printf( "Modify initiated with id %d\n", id );
5940Sstevel@tonic-gate 				}
5950Sstevel@tonic-gate 				else {
5960Sstevel@tonic-gate 					if ( (id = ldap_modify( ld, dn, mods )) == -1 )
5970Sstevel@tonic-gate 						ldap_perror( ld, "ldap_modify" );
5980Sstevel@tonic-gate 					else
5990Sstevel@tonic-gate 						printf( "Modify initiated with id %d\n", id );
6000Sstevel@tonic-gate 				}
6010Sstevel@tonic-gate 			} else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
602*13093SRoger.Faulkner@Oracle.COM 				getaline( dn, sizeof(dn), stdin, "dn? " );
6030Sstevel@tonic-gate 				strcat( dn, dnsuffix );
604*13093SRoger.Faulkner@Oracle.COM 				getaline( rdn, sizeof(rdn), stdin, "newrdn? " );
605*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin, "delete old rdn (0=>no, 1=>yes)?");
6060Sstevel@tonic-gate 				delrdn = atoi(line);
6070Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
6080Sstevel@tonic-gate 					if ((err = ldap_rename(ld, dn, rdn, NULL, delrdn, NULL,NULL, &id)) != LDAP_SUCCESS){
6090Sstevel@tonic-gate 						printf( "Error in ldap_rename (modrdn): %s\n", ldap_err2string(err));
6100Sstevel@tonic-gate 					}
6110Sstevel@tonic-gate 					else
6120Sstevel@tonic-gate 						printf( "Modrdn initiated with id %d\n", id );
6130Sstevel@tonic-gate 				}
6140Sstevel@tonic-gate 				else {
6150Sstevel@tonic-gate 					if ( (id = ldap_modrdn( ld, dn, rdn, delrdn )) == -1 )
6160Sstevel@tonic-gate 						ldap_perror( ld, "ldap_modrdn" );
6170Sstevel@tonic-gate 					else
6180Sstevel@tonic-gate 						printf( "Modrdn initiated with id %d\n", id );
6190Sstevel@tonic-gate 				}
6200Sstevel@tonic-gate 			} else {
6210Sstevel@tonic-gate 				printf( "Possibilities: [modi]fy, [modr]dn\n" );
6220Sstevel@tonic-gate 			}
6230Sstevel@tonic-gate 			break;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		case 'q':	/* quit */
6260Sstevel@tonic-gate #ifdef CLDAP
6270Sstevel@tonic-gate 			if ( cldapflg )
6280Sstevel@tonic-gate 				cldap_close( ld );
6290Sstevel@tonic-gate #endif /* CLDAP */
6300Sstevel@tonic-gate 			if ( !cldapflg )
6310Sstevel@tonic-gate 				ldap_unbind( ld );
6320Sstevel@tonic-gate 			exit( 0 );
6330Sstevel@tonic-gate 			break;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		case 'r':	/* result or remove */
6360Sstevel@tonic-gate 			switch ( command3 ) {
6370Sstevel@tonic-gate 			case 's':	/* result */
638*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
6390Sstevel@tonic-gate 				    "msgid (-1=>any)? " );
6400Sstevel@tonic-gate 				if ( line[0] == '\0' )
6410Sstevel@tonic-gate 					id = -1;
6420Sstevel@tonic-gate 				else
6430Sstevel@tonic-gate 					id = atoi( line );
644*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
6450Sstevel@tonic-gate 				    "all (0=>any, 1=>all)? " );
6460Sstevel@tonic-gate 				if ( line[0] == '\0' )
6470Sstevel@tonic-gate 					all = 1;
6480Sstevel@tonic-gate 				else
6490Sstevel@tonic-gate 					all = atoi( line );
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 				if (( msgtype = ldap_result( ld, id, all,
6520Sstevel@tonic-gate 				    resultusetimelimit ? &timelimit : &timeout, &res )) < 1 ) {
6530Sstevel@tonic-gate 					ldap_perror( ld, "ldap_result" );
6540Sstevel@tonic-gate 					break;
6550Sstevel@tonic-gate 				}
6560Sstevel@tonic-gate 				printf( "\nresult: msgtype %d msgid %d\n",
6570Sstevel@tonic-gate 				    msgtype, res->lm_msgid );
6580Sstevel@tonic-gate 				handle_result( ld, res );
6590Sstevel@tonic-gate 				if (all || msgtype == LDAP_RES_SEARCH_RESULT)
6600Sstevel@tonic-gate 					resultusetimelimit = 0;
6610Sstevel@tonic-gate 				res = NULLMSG;
6620Sstevel@tonic-gate 				break;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 			case 'm':	/* remove */
665*13093SRoger.Faulkner@Oracle.COM 				getaline( dn, sizeof(dn), stdin, "dn? " );
6660Sstevel@tonic-gate 				strcat( dn, dnsuffix );
6670Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
6680Sstevel@tonic-gate 					if ((err = ldap_delete_ext( ld, dn, NULL, NULL, &id )) != LDAP_SUCCESS )
6690Sstevel@tonic-gate 						printf( "Error in ldap_delete_ext: %s\n", ldap_err2string(err) );
6700Sstevel@tonic-gate 					else
6710Sstevel@tonic-gate 						printf( "Remove initiated with id %d\n", id );
6720Sstevel@tonic-gate 				} else {
6730Sstevel@tonic-gate 					if ( (id = ldap_delete( ld, dn )) == -1 )
6740Sstevel@tonic-gate 						ldap_perror( ld, "ldap_delete" );
6750Sstevel@tonic-gate 					else
6760Sstevel@tonic-gate 						printf( "Remove initiated with id %d\n", id );
6770Sstevel@tonic-gate 				}
6780Sstevel@tonic-gate 				break;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 			default:
6810Sstevel@tonic-gate 				printf( "Possibilities: [rem]ove, [res]ult\n" );
6820Sstevel@tonic-gate 				break;
6830Sstevel@tonic-gate 			}
6840Sstevel@tonic-gate 			break;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		case 's':	/* search */
687*13093SRoger.Faulkner@Oracle.COM 			getaline( dn, sizeof(dn), stdin, "searchbase? " );
6880Sstevel@tonic-gate 			strcat( dn, dnsuffix );
689*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
6900Sstevel@tonic-gate 			    "scope (0=Base, 1=One Level, 2=Subtree)? " );
6910Sstevel@tonic-gate 			scope = atoi( line );
692*13093SRoger.Faulkner@Oracle.COM 			getaline( filter, sizeof(filter), stdin,
6930Sstevel@tonic-gate 			    "search filter (e.g. sn=jones)? " );
6940Sstevel@tonic-gate 			types = get_list( "attrs to return? " );
695*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
6960Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
6970Sstevel@tonic-gate 			attrsonly = atoi( line );
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 			if ( cldapflg ) {
7000Sstevel@tonic-gate #ifdef CLDAP
701*13093SRoger.Faulkner@Oracle.COM 			    getaline( line, sizeof(line), stdin,
7020Sstevel@tonic-gate 				"Requestor DN (for logging)? " );
7030Sstevel@tonic-gate 			    if ( cldap_search_s( ld, dn, scope, filter, types,
7040Sstevel@tonic-gate 				    attrsonly, &res, line ) != 0 ) {
7050Sstevel@tonic-gate 				ldap_perror( ld, "cldap_search_s" );
7060Sstevel@tonic-gate 			    } else {
7070Sstevel@tonic-gate 				printf( "\nresult: msgid %d\n",
7080Sstevel@tonic-gate 				    res->lm_msgid );
7090Sstevel@tonic-gate 				handle_result( ld, res );
7100Sstevel@tonic-gate 				res = NULLMSG;
7110Sstevel@tonic-gate 			    }
7120Sstevel@tonic-gate #endif /* CLDAP */
7130Sstevel@tonic-gate 			} else {
7140Sstevel@tonic-gate 				theInt = 0;
7150Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
7160Sstevel@tonic-gate 					resultusetimelimit = 1;
717*13093SRoger.Faulkner@Oracle.COM 					getaline( line, sizeof(line), stdin,
7180Sstevel@tonic-gate 							 "ldap_search_ext (0=>no, 1=>yes - default: yes)? " );
7190Sstevel@tonic-gate 					if (line[0] == '\0')
7200Sstevel@tonic-gate 						theInt = 1;
7210Sstevel@tonic-gate 					else
7220Sstevel@tonic-gate 						theInt = atoi( line );
7230Sstevel@tonic-gate 				}
7240Sstevel@tonic-gate 				if (theInt){
725*13093SRoger.Faulkner@Oracle.COM 					getaline(line, sizeof(line), stdin, "time limit?");
7260Sstevel@tonic-gate 					timelimit.tv_sec = atoi(line);
7270Sstevel@tonic-gate 					resultusetimelimit = 1;
728*13093SRoger.Faulkner@Oracle.COM 					getaline(line, sizeof(line), stdin, "size limit?");
7290Sstevel@tonic-gate 					sizelimit = atoi(line);
7300Sstevel@tonic-gate 					if (( err = ldap_search_ext(ld, dn, scope, filter, types, attrsonly, NULL, NULL,
7310Sstevel@tonic-gate 												&timelimit, sizelimit, &id)) != LDAP_SUCCESS){
7320Sstevel@tonic-gate 						printf( "Error in ldap_search_ext: %s\n", ldap_err2string(err));
7330Sstevel@tonic-gate 					} else {
7340Sstevel@tonic-gate 						printf( "Search initiated with id %d\n", id );
7350Sstevel@tonic-gate 					}
7360Sstevel@tonic-gate 				} else {
7370Sstevel@tonic-gate 					if (( id = ldap_search( ld, dn, scope, filter,
7380Sstevel@tonic-gate 											types, attrsonly  )) == -1 ) {
7390Sstevel@tonic-gate 						ldap_perror( ld, "ldap_search" );
7400Sstevel@tonic-gate 					} else {
7410Sstevel@tonic-gate 						printf( "Search initiated with id %d\n", id );
7420Sstevel@tonic-gate 					}
7430Sstevel@tonic-gate 				}
7440Sstevel@tonic-gate 			}
7450Sstevel@tonic-gate 			free_list( types );
7460Sstevel@tonic-gate 			break;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		case 't':	/* set timeout value */
749*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "timeout? " );
7500Sstevel@tonic-gate 			timeout.tv_sec = atoi( line );
7510Sstevel@tonic-gate 			break;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		case 'U':	/* set ufn search prefix */
754*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "ufn prefix? " );
7550Sstevel@tonic-gate 			ldap_ufn_setprefix( ld, line );
7560Sstevel@tonic-gate 			break;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 		case 'u':	/* user friendly search w/optional timeout */
759*13093SRoger.Faulkner@Oracle.COM 			getaline( dn, sizeof(dn), stdin, "ufn? " );
7600Sstevel@tonic-gate 			strcat( dn, dnsuffix );
7610Sstevel@tonic-gate 			types = get_list( "attrs to return? " );
762*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
7630Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
7640Sstevel@tonic-gate 			attrsonly = atoi( line );
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 			if ( command2 == 't' ) {
7670Sstevel@tonic-gate 				id = ldap_ufn_search_c( ld, dn, types,
7680Sstevel@tonic-gate 				    attrsonly, &res, ldap_ufn_timeout,
7690Sstevel@tonic-gate 				    &timeout );
7700Sstevel@tonic-gate 			} else {
7710Sstevel@tonic-gate 				id = ldap_ufn_search_s( ld, dn, types,
7720Sstevel@tonic-gate 				    attrsonly, &res );
7730Sstevel@tonic-gate 			}
7740Sstevel@tonic-gate 			if ( res == NULL )
7750Sstevel@tonic-gate 				ldap_perror( ld, "ldap_ufn_search" );
7760Sstevel@tonic-gate 			else {
7770Sstevel@tonic-gate 				printf( "\nresult: err %d\n", id );
7780Sstevel@tonic-gate 				handle_result( ld, res );
7790Sstevel@tonic-gate 				res = NULLMSG;
7800Sstevel@tonic-gate 			}
7810Sstevel@tonic-gate 			free_list( types );
7820Sstevel@tonic-gate 			break;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		case 'l':	/* URL search */
785*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
7860Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
7870Sstevel@tonic-gate 			attrsonly = atoi( line );
788*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "LDAP URL? " );
7890Sstevel@tonic-gate 			if (( id = ldap_url_search( ld, line, attrsonly  ))
7900Sstevel@tonic-gate 				== -1 ) {
7910Sstevel@tonic-gate 			    ldap_perror( ld, "ldap_url_search" );
7920Sstevel@tonic-gate 			} else {
7930Sstevel@tonic-gate 			    printf( "URL search initiated with id %d\n", id );
7940Sstevel@tonic-gate 			}
7950Sstevel@tonic-gate 			break;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		case 'p':	/* parse LDAP URL */
798*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "LDAP URL? " );
7990Sstevel@tonic-gate 			if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
8000Sstevel@tonic-gate 			    fprintf( stderr, "ldap_url_parse: error %d\n", i );
8010Sstevel@tonic-gate 			} else {
8020Sstevel@tonic-gate 			    printf( "\t  host: " );
8030Sstevel@tonic-gate 			    if ( ludp->lud_host == NULL ) {
8040Sstevel@tonic-gate 				printf( "DEFAULT\n" );
8050Sstevel@tonic-gate 			    } else {
8060Sstevel@tonic-gate 				printf( "<%s>\n", ludp->lud_host );
8070Sstevel@tonic-gate 			    }
8080Sstevel@tonic-gate 			    printf( "\t  port: " );
8090Sstevel@tonic-gate 			    if ( ludp->lud_port == 0 ) {
8100Sstevel@tonic-gate 				printf( "DEFAULT\n" );
8110Sstevel@tonic-gate 			    } else {
8120Sstevel@tonic-gate 				printf( "%d\n", ludp->lud_port );
8130Sstevel@tonic-gate 			    }
8140Sstevel@tonic-gate 			    printf( "\t    dn: <%s>\n", ludp->lud_dn );
8150Sstevel@tonic-gate 			    printf( "\t attrs:" );
8160Sstevel@tonic-gate 			    if ( ludp->lud_attrs == NULL ) {
8170Sstevel@tonic-gate 				printf( " ALL" );
8180Sstevel@tonic-gate 			    } else {
8190Sstevel@tonic-gate 				for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
8200Sstevel@tonic-gate 				    printf( " <%s>", ludp->lud_attrs[ i ] );
8210Sstevel@tonic-gate 				}
8220Sstevel@tonic-gate 			    }
8230Sstevel@tonic-gate 			    printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_UNKNOWN ? "DEFAULT (base)" :
8240Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "ONE" :
8250Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
8260Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
8270Sstevel@tonic-gate 			    printf( "\tfilter: <%s>\n", ludp->lud_filter ? ludp->lud_filter : "NONE");
8280Sstevel@tonic-gate 				if (ludp->lud_extensions){
8290Sstevel@tonic-gate 					printf("\textensions: \n");
8300Sstevel@tonic-gate 					for (i = 0; ludp->lud_extensions[i] != NULL; i++)
8310Sstevel@tonic-gate 						printf("\t\t%s (%s)\n", ludp->lud_extensions[i]->lue_type,
8320Sstevel@tonic-gate 							   ludp->lud_extensions[i]->lue_iscritical ? "Critical" : "Non critical");
8330Sstevel@tonic-gate 				}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 			    ldap_free_urldesc( ludp );
8360Sstevel@tonic-gate 			}
8370Sstevel@tonic-gate 			    break;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		case 'n':	/* set dn suffix, for convenience */
840*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "DN suffix? " );
8410Sstevel@tonic-gate 			strcpy( dnsuffix, line );
8420Sstevel@tonic-gate 			break;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		case 'e':	/* enable cache */
8450Sstevel@tonic-gate #ifdef NO_CACHE
8460Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
8470Sstevel@tonic-gate #else /* NO_CACHE */
848*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
8490Sstevel@tonic-gate 			i = atoi( line );
850*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
8510Sstevel@tonic-gate 			if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
8520Sstevel@tonic-gate 				printf( "local cache is on\n" );
8530Sstevel@tonic-gate 			} else {
8540Sstevel@tonic-gate 				printf( "ldap_enable_cache failed\n" );
8550Sstevel@tonic-gate 			}
8560Sstevel@tonic-gate #endif /* NO_CACHE */
8570Sstevel@tonic-gate 			break;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		case 'x':	/* uncache entry */
8600Sstevel@tonic-gate #ifdef NO_CACHE
8610Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
8620Sstevel@tonic-gate #else /* NO_CACHE */
863*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "DN? " );
8640Sstevel@tonic-gate 			ldap_uncache_entry( ld, line );
8650Sstevel@tonic-gate #endif /* NO_CACHE */
8660Sstevel@tonic-gate 			break;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 		case 'X':	/* uncache request */
8690Sstevel@tonic-gate #ifdef NO_CACHE
8700Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
8710Sstevel@tonic-gate #else /* NO_CACHE */
872*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "request msgid? " );
8730Sstevel@tonic-gate 			ldap_uncache_request( ld, atoi( line ));
8740Sstevel@tonic-gate #endif /* NO_CACHE */
8750Sstevel@tonic-gate 			break;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		case 'o':	/* set ldap options */
878*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
8790Sstevel@tonic-gate 			theInt = atoi(line);
8800Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_DEREF, &theInt );
881*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "timelimit?" );
8820Sstevel@tonic-gate 			theInt = atoi(line);
8830Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_TIMELIMIT,  &theInt);
884*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "sizelimit?" );
8850Sstevel@tonic-gate 			theInt = atoi(line);
8860Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &theInt);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 			ld->ld_options = 0;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate #ifdef STR_TRANSLATION
891*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
8920Sstevel@tonic-gate 				"Automatic translation of T.61 strings (0=no, 1=yes)?" );
8930Sstevel@tonic-gate 			if ( atoi( line ) == 0 ) {
8940Sstevel@tonic-gate 				ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
8950Sstevel@tonic-gate 			} else {
8960Sstevel@tonic-gate 				ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
8970Sstevel@tonic-gate #ifdef LDAP_CHARSET_8859
898*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
8990Sstevel@tonic-gate 					"Translate to/from ISO-8859 (0=no, 1=yes?" );
9000Sstevel@tonic-gate 				if ( atoi( line ) != 0 ) {
9010Sstevel@tonic-gate 					ldap_set_string_translators( ld,
9020Sstevel@tonic-gate 					    ldap_8859_to_t61,
9030Sstevel@tonic-gate 					    ldap_t61_to_8859 );
9040Sstevel@tonic-gate 				}
9050Sstevel@tonic-gate #endif /* LDAP_CHARSET_8859 */
9060Sstevel@tonic-gate 			}
9070Sstevel@tonic-gate #endif /* STR_TRANSLATION */
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate #ifdef LDAP_DNS
910*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
9110Sstevel@tonic-gate 				"Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
9120Sstevel@tonic-gate 			if ( atoi( line ) != 0 ) {
9130Sstevel@tonic-gate 				ld->ld_options |= LDAP_OPT_DNS;
9140Sstevel@tonic-gate 			}
9150Sstevel@tonic-gate #endif /* LDAP_DNS */
9160Sstevel@tonic-gate 
917*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
9180Sstevel@tonic-gate 				"Recognize and chase referrals (0=no, 1=yes)?" );
9190Sstevel@tonic-gate 			if ( atoi( line ) != 0 ) {
9200Sstevel@tonic-gate 				theInt = LDAP_OPT_ON;
921*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
9220Sstevel@tonic-gate 						 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
9230Sstevel@tonic-gate 				if ( atoi( line ) != 0 ) {
9240Sstevel@tonic-gate 					ldap_set_option( ld, LDAP_OPT_REBIND_FN, bind_prompt );
9250Sstevel@tonic-gate 				}
9260Sstevel@tonic-gate 			} else {
9270Sstevel@tonic-gate 				theInt = LDAP_OPT_OFF;
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_REFERRALS, &theInt);
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		case 'k': /* Set some controls */
933*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin,
9340Sstevel@tonic-gate 					 "Set control: (0 for none, 1 for ManageDSA, 2 for preferredLang, 3 for BAD)?");
9350Sstevel@tonic-gate 			theInt = atoi(line);
9360Sstevel@tonic-gate 			switch (theInt){
9370Sstevel@tonic-gate 			case 0:
9380Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, NULL);
9390Sstevel@tonic-gate 				break;
9400Sstevel@tonic-gate 			case 1:
9410Sstevel@tonic-gate 				aCtrl.ldctl_oid = "2.16.840.1.113730.3.4.2";
9420Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = 1;
9430Sstevel@tonic-gate 				aCtrl.ldctl_value = NULL;
9440Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
9450Sstevel@tonic-gate 				break;
9460Sstevel@tonic-gate 			case 2:
947*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
9480Sstevel@tonic-gate 						 "Preferred Language Control : lang ?");
9490Sstevel@tonic-gate 				aCtrl.ldctl_oid = "1.3.6.1.4.1.1466.20035";
9500Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = 1;
9510Sstevel@tonic-gate 				bv.bv_val = strdup(line);
9520Sstevel@tonic-gate 				bv.bv_len = strlen(line);
9530Sstevel@tonic-gate 				aCtrl.ldctl_value = &bv;
9540Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
9550Sstevel@tonic-gate 				break;
9560Sstevel@tonic-gate 			default:
957*13093SRoger.Faulkner@Oracle.COM 				getaline( line, sizeof(line), stdin,
9580Sstevel@tonic-gate 						 "Bad Control is critical (0=false, 1=true)?");
9590Sstevel@tonic-gate 				aCtrl.ldctl_oid = "1.1.1.1.1.1";
9600Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = atoi(line);
9610Sstevel@tonic-gate 				aCtrl.ldctl_value = NULL;
9620Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
9630Sstevel@tonic-gate 				break;
9640Sstevel@tonic-gate 			}
9650Sstevel@tonic-gate 			break;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		case 'O':	/* set cache options */
9680Sstevel@tonic-gate #ifdef NO_CACHE
9690Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
9700Sstevel@tonic-gate #else /* NO_CACHE */
971*13093SRoger.Faulkner@Oracle.COM 			getaline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
9720Sstevel@tonic-gate 			switch( atoi( line )) {
9730Sstevel@tonic-gate 			case 0:
9740Sstevel@tonic-gate 				ldap_set_cache_options( ld, 0 );
9750Sstevel@tonic-gate 				break;
9760Sstevel@tonic-gate 			case 1:
9770Sstevel@tonic-gate 				ldap_set_cache_options( ld,
9780Sstevel@tonic-gate 					LDAP_CACHE_OPT_CACHENOERRS );
9790Sstevel@tonic-gate 				break;
9800Sstevel@tonic-gate 			case 2:
9810Sstevel@tonic-gate 				ldap_set_cache_options( ld,
9820Sstevel@tonic-gate 					LDAP_CACHE_OPT_CACHEALLERRS );
9830Sstevel@tonic-gate 				break;
9840Sstevel@tonic-gate 			default:
9850Sstevel@tonic-gate 				printf( "not a valid cache option\n" );
9860Sstevel@tonic-gate 			}
9870Sstevel@tonic-gate #endif /* NO_CACHE */
9880Sstevel@tonic-gate 			break;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 		case '?':	/* help */
9910Sstevel@tonic-gate     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
9920Sstevel@tonic-gate     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
9930Sstevel@tonic-gate     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
9940Sstevel@tonic-gate     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
9950Sstevel@tonic-gate     printf( "          [u]fn search  [ut]fn search with timeout\n" );
9960Sstevel@tonic-gate     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
9970Sstevel@tonic-gate     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
9980Sstevel@tonic-gate     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
9990Sstevel@tonic-gate     printf( "          [?]help       [o]ptions         [O]cache options\n" );
10000Sstevel@tonic-gate     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
10010Sstevel@tonic-gate 			break;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		default:
10040Sstevel@tonic-gate 			printf( "Invalid command.  Type ? for help.\n" );
10050Sstevel@tonic-gate 			break;
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		(void) memset( line, '\0', sizeof(line) );
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	return( 0 );
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate static void
handle_result(LDAP * ld,LDAPMessage * lm)10150Sstevel@tonic-gate handle_result( LDAP *ld, LDAPMessage *lm )
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	switch ( lm->lm_msgtype ) {
10180Sstevel@tonic-gate 	case LDAP_RES_COMPARE:
10190Sstevel@tonic-gate 		printf( "Compare result\n" );
10200Sstevel@tonic-gate 		print_ldap_result( ld, lm, "compare" );
10210Sstevel@tonic-gate 		break;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	case LDAP_RES_SEARCH_RESULT:
10240Sstevel@tonic-gate 		printf( "Search result\n" );
10250Sstevel@tonic-gate 		print_ldap_result( ld, lm, "search" );
10260Sstevel@tonic-gate 		break;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	case LDAP_RES_SEARCH_REFERENCE:
10290Sstevel@tonic-gate 		printf( "Search reference\n" );
10300Sstevel@tonic-gate 		print_search_entry( ld, lm );
10310Sstevel@tonic-gate 		break;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	case LDAP_RES_SEARCH_ENTRY:
10340Sstevel@tonic-gate 		printf( "Search entry\n" );
10350Sstevel@tonic-gate 		print_search_entry( ld, lm );
10360Sstevel@tonic-gate 		break;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	case LDAP_RES_ADD:
10390Sstevel@tonic-gate 		printf( "Add result\n" );
10400Sstevel@tonic-gate 		print_ldap_result( ld, lm, "add" );
10410Sstevel@tonic-gate 		break;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	case LDAP_RES_DELETE:
10440Sstevel@tonic-gate 		printf( "Delete result\n" );
10450Sstevel@tonic-gate 		print_ldap_result( ld, lm, "delete" );
10460Sstevel@tonic-gate 		break;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	case LDAP_RES_MODIFY:
10490Sstevel@tonic-gate 		printf( "Modify result\n" );
10500Sstevel@tonic-gate 		print_ldap_result( ld, lm, "modify" );
10510Sstevel@tonic-gate 		break;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	case LDAP_RES_MODRDN:
10540Sstevel@tonic-gate 		printf( "ModRDN result\n" );
10550Sstevel@tonic-gate 		print_ldap_result( ld, lm, "modrdn" );
10560Sstevel@tonic-gate 		break;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	case LDAP_RES_BIND:
10590Sstevel@tonic-gate 		printf( "Bind result\n" );
10600Sstevel@tonic-gate 		print_ldap_result( ld, lm, "bind" );
10610Sstevel@tonic-gate 		break;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	default:
10640Sstevel@tonic-gate 		printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
10650Sstevel@tonic-gate 		print_ldap_result( ld, lm, "unknown" );
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate static void
print_ldap_result(LDAP * ld,LDAPMessage * lm,char * s)10700Sstevel@tonic-gate print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate 	int rc, i;
10730Sstevel@tonic-gate 	int errCode;
10740Sstevel@tonic-gate 	char *matched = NULL, *errMsg = NULL, **referrals = NULL;
10750Sstevel@tonic-gate 	LDAPControl **srvctrls = NULL;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if ((rc = ldap_parse_result(ld, lm, &errCode, &matched, &errMsg, &referrals, &srvctrls, 0)) != LDAP_SUCCESS){
10780Sstevel@tonic-gate 		fprintf(stderr, "%s: error while parsing result (%s)\n", s, ldap_err2string(rc));
10790Sstevel@tonic-gate 		return;
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	fprintf(stderr, "%s: %s\n", s, ldap_err2string(errCode));
10840Sstevel@tonic-gate 	if (errCode == LDAP_REFERRAL){
10850Sstevel@tonic-gate 		fprintf(stderr, "\tReferrals returned: \n");
10860Sstevel@tonic-gate 		for (i = 0; referrals[i] != NULL; i++)
10870Sstevel@tonic-gate 			fprintf(stderr, "\t\t%s\n", referrals[i]);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	if (errMsg && *errMsg)
10900Sstevel@tonic-gate 		fprintf(stderr, "\tAdditional info: %s\n", errMsg);
10910Sstevel@tonic-gate 	free(errMsg);
10920Sstevel@tonic-gate 	if (NAME_ERROR(errCode) && matched && *matched){
10930Sstevel@tonic-gate 		fprintf(stderr, "\tMatched DN: %s\n", matched);
10940Sstevel@tonic-gate 		free(matched);
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 	if (srvctrls != NULL){
10970Sstevel@tonic-gate 		fprintf(stderr, "\tLDAPControls returned: \n");
10980Sstevel@tonic-gate 		for (i=0;srvctrls[i] != NULL; i++)
10990Sstevel@tonic-gate 			fprintf(stderr, "\t\t%s (%s)\n", srvctrls[i]->ldctl_oid, srvctrls[i]->ldctl_iscritical ? "Critical" : "Not critical");
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 	return;
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate static void
print_search_entry(LDAP * ld,LDAPMessage * res)11050Sstevel@tonic-gate print_search_entry( LDAP *ld, LDAPMessage *res )
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	BerElement	*ber;
11080Sstevel@tonic-gate 	char		*a, *dn, *ufn;
11090Sstevel@tonic-gate 	struct berval	**vals;
11100Sstevel@tonic-gate 	int		i;
11110Sstevel@tonic-gate 	LDAPMessage	*e;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	for ( e = ldap_first_message( ld, res ); e != NULLMSG;
11140Sstevel@tonic-gate 	    e = ldap_next_message( ld, e ) ) {
11150Sstevel@tonic-gate 		if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
11160Sstevel@tonic-gate 			break;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 		dn = ldap_get_dn( ld, e );
11190Sstevel@tonic-gate 		printf( "\tDN: %s\n", dn );
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		ufn = ldap_dn2ufn( dn );
11220Sstevel@tonic-gate 		printf( "\tUFN: %s\n", ufn );
11230Sstevel@tonic-gate 		free( dn );
11240Sstevel@tonic-gate 		free( ufn );
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		if ( e->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ){
11270Sstevel@tonic-gate 			char **urls = ldap_get_reference_urls(ld, e);
11280Sstevel@tonic-gate 			if (urls == NULL){
11290Sstevel@tonic-gate 				printf("\t\tError with references: %s\n", ldap_err2string(ld->ld_errno));
11300Sstevel@tonic-gate 			} else {
11310Sstevel@tonic-gate 				for (i=0;urls[i] != NULL;i++)
11320Sstevel@tonic-gate 					printf("\t\tURL: %s\n", urls[i]);
11330Sstevel@tonic-gate 			}
11340Sstevel@tonic-gate 		} else {
11350Sstevel@tonic-gate 			for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
11360Sstevel@tonic-gate 				  a = ldap_next_attribute( ld, e, ber ) ) {
11370Sstevel@tonic-gate 				printf( "\t\tATTR: %s\n", a );
11380Sstevel@tonic-gate 				if ( (vals = ldap_get_values_len( ld, e, a ))
11390Sstevel@tonic-gate 					 == NULL ) {
11400Sstevel@tonic-gate 					printf( "\t\t\t(no values)\n" );
11410Sstevel@tonic-gate 				} else {
11420Sstevel@tonic-gate 					for ( i = 0; vals[i] != NULL; i++ ) {
11430Sstevel@tonic-gate 						int	j, nonascii;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 						nonascii = 0;
11460Sstevel@tonic-gate 						for ( j = 0; j < vals[i]->bv_len; j++ )
11470Sstevel@tonic-gate 							if ( !isascii( vals[i]->bv_val[j] ) ) {
11480Sstevel@tonic-gate 							nonascii = 1;
11490Sstevel@tonic-gate 							break;
11500Sstevel@tonic-gate 							}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 						if ( nonascii ) {
11530Sstevel@tonic-gate 							printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
11540Sstevel@tonic-gate #ifdef BPRINT_NONASCII
11550Sstevel@tonic-gate 							lber_bprint( vals[i]->bv_val,
11560Sstevel@tonic-gate 										 vals[i]->bv_len );
11570Sstevel@tonic-gate #endif /* BPRINT_NONASCII */
11580Sstevel@tonic-gate 							continue;
11590Sstevel@tonic-gate 						}
11600Sstevel@tonic-gate 						printf( "\t\t\tlength (%ld) %s\n",
11610Sstevel@tonic-gate 								vals[i]->bv_len, vals[i]->bv_val );
11620Sstevel@tonic-gate 					}
11630Sstevel@tonic-gate 					ber_bvecfree( vals );
11640Sstevel@tonic-gate 				}
11650Sstevel@tonic-gate 			}
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
11700Sstevel@tonic-gate 	    || res->lm_chain != NULLMSG )
11710Sstevel@tonic-gate 		print_ldap_result( ld, res, "search" );
11720Sstevel@tonic-gate }
1173