1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc. All Rights Reserved
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  */
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #include <stdio.h>
10*0Sstevel@tonic-gate #include <ctype.h>
11*0Sstevel@tonic-gate #include <string.h>
12*0Sstevel@tonic-gate #include <sys/types.h>
13*0Sstevel@tonic-gate #include <sys/socket.h>
14*0Sstevel@tonic-gate #include <sys/time.h>
15*0Sstevel@tonic-gate #include <sys/stat.h>
16*0Sstevel@tonic-gate #include <sys/file.h>
17*0Sstevel@tonic-gate #include <fcntl.h>
18*0Sstevel@tonic-gate #include <unistd.h>
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include "lber.h"
21*0Sstevel@tonic-gate #include "ldap.h"
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #define MOD_USE_BVALS
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #ifdef NEEDPROTOS
26*0Sstevel@tonic-gate static void handle_result( LDAP *ld, LDAPMessage *lm );
27*0Sstevel@tonic-gate static void print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s );
28*0Sstevel@tonic-gate static void print_search_entry( LDAP *ld, LDAPMessage *res );
29*0Sstevel@tonic-gate static void free_list( char **list );
30*0Sstevel@tonic-gate #else
31*0Sstevel@tonic-gate static void handle_result();
32*0Sstevel@tonic-gate static void print_ldap_result();
33*0Sstevel@tonic-gate static void print_search_entry();
34*0Sstevel@tonic-gate static void free_list();
35*0Sstevel@tonic-gate #endif /* NEEDPROTOS */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #define NOCACHEERRMSG	"don't compile with -DNO_CACHE if you desire local caching"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate char *dnsuffix;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static char *
42*0Sstevel@tonic-gate getline( char *line, int len, FILE *fp, char *prompt )
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	printf(prompt);
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	if ( fgets( line, len, fp ) == NULL )
47*0Sstevel@tonic-gate 		return( NULL );
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	line[ strlen( line ) - 1 ] = '\0';
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	return( line );
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static char **
55*0Sstevel@tonic-gate get_list( char *prompt )
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	static char	buf[256];
58*0Sstevel@tonic-gate 	int		num;
59*0Sstevel@tonic-gate 	char		**result;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	num = 0;
62*0Sstevel@tonic-gate 	result = (char **) 0;
63*0Sstevel@tonic-gate 	while ( 1 ) {
64*0Sstevel@tonic-gate 		getline( buf, sizeof(buf), stdin, prompt );
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 		if ( *buf == '\0' )
67*0Sstevel@tonic-gate 			break;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 		if ( result == (char **) 0 )
70*0Sstevel@tonic-gate 			result = (char **) malloc( sizeof(char *) );
71*0Sstevel@tonic-gate 		else
72*0Sstevel@tonic-gate 			result = (char **) realloc( result,
73*0Sstevel@tonic-gate 			    sizeof(char *) * (num + 1) );
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 		result[num++] = (char *) strdup( buf );
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 	if ( result == (char **) 0 )
78*0Sstevel@tonic-gate 		return( NULL );
79*0Sstevel@tonic-gate 	result = (char **) realloc( result, sizeof(char *) * (num + 1) );
80*0Sstevel@tonic-gate 	result[num] = NULL;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	return( result );
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static void
87*0Sstevel@tonic-gate free_list( char **list )
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	int	i;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if ( list != NULL ) {
92*0Sstevel@tonic-gate 		for ( i = 0; list[ i ] != NULL; ++i ) {
93*0Sstevel@tonic-gate 			free( list[ i ] );
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 		free( (char *)list );
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate #ifdef MOD_USE_BVALS
101*0Sstevel@tonic-gate static int
102*0Sstevel@tonic-gate file_read( char *path, struct berval *bv )
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	FILE		*fp;
105*0Sstevel@tonic-gate 	long		rlen;
106*0Sstevel@tonic-gate 	int		eof;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	if (( fp = fopen( path, "r" )) == NULL ) {
109*0Sstevel@tonic-gate 	    	perror( path );
110*0Sstevel@tonic-gate 		return( -1 );
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
114*0Sstevel@tonic-gate 		perror( path );
115*0Sstevel@tonic-gate 		fclose( fp );
116*0Sstevel@tonic-gate 		return( -1 );
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	bv->bv_len = ftell( fp );
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
122*0Sstevel@tonic-gate 		perror( "malloc" );
123*0Sstevel@tonic-gate 		fclose( fp );
124*0Sstevel@tonic-gate 		return( -1 );
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
128*0Sstevel@tonic-gate 		perror( path );
129*0Sstevel@tonic-gate 		fclose( fp );
130*0Sstevel@tonic-gate 		return( -1 );
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
134*0Sstevel@tonic-gate 	eof = feof( fp );
135*0Sstevel@tonic-gate 	fclose( fp );
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	if ( rlen != bv->bv_len ) {
138*0Sstevel@tonic-gate 		perror( path );
139*0Sstevel@tonic-gate 		free( bv->bv_val );
140*0Sstevel@tonic-gate 		return( -1 );
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	return( bv->bv_len );
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate static LDAPMod **
149*0Sstevel@tonic-gate get_modlist( char *prompt1, char *prompt2, char *prompt3 )
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate 	static char	buf[256];
152*0Sstevel@tonic-gate 	int		num;
153*0Sstevel@tonic-gate 	LDAPMod		tmp;
154*0Sstevel@tonic-gate 	LDAPMod		**result;
155*0Sstevel@tonic-gate #ifdef MOD_USE_BVALS
156*0Sstevel@tonic-gate 	struct berval	**bvals;
157*0Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	num = 0;
160*0Sstevel@tonic-gate 	result = NULL;
161*0Sstevel@tonic-gate 	while ( 1 ) {
162*0Sstevel@tonic-gate 		if ( prompt1 ) {
163*0Sstevel@tonic-gate 			getline( buf, sizeof(buf), stdin, prompt1 );
164*0Sstevel@tonic-gate 			tmp.mod_op = atoi( buf );
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 			if ( tmp.mod_op == -1 || buf[0] == '\0' )
167*0Sstevel@tonic-gate 				break;
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 		getline( buf, sizeof(buf), stdin, prompt2 );
171*0Sstevel@tonic-gate 		if ( buf[0] == '\0' )
172*0Sstevel@tonic-gate 			break;
173*0Sstevel@tonic-gate 		tmp.mod_type = strdup( buf );
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		tmp.mod_values = get_list( prompt3 );
176*0Sstevel@tonic-gate #ifdef MOD_USE_BVALS
177*0Sstevel@tonic-gate 		if ( tmp.mod_values != NULL ) {
178*0Sstevel@tonic-gate 			int	i;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 			for ( i = 0; tmp.mod_values[i] != NULL; ++i )
181*0Sstevel@tonic-gate 				;
182*0Sstevel@tonic-gate 			bvals = (struct berval **)calloc( i + 1,
183*0Sstevel@tonic-gate 			    sizeof( struct berval *));
184*0Sstevel@tonic-gate 			for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
185*0Sstevel@tonic-gate 				bvals[i] = (struct berval *)malloc(
186*0Sstevel@tonic-gate 				    sizeof( struct berval ));
187*0Sstevel@tonic-gate 				if ( strncmp( tmp.mod_values[i], "{FILE}",
188*0Sstevel@tonic-gate 				    6 ) == 0 ) {
189*0Sstevel@tonic-gate 					if ( file_read( tmp.mod_values[i] + 6,
190*0Sstevel@tonic-gate 					    bvals[i] ) < 0 ) {
191*0Sstevel@tonic-gate 						return( NULL );
192*0Sstevel@tonic-gate 					}
193*0Sstevel@tonic-gate 				} else {
194*0Sstevel@tonic-gate 					bvals[i]->bv_val = tmp.mod_values[i];
195*0Sstevel@tonic-gate 					bvals[i]->bv_len =
196*0Sstevel@tonic-gate 					    strlen( tmp.mod_values[i] );
197*0Sstevel@tonic-gate 				}
198*0Sstevel@tonic-gate 			}
199*0Sstevel@tonic-gate 			tmp.mod_bvalues = bvals;
200*0Sstevel@tonic-gate 			tmp.mod_op |= LDAP_MOD_BVALUES;
201*0Sstevel@tonic-gate 		}
202*0Sstevel@tonic-gate #endif /* MOD_USE_BVALS */
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		if ( result == NULL )
205*0Sstevel@tonic-gate 			result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
206*0Sstevel@tonic-gate 		else
207*0Sstevel@tonic-gate 			result = (LDAPMod **) realloc( result,
208*0Sstevel@tonic-gate 			    sizeof(LDAPMod *) * (num + 1) );
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 		result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
211*0Sstevel@tonic-gate 		*(result[num]) = tmp;	/* struct copy */
212*0Sstevel@tonic-gate 		num++;
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 	if ( result == NULL )
215*0Sstevel@tonic-gate 		return( NULL );
216*0Sstevel@tonic-gate 	result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
217*0Sstevel@tonic-gate 	result[num] = NULL;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	return( result );
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate int
224*0Sstevel@tonic-gate bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
225*0Sstevel@tonic-gate 	int freeit )
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	static char	dn[256], passwd[256];
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if ( !freeit ) {
230*0Sstevel@tonic-gate #ifdef KERBEROS
231*0Sstevel@tonic-gate 		getline( dn, sizeof(dn), stdin,
232*0Sstevel@tonic-gate 		    "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
233*0Sstevel@tonic-gate 		if (( *authmethodp = atoi( dn )) == 3 ) {
234*0Sstevel@tonic-gate 			*authmethodp = LDAP_AUTH_KRBV4;
235*0Sstevel@tonic-gate 		} else {
236*0Sstevel@tonic-gate 			*authmethodp |= 0x80;
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate #else /* KERBEROS */
239*0Sstevel@tonic-gate 		*authmethodp = LDAP_AUTH_SIMPLE;
240*0Sstevel@tonic-gate #endif /* KERBEROS */
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		getline( dn, sizeof(dn), stdin, "re-bind dn? " );
243*0Sstevel@tonic-gate 		strcat( dn, dnsuffix );
244*0Sstevel@tonic-gate 		*dnp = dn;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 		if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
247*0Sstevel@tonic-gate 			getline( passwd, sizeof(passwd), stdin,
248*0Sstevel@tonic-gate 			    "re-bind password? " );
249*0Sstevel@tonic-gate 		} else {
250*0Sstevel@tonic-gate 			passwd[0] = '\0';
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 		*passwdp = passwd;
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	return( LDAP_SUCCESS );
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate int
260*0Sstevel@tonic-gate main(int argc, char **argv )
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	LDAP	*ld;
263*0Sstevel@tonic-gate 	int		i, c, port, cldapflg, errflg, method, id,
264*0Sstevel@tonic-gate 		msgtype, delrdn, theInt, sizelimit, err;
265*0Sstevel@tonic-gate 	char	line[256], command1, command2, command3;
266*0Sstevel@tonic-gate 	char	passwd[64], dn[256], rdn[64], attr[64], value[256];
267*0Sstevel@tonic-gate 	char	filter[256], *host, **types;
268*0Sstevel@tonic-gate 	char 	*mechanism;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	char	**exdn;
271*0Sstevel@tonic-gate 	char	*usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
272*0Sstevel@tonic-gate 	int		bound, all, scope, attrsonly;
273*0Sstevel@tonic-gate 	LDAPMessage	*res;
274*0Sstevel@tonic-gate 	LDAPMod	**mods, **attrs;
275*0Sstevel@tonic-gate 	struct timeval	timeout, timelimit;
276*0Sstevel@tonic-gate 	char	*copyfname = NULL;
277*0Sstevel@tonic-gate 	int		copyoptions = 0, resultusetimelimit = 0;
278*0Sstevel@tonic-gate 	LDAPURLDesc	*ludp;
279*0Sstevel@tonic-gate 	struct berval bv, cred, *srvcrds = NULL;
280*0Sstevel@tonic-gate 	extern char	*optarg;
281*0Sstevel@tonic-gate 	extern int	optind;
282*0Sstevel@tonic-gate 	LDAPControl *ctrls[2];
283*0Sstevel@tonic-gate 	LDAPControl aCtrl;
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate #ifdef MACOS
287*0Sstevel@tonic-gate 	if (( argv = get_list( "cmd line arg?" )) == NULL ) {
288*0Sstevel@tonic-gate 		exit( 1 );
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 	for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
291*0Sstevel@tonic-gate 		;
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate #endif /* MACOS */
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	host = NULL;
296*0Sstevel@tonic-gate 	port = LDAP_PORT;
297*0Sstevel@tonic-gate 	dnsuffix = "";
298*0Sstevel@tonic-gate 	cldapflg = errflg = 0;
299*0Sstevel@tonic-gate 	ctrls[0] = &aCtrl;
300*0Sstevel@tonic-gate 	ctrls[1] = NULL;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
303*0Sstevel@tonic-gate 		switch( c ) {
304*0Sstevel@tonic-gate 		case 'u':
305*0Sstevel@tonic-gate #ifdef CLDAP
306*0Sstevel@tonic-gate 			cldapflg++;
307*0Sstevel@tonic-gate #else /* CLDAP */
308*0Sstevel@tonic-gate 			printf( "Compile with -DCLDAP for UDP support\n" );
309*0Sstevel@tonic-gate #endif /* CLDAP */
310*0Sstevel@tonic-gate 			break;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		case 'd':
313*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
314*0Sstevel@tonic-gate 			ldap_debug = atoi( optarg );
315*0Sstevel@tonic-gate 			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
316*0Sstevel@tonic-gate 				lber_debug = ldap_debug;
317*0Sstevel@tonic-gate 			}
318*0Sstevel@tonic-gate #else
319*0Sstevel@tonic-gate 			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
320*0Sstevel@tonic-gate #endif
321*0Sstevel@tonic-gate 			break;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 		case 'h':
324*0Sstevel@tonic-gate 			host = optarg;
325*0Sstevel@tonic-gate 			break;
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 		case 's':
328*0Sstevel@tonic-gate 			dnsuffix = optarg;
329*0Sstevel@tonic-gate 			break;
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		case 'p':
332*0Sstevel@tonic-gate 			port = atoi( optarg );
333*0Sstevel@tonic-gate 			break;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS)
336*0Sstevel@tonic-gate 		case 't':	/* copy ber's to given file */
337*0Sstevel@tonic-gate 			copyfname = strdup( optarg );
338*0Sstevel@tonic-gate 			copyoptions = LBER_TO_FILE;
339*0Sstevel@tonic-gate 			break;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 		case 'T':	/* only output ber's to given file */
342*0Sstevel@tonic-gate 			copyfname = strdup( optarg );
343*0Sstevel@tonic-gate 			copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
344*0Sstevel@tonic-gate 			break;
345*0Sstevel@tonic-gate #endif
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 		default:
348*0Sstevel@tonic-gate 		    ++errflg;
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	if ( host == NULL && optind == argc - 1 ) {
353*0Sstevel@tonic-gate 		host = argv[ optind ];
354*0Sstevel@tonic-gate 		++optind;
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	if ( errflg || optind < argc - 1 ) {
358*0Sstevel@tonic-gate 		fprintf( stderr, usage, argv[ 0 ] );
359*0Sstevel@tonic-gate 		exit( 1 );
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	printf( "%s( %s, %d )\n", cldapflg ? "cldap_open" : "ldap_init",
363*0Sstevel@tonic-gate 		host == NULL ? "(null)" : host, port );
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	if ( cldapflg ) {
366*0Sstevel@tonic-gate #ifdef CLDAP
367*0Sstevel@tonic-gate 		ld = cldap_open( host, port );
368*0Sstevel@tonic-gate #endif /* CLDAP */
369*0Sstevel@tonic-gate 	} else {
370*0Sstevel@tonic-gate 		ld = ldap_init( host, port );
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	if ( ld == NULL ) {
374*0Sstevel@tonic-gate 		perror( "ldap_init" );
375*0Sstevel@tonic-gate 		exit(1);
376*0Sstevel@tonic-gate 	}
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS)
379*0Sstevel@tonic-gate 	if ( copyfname != NULL ) {
380*0Sstevel@tonic-gate 		if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
381*0Sstevel@tonic-gate 		    0600 ))  == -1 ) {
382*0Sstevel@tonic-gate 			perror( copyfname );
383*0Sstevel@tonic-gate 			exit ( 1 );
384*0Sstevel@tonic-gate 		}
385*0Sstevel@tonic-gate 		ld->ld_sb.sb_options = copyoptions;
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate #endif
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	bound = 0;
390*0Sstevel@tonic-gate 	timeout.tv_sec = 0;
391*0Sstevel@tonic-gate 	timeout.tv_usec = 0;
392*0Sstevel@tonic-gate 	timelimit.tv_sec = 0;
393*0Sstevel@tonic-gate 	timelimit.tv_usec = 0;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	(void) memset( line, '\0', sizeof(line) );
396*0Sstevel@tonic-gate 	while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
397*0Sstevel@tonic-gate 		command1 = line[0];
398*0Sstevel@tonic-gate 		command2 = line[1];
399*0Sstevel@tonic-gate 		command3 = line[2];
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		switch ( command1 ) {
402*0Sstevel@tonic-gate 		case 'a':	/* add or abandon */
403*0Sstevel@tonic-gate 			switch ( command2 ) {
404*0Sstevel@tonic-gate 			case 'd':	/* add */
405*0Sstevel@tonic-gate 				getline( dn, sizeof(dn), stdin, "dn? " );
406*0Sstevel@tonic-gate 				strcat( dn, dnsuffix );
407*0Sstevel@tonic-gate 				if ( (attrs = get_modlist( NULL, "attr? ",
408*0Sstevel@tonic-gate 				    "value? " )) == NULL )
409*0Sstevel@tonic-gate 					break;
410*0Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
411*0Sstevel@tonic-gate 					if ((err = ldap_add_ext( ld, dn, attrs, NULL, NULL, &id )) != LDAP_SUCCESS )
412*0Sstevel@tonic-gate 						printf( "Error in ldap_add_ext: %s\n", ldap_err2string(err) );
413*0Sstevel@tonic-gate 					else
414*0Sstevel@tonic-gate 						printf( "Add initiated with id %d\n", id );
415*0Sstevel@tonic-gate 				}
416*0Sstevel@tonic-gate 				else {
417*0Sstevel@tonic-gate 					if ( (id = ldap_add( ld, dn, attrs )) == -1 )
418*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_add" );
419*0Sstevel@tonic-gate 					else
420*0Sstevel@tonic-gate 						printf( "Add initiated with id %d\n", id );
421*0Sstevel@tonic-gate 				}
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 				break;
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 			case 'b':	/* abandon */
426*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin, "msgid? " );
427*0Sstevel@tonic-gate 				id = atoi( line );
428*0Sstevel@tonic-gate 				if ( ldap_abandon( ld, id ) != 0 )
429*0Sstevel@tonic-gate 					ldap_perror( ld, "ldap_abandon" );
430*0Sstevel@tonic-gate 				else
431*0Sstevel@tonic-gate 					printf( "Abandon successful\n" );
432*0Sstevel@tonic-gate 				break;
433*0Sstevel@tonic-gate 			default:
434*0Sstevel@tonic-gate 				printf( "Possibilities: [ad]d, [ab]ort\n" );
435*0Sstevel@tonic-gate 			}
436*0Sstevel@tonic-gate 			break;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 		case 'b':	/* asynch bind */
439*0Sstevel@tonic-gate #ifdef KERBEROS
440*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
441*0Sstevel@tonic-gate 			    "method (0->simple, 1->krbv41, 2->krbv42)? " );
442*0Sstevel@tonic-gate 			method = atoi( line ) | 0x80;
443*0Sstevel@tonic-gate #else /* KERBEROS */
444*0Sstevel@tonic-gate 			method = LDAP_AUTH_SIMPLE;
445*0Sstevel@tonic-gate #endif /* KERBEROS */
446*0Sstevel@tonic-gate 			getline( dn, sizeof(dn), stdin, "dn? " );
447*0Sstevel@tonic-gate 			strcat( dn, dnsuffix );
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 			if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
450*0Sstevel@tonic-gate 				getline( passwd, sizeof(passwd), stdin,
451*0Sstevel@tonic-gate 				    "password? " );
452*0Sstevel@tonic-gate 			else
453*0Sstevel@tonic-gate 				passwd[0] = '\0';
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 			if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
456*0Sstevel@tonic-gate 				fprintf( stderr, "ldap_bind failed\n" );
457*0Sstevel@tonic-gate 				ldap_perror( ld, "ldap_bind" );
458*0Sstevel@tonic-gate 			} else {
459*0Sstevel@tonic-gate 				printf( "Bind initiated\n" );
460*0Sstevel@tonic-gate 				bound = 1;
461*0Sstevel@tonic-gate 			}
462*0Sstevel@tonic-gate 			break;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 		case 'B':	/* synch bind */
465*0Sstevel@tonic-gate #ifdef KERBEROS
466*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
467*0Sstevel@tonic-gate 			    "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
468*0Sstevel@tonic-gate 			method = atoi( line );
469*0Sstevel@tonic-gate 			if ( method == 3 )
470*0Sstevel@tonic-gate 				method = LDAP_AUTH_KRBV4;
471*0Sstevel@tonic-gate 			else
472*0Sstevel@tonic-gate 				method = method | 0x80;
473*0Sstevel@tonic-gate #else /* KERBEROS */
474*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
475*0Sstevel@tonic-gate 					 "method 0->simple, 1->SASL? ");
476*0Sstevel@tonic-gate 			method = atoi (line);
477*0Sstevel@tonic-gate 			if (method == 1){
478*0Sstevel@tonic-gate 				method = LDAP_AUTH_SASL;
479*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
480*0Sstevel@tonic-gate 						 "mechanism 0->CRAM_MD5, 1->TLS? ");
481*0Sstevel@tonic-gate 				theInt = atoi(line);
482*0Sstevel@tonic-gate 				if (theInt == 0){
483*0Sstevel@tonic-gate 					mechanism = LDAP_SASL_CRAM_MD5;
484*0Sstevel@tonic-gate 				}
485*0Sstevel@tonic-gate 				else{
486*0Sstevel@tonic-gate 					mechanism = LDAP_SASL_X511_STRONG;
487*0Sstevel@tonic-gate 				}
488*0Sstevel@tonic-gate 			} else {
489*0Sstevel@tonic-gate 				method = LDAP_AUTH_SIMPLE;
490*0Sstevel@tonic-gate 			}
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate #endif /* KERBEROS */
493*0Sstevel@tonic-gate 			getline( dn, sizeof(dn), stdin, "dn? " );
494*0Sstevel@tonic-gate 			strcat( dn, dnsuffix );
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 			if ( dn[0] != '\0' )
497*0Sstevel@tonic-gate 				getline( passwd, sizeof(passwd), stdin,
498*0Sstevel@tonic-gate 				    "password? " );
499*0Sstevel@tonic-gate 			else
500*0Sstevel@tonic-gate 				passwd[0] = '\0';
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 			if (method == LDAP_AUTH_SIMPLE) {
503*0Sstevel@tonic-gate 				if ( ldap_bind_s( ld, dn, passwd, method ) !=
504*0Sstevel@tonic-gate 					 LDAP_SUCCESS ) {
505*0Sstevel@tonic-gate 					fprintf( stderr, "ldap_bind_s failed\n" );
506*0Sstevel@tonic-gate 					ldap_perror( ld, "ldap_bind_s" );
507*0Sstevel@tonic-gate 				} else {
508*0Sstevel@tonic-gate 					printf( "Bind successful\n" );
509*0Sstevel@tonic-gate 					bound = 1;
510*0Sstevel@tonic-gate 				}
511*0Sstevel@tonic-gate 			} else {
512*0Sstevel@tonic-gate 				if (strcmp(mechanism, LDAP_SASL_CRAM_MD5) == 0){
513*0Sstevel@tonic-gate 					cred.bv_val = passwd;
514*0Sstevel@tonic-gate 					cred.bv_len = strlen(passwd);
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 					if ( ldap_sasl_cram_md5_bind_s(ld, dn, &cred, NULL, NULL) != LDAP_SUCCESS ){
517*0Sstevel@tonic-gate 						fprintf( stderr, "ldap_sasl_cram_md5_bind_s failed\n" );
518*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_sasl_cram_md5_bind_s" );
519*0Sstevel@tonic-gate 					} else {
520*0Sstevel@tonic-gate 						printf ( "Bind successful\n");
521*0Sstevel@tonic-gate 						bound = 1;
522*0Sstevel@tonic-gate 					}
523*0Sstevel@tonic-gate 				} else {
524*0Sstevel@tonic-gate 					if (ldap_sasl_bind_s(ld, dn, mechanism, &cred, NULL, NULL, &srvcrds ) != LDAP_SUCCESS){
525*0Sstevel@tonic-gate 						fprintf( stderr, "ldap_sasl_bind_s failed\n" );
526*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_sasl_bind_s" );
527*0Sstevel@tonic-gate 					}
528*0Sstevel@tonic-gate 				}
529*0Sstevel@tonic-gate 			}
530*0Sstevel@tonic-gate 			break;
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		case 'c':	/* compare */
533*0Sstevel@tonic-gate 			getline( dn, sizeof(dn), stdin, "dn? " );
534*0Sstevel@tonic-gate 			strcat( dn, dnsuffix );
535*0Sstevel@tonic-gate 			getline( attr, sizeof(attr), stdin, "attr? " );
536*0Sstevel@tonic-gate 			getline( value, sizeof(value), stdin, "value? " );
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 			if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
539*0Sstevel@tonic-gate 				bv.bv_val = value;
540*0Sstevel@tonic-gate 				bv.bv_len = strlen(value);
541*0Sstevel@tonic-gate 				if ((err = ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &id )) != LDAP_SUCCESS )
542*0Sstevel@tonic-gate 					printf( "Error in ldap_compare_ext: %s\n", ldap_err2string(err) );
543*0Sstevel@tonic-gate 				else
544*0Sstevel@tonic-gate 					printf( "Compare initiated with id %d\n", id );
545*0Sstevel@tonic-gate 			} else {
546*0Sstevel@tonic-gate 				if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
547*0Sstevel@tonic-gate 					ldap_perror( ld, "ldap_compare" );
548*0Sstevel@tonic-gate 				else
549*0Sstevel@tonic-gate 					printf( "Compare initiated with id %d\n", id );
550*0Sstevel@tonic-gate 			}
551*0Sstevel@tonic-gate 			break;
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 		case 'd':	/* turn on debugging */
554*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
555*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "debug level? " );
556*0Sstevel@tonic-gate 			ldap_debug = atoi( line );
557*0Sstevel@tonic-gate 			if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
558*0Sstevel@tonic-gate 				lber_debug = ldap_debug;
559*0Sstevel@tonic-gate 			}
560*0Sstevel@tonic-gate #else
561*0Sstevel@tonic-gate 			printf( "Compile with -DLDAP_DEBUG for debugging\n" );
562*0Sstevel@tonic-gate #endif
563*0Sstevel@tonic-gate 			break;
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate 		case 'E':	/* explode a dn */
566*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "dn? " );
567*0Sstevel@tonic-gate 			exdn = ldap_explode_dn( line, 0 );
568*0Sstevel@tonic-gate 			for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
569*0Sstevel@tonic-gate 				printf( "\t%s\n", exdn[i] );
570*0Sstevel@tonic-gate 			}
571*0Sstevel@tonic-gate 			break;
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 		case 'g':	/* set next msgid */
574*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "msgid? " );
575*0Sstevel@tonic-gate 			ld->ld_msgid = atoi( line );
576*0Sstevel@tonic-gate 			break;
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 		case 'v':	/* set version number */
579*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "version? " );
580*0Sstevel@tonic-gate 			theInt = atoi(line);
581*0Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &theInt);
582*0Sstevel@tonic-gate 			break;
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 		case 'm':	/* modify or modifyrdn */
585*0Sstevel@tonic-gate 			if ( strncmp( line, "modify", 4 ) == 0 ) {
586*0Sstevel@tonic-gate 				getline( dn, sizeof(dn), stdin, "dn? " );
587*0Sstevel@tonic-gate 				strcat( dn, dnsuffix );
588*0Sstevel@tonic-gate 				if ( (mods = get_modlist(
589*0Sstevel@tonic-gate 				    "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
590*0Sstevel@tonic-gate 				    "attribute type? ", "attribute value? " ))
591*0Sstevel@tonic-gate 				    == NULL )
592*0Sstevel@tonic-gate 					break;
593*0Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
594*0Sstevel@tonic-gate 					if ((err = ldap_modify_ext( ld, dn, mods, NULL, NULL, &id )) != LDAP_SUCCESS )
595*0Sstevel@tonic-gate 						printf( "Error in ldap_modify_ext: %s\n", ldap_err2string(err) );
596*0Sstevel@tonic-gate 					else
597*0Sstevel@tonic-gate 						printf( "Modify initiated with id %d\n", id );
598*0Sstevel@tonic-gate 				}
599*0Sstevel@tonic-gate 				else {
600*0Sstevel@tonic-gate 					if ( (id = ldap_modify( ld, dn, mods )) == -1 )
601*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_modify" );
602*0Sstevel@tonic-gate 					else
603*0Sstevel@tonic-gate 						printf( "Modify initiated with id %d\n", id );
604*0Sstevel@tonic-gate 				}
605*0Sstevel@tonic-gate 			} else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
606*0Sstevel@tonic-gate 				getline( dn, sizeof(dn), stdin, "dn? " );
607*0Sstevel@tonic-gate 				strcat( dn, dnsuffix );
608*0Sstevel@tonic-gate 				getline( rdn, sizeof(rdn), stdin, "newrdn? " );
609*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin, "delete old rdn (0=>no, 1=>yes)?");
610*0Sstevel@tonic-gate 				delrdn = atoi(line);
611*0Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
612*0Sstevel@tonic-gate 					if ((err = ldap_rename(ld, dn, rdn, NULL, delrdn, NULL,NULL, &id)) != LDAP_SUCCESS){
613*0Sstevel@tonic-gate 						printf( "Error in ldap_rename (modrdn): %s\n", ldap_err2string(err));
614*0Sstevel@tonic-gate 					}
615*0Sstevel@tonic-gate 					else
616*0Sstevel@tonic-gate 						printf( "Modrdn initiated with id %d\n", id );
617*0Sstevel@tonic-gate 				}
618*0Sstevel@tonic-gate 				else {
619*0Sstevel@tonic-gate 					if ( (id = ldap_modrdn( ld, dn, rdn, delrdn )) == -1 )
620*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_modrdn" );
621*0Sstevel@tonic-gate 					else
622*0Sstevel@tonic-gate 						printf( "Modrdn initiated with id %d\n", id );
623*0Sstevel@tonic-gate 				}
624*0Sstevel@tonic-gate 			} else {
625*0Sstevel@tonic-gate 				printf( "Possibilities: [modi]fy, [modr]dn\n" );
626*0Sstevel@tonic-gate 			}
627*0Sstevel@tonic-gate 			break;
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 		case 'q':	/* quit */
630*0Sstevel@tonic-gate #ifdef CLDAP
631*0Sstevel@tonic-gate 			if ( cldapflg )
632*0Sstevel@tonic-gate 				cldap_close( ld );
633*0Sstevel@tonic-gate #endif /* CLDAP */
634*0Sstevel@tonic-gate 			if ( !cldapflg )
635*0Sstevel@tonic-gate 				ldap_unbind( ld );
636*0Sstevel@tonic-gate 			exit( 0 );
637*0Sstevel@tonic-gate 			break;
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		case 'r':	/* result or remove */
640*0Sstevel@tonic-gate 			switch ( command3 ) {
641*0Sstevel@tonic-gate 			case 's':	/* result */
642*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
643*0Sstevel@tonic-gate 				    "msgid (-1=>any)? " );
644*0Sstevel@tonic-gate 				if ( line[0] == '\0' )
645*0Sstevel@tonic-gate 					id = -1;
646*0Sstevel@tonic-gate 				else
647*0Sstevel@tonic-gate 					id = atoi( line );
648*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
649*0Sstevel@tonic-gate 				    "all (0=>any, 1=>all)? " );
650*0Sstevel@tonic-gate 				if ( line[0] == '\0' )
651*0Sstevel@tonic-gate 					all = 1;
652*0Sstevel@tonic-gate 				else
653*0Sstevel@tonic-gate 					all = atoi( line );
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 				if (( msgtype = ldap_result( ld, id, all,
656*0Sstevel@tonic-gate 				    resultusetimelimit ? &timelimit : &timeout, &res )) < 1 ) {
657*0Sstevel@tonic-gate 					ldap_perror( ld, "ldap_result" );
658*0Sstevel@tonic-gate 					break;
659*0Sstevel@tonic-gate 				}
660*0Sstevel@tonic-gate 				printf( "\nresult: msgtype %d msgid %d\n",
661*0Sstevel@tonic-gate 				    msgtype, res->lm_msgid );
662*0Sstevel@tonic-gate 				handle_result( ld, res );
663*0Sstevel@tonic-gate 				if (all || msgtype == LDAP_RES_SEARCH_RESULT)
664*0Sstevel@tonic-gate 					resultusetimelimit = 0;
665*0Sstevel@tonic-gate 				res = NULLMSG;
666*0Sstevel@tonic-gate 				break;
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 			case 'm':	/* remove */
669*0Sstevel@tonic-gate 				getline( dn, sizeof(dn), stdin, "dn? " );
670*0Sstevel@tonic-gate 				strcat( dn, dnsuffix );
671*0Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
672*0Sstevel@tonic-gate 					if ((err = ldap_delete_ext( ld, dn, NULL, NULL, &id )) != LDAP_SUCCESS )
673*0Sstevel@tonic-gate 						printf( "Error in ldap_delete_ext: %s\n", ldap_err2string(err) );
674*0Sstevel@tonic-gate 					else
675*0Sstevel@tonic-gate 						printf( "Remove initiated with id %d\n", id );
676*0Sstevel@tonic-gate 				} else {
677*0Sstevel@tonic-gate 					if ( (id = ldap_delete( ld, dn )) == -1 )
678*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_delete" );
679*0Sstevel@tonic-gate 					else
680*0Sstevel@tonic-gate 						printf( "Remove initiated with id %d\n", id );
681*0Sstevel@tonic-gate 				}
682*0Sstevel@tonic-gate 				break;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 			default:
685*0Sstevel@tonic-gate 				printf( "Possibilities: [rem]ove, [res]ult\n" );
686*0Sstevel@tonic-gate 				break;
687*0Sstevel@tonic-gate 			}
688*0Sstevel@tonic-gate 			break;
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 		case 's':	/* search */
691*0Sstevel@tonic-gate 			getline( dn, sizeof(dn), stdin, "searchbase? " );
692*0Sstevel@tonic-gate 			strcat( dn, dnsuffix );
693*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
694*0Sstevel@tonic-gate 			    "scope (0=Base, 1=One Level, 2=Subtree)? " );
695*0Sstevel@tonic-gate 			scope = atoi( line );
696*0Sstevel@tonic-gate 			getline( filter, sizeof(filter), stdin,
697*0Sstevel@tonic-gate 			    "search filter (e.g. sn=jones)? " );
698*0Sstevel@tonic-gate 			types = get_list( "attrs to return? " );
699*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
700*0Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
701*0Sstevel@tonic-gate 			attrsonly = atoi( line );
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 			if ( cldapflg ) {
704*0Sstevel@tonic-gate #ifdef CLDAP
705*0Sstevel@tonic-gate 			    getline( line, sizeof(line), stdin,
706*0Sstevel@tonic-gate 				"Requestor DN (for logging)? " );
707*0Sstevel@tonic-gate 			    if ( cldap_search_s( ld, dn, scope, filter, types,
708*0Sstevel@tonic-gate 				    attrsonly, &res, line ) != 0 ) {
709*0Sstevel@tonic-gate 				ldap_perror( ld, "cldap_search_s" );
710*0Sstevel@tonic-gate 			    } else {
711*0Sstevel@tonic-gate 				printf( "\nresult: msgid %d\n",
712*0Sstevel@tonic-gate 				    res->lm_msgid );
713*0Sstevel@tonic-gate 				handle_result( ld, res );
714*0Sstevel@tonic-gate 				res = NULLMSG;
715*0Sstevel@tonic-gate 			    }
716*0Sstevel@tonic-gate #endif /* CLDAP */
717*0Sstevel@tonic-gate 			} else {
718*0Sstevel@tonic-gate 				theInt = 0;
719*0Sstevel@tonic-gate 				if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){
720*0Sstevel@tonic-gate 					resultusetimelimit = 1;
721*0Sstevel@tonic-gate 					getline( line, sizeof(line), stdin,
722*0Sstevel@tonic-gate 							 "ldap_search_ext (0=>no, 1=>yes - default: yes)? " );
723*0Sstevel@tonic-gate 					if (line[0] == '\0')
724*0Sstevel@tonic-gate 						theInt = 1;
725*0Sstevel@tonic-gate 					else
726*0Sstevel@tonic-gate 						theInt = atoi( line );
727*0Sstevel@tonic-gate 				}
728*0Sstevel@tonic-gate 				if (theInt){
729*0Sstevel@tonic-gate 					getline(line, sizeof(line), stdin, "time limit?");
730*0Sstevel@tonic-gate 					timelimit.tv_sec = atoi(line);
731*0Sstevel@tonic-gate 					resultusetimelimit = 1;
732*0Sstevel@tonic-gate 					getline(line, sizeof(line), stdin, "size limit?");
733*0Sstevel@tonic-gate 					sizelimit = atoi(line);
734*0Sstevel@tonic-gate 					if (( err = ldap_search_ext(ld, dn, scope, filter, types, attrsonly, NULL, NULL,
735*0Sstevel@tonic-gate 												&timelimit, sizelimit, &id)) != LDAP_SUCCESS){
736*0Sstevel@tonic-gate 						printf( "Error in ldap_search_ext: %s\n", ldap_err2string(err));
737*0Sstevel@tonic-gate 					} else {
738*0Sstevel@tonic-gate 						printf( "Search initiated with id %d\n", id );
739*0Sstevel@tonic-gate 					}
740*0Sstevel@tonic-gate 				} else {
741*0Sstevel@tonic-gate 					if (( id = ldap_search( ld, dn, scope, filter,
742*0Sstevel@tonic-gate 											types, attrsonly  )) == -1 ) {
743*0Sstevel@tonic-gate 						ldap_perror( ld, "ldap_search" );
744*0Sstevel@tonic-gate 					} else {
745*0Sstevel@tonic-gate 						printf( "Search initiated with id %d\n", id );
746*0Sstevel@tonic-gate 					}
747*0Sstevel@tonic-gate 				}
748*0Sstevel@tonic-gate 			}
749*0Sstevel@tonic-gate 			free_list( types );
750*0Sstevel@tonic-gate 			break;
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 		case 't':	/* set timeout value */
753*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "timeout? " );
754*0Sstevel@tonic-gate 			timeout.tv_sec = atoi( line );
755*0Sstevel@tonic-gate 			break;
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 		case 'U':	/* set ufn search prefix */
758*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "ufn prefix? " );
759*0Sstevel@tonic-gate 			ldap_ufn_setprefix( ld, line );
760*0Sstevel@tonic-gate 			break;
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 		case 'u':	/* user friendly search w/optional timeout */
763*0Sstevel@tonic-gate 			getline( dn, sizeof(dn), stdin, "ufn? " );
764*0Sstevel@tonic-gate 			strcat( dn, dnsuffix );
765*0Sstevel@tonic-gate 			types = get_list( "attrs to return? " );
766*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
767*0Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
768*0Sstevel@tonic-gate 			attrsonly = atoi( line );
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 			if ( command2 == 't' ) {
771*0Sstevel@tonic-gate 				id = ldap_ufn_search_c( ld, dn, types,
772*0Sstevel@tonic-gate 				    attrsonly, &res, ldap_ufn_timeout,
773*0Sstevel@tonic-gate 				    &timeout );
774*0Sstevel@tonic-gate 			} else {
775*0Sstevel@tonic-gate 				id = ldap_ufn_search_s( ld, dn, types,
776*0Sstevel@tonic-gate 				    attrsonly, &res );
777*0Sstevel@tonic-gate 			}
778*0Sstevel@tonic-gate 			if ( res == NULL )
779*0Sstevel@tonic-gate 				ldap_perror( ld, "ldap_ufn_search" );
780*0Sstevel@tonic-gate 			else {
781*0Sstevel@tonic-gate 				printf( "\nresult: err %d\n", id );
782*0Sstevel@tonic-gate 				handle_result( ld, res );
783*0Sstevel@tonic-gate 				res = NULLMSG;
784*0Sstevel@tonic-gate 			}
785*0Sstevel@tonic-gate 			free_list( types );
786*0Sstevel@tonic-gate 			break;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 		case 'l':	/* URL search */
789*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
790*0Sstevel@tonic-gate 			    "attrsonly (0=attrs&values, 1=attrs only)? " );
791*0Sstevel@tonic-gate 			attrsonly = atoi( line );
792*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "LDAP URL? " );
793*0Sstevel@tonic-gate 			if (( id = ldap_url_search( ld, line, attrsonly  ))
794*0Sstevel@tonic-gate 				== -1 ) {
795*0Sstevel@tonic-gate 			    ldap_perror( ld, "ldap_url_search" );
796*0Sstevel@tonic-gate 			} else {
797*0Sstevel@tonic-gate 			    printf( "URL search initiated with id %d\n", id );
798*0Sstevel@tonic-gate 			}
799*0Sstevel@tonic-gate 			break;
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 		case 'p':	/* parse LDAP URL */
802*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "LDAP URL? " );
803*0Sstevel@tonic-gate 			if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
804*0Sstevel@tonic-gate 			    fprintf( stderr, "ldap_url_parse: error %d\n", i );
805*0Sstevel@tonic-gate 			} else {
806*0Sstevel@tonic-gate 			    printf( "\t  host: " );
807*0Sstevel@tonic-gate 			    if ( ludp->lud_host == NULL ) {
808*0Sstevel@tonic-gate 				printf( "DEFAULT\n" );
809*0Sstevel@tonic-gate 			    } else {
810*0Sstevel@tonic-gate 				printf( "<%s>\n", ludp->lud_host );
811*0Sstevel@tonic-gate 			    }
812*0Sstevel@tonic-gate 			    printf( "\t  port: " );
813*0Sstevel@tonic-gate 			    if ( ludp->lud_port == 0 ) {
814*0Sstevel@tonic-gate 				printf( "DEFAULT\n" );
815*0Sstevel@tonic-gate 			    } else {
816*0Sstevel@tonic-gate 				printf( "%d\n", ludp->lud_port );
817*0Sstevel@tonic-gate 			    }
818*0Sstevel@tonic-gate 			    printf( "\t    dn: <%s>\n", ludp->lud_dn );
819*0Sstevel@tonic-gate 			    printf( "\t attrs:" );
820*0Sstevel@tonic-gate 			    if ( ludp->lud_attrs == NULL ) {
821*0Sstevel@tonic-gate 				printf( " ALL" );
822*0Sstevel@tonic-gate 			    } else {
823*0Sstevel@tonic-gate 				for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
824*0Sstevel@tonic-gate 				    printf( " <%s>", ludp->lud_attrs[ i ] );
825*0Sstevel@tonic-gate 				}
826*0Sstevel@tonic-gate 			    }
827*0Sstevel@tonic-gate 			    printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_UNKNOWN ? "DEFAULT (base)" :
828*0Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "ONE" :
829*0Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
830*0Sstevel@tonic-gate 						ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
831*0Sstevel@tonic-gate 			    printf( "\tfilter: <%s>\n", ludp->lud_filter ? ludp->lud_filter : "NONE");
832*0Sstevel@tonic-gate 				if (ludp->lud_extensions){
833*0Sstevel@tonic-gate 					printf("\textensions: \n");
834*0Sstevel@tonic-gate 					for (i = 0; ludp->lud_extensions[i] != NULL; i++)
835*0Sstevel@tonic-gate 						printf("\t\t%s (%s)\n", ludp->lud_extensions[i]->lue_type,
836*0Sstevel@tonic-gate 							   ludp->lud_extensions[i]->lue_iscritical ? "Critical" : "Non critical");
837*0Sstevel@tonic-gate 				}
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate 			    ldap_free_urldesc( ludp );
840*0Sstevel@tonic-gate 			}
841*0Sstevel@tonic-gate 			    break;
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 		case 'n':	/* set dn suffix, for convenience */
844*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "DN suffix? " );
845*0Sstevel@tonic-gate 			strcpy( dnsuffix, line );
846*0Sstevel@tonic-gate 			break;
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate 		case 'e':	/* enable cache */
849*0Sstevel@tonic-gate #ifdef NO_CACHE
850*0Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
851*0Sstevel@tonic-gate #else /* NO_CACHE */
852*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
853*0Sstevel@tonic-gate 			i = atoi( line );
854*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
855*0Sstevel@tonic-gate 			if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
856*0Sstevel@tonic-gate 				printf( "local cache is on\n" );
857*0Sstevel@tonic-gate 			} else {
858*0Sstevel@tonic-gate 				printf( "ldap_enable_cache failed\n" );
859*0Sstevel@tonic-gate 			}
860*0Sstevel@tonic-gate #endif /* NO_CACHE */
861*0Sstevel@tonic-gate 			break;
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 		case 'x':	/* uncache entry */
864*0Sstevel@tonic-gate #ifdef NO_CACHE
865*0Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
866*0Sstevel@tonic-gate #else /* NO_CACHE */
867*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "DN? " );
868*0Sstevel@tonic-gate 			ldap_uncache_entry( ld, line );
869*0Sstevel@tonic-gate #endif /* NO_CACHE */
870*0Sstevel@tonic-gate 			break;
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 		case 'X':	/* uncache request */
873*0Sstevel@tonic-gate #ifdef NO_CACHE
874*0Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
875*0Sstevel@tonic-gate #else /* NO_CACHE */
876*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "request msgid? " );
877*0Sstevel@tonic-gate 			ldap_uncache_request( ld, atoi( line ));
878*0Sstevel@tonic-gate #endif /* NO_CACHE */
879*0Sstevel@tonic-gate 			break;
880*0Sstevel@tonic-gate 
881*0Sstevel@tonic-gate 		case 'o':	/* set ldap options */
882*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
883*0Sstevel@tonic-gate 			theInt = atoi(line);
884*0Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_DEREF, &theInt );
885*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "timelimit?" );
886*0Sstevel@tonic-gate 			theInt = atoi(line);
887*0Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_TIMELIMIT,  &theInt);
888*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "sizelimit?" );
889*0Sstevel@tonic-gate 			theInt = atoi(line);
890*0Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &theInt);
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 			ld->ld_options = 0;
893*0Sstevel@tonic-gate 
894*0Sstevel@tonic-gate #ifdef STR_TRANSLATION
895*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
896*0Sstevel@tonic-gate 				"Automatic translation of T.61 strings (0=no, 1=yes)?" );
897*0Sstevel@tonic-gate 			if ( atoi( line ) == 0 ) {
898*0Sstevel@tonic-gate 				ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
899*0Sstevel@tonic-gate 			} else {
900*0Sstevel@tonic-gate 				ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
901*0Sstevel@tonic-gate #ifdef LDAP_CHARSET_8859
902*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
903*0Sstevel@tonic-gate 					"Translate to/from ISO-8859 (0=no, 1=yes?" );
904*0Sstevel@tonic-gate 				if ( atoi( line ) != 0 ) {
905*0Sstevel@tonic-gate 					ldap_set_string_translators( ld,
906*0Sstevel@tonic-gate 					    ldap_8859_to_t61,
907*0Sstevel@tonic-gate 					    ldap_t61_to_8859 );
908*0Sstevel@tonic-gate 				}
909*0Sstevel@tonic-gate #endif /* LDAP_CHARSET_8859 */
910*0Sstevel@tonic-gate 			}
911*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate #ifdef LDAP_DNS
914*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
915*0Sstevel@tonic-gate 				"Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
916*0Sstevel@tonic-gate 			if ( atoi( line ) != 0 ) {
917*0Sstevel@tonic-gate 				ld->ld_options |= LDAP_OPT_DNS;
918*0Sstevel@tonic-gate 			}
919*0Sstevel@tonic-gate #endif /* LDAP_DNS */
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
922*0Sstevel@tonic-gate 				"Recognize and chase referrals (0=no, 1=yes)?" );
923*0Sstevel@tonic-gate 			if ( atoi( line ) != 0 ) {
924*0Sstevel@tonic-gate 				theInt = LDAP_OPT_ON;
925*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
926*0Sstevel@tonic-gate 						 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
927*0Sstevel@tonic-gate 				if ( atoi( line ) != 0 ) {
928*0Sstevel@tonic-gate 					ldap_set_option( ld, LDAP_OPT_REBIND_FN, bind_prompt );
929*0Sstevel@tonic-gate 				}
930*0Sstevel@tonic-gate 			} else {
931*0Sstevel@tonic-gate 				theInt = LDAP_OPT_OFF;
932*0Sstevel@tonic-gate 			}
933*0Sstevel@tonic-gate 			ldap_set_option(ld, LDAP_OPT_REFERRALS, &theInt);
934*0Sstevel@tonic-gate 			break;
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate 		case 'k': /* Set some controls */
937*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin,
938*0Sstevel@tonic-gate 					 "Set control: (0 for none, 1 for ManageDSA, 2 for preferredLang, 3 for BAD)?");
939*0Sstevel@tonic-gate 			theInt = atoi(line);
940*0Sstevel@tonic-gate 			switch (theInt){
941*0Sstevel@tonic-gate 			case 0:
942*0Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, NULL);
943*0Sstevel@tonic-gate 				break;
944*0Sstevel@tonic-gate 			case 1:
945*0Sstevel@tonic-gate 				aCtrl.ldctl_oid = "2.16.840.1.113730.3.4.2";
946*0Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = 1;
947*0Sstevel@tonic-gate 				aCtrl.ldctl_value = NULL;
948*0Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
949*0Sstevel@tonic-gate 				break;
950*0Sstevel@tonic-gate 			case 2:
951*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
952*0Sstevel@tonic-gate 						 "Preferred Language Control : lang ?");
953*0Sstevel@tonic-gate 				aCtrl.ldctl_oid = "1.3.6.1.4.1.1466.20035";
954*0Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = 1;
955*0Sstevel@tonic-gate 				bv.bv_val = strdup(line);
956*0Sstevel@tonic-gate 				bv.bv_len = strlen(line);
957*0Sstevel@tonic-gate 				aCtrl.ldctl_value = &bv;
958*0Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
959*0Sstevel@tonic-gate 				break;
960*0Sstevel@tonic-gate 			default:
961*0Sstevel@tonic-gate 				getline( line, sizeof(line), stdin,
962*0Sstevel@tonic-gate 						 "Bad Control is critical (0=false, 1=true)?");
963*0Sstevel@tonic-gate 				aCtrl.ldctl_oid = "1.1.1.1.1.1";
964*0Sstevel@tonic-gate 				aCtrl.ldctl_iscritical = atoi(line);
965*0Sstevel@tonic-gate 				aCtrl.ldctl_value = NULL;
966*0Sstevel@tonic-gate 				ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls);
967*0Sstevel@tonic-gate 				break;
968*0Sstevel@tonic-gate 			}
969*0Sstevel@tonic-gate 			break;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 		case 'O':	/* set cache options */
972*0Sstevel@tonic-gate #ifdef NO_CACHE
973*0Sstevel@tonic-gate 			printf( NOCACHEERRMSG );
974*0Sstevel@tonic-gate #else /* NO_CACHE */
975*0Sstevel@tonic-gate 			getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
976*0Sstevel@tonic-gate 			switch( atoi( line )) {
977*0Sstevel@tonic-gate 			case 0:
978*0Sstevel@tonic-gate 				ldap_set_cache_options( ld, 0 );
979*0Sstevel@tonic-gate 				break;
980*0Sstevel@tonic-gate 			case 1:
981*0Sstevel@tonic-gate 				ldap_set_cache_options( ld,
982*0Sstevel@tonic-gate 					LDAP_CACHE_OPT_CACHENOERRS );
983*0Sstevel@tonic-gate 				break;
984*0Sstevel@tonic-gate 			case 2:
985*0Sstevel@tonic-gate 				ldap_set_cache_options( ld,
986*0Sstevel@tonic-gate 					LDAP_CACHE_OPT_CACHEALLERRS );
987*0Sstevel@tonic-gate 				break;
988*0Sstevel@tonic-gate 			default:
989*0Sstevel@tonic-gate 				printf( "not a valid cache option\n" );
990*0Sstevel@tonic-gate 			}
991*0Sstevel@tonic-gate #endif /* NO_CACHE */
992*0Sstevel@tonic-gate 			break;
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 		case '?':	/* help */
995*0Sstevel@tonic-gate     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
996*0Sstevel@tonic-gate     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
997*0Sstevel@tonic-gate     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
998*0Sstevel@tonic-gate     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
999*0Sstevel@tonic-gate     printf( "          [u]fn search  [ut]fn search with timeout\n" );
1000*0Sstevel@tonic-gate     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
1001*0Sstevel@tonic-gate     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
1002*0Sstevel@tonic-gate     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
1003*0Sstevel@tonic-gate     printf( "          [?]help       [o]ptions         [O]cache options\n" );
1004*0Sstevel@tonic-gate     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
1005*0Sstevel@tonic-gate 			break;
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 		default:
1008*0Sstevel@tonic-gate 			printf( "Invalid command.  Type ? for help.\n" );
1009*0Sstevel@tonic-gate 			break;
1010*0Sstevel@tonic-gate 		}
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 		(void) memset( line, '\0', sizeof(line) );
1013*0Sstevel@tonic-gate 	}
1014*0Sstevel@tonic-gate 
1015*0Sstevel@tonic-gate 	return( 0 );
1016*0Sstevel@tonic-gate }
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate static void
1019*0Sstevel@tonic-gate handle_result( LDAP *ld, LDAPMessage *lm )
1020*0Sstevel@tonic-gate {
1021*0Sstevel@tonic-gate 	switch ( lm->lm_msgtype ) {
1022*0Sstevel@tonic-gate 	case LDAP_RES_COMPARE:
1023*0Sstevel@tonic-gate 		printf( "Compare result\n" );
1024*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "compare" );
1025*0Sstevel@tonic-gate 		break;
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate 	case LDAP_RES_SEARCH_RESULT:
1028*0Sstevel@tonic-gate 		printf( "Search result\n" );
1029*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "search" );
1030*0Sstevel@tonic-gate 		break;
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 	case LDAP_RES_SEARCH_REFERENCE:
1033*0Sstevel@tonic-gate 		printf( "Search reference\n" );
1034*0Sstevel@tonic-gate 		print_search_entry( ld, lm );
1035*0Sstevel@tonic-gate 		break;
1036*0Sstevel@tonic-gate 
1037*0Sstevel@tonic-gate 	case LDAP_RES_SEARCH_ENTRY:
1038*0Sstevel@tonic-gate 		printf( "Search entry\n" );
1039*0Sstevel@tonic-gate 		print_search_entry( ld, lm );
1040*0Sstevel@tonic-gate 		break;
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 	case LDAP_RES_ADD:
1043*0Sstevel@tonic-gate 		printf( "Add result\n" );
1044*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "add" );
1045*0Sstevel@tonic-gate 		break;
1046*0Sstevel@tonic-gate 
1047*0Sstevel@tonic-gate 	case LDAP_RES_DELETE:
1048*0Sstevel@tonic-gate 		printf( "Delete result\n" );
1049*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "delete" );
1050*0Sstevel@tonic-gate 		break;
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 	case LDAP_RES_MODIFY:
1053*0Sstevel@tonic-gate 		printf( "Modify result\n" );
1054*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "modify" );
1055*0Sstevel@tonic-gate 		break;
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate 	case LDAP_RES_MODRDN:
1058*0Sstevel@tonic-gate 		printf( "ModRDN result\n" );
1059*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "modrdn" );
1060*0Sstevel@tonic-gate 		break;
1061*0Sstevel@tonic-gate 
1062*0Sstevel@tonic-gate 	case LDAP_RES_BIND:
1063*0Sstevel@tonic-gate 		printf( "Bind result\n" );
1064*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "bind" );
1065*0Sstevel@tonic-gate 		break;
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 	default:
1068*0Sstevel@tonic-gate 		printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
1069*0Sstevel@tonic-gate 		print_ldap_result( ld, lm, "unknown" );
1070*0Sstevel@tonic-gate 	}
1071*0Sstevel@tonic-gate }
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate static void
1074*0Sstevel@tonic-gate print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
1075*0Sstevel@tonic-gate {
1076*0Sstevel@tonic-gate 	int rc, i;
1077*0Sstevel@tonic-gate 	int errCode;
1078*0Sstevel@tonic-gate 	char *matched = NULL, *errMsg = NULL, **referrals = NULL;
1079*0Sstevel@tonic-gate 	LDAPControl **srvctrls = NULL;
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 	if ((rc = ldap_parse_result(ld, lm, &errCode, &matched, &errMsg, &referrals, &srvctrls, 0)) != LDAP_SUCCESS){
1082*0Sstevel@tonic-gate 		fprintf(stderr, "%s: error while parsing result (%s)\n", s, ldap_err2string(rc));
1083*0Sstevel@tonic-gate 		return;
1084*0Sstevel@tonic-gate 	}
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate 	fprintf(stderr, "%s: %s\n", s, ldap_err2string(errCode));
1088*0Sstevel@tonic-gate 	if (errCode == LDAP_REFERRAL){
1089*0Sstevel@tonic-gate 		fprintf(stderr, "\tReferrals returned: \n");
1090*0Sstevel@tonic-gate 		for (i = 0; referrals[i] != NULL; i++)
1091*0Sstevel@tonic-gate 			fprintf(stderr, "\t\t%s\n", referrals[i]);
1092*0Sstevel@tonic-gate 	}
1093*0Sstevel@tonic-gate 	if (errMsg && *errMsg)
1094*0Sstevel@tonic-gate 		fprintf(stderr, "\tAdditional info: %s\n", errMsg);
1095*0Sstevel@tonic-gate 	free(errMsg);
1096*0Sstevel@tonic-gate 	if (NAME_ERROR(errCode) && matched && *matched){
1097*0Sstevel@tonic-gate 		fprintf(stderr, "\tMatched DN: %s\n", matched);
1098*0Sstevel@tonic-gate 		free(matched);
1099*0Sstevel@tonic-gate 	}
1100*0Sstevel@tonic-gate 	if (srvctrls != NULL){
1101*0Sstevel@tonic-gate 		fprintf(stderr, "\tLDAPControls returned: \n");
1102*0Sstevel@tonic-gate 		for (i=0;srvctrls[i] != NULL; i++)
1103*0Sstevel@tonic-gate 			fprintf(stderr, "\t\t%s (%s)\n", srvctrls[i]->ldctl_oid, srvctrls[i]->ldctl_iscritical ? "Critical" : "Not critical");
1104*0Sstevel@tonic-gate 	}
1105*0Sstevel@tonic-gate 	return;
1106*0Sstevel@tonic-gate }
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate static void
1109*0Sstevel@tonic-gate print_search_entry( LDAP *ld, LDAPMessage *res )
1110*0Sstevel@tonic-gate {
1111*0Sstevel@tonic-gate 	BerElement	*ber;
1112*0Sstevel@tonic-gate 	char		*a, *dn, *ufn;
1113*0Sstevel@tonic-gate 	struct berval	**vals;
1114*0Sstevel@tonic-gate 	int		i;
1115*0Sstevel@tonic-gate 	LDAPMessage	*e;
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 	for ( e = ldap_first_message( ld, res ); e != NULLMSG;
1118*0Sstevel@tonic-gate 	    e = ldap_next_message( ld, e ) ) {
1119*0Sstevel@tonic-gate 		if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
1120*0Sstevel@tonic-gate 			break;
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 		dn = ldap_get_dn( ld, e );
1123*0Sstevel@tonic-gate 		printf( "\tDN: %s\n", dn );
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate 		ufn = ldap_dn2ufn( dn );
1126*0Sstevel@tonic-gate 		printf( "\tUFN: %s\n", ufn );
1127*0Sstevel@tonic-gate 		free( dn );
1128*0Sstevel@tonic-gate 		free( ufn );
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 		if ( e->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ){
1131*0Sstevel@tonic-gate 			char **urls = ldap_get_reference_urls(ld, e);
1132*0Sstevel@tonic-gate 			if (urls == NULL){
1133*0Sstevel@tonic-gate 				printf("\t\tError with references: %s\n", ldap_err2string(ld->ld_errno));
1134*0Sstevel@tonic-gate 			} else {
1135*0Sstevel@tonic-gate 				for (i=0;urls[i] != NULL;i++)
1136*0Sstevel@tonic-gate 					printf("\t\tURL: %s\n", urls[i]);
1137*0Sstevel@tonic-gate 			}
1138*0Sstevel@tonic-gate 		} else {
1139*0Sstevel@tonic-gate 			for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
1140*0Sstevel@tonic-gate 				  a = ldap_next_attribute( ld, e, ber ) ) {
1141*0Sstevel@tonic-gate 				printf( "\t\tATTR: %s\n", a );
1142*0Sstevel@tonic-gate 				if ( (vals = ldap_get_values_len( ld, e, a ))
1143*0Sstevel@tonic-gate 					 == NULL ) {
1144*0Sstevel@tonic-gate 					printf( "\t\t\t(no values)\n" );
1145*0Sstevel@tonic-gate 				} else {
1146*0Sstevel@tonic-gate 					for ( i = 0; vals[i] != NULL; i++ ) {
1147*0Sstevel@tonic-gate 						int	j, nonascii;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 						nonascii = 0;
1150*0Sstevel@tonic-gate 						for ( j = 0; j < vals[i]->bv_len; j++ )
1151*0Sstevel@tonic-gate 							if ( !isascii( vals[i]->bv_val[j] ) ) {
1152*0Sstevel@tonic-gate 							nonascii = 1;
1153*0Sstevel@tonic-gate 							break;
1154*0Sstevel@tonic-gate 							}
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 						if ( nonascii ) {
1157*0Sstevel@tonic-gate 							printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
1158*0Sstevel@tonic-gate #ifdef BPRINT_NONASCII
1159*0Sstevel@tonic-gate 							lber_bprint( vals[i]->bv_val,
1160*0Sstevel@tonic-gate 										 vals[i]->bv_len );
1161*0Sstevel@tonic-gate #endif /* BPRINT_NONASCII */
1162*0Sstevel@tonic-gate 							continue;
1163*0Sstevel@tonic-gate 						}
1164*0Sstevel@tonic-gate 						printf( "\t\t\tlength (%ld) %s\n",
1165*0Sstevel@tonic-gate 								vals[i]->bv_len, vals[i]->bv_val );
1166*0Sstevel@tonic-gate 					}
1167*0Sstevel@tonic-gate 					ber_bvecfree( vals );
1168*0Sstevel@tonic-gate 				}
1169*0Sstevel@tonic-gate 			}
1170*0Sstevel@tonic-gate 		}
1171*0Sstevel@tonic-gate 	}
1172*0Sstevel@tonic-gate 
1173*0Sstevel@tonic-gate 	if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
1174*0Sstevel@tonic-gate 	    || res->lm_chain != NULLMSG )
1175*0Sstevel@tonic-gate 		print_ldap_result( ld, res, "search" );
1176*0Sstevel@tonic-gate }
1177