1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
10*0Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
11*0Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
12*0Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
15*0Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*0Sstevel@tonic-gate  * implied. See the License for the specific language governing
17*0Sstevel@tonic-gate  * rights and limitations under the License.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
20*0Sstevel@tonic-gate  * March 31, 1998.
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*0Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*0Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*0Sstevel@tonic-gate  * Rights Reserved.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * Contributor(s):
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /* ldapsearch.c - generic program to search LDAP */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include "ldaptool.h"
33*0Sstevel@tonic-gate #include "fileurl.h"
34*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
35*0Sstevel@tonic-gate #include <locale.h>
36*0Sstevel@tonic-gate #include "solaris-int.h"
37*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #define VLV_PARAM_SEP ':'
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
42*0Sstevel@tonic-gate #define gettext(s) s
43*0Sstevel@tonic-gate #endif
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static void usage( void );
46*0Sstevel@tonic-gate static int dosearch( LDAP *ld, char *base, int scope, char **attrs,
47*0Sstevel@tonic-gate 			 int attrsonly, char *filtpatt, char *value);
48*0Sstevel@tonic-gate static void write_string_attr_value( char *attrname, char *strval,
49*0Sstevel@tonic-gate 	unsigned long opts );
50*0Sstevel@tonic-gate #define LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME	0x01
51*0Sstevel@tonic-gate static int write_ldif_value( char *type, char *value, unsigned long vallen,
52*0Sstevel@tonic-gate 	unsigned long ldifoptions );
53*0Sstevel@tonic-gate static void print_entry( LDAP *ld, LDAPMessage *entry, int attrsonly );
54*0Sstevel@tonic-gate static void options_callback( int option, char *optarg );
55*0Sstevel@tonic-gate static void parse_and_display_reference( LDAP *ld, LDAPMessage *ref );
56*0Sstevel@tonic-gate static char *sortresult2string(unsigned long result);
57*0Sstevel@tonic-gate static char *changetype_num2string( int chgtype );
58*0Sstevel@tonic-gate static char *msgtype2str( int msgtype );
59*0Sstevel@tonic-gate static char  **get_effectiverights_attrlist(char * optarg);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
62*0Sstevel@tonic-gate static void fill_ldapsearch_msgtypes( void );
63*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Prefix used in names of pseudo attributes added to the entry LDIF
67*0Sstevel@tonic-gate  * output if we receive an entryChangeNotification control with an entry
68*0Sstevel@tonic-gate  * (requested using persistent search).
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate #define LDAPTOOL_PSEARCH_ATTR_PREFIX	"persistentSearch-"
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static void
74*0Sstevel@tonic-gate usage( void )
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate     fprintf( stderr, gettext("usage: %s -b basedn [options] filter [attributes...]\n"), ldaptool_progname );
77*0Sstevel@tonic-gate     fprintf( stderr, gettext("       %s -b basedn [options] -f file [attributes...]\nwhere:\n"), ldaptool_progname );
78*0Sstevel@tonic-gate     fprintf( stderr, gettext("    basedn\tbase dn for search\n") );
79*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t(if the environment variable LDAP_BASEDN is set,\n") );
80*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\tthen the -b flag is not required)\n") );
81*0Sstevel@tonic-gate     fprintf( stderr, gettext("    filter\tRFC-2254 compliant LDAP search filter\n") );
82*0Sstevel@tonic-gate     fprintf( stderr, gettext("    file\tfile containing a sequence of LDAP search filters to use\n") );
83*0Sstevel@tonic-gate     fprintf( stderr, gettext("    attributes\twhitespace-separated list of attributes to retrieve\n") );
84*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t(if no attribute list is given, all are retrieved)\n") );
85*0Sstevel@tonic-gate     fprintf( stderr, gettext("options:\n") );
86*0Sstevel@tonic-gate     ldaptool_common_usage( 0 );
87*0Sstevel@tonic-gate #if defined( XP_WIN32 )
88*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -t\t\twrite values to files in temp directory.\n") );
89*0Sstevel@tonic-gate #else
90*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -t\t\twrite values to files in /tmp\n") );
91*0Sstevel@tonic-gate #endif
92*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -U\t\tproduce file URLs in conjunction with -t\n") );
93*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -e\t\tminimize base-64 encoding of values\n") );
94*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -u\t\tinclude User Friendly entry names in the output\n") );
95*0Sstevel@tonic-gate #ifndef HAVE_SASL_OPTIONS
96*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -o\t\tprint entries using old format (default is LDIF)\n") );
97*0Sstevel@tonic-gate #endif
98*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -T\t\tdon't fold (wrap) long lines (default is to fold)\n") );
99*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -1\t\tomit leading \"version: %d\" line in LDIF output\n"), LDIF_VERSION_ONE );
100*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -A\t\tretrieve attribute names only (no values)\n") );
101*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -B\t\tprint non-ASCII values and use old output format (attr=value)\n") );
102*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -x\t\tperforming sorting on server\n") );
103*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
104*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -r\t\tprint entries using old format (default is LDIF)\n") );
105*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
106*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -F sep\tprint `sep' instead of `%s' between attribute names\n"), LDAPTOOL_DEFSEP );
107*0Sstevel@tonic-gate     fprintf( stderr, gettext("          \tand values\n") );
108*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -S attr\tsort the results by attribute `attr'\n") );
109*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -s scope\tone of base, one, or sub (default is sub)\n") );
110*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -a deref\tone of never, always, search, or find (default: never)\n") );
111*0Sstevel@tonic-gate     fprintf( stderr, gettext("            \t(alias dereferencing)\n") );
112*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -l timelim\ttime limit (in seconds) for search (default is no limit)\n") );
113*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -z sizelim\tsize limit (in entries) for search (default is no limit)\n") );
114*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -C ps:changetype[:changesonly[:entrychgcontrols]]\n") );
115*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\tchangetypes are add,delete,modify,moddn,any\n") );
116*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\tchangesonly and  entrychgcontrols are boolean values\n") );
117*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t(default is 1)\n") );
118*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -G before%cafter%cindex%ccount | before%cafter%cvalue where 'before' and\n"), VLV_PARAM_SEP, VLV_PARAM_SEP, VLV_PARAM_SEP, VLV_PARAM_SEP, VLV_PARAM_SEP );
119*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t'after' are the number of entries surrounding 'index.'\n"));
120*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t'count' is the content count, 'value' is the search value.\n"));
121*0Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
122*0Sstevel@tonic-gate     fprintf( stderr, gettext("    -c authzid\tspecifies the getEffectiveRights control authzid\n"));
123*0Sstevel@tonic-gate 	fprintf( stderr, gettext("\t\t eg. dn:uid=bjensen,dc=example,dc=com\n"));
124*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t A value of \"\" means \"the authorization id for the operation\".\n"));
125*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t A value of \"dn:\" means \"anonymous\"\n"));
126*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t (The aclRights operational attribute must be requested)\n"));
127*0Sstevel@tonic-gate 	fprintf( stderr, gettext("    -X attrlist\tspecifies the getEffectiveRights control specific attribute list.\n"));
128*0Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t eg. \"nsroledn userPassword\"\n"));
129*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate     exit( LDAP_PARAM_ERROR );
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate static char	*base = NULL;
135*0Sstevel@tonic-gate static char	*sep = LDAPTOOL_DEFSEP;
136*0Sstevel@tonic-gate static char	**sortattr = NULL;
137*0Sstevel@tonic-gate static char     *vlv_value = NULL;
138*0Sstevel@tonic-gate static int	sortsize = 0;
139*0Sstevel@tonic-gate static int	*skipsortattr = NULL;
140*0Sstevel@tonic-gate static int	includeufn, allow_binary, vals2tmp, ldif, scope, deref;
141*0Sstevel@tonic-gate static int	attrsonly, timelimit, sizelimit, server_sort, fold;
142*0Sstevel@tonic-gate static int	minimize_base64, produce_file_urls;
143*0Sstevel@tonic-gate static int	use_vlv = 0, vlv_before, vlv_after, vlv_index, vlv_count;
144*0Sstevel@tonic-gate static int	use_psearch=0;
145*0Sstevel@tonic-gate static int	write_ldif_version = 1;
146*0Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
147*0Sstevel@tonic-gate static char *get_effectiverights_control_target_dn = NULL; /* -c */
148*0Sstevel@tonic-gate static char **get_effectiverights_control_attrlist = NULL;  /* -X */
149*0Sstevel@tonic-gate static int	do_effective_rights_control = 0;
150*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /* Persistent search variables */
153*0Sstevel@tonic-gate static int	chgtype=0, changesonly=1, return_echg_ctls=1;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate int
157*0Sstevel@tonic-gate main( int argc, char **argv )
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate     char		*filtpattern, **attrs;
160*0Sstevel@tonic-gate     int			rc, optind, i, first, free_filtpattern;
161*0Sstevel@tonic-gate     LDAP		*ld;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
164*0Sstevel@tonic-gate     char *locale = setlocale(LC_ALL, "");
165*0Sstevel@tonic-gate     textdomain(TEXT_DOMAIN);
166*0Sstevel@tonic-gate     ldaptool_require_binddn = 0;
167*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate     free_filtpattern = 0;
170*0Sstevel@tonic-gate     deref = LDAP_DEREF_NEVER;
171*0Sstevel@tonic-gate     allow_binary = vals2tmp = attrsonly = 0;
172*0Sstevel@tonic-gate     minimize_base64 = produce_file_urls = 0;
173*0Sstevel@tonic-gate     ldif = 1;
174*0Sstevel@tonic-gate     fold = 1;
175*0Sstevel@tonic-gate     sizelimit = timelimit = 0;
176*0Sstevel@tonic-gate     scope = LDAP_SCOPE_SUBTREE;
177*0Sstevel@tonic-gate 	server_sort = 0;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate #ifdef notdef
181*0Sstevel@tonic-gate #ifdef HPUX11
182*0Sstevel@tonic-gate #ifndef __LP64__
183*0Sstevel@tonic-gate 	_main( argc, argv);
184*0Sstevel@tonic-gate #endif /* __LP64_ */
185*0Sstevel@tonic-gate #endif /* HPUX11 */
186*0Sstevel@tonic-gate #endif
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate     ldaptool_reset_control_array( ldaptool_request_ctrls );
190*0Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS
191*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
192*0Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "ABLTU1etuxra:b:F:G:l:S:s:z:C:",
193*0Sstevel@tonic-gate         0, options_callback );
194*0Sstevel@tonic-gate #else
195*0Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "ABLTU1etuxa:b:F:G:l:S:s:z:C:c:",
196*0Sstevel@tonic-gate         0, options_callback );
197*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
198*0Sstevel@tonic-gate #else
199*0Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "ABLTU1eotuxa:b:F:G:l:S:s:z:C:c:",
200*0Sstevel@tonic-gate         0, options_callback );
201*0Sstevel@tonic-gate #endif	/* HAVE_SASL_OPTIONS */
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate     if ( optind == -1 ) {
204*0Sstevel@tonic-gate 	usage();
205*0Sstevel@tonic-gate     }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate     if ( base == NULL ) {
208*0Sstevel@tonic-gate 	if (( base = getenv( "LDAP_BASEDN" )) == NULL ) {
209*0Sstevel@tonic-gate 	    usage();
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate     }
212*0Sstevel@tonic-gate     if ( sortattr ) {
213*0Sstevel@tonic-gate 	for ( sortsize = 0; sortattr[sortsize] != NULL; sortsize++ ) {
214*0Sstevel@tonic-gate 	    ;       /* NULL */
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 	sortsize++;             /* add in the final NULL field */
217*0Sstevel@tonic-gate 	skipsortattr = (int *) malloc( sortsize * sizeof(int *) );
218*0Sstevel@tonic-gate 	if ( skipsortattr == NULL ) {
219*0Sstevel@tonic-gate     		fprintf( stderr, gettext("Out of memory\n") );
220*0Sstevel@tonic-gate 		exit( LDAP_NO_MEMORY );
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 	memset( (char *) skipsortattr, 0, sortsize * sizeof(int *) );
223*0Sstevel@tonic-gate     } else if ( server_sort ) {
224*0Sstevel@tonic-gate 	server_sort = 0;   /* ignore this option if no sortattrs were given */
225*0Sstevel@tonic-gate     }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate     if ( argc - optind < 1 ) {
228*0Sstevel@tonic-gate 	if ( ldaptool_fp == NULL ) {
229*0Sstevel@tonic-gate 	    usage();
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 	attrs = NULL;
232*0Sstevel@tonic-gate 	filtpattern = "%s";
233*0Sstevel@tonic-gate     } else {	/* there are additional args (filter + attrs) */
234*0Sstevel@tonic-gate 	if ( ldaptool_fp == NULL || strstr( argv[ optind ], "%s" ) != NULL ) {
235*0Sstevel@tonic-gate 	    filtpattern = ldaptool_local2UTF8( argv[ optind ] );
236*0Sstevel@tonic-gate 	    /* since local2UTF8 always allocates something, we should free it */
237*0Sstevel@tonic-gate 	    free_filtpattern = 1;
238*0Sstevel@tonic-gate 	    ++optind;
239*0Sstevel@tonic-gate 	} else {
240*0Sstevel@tonic-gate 	    filtpattern = "%s";
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if ( argv[ optind ] == NULL ) {
244*0Sstevel@tonic-gate 	    attrs = NULL;
245*0Sstevel@tonic-gate 	} else if ( sortattr == NULL || *sortattr == '\0' || server_sort) {
246*0Sstevel@tonic-gate 	    attrs = &argv[ optind ];
247*0Sstevel@tonic-gate 	} else {
248*0Sstevel@tonic-gate 	    attrs = ldap_charray_dup( &argv[ optind ] );
249*0Sstevel@tonic-gate 	    if ( attrs == NULL ) {
250*0Sstevel@tonic-gate 		fprintf( stderr, gettext("Out of memory\n") );
251*0Sstevel@tonic-gate 		exit( LDAP_NO_MEMORY );
252*0Sstevel@tonic-gate 	    }
253*0Sstevel@tonic-gate 	    for ( i = 0; i < (sortsize - 1); i++ ) {
254*0Sstevel@tonic-gate                 if ( !ldap_charray_inlist( attrs, sortattr[i] ) ) {
255*0Sstevel@tonic-gate                     if ( ldap_charray_add( &attrs, sortattr[i] ) != 0 ) {
256*0Sstevel@tonic-gate     			fprintf( stderr, gettext("Out of memory\n") );
257*0Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
258*0Sstevel@tonic-gate 		    }
259*0Sstevel@tonic-gate                     /*
260*0Sstevel@tonic-gate                      * attribute in the search list only for the purpose of
261*0Sstevel@tonic-gate                      * sorting
262*0Sstevel@tonic-gate                      */
263*0Sstevel@tonic-gate                     skipsortattr[i] = 1;
264*0Sstevel@tonic-gate                 }
265*0Sstevel@tonic-gate 	    }
266*0Sstevel@tonic-gate         }
267*0Sstevel@tonic-gate     }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate     ld = ldaptool_ldap_init( 0 );
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate     if ( !ldaptool_not ) {
272*0Sstevel@tonic-gate 	ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
273*0Sstevel@tonic-gate 	ldap_set_option( ld, LDAP_OPT_TIMELIMIT, &timelimit );
274*0Sstevel@tonic-gate 	ldap_set_option( ld, LDAP_OPT_SIZELIMIT, &sizelimit );
275*0Sstevel@tonic-gate     }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate     ldaptool_bind( ld );
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate     if ( ldaptool_verbose ) {
280*0Sstevel@tonic-gate 	printf( gettext("filter pattern: %s\nreturning: "), filtpattern );
281*0Sstevel@tonic-gate 	if ( attrs == NULL ) {
282*0Sstevel@tonic-gate 	    printf( gettext("ALL") );
283*0Sstevel@tonic-gate 	} else {
284*0Sstevel@tonic-gate 	    for ( i = 0; attrs[ i ] != NULL; ++i ) {
285*0Sstevel@tonic-gate 		printf( "%s ", attrs[ i ] );
286*0Sstevel@tonic-gate 	    }
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 	putchar( '\n' );
289*0Sstevel@tonic-gate     }
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate     if ( ldaptool_fp == NULL ) {
292*0Sstevel@tonic-gate 	char *conv;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	conv = ldaptool_local2UTF8( base );
295*0Sstevel@tonic-gate 	rc = dosearch( ld, conv, scope, attrs, attrsonly, filtpattern, "" );
296*0Sstevel@tonic-gate 	if( conv != NULL )
297*0Sstevel@tonic-gate             free( conv );
298*0Sstevel@tonic-gate     } else {
299*0Sstevel@tonic-gate 	int done = 0;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	rc = LDAP_SUCCESS;
302*0Sstevel@tonic-gate 	first = 1;
303*0Sstevel@tonic-gate 	while ( rc == LDAP_SUCCESS && !done ) {
304*0Sstevel@tonic-gate 	    char *linep = NULL;
305*0Sstevel@tonic-gate 	    int   increment = 0;
306*0Sstevel@tonic-gate 	    int	  c, index;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	    /* allocate initial block of memory */
309*0Sstevel@tonic-gate 	    if ((linep = (char *)malloc(BUFSIZ)) == NULL) {
310*0Sstevel@tonic-gate 	        fprintf( stderr, gettext("Out of memory\n") );
311*0Sstevel@tonic-gate 		exit( LDAP_NO_MEMORY );
312*0Sstevel@tonic-gate 	    }
313*0Sstevel@tonic-gate 	    increment++;
314*0Sstevel@tonic-gate 	    index = 0;
315*0Sstevel@tonic-gate 	    while ((c = fgetc( ldaptool_fp )) != '\n' && c != EOF) {
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	        /* check if we will overflow the buffer */
318*0Sstevel@tonic-gate 	        if ((c != EOF) && (index == ((increment * BUFSIZ) -1))) {
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 		    /* if we did, add another BUFSIZ worth of bytes */
321*0Sstevel@tonic-gate 	   	    if ((linep = (char *)
322*0Sstevel@tonic-gate 			realloc(linep, (increment + 1) * BUFSIZ)) == NULL) {
323*0Sstevel@tonic-gate 			fprintf( stderr, gettext("Out of memory\n") );
324*0Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
325*0Sstevel@tonic-gate 		    }
326*0Sstevel@tonic-gate 		    increment++;
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 		linep[index++] = c;
329*0Sstevel@tonic-gate 	    }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	    if (c == EOF) {
332*0Sstevel@tonic-gate 		done = 1;
333*0Sstevel@tonic-gate 		break;
334*0Sstevel@tonic-gate 	    }
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	    linep[index] = '\0';
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	    if ( !first ) {
339*0Sstevel@tonic-gate 		putchar( '\n' );
340*0Sstevel@tonic-gate 	    } else {
341*0Sstevel@tonic-gate 		first = 0;
342*0Sstevel@tonic-gate 	    }
343*0Sstevel@tonic-gate 	    rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
344*0Sstevel@tonic-gate 		    linep );
345*0Sstevel@tonic-gate 	    free (linep);
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate     }
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate     ldaptool_cleanup( ld );
350*0Sstevel@tonic-gate     if (free_filtpattern != 0 && filtpattern != NULL) {
351*0Sstevel@tonic-gate 	free (filtpattern);
352*0Sstevel@tonic-gate     }
353*0Sstevel@tonic-gate     return( rc );
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate static void
358*0Sstevel@tonic-gate options_callback( int option, char *optarg )
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate     char *s, *temp_arg, *ps_ptr, *ps_arg;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate     switch( option ) {
363*0Sstevel@tonic-gate     case 'u':	/* include UFN */
364*0Sstevel@tonic-gate 	++includeufn;
365*0Sstevel@tonic-gate 	break;
366*0Sstevel@tonic-gate     case 't':	/* write attribute values to /tmp files */
367*0Sstevel@tonic-gate 	++vals2tmp;
368*0Sstevel@tonic-gate 	break;
369*0Sstevel@tonic-gate     case 'U':	/* produce file URLs in conjunction with -t */
370*0Sstevel@tonic-gate 	++produce_file_urls;
371*0Sstevel@tonic-gate 	break;
372*0Sstevel@tonic-gate     case 'e':	/* minimize base-64 encoding of values */
373*0Sstevel@tonic-gate 	++minimize_base64;
374*0Sstevel@tonic-gate 	break;
375*0Sstevel@tonic-gate     case 'A':	/* retrieve attribute names only -- no values */
376*0Sstevel@tonic-gate 	++attrsonly;
377*0Sstevel@tonic-gate 	break;
378*0Sstevel@tonic-gate     case 'L':       /* print entries in LDIF format -- now the default */
379*0Sstevel@tonic-gate 	break;
380*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
381*0Sstevel@tonic-gate     case 'r':	/* print entries in the old format */
382*0Sstevel@tonic-gate 	ldif = 0;
383*0Sstevel@tonic-gate 	break;
384*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
385*0Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS
386*0Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS_2
387*0Sstevel@tonic-gate     case 'o':	/* print entries using old ldapsearch format */
388*0Sstevel@tonic-gate 	ldif = 0;
389*0Sstevel@tonic-gate 	break;
390*0Sstevel@tonic-gate #endif
391*0Sstevel@tonic-gate #else
392*0Sstevel@tonic-gate     case 'o':	/* print entries using old ldapsearch format */
393*0Sstevel@tonic-gate 	ldif = 0;
394*0Sstevel@tonic-gate 	break;
395*0Sstevel@tonic-gate #endif
396*0Sstevel@tonic-gate     case 'B':	/* allow binary values to be printed, use old format */
397*0Sstevel@tonic-gate 	++allow_binary;
398*0Sstevel@tonic-gate 	ldif = 0;
399*0Sstevel@tonic-gate 	break;
400*0Sstevel@tonic-gate     case '1':	/* omit leading "version: #" line from LDIF output */
401*0Sstevel@tonic-gate 	write_ldif_version = 0;
402*0Sstevel@tonic-gate 	break;
403*0Sstevel@tonic-gate     case 's':	/* search scope */
404*0Sstevel@tonic-gate 	if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
405*0Sstevel@tonic-gate 	    scope = LDAP_SCOPE_BASE;
406*0Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
407*0Sstevel@tonic-gate 	    scope = LDAP_SCOPE_ONELEVEL;
408*0Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
409*0Sstevel@tonic-gate 	    scope = LDAP_SCOPE_SUBTREE;
410*0Sstevel@tonic-gate 	} else {
411*0Sstevel@tonic-gate 	    fprintf( stderr, gettext("scope should be base, one, or sub\n") );
412*0Sstevel@tonic-gate 	    usage();
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 	break;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate     case 'a':	/* set alias deref option */
417*0Sstevel@tonic-gate 	if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
418*0Sstevel@tonic-gate 	    deref = LDAP_DEREF_NEVER;
419*0Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
420*0Sstevel@tonic-gate 	    deref = LDAP_DEREF_SEARCHING;
421*0Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
422*0Sstevel@tonic-gate 	    deref = LDAP_DEREF_FINDING;
423*0Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
424*0Sstevel@tonic-gate 	    deref = LDAP_DEREF_ALWAYS;
425*0Sstevel@tonic-gate 	} else {
426*0Sstevel@tonic-gate 	    fprintf( stderr, gettext("alias deref should be never, search, find, or always\n") );
427*0Sstevel@tonic-gate 	    usage();
428*0Sstevel@tonic-gate 	}
429*0Sstevel@tonic-gate 	break;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate     case 'F':	/* field separator */
432*0Sstevel@tonic-gate 	sep = strdup( optarg );
433*0Sstevel@tonic-gate 	ldif = 0;
434*0Sstevel@tonic-gate 	break;
435*0Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
436*0Sstevel@tonic-gate 	case 'c':
437*0Sstevel@tonic-gate 		if ( optarg && optarg[0] == '\0' ) {
438*0Sstevel@tonic-gate 			/* -c ""
439*0Sstevel@tonic-gate 				means "This user"
440*0Sstevel@tonic-gate 			*/
441*0Sstevel@tonic-gate 			get_effectiverights_control_target_dn = NULL;
442*0Sstevel@tonic-gate 			do_effective_rights_control = 1;
443*0Sstevel@tonic-gate 		}else if ( strlen(optarg) < 3 || (strncasecmp(optarg, "dn:", 3) != 0) ) {
444*0Sstevel@tonic-gate 			fprintf(stderr, gettext("-c wrong format--should be \"\" or \"dn:...\".\n"
445*0Sstevel@tonic-gate 				"\"dn:\" means anonymous user."));
446*0Sstevel@tonic-gate 			usage();
447*0Sstevel@tonic-gate 		} else {
448*0Sstevel@tonic-gate 			get_effectiverights_control_target_dn = strdup(optarg);
449*0Sstevel@tonic-gate 			do_effective_rights_control = 1;
450*0Sstevel@tonic-gate 		}
451*0Sstevel@tonic-gate 	break;
452*0Sstevel@tonic-gate 	case 'X':
453*0Sstevel@tonic-gate 		get_effectiverights_control_attrlist = get_effectiverights_attrlist(optarg);
454*0Sstevel@tonic-gate 		do_effective_rights_control = 1;
455*0Sstevel@tonic-gate 	break;
456*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
457*0Sstevel@tonic-gate     case 'b':	/* searchbase */
458*0Sstevel@tonic-gate 	base = strdup( optarg );
459*0Sstevel@tonic-gate 	break;
460*0Sstevel@tonic-gate     case 'l':	/* time limit */
461*0Sstevel@tonic-gate 	timelimit = atoi( optarg );
462*0Sstevel@tonic-gate 	break;
463*0Sstevel@tonic-gate     case 'x':	/* server sorting requested */
464*0Sstevel@tonic-gate 	server_sort = 1;
465*0Sstevel@tonic-gate 	break;
466*0Sstevel@tonic-gate     case 'z':	/* size limit */
467*0Sstevel@tonic-gate 	sizelimit = atoi( optarg );
468*0Sstevel@tonic-gate 	break;
469*0Sstevel@tonic-gate     case 'S':	/* sort attribute */
470*0Sstevel@tonic-gate 	ldap_charray_add( &sortattr, strdup( optarg ) );
471*0Sstevel@tonic-gate 	break;
472*0Sstevel@tonic-gate     case 'T':	/* don't fold lines */
473*0Sstevel@tonic-gate 	fold = 0;
474*0Sstevel@tonic-gate 	break;
475*0Sstevel@tonic-gate     case 'G':  /* do the virtual list setup */
476*0Sstevel@tonic-gate 	use_vlv++;
477*0Sstevel@tonic-gate 	s = strchr(optarg, VLV_PARAM_SEP );
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	if (s != NULL)
480*0Sstevel@tonic-gate 	{
481*0Sstevel@tonic-gate 	    vlv_before = atoi(optarg);
482*0Sstevel@tonic-gate 	    s++;
483*0Sstevel@tonic-gate 	    vlv_after = atoi( s );
484*0Sstevel@tonic-gate 	    s = strchr(s, VLV_PARAM_SEP );
485*0Sstevel@tonic-gate 	    if (s != NULL)
486*0Sstevel@tonic-gate 	    {
487*0Sstevel@tonic-gate 		s++;
488*0Sstevel@tonic-gate 		/* below is a small set of logic to implement the following cases
489*0Sstevel@tonic-gate 		 * -G23:23:wilber
490*0Sstevel@tonic-gate 		 * -G23:23:"wilber:wright"
491*0Sstevel@tonic-gate 		 * -G23:23:'wilber'
492*0Sstevel@tonic-gate 		 * -G23:23:wilber wright
493*0Sstevel@tonic-gate 		 * all of the above are before, after, value -  NOTE: a colon not in a quoted
494*0Sstevel@tonic-gate 		 * string will break the parser!!!!
495*0Sstevel@tonic-gate 		 * -G23:23:45:600
496*0Sstevel@tonic-gate 		 * above is index, count encoding
497*0Sstevel@tonic-gate 		 */
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 		if (*s == '\'' || *s == '"')
500*0Sstevel@tonic-gate 		{
501*0Sstevel@tonic-gate 		    vlv_value = strdup( s );
502*0Sstevel@tonic-gate 		}
503*0Sstevel@tonic-gate 		else
504*0Sstevel@tonic-gate 		{
505*0Sstevel@tonic-gate 		    if (strchr( s, VLV_PARAM_SEP ))
506*0Sstevel@tonic-gate 		    {
507*0Sstevel@tonic-gate 			/* we have an index + count option */
508*0Sstevel@tonic-gate 			vlv_index = atoi( s );
509*0Sstevel@tonic-gate 			vlv_count = atoi( strchr( s, VLV_PARAM_SEP) + 1);
510*0Sstevel@tonic-gate 		    }
511*0Sstevel@tonic-gate 		    else
512*0Sstevel@tonic-gate 		    {
513*0Sstevel@tonic-gate 			/* we don't have a quote surrounding the assertion value
514*0Sstevel@tonic-gate 			 * do we need to???
515*0Sstevel@tonic-gate 			 */
516*0Sstevel@tonic-gate 			vlv_value = strdup( s );
517*0Sstevel@tonic-gate 		    }
518*0Sstevel@tonic-gate 		}
519*0Sstevel@tonic-gate 	    }
520*0Sstevel@tonic-gate 	    else
521*0Sstevel@tonic-gate 	    {
522*0Sstevel@tonic-gate 		fprintf( stderr,gettext("Illegal 'after' paramater for virtual list\n") );
523*0Sstevel@tonic-gate 		exit( LDAP_PARAM_ERROR );
524*0Sstevel@tonic-gate 	    }
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	}
527*0Sstevel@tonic-gate 	else
528*0Sstevel@tonic-gate 	{
529*0Sstevel@tonic-gate 	    fprintf( stderr,gettext("Illegal 'before' paramater for virtual list\n") );
530*0Sstevel@tonic-gate 	    exit( LDAP_PARAM_ERROR );
531*0Sstevel@tonic-gate 	}
532*0Sstevel@tonic-gate 	break;
533*0Sstevel@tonic-gate     case 'C':
534*0Sstevel@tonic-gate 	use_psearch++;
535*0Sstevel@tonic-gate 	if ( (ps_arg = strdup( optarg)) == NULL ) {
536*0Sstevel@tonic-gate 	    perror ("strdup");
537*0Sstevel@tonic-gate 	    exit (LDAP_NO_MEMORY);
538*0Sstevel@tonic-gate 	}
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	ps_ptr=strtok(ps_arg, ":");
541*0Sstevel@tonic-gate 	if (ps_ptr == NULL || (strcasecmp(ps_ptr, "ps")) ) {
542*0Sstevel@tonic-gate 	    fprintf (stderr, gettext("Invalid argument for -C\n"));
543*0Sstevel@tonic-gate 	    usage();
544*0Sstevel@tonic-gate 	}
545*0Sstevel@tonic-gate 	if (NULL != (ps_ptr=strtok(NULL, ":"))) {
546*0Sstevel@tonic-gate 	    if ( (temp_arg = strdup( ps_ptr )) == NULL ) {
547*0Sstevel@tonic-gate 	        perror ("strdup");
548*0Sstevel@tonic-gate 	    	exit (LDAP_NO_MEMORY);
549*0Sstevel@tonic-gate 	    }
550*0Sstevel@tonic-gate 	} else {
551*0Sstevel@tonic-gate 	    fprintf (stderr, gettext("Invalid argument for -C\n"));
552*0Sstevel@tonic-gate 	    usage();
553*0Sstevel@tonic-gate 	}
554*0Sstevel@tonic-gate 	if (NULL != (ps_ptr=strtok(NULL, ":"))) {
555*0Sstevel@tonic-gate 	    if ( (changesonly = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
556*0Sstevel@tonic-gate 		fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
557*0Sstevel@tonic-gate 		usage();
558*0Sstevel@tonic-gate 	    }
559*0Sstevel@tonic-gate 	}
560*0Sstevel@tonic-gate 	if (NULL != (ps_ptr=strtok(NULL, ":"))) {
561*0Sstevel@tonic-gate 	    if ( (return_echg_ctls = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
562*0Sstevel@tonic-gate 		fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
563*0Sstevel@tonic-gate 		usage();
564*0Sstevel@tonic-gate 	    }
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	/* Now parse the temp_arg and build chgtype as
568*0Sstevel@tonic-gate 	 * the changetypes are encountered */
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	if ((ps_ptr = strtok( temp_arg, "," )) == NULL) {
571*0Sstevel@tonic-gate 		usage();
572*0Sstevel@tonic-gate 	} else {
573*0Sstevel@tonic-gate 	    while ( ps_ptr ) {
574*0Sstevel@tonic-gate 		if ((strcasecmp(ps_ptr, "add"))==0)
575*0Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_ADD;
576*0Sstevel@tonic-gate 		else if ((strcasecmp(ps_ptr, "delete"))==0)
577*0Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_DELETE;
578*0Sstevel@tonic-gate 		else if ((strcasecmp(ps_ptr, "modify"))==0)
579*0Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_MODIFY;
580*0Sstevel@tonic-gate 		else if ((strcasecmp(ps_ptr, "moddn"))==0)
581*0Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_MODDN;
582*0Sstevel@tonic-gate 		else if ((strcasecmp(ps_ptr, "any"))==0)
583*0Sstevel@tonic-gate 		    chgtype = LDAP_CHANGETYPE_ANY;
584*0Sstevel@tonic-gate 		else {
585*0Sstevel@tonic-gate 			fprintf(stderr, gettext("Unknown changetype: %s\n"), ps_ptr);
586*0Sstevel@tonic-gate 			usage();
587*0Sstevel@tonic-gate 		}
588*0Sstevel@tonic-gate 		ps_ptr = strtok( NULL, "," );
589*0Sstevel@tonic-gate 	    }
590*0Sstevel@tonic-gate 	  }
591*0Sstevel@tonic-gate 	break;
592*0Sstevel@tonic-gate     default:
593*0Sstevel@tonic-gate 	usage();
594*0Sstevel@tonic-gate 	break;
595*0Sstevel@tonic-gate     }
596*0Sstevel@tonic-gate }
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate static int
600*0Sstevel@tonic-gate dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
601*0Sstevel@tonic-gate     LDAP	*ld;
602*0Sstevel@tonic-gate     char	*base;
603*0Sstevel@tonic-gate     int		scope;
604*0Sstevel@tonic-gate     char	**attrs;
605*0Sstevel@tonic-gate     int		attrsonly;
606*0Sstevel@tonic-gate     char	*filtpatt;
607*0Sstevel@tonic-gate     char	*value;
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate     char		**refs = NULL, filter[ BUFSIZ ], *filterp = NULL;
610*0Sstevel@tonic-gate     int			rc, first, matches;
611*0Sstevel@tonic-gate     LDAPMessage		*res, *e;
612*0Sstevel@tonic-gate     LDAPControl		*ldctrl;
613*0Sstevel@tonic-gate     LDAPControl		**ctrl_response_array = NULL;
614*0Sstevel@tonic-gate     LDAPVirtualList	vlv_data;
615*0Sstevel@tonic-gate     int			msgid = 0;
616*0Sstevel@tonic-gate     int			length = 0;
617*0Sstevel@tonic-gate     int			mallocd_filter = 0;
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate     if ( strstr( filtpatt, "%s" ) == NULL ) {	/* no need to sprintf() */
620*0Sstevel@tonic-gate 	    filterp = filtpatt;
621*0Sstevel@tonic-gate     } else {
622*0Sstevel@tonic-gate 	length = strlen( filtpatt ) + strlen ( value ) +1;
623*0Sstevel@tonic-gate 	if ( length > BUFSIZ ) {
624*0Sstevel@tonic-gate 	    if ((filterp = (char *)
625*0Sstevel@tonic-gate 		 malloc ( length )) == NULL) {
626*0Sstevel@tonic-gate 		perror( gettext("filter and/or pattern too long?") );
627*0Sstevel@tonic-gate 		exit (LDAP_PARAM_ERROR);
628*0Sstevel@tonic-gate 	    }
629*0Sstevel@tonic-gate 	    mallocd_filter = 1;
630*0Sstevel@tonic-gate 	} else {
631*0Sstevel@tonic-gate 	    filterp = filter;
632*0Sstevel@tonic-gate 	}
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate #ifdef HAVE_SNPRINTF
635*0Sstevel@tonic-gate 	if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {
636*0Sstevel@tonic-gate 	    perror( gettext("snprintf filter (filter and/or pattern too long?)") );
637*0Sstevel@tonic-gate 	    exit( LDAP_PARAM_ERROR );
638*0Sstevel@tonic-gate 	}
639*0Sstevel@tonic-gate #else
640*0Sstevel@tonic-gate 	sprintf( filterp, filtpatt, value );
641*0Sstevel@tonic-gate #endif
642*0Sstevel@tonic-gate     }
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate     if ( *filterp == '\0' ) {	/* treat empty filter is a shortcut for oc=* */
645*0Sstevel@tonic-gate 	if (mallocd_filter) {
646*0Sstevel@tonic-gate 	    free(filterp);
647*0Sstevel@tonic-gate 	    mallocd_filter = 0;
648*0Sstevel@tonic-gate 	}
649*0Sstevel@tonic-gate 	filterp = "(objectclass=*)";
650*0Sstevel@tonic-gate     }
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate     if ( ldaptool_verbose ) {
653*0Sstevel@tonic-gate 	/*
654*0Sstevel@tonic-gate 	 * Display the filter that will be used.  Add surrounding parens.
655*0Sstevel@tonic-gate 	 * if they are missing.
656*0Sstevel@tonic-gate 	 */
657*0Sstevel@tonic-gate 	if ( '(' == *filterp ) {
658*0Sstevel@tonic-gate 	    printf( "filter is: %s\n", filterp );
659*0Sstevel@tonic-gate 	} else {
660*0Sstevel@tonic-gate 	    printf( "filter is: (%s)\n", filterp );
661*0Sstevel@tonic-gate 	}
662*0Sstevel@tonic-gate     }
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate     if ( ldaptool_not ) {
665*0Sstevel@tonic-gate 	if (mallocd_filter) free(filterp);
666*0Sstevel@tonic-gate 	return( LDAP_SUCCESS );
667*0Sstevel@tonic-gate     }
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
670*0Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
671*0Sstevel@tonic-gate     }
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
674*0Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
675*0Sstevel@tonic-gate     }
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
678*0Sstevel@tonic-gate     if ( do_effective_rights_control ) {
679*0Sstevel@tonic-gate         if ((ldctrl = ldaptool_create_geteffectiveRights_control(ld,
680*0Sstevel@tonic-gate 							       get_effectiverights_control_target_dn,
681*0Sstevel@tonic-gate 							       (const char**)	get_effectiverights_control_attrlist)) !=NULL) {
682*0Sstevel@tonic-gate 	    ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
683*0Sstevel@tonic-gate         }
684*0Sstevel@tonic-gate     }
685*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate     if (use_psearch) {
688*0Sstevel@tonic-gate 	if ( ldap_create_persistentsearch_control( ld, chgtype,
689*0Sstevel@tonic-gate                 changesonly, return_echg_ctls,
690*0Sstevel@tonic-gate 		1, &ldctrl ) != LDAP_SUCCESS )
691*0Sstevel@tonic-gate 	{
692*0Sstevel@tonic-gate 		ldap_perror( ld, "ldap_create_persistentsearch_control" );
693*0Sstevel@tonic-gate 		return (1);
694*0Sstevel@tonic-gate 	}
695*0Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
696*0Sstevel@tonic-gate     }
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate     if (server_sort) {
700*0Sstevel@tonic-gate 	/* First make a sort key list from the attribute list we have */
701*0Sstevel@tonic-gate 	LDAPsortkey **keylist = NULL;
702*0Sstevel@tonic-gate 	int i = 0;
703*0Sstevel@tonic-gate 	char *sortattrs = NULL;
704*0Sstevel@tonic-gate 	char *s = NULL;
705*0Sstevel@tonic-gate 	int string_length = 0;
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	/* Count the sort strings */
708*0Sstevel@tonic-gate 	for (i = 0; i < sortsize - 1 ; i++) {
709*0Sstevel@tonic-gate 	    string_length += strlen(sortattr[i]) + 1;
710*0Sstevel@tonic-gate 	}
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	sortattrs = (char *) malloc(string_length + 1);
713*0Sstevel@tonic-gate 	if (NULL == sortattrs) {
714*0Sstevel@tonic-gate 	    fprintf( stderr, gettext("Out of memory\n") );
715*0Sstevel@tonic-gate 	    exit( LDAP_NO_MEMORY );
716*0Sstevel@tonic-gate 	}
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 	s = sortattrs;
719*0Sstevel@tonic-gate 	for (i = 0; i < sortsize - 1 ; i++) {
720*0Sstevel@tonic-gate 	    memcpy(s, sortattr[i], strlen(sortattr[i]));
721*0Sstevel@tonic-gate 	    s += strlen(sortattr[i]);
722*0Sstevel@tonic-gate 	    *s++ = ' ';
723*0Sstevel@tonic-gate 	}
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	sortattrs[string_length] = '\0';
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	ldap_create_sort_keylist(&keylist,sortattrs);
728*0Sstevel@tonic-gate 	free(sortattrs);
729*0Sstevel@tonic-gate 	sortattrs = NULL;
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	/* Then make a control for the sort attributes we have */
732*0Sstevel@tonic-gate 	rc = ldap_create_sort_control(ld,keylist,0,&ldctrl);
733*0Sstevel@tonic-gate 	ldap_free_sort_keylist(keylist);
734*0Sstevel@tonic-gate 	if ( rc != LDAP_SUCCESS ) {
735*0Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
736*0Sstevel@tonic-gate 	    return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
737*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
738*0Sstevel@tonic-gate 	}
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate     }
743*0Sstevel@tonic-gate     /* remember server side sorting must be available for vlv!!!! */
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate     if (use_vlv)
746*0Sstevel@tonic-gate     {
747*0Sstevel@tonic-gate 	vlv_data.ldvlist_before_count = vlv_before;
748*0Sstevel@tonic-gate 	vlv_data.ldvlist_after_count = vlv_after;
749*0Sstevel@tonic-gate 	if ( ldaptool_verbose ) {
750*0Sstevel@tonic-gate 	    printf( gettext("vlv data %lu, %lu, "),
751*0Sstevel@tonic-gate 		    vlv_data.ldvlist_before_count,
752*0Sstevel@tonic-gate 		    vlv_data.ldvlist_after_count
753*0Sstevel@tonic-gate 		);
754*0Sstevel@tonic-gate 	}
755*0Sstevel@tonic-gate 	if (vlv_value)
756*0Sstevel@tonic-gate 	{
757*0Sstevel@tonic-gate 	    vlv_data.ldvlist_attrvalue = vlv_value;
758*0Sstevel@tonic-gate 	    vlv_data.ldvlist_size = 0;
759*0Sstevel@tonic-gate 	    vlv_data.ldvlist_index = 0;
760*0Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
761*0Sstevel@tonic-gate 		printf( "%s, 0, 0\n", vlv_data.ldvlist_attrvalue);
762*0Sstevel@tonic-gate 	    }
763*0Sstevel@tonic-gate 	}
764*0Sstevel@tonic-gate 	else
765*0Sstevel@tonic-gate 	{
766*0Sstevel@tonic-gate 	    vlv_data.ldvlist_attrvalue = NULL;
767*0Sstevel@tonic-gate 	    vlv_data.ldvlist_size = vlv_count;
768*0Sstevel@tonic-gate 	    vlv_data.ldvlist_index = vlv_index;
769*0Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
770*0Sstevel@tonic-gate 		printf( "(null), %lu, %lu\n", vlv_data.ldvlist_size, vlv_data.ldvlist_index );
771*0Sstevel@tonic-gate 	    }
772*0Sstevel@tonic-gate 	}
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	if ( rc != LDAP_SUCCESS ) {
775*0Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
776*0Sstevel@tonic-gate 	    return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
777*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
778*0Sstevel@tonic-gate 	}
779*0Sstevel@tonic-gate 	if (LDAP_SUCCESS != (rc = ldap_create_virtuallist_control(ld,
780*0Sstevel@tonic-gate 		&vlv_data, &ldctrl)))
781*0Sstevel@tonic-gate 	{
782*0Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
783*0Sstevel@tonic-gate 	    return( ldaptool_print_lderror( ld,
784*0Sstevel@tonic-gate 		"ldap_create_virtuallist_control",
785*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
786*0Sstevel@tonic-gate 	}
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate     }
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate     if ( ldap_search_ext( ld, base, scope, filterp, attrs, attrsonly,
793*0Sstevel@tonic-gate 	    ldaptool_request_ctrls, NULL, NULL, -1, &msgid )
794*0Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
795*0Sstevel@tonic-gate 	if (mallocd_filter) free(filterp);
796*0Sstevel@tonic-gate 	return( ldaptool_print_lderror( ld, "ldap_search",
797*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
798*0Sstevel@tonic-gate     }
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate     matches = 0;
802*0Sstevel@tonic-gate     first = 1;
803*0Sstevel@tonic-gate     if ( sortattr && !server_sort ) {
804*0Sstevel@tonic-gate         rc = ldap_result( ld, LDAP_RES_ANY, 1, NULL, &res );
805*0Sstevel@tonic-gate     } else {
806*0Sstevel@tonic-gate         while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res )) !=
807*0Sstevel@tonic-gate 		LDAP_RES_SEARCH_RESULT && rc != -1 ) {
808*0Sstevel@tonic-gate 	    if ( rc != LDAP_RES_SEARCH_ENTRY ) {
809*0Sstevel@tonic-gate 		if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
810*0Sstevel@tonic-gate 		    parse_and_display_reference( ld, res );
811*0Sstevel@tonic-gate 		} else if ( rc == LDAP_RES_EXTENDED
812*0Sstevel@tonic-gate 			&& ldap_msgid( res ) == LDAP_RES_UNSOLICITED ) {
813*0Sstevel@tonic-gate 		    ldaptool_print_extended_response( ld, res,
814*0Sstevel@tonic-gate 			    gettext("Unsolicited response") );
815*0Sstevel@tonic-gate 		} else {
816*0Sstevel@tonic-gate 		    fprintf( stderr, gettext("%s: ignoring LDAP response message"
817*0Sstevel@tonic-gate 			    " type 0x%x (%s)\n"),
818*0Sstevel@tonic-gate 			    ldaptool_progname, rc, msgtype2str( rc ));
819*0Sstevel@tonic-gate 		}
820*0Sstevel@tonic-gate 		ldap_msgfree( res );
821*0Sstevel@tonic-gate 		continue;
822*0Sstevel@tonic-gate 	    }
823*0Sstevel@tonic-gate 	    matches++;
824*0Sstevel@tonic-gate 	    e = ldap_first_entry( ld, res );
825*0Sstevel@tonic-gate 	    if ( !first ) {
826*0Sstevel@tonic-gate 	        putchar( '\n' );
827*0Sstevel@tonic-gate 	    } else {
828*0Sstevel@tonic-gate 	        first = 0;
829*0Sstevel@tonic-gate 	    }
830*0Sstevel@tonic-gate 	    print_entry( ld, e, attrsonly );
831*0Sstevel@tonic-gate 	    ldap_msgfree( res );
832*0Sstevel@tonic-gate         }
833*0Sstevel@tonic-gate     }
834*0Sstevel@tonic-gate     if ( rc == -1 ) {
835*0Sstevel@tonic-gate 	if (mallocd_filter) free(filterp);
836*0Sstevel@tonic-gate 	return( ldaptool_print_lderror( ld, "ldap_result",
837*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
838*0Sstevel@tonic-gate     }
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate     if ( ldap_parse_result( ld, res, &rc, NULL, NULL, &refs,
841*0Sstevel@tonic-gate 	    &ctrl_response_array, 0 ) != LDAP_SUCCESS ) {
842*0Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_parse_result",
843*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
844*0Sstevel@tonic-gate     } else if ( rc != LDAP_SUCCESS ) {
845*0Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_search",
846*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
847*0Sstevel@tonic-gate     }
848*0Sstevel@tonic-gate     /* Parse the returned sort control */
849*0Sstevel@tonic-gate     if (server_sort) {
850*0Sstevel@tonic-gate 	unsigned long result = 0;
851*0Sstevel@tonic-gate 	char *attribute;
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 	if ( LDAP_SUCCESS != ldap_parse_sort_control(ld,ctrl_response_array,&result,&attribute) ) {
854*0Sstevel@tonic-gate 	    ldaptool_print_lderror(ld, "ldap_parse_sort_control",
855*0Sstevel@tonic-gate 		    LDAPTOOL_CHECK4SSL_IF_APPROP );
856*0Sstevel@tonic-gate 	    ldap_controls_free(ctrl_response_array);
857*0Sstevel@tonic-gate 	    ldap_msgfree(res);
858*0Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
859*0Sstevel@tonic-gate 	    return ( ldap_get_lderrno( ld, NULL, NULL ) );
860*0Sstevel@tonic-gate 	}
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	if (0 == result) {
863*0Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
864*0Sstevel@tonic-gate 		printf( gettext("Server indicated results sorted OK\n"));
865*0Sstevel@tonic-gate 	    }
866*0Sstevel@tonic-gate 	} else {
867*0Sstevel@tonic-gate 	    if (NULL != attribute) {
868*0Sstevel@tonic-gate 		printf(gettext("Server reported sorting error %ld: %s, attribute in error\"%s\"\n"),result,sortresult2string(result),attribute);
869*0Sstevel@tonic-gate 	    } else {
870*0Sstevel@tonic-gate 		printf(gettext("Server reported sorting error %ld: %s\n"),result,sortresult2string(result));
871*0Sstevel@tonic-gate 	    }
872*0Sstevel@tonic-gate 	}
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate     }
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate     if (use_vlv)
877*0Sstevel@tonic-gate     {
878*0Sstevel@tonic-gate 	unsigned long vpos, vcount;
879*0Sstevel@tonic-gate 	int vresult;
880*0Sstevel@tonic-gate 	if ( LDAP_SUCCESS != ldap_parse_virtuallist_control(ld,ctrl_response_array,&vpos, &vcount,&vresult) ) {
881*0Sstevel@tonic-gate 	    ldaptool_print_lderror( ld, "ldap_parse_virtuallist_control",
882*0Sstevel@tonic-gate 		    LDAPTOOL_CHECK4SSL_IF_APPROP );
883*0Sstevel@tonic-gate 	    ldap_controls_free(ctrl_response_array);
884*0Sstevel@tonic-gate 	    ldap_msgfree(res);
885*0Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
886*0Sstevel@tonic-gate 	    return ( ldap_get_lderrno( ld, NULL, NULL ) );
887*0Sstevel@tonic-gate 	}
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 	if (0 == vresult) {
890*0Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
891*0Sstevel@tonic-gate 		printf( gettext("Server indicated virtual list positioning OK\n"));
892*0Sstevel@tonic-gate 	    }
893*0Sstevel@tonic-gate 	    printf(gettext("index %lu content count %lu\n"), vpos, vcount);
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 	} else {
896*0Sstevel@tonic-gate 	    printf(gettext("Server reported sorting error %d: %s\n"),vresult,sortresult2string(vresult));
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate     }
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate     ldap_controls_free(ctrl_response_array);
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate     if ( sortattr != NULL && !server_sort) {
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 	(void) ldap_multisort_entries( ld, &res,
907*0Sstevel@tonic-gate 				       ( *sortattr == NULL ) ? NULL : sortattr,
908*0Sstevel@tonic-gate 				       (LDAP_CMP_CALLBACK *)strcasecmp );
909*0Sstevel@tonic-gate 	matches = 0;
910*0Sstevel@tonic-gate 	first = 1;
911*0Sstevel@tonic-gate 	for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
912*0Sstevel@tonic-gate 	      e = ldap_next_entry( ld, e ) ) {
913*0Sstevel@tonic-gate 	    matches++;
914*0Sstevel@tonic-gate 	    if ( !first ) {
915*0Sstevel@tonic-gate 		putchar( '\n' );
916*0Sstevel@tonic-gate 	    } else {
917*0Sstevel@tonic-gate 		first = 0;
918*0Sstevel@tonic-gate 	    }
919*0Sstevel@tonic-gate 	    print_entry( ld, e, attrsonly );
920*0Sstevel@tonic-gate 	}
921*0Sstevel@tonic-gate     }
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate     if ( ldaptool_verbose ) {
924*0Sstevel@tonic-gate 	printf( gettext("%d matches\n"), matches );
925*0Sstevel@tonic-gate     }
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate     if ( refs != NULL ) {
928*0Sstevel@tonic-gate 	ldaptool_print_referrals( refs );
929*0Sstevel@tonic-gate 	ldap_value_free( refs );
930*0Sstevel@tonic-gate     }
931*0Sstevel@tonic-gate 
932*0Sstevel@tonic-gate     if (mallocd_filter) free(filterp);
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate     ldap_msgfree( res );
935*0Sstevel@tonic-gate     return( rc );
936*0Sstevel@tonic-gate }
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate static void
940*0Sstevel@tonic-gate print_entry( ld, entry, attrsonly )
941*0Sstevel@tonic-gate     LDAP	*ld;
942*0Sstevel@tonic-gate     LDAPMessage	*entry;
943*0Sstevel@tonic-gate     int		attrsonly;
944*0Sstevel@tonic-gate {
945*0Sstevel@tonic-gate     char		*a, *dn, *ufn, tmpfname[ BUFSIZ ];
946*0Sstevel@tonic-gate     int			i, notascii;
947*0Sstevel@tonic-gate     BerElement		*ber;
948*0Sstevel@tonic-gate     struct berval	**bvals;
949*0Sstevel@tonic-gate     FILE		*tmpfp;
950*0Sstevel@tonic-gate #if defined( XP_WIN32 )
951*0Sstevel@tonic-gate 	char	mode[20] = "w+b";
952*0Sstevel@tonic-gate #else
953*0Sstevel@tonic-gate 	char	mode[20] = "w";
954*0Sstevel@tonic-gate #endif
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate     dn = ldap_get_dn( ld, entry );
957*0Sstevel@tonic-gate     write_string_attr_value( "dn", dn, LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
958*0Sstevel@tonic-gate     if ( includeufn ) {
959*0Sstevel@tonic-gate 	ufn = ldap_dn2ufn( dn );
960*0Sstevel@tonic-gate 	write_string_attr_value( "ufn", ufn,
961*0Sstevel@tonic-gate 			LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
962*0Sstevel@tonic-gate 	free( ufn );
963*0Sstevel@tonic-gate     }
964*0Sstevel@tonic-gate     ldap_memfree( dn );
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate     if ( use_psearch ) {
967*0Sstevel@tonic-gate 	LDAPControl	**ectrls;
968*0Sstevel@tonic-gate 	int		chgtype, chgnumpresent;
969*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
970*0Sstevel@tonic-gate 	ber_int_t	chgnum;
971*0Sstevel@tonic-gate #else
972*0Sstevel@tonic-gate 	long		chgnum;
973*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
974*0Sstevel@tonic-gate 	char		*prevdn, longbuf[ 128 ];
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 	if ( ldap_get_entry_controls( ld, entry, &ectrls ) == LDAP_SUCCESS ) {
977*0Sstevel@tonic-gate 	    if ( ldap_parse_entrychange_control( ld, ectrls, &chgtype,
978*0Sstevel@tonic-gate 			&prevdn, &chgnumpresent, &chgnum ) == LDAP_SUCCESS ) {
979*0Sstevel@tonic-gate 		write_string_attr_value(
980*0Sstevel@tonic-gate 			LDAPTOOL_PSEARCH_ATTR_PREFIX "changeType",
981*0Sstevel@tonic-gate 			changetype_num2string( chgtype ), 0 );
982*0Sstevel@tonic-gate 		if ( chgnumpresent ) {
983*0Sstevel@tonic-gate 		    sprintf( longbuf, "%d", chgnum );
984*0Sstevel@tonic-gate 		    write_string_attr_value(
985*0Sstevel@tonic-gate 			    LDAPTOOL_PSEARCH_ATTR_PREFIX "changeNumber",
986*0Sstevel@tonic-gate 			    longbuf, 0 );
987*0Sstevel@tonic-gate 		}
988*0Sstevel@tonic-gate 		if ( NULL != prevdn ) {
989*0Sstevel@tonic-gate 		    write_string_attr_value(
990*0Sstevel@tonic-gate 			    LDAPTOOL_PSEARCH_ATTR_PREFIX "previousDN",
991*0Sstevel@tonic-gate 			    prevdn, 0 );
992*0Sstevel@tonic-gate 		    ldap_memfree( prevdn );
993*0Sstevel@tonic-gate 		}
994*0Sstevel@tonic-gate 	    }
995*0Sstevel@tonic-gate 	    ldap_controls_free (ectrls);
996*0Sstevel@tonic-gate 	}
997*0Sstevel@tonic-gate     }
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1000*0Sstevel@tonic-gate 	    a = ldap_next_attribute( ld, entry, ber ) ) {
1001*0Sstevel@tonic-gate 	if ( ldap_charray_inlist(sortattr, a) &&            /* in the list*/
1002*0Sstevel@tonic-gate 	    skipsortattr[ldap_charray_position(sortattr, a)] ) {/* and skip it*/
1003*0Sstevel@tonic-gate 	    continue;                                       /* so skip it! */
1004*0Sstevel@tonic-gate 	}
1005*0Sstevel@tonic-gate 	if ( attrsonly ) {
1006*0Sstevel@tonic-gate 	    if ( ldif ) {
1007*0Sstevel@tonic-gate 		write_ldif_value( a, "", 0, 0 );
1008*0Sstevel@tonic-gate 	    } else {
1009*0Sstevel@tonic-gate 		printf( "%s\n", a );
1010*0Sstevel@tonic-gate 	    }
1011*0Sstevel@tonic-gate 	} else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1012*0Sstevel@tonic-gate 	    for ( i = 0; bvals[i] != NULL; i++ ) {
1013*0Sstevel@tonic-gate 		if ( vals2tmp ) {
1014*0Sstevel@tonic-gate #ifdef HAVE_SNPRINTF
1015*0Sstevel@tonic-gate 		    if ( snprintf( tmpfname, sizeof(tmpfname),
1016*0Sstevel@tonic-gate 			    "%s/ldapsearch-%s-XXXXXX",
1017*0Sstevel@tonic-gate 			    ldaptool_get_tmp_dir(), a ) < 0 ) {
1018*0Sstevel@tonic-gate 			perror( gettext("snprintf tmpfname (attribute name too long?)") );
1019*0Sstevel@tonic-gate 			exit( LDAP_PARAM_ERROR );
1020*0Sstevel@tonic-gate 		    }
1021*0Sstevel@tonic-gate #else
1022*0Sstevel@tonic-gate 		    sprintf( tmpfname, "%s/ldapsearch-%s-XXXXXX",
1023*0Sstevel@tonic-gate 			    ldaptool_get_tmp_dir(), a );
1024*0Sstevel@tonic-gate #endif
1025*0Sstevel@tonic-gate 		    tmpfp = NULL;
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate 		    if ( LDAPTOOL_MKTEMP( tmpfname ) == NULL ) {
1028*0Sstevel@tonic-gate 			perror( tmpfname );
1029*0Sstevel@tonic-gate 		    } else if (( tmpfp = ldaptool_open_file( tmpfname, mode)) == NULL ) {
1030*0Sstevel@tonic-gate 			perror( tmpfname );
1031*0Sstevel@tonic-gate 		    } else if ( bvals[ i ]->bv_len > 0 &&
1032*0Sstevel@tonic-gate 			    fwrite( bvals[ i ]->bv_val,
1033*0Sstevel@tonic-gate 			    bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
1034*0Sstevel@tonic-gate 			perror( tmpfname );
1035*0Sstevel@tonic-gate 		    } else if ( ldif ) {
1036*0Sstevel@tonic-gate 			if ( produce_file_urls ) {
1037*0Sstevel@tonic-gate 			    char	*url;
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate 			    if ( ldaptool_path2fileurl( tmpfname, &url ) !=
1040*0Sstevel@tonic-gate 				LDAPTOOL_FILEURL_SUCCESS ) {
1041*0Sstevel@tonic-gate 				    perror( "ldaptool_path2fileurl" );
1042*0Sstevel@tonic-gate 				} else {
1043*0Sstevel@tonic-gate 				    write_ldif_value( a, url, strlen( url ),
1044*0Sstevel@tonic-gate 					    LDIF_OPT_VALUE_IS_URL );
1045*0Sstevel@tonic-gate 				    free( url );
1046*0Sstevel@tonic-gate 				}
1047*0Sstevel@tonic-gate 			} else {
1048*0Sstevel@tonic-gate 			    write_ldif_value( a, tmpfname, strlen( tmpfname ),
1049*0Sstevel@tonic-gate 				    0 );
1050*0Sstevel@tonic-gate 			}
1051*0Sstevel@tonic-gate 		    } else {
1052*0Sstevel@tonic-gate 			printf( "%s%s%s\n", a, sep, tmpfname );
1053*0Sstevel@tonic-gate 		    }
1054*0Sstevel@tonic-gate 
1055*0Sstevel@tonic-gate 		    if ( tmpfp != NULL ) {
1056*0Sstevel@tonic-gate 			fclose( tmpfp );
1057*0Sstevel@tonic-gate 		    }
1058*0Sstevel@tonic-gate 		} else {
1059*0Sstevel@tonic-gate 		    notascii = 0;
1060*0Sstevel@tonic-gate 		    if ( !ldif && !allow_binary ) {
1061*0Sstevel@tonic-gate 			notascii = !ldaptool_berval_is_ascii( bvals[i] );
1062*0Sstevel@tonic-gate 		    }
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 		    if ( ldif ) {
1065*0Sstevel@tonic-gate 			write_ldif_value( a, bvals[ i ]->bv_val,
1066*0Sstevel@tonic-gate 				bvals[ i ]->bv_len, 0 );
1067*0Sstevel@tonic-gate 		    } else {
1068*0Sstevel@tonic-gate 			printf( "%s%s%s\n", a, sep,
1069*0Sstevel@tonic-gate 				notascii ? gettext("NOT ASCII") : bvals[ i ]->bv_val );
1070*0Sstevel@tonic-gate 		    }
1071*0Sstevel@tonic-gate 		}
1072*0Sstevel@tonic-gate 	    }
1073*0Sstevel@tonic-gate 	    ber_bvecfree( bvals );
1074*0Sstevel@tonic-gate 	}
1075*0Sstevel@tonic-gate 	ldap_memfree( a );
1076*0Sstevel@tonic-gate     }
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate     if ( ldap_get_lderrno( ld, NULL, NULL ) != LDAP_SUCCESS ) {
1079*0Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_first_attribute/ldap_next_attribute",
1080*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
1081*0Sstevel@tonic-gate     }
1082*0Sstevel@tonic-gate 
1083*0Sstevel@tonic-gate     if ( ber != NULL ) {
1084*0Sstevel@tonic-gate 	ber_free( ber, 0 );
1085*0Sstevel@tonic-gate     }
1086*0Sstevel@tonic-gate }
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate static void
1090*0Sstevel@tonic-gate write_string_attr_value( char *attrname, char *strval, unsigned long opts )
1091*0Sstevel@tonic-gate {
1092*0Sstevel@tonic-gate     if ( strval == NULL ) {
1093*0Sstevel@tonic-gate 	strval = "";
1094*0Sstevel@tonic-gate     }
1095*0Sstevel@tonic-gate     if ( ldif ) {
1096*0Sstevel@tonic-gate 	write_ldif_value( attrname, strval, strlen( strval ), 0 );
1097*0Sstevel@tonic-gate     } else if ( 0 != ( opts & LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME )) {
1098*0Sstevel@tonic-gate 	printf( "%s\n", strval );
1099*0Sstevel@tonic-gate     } else {
1100*0Sstevel@tonic-gate 	printf( "%s%s%s\n", attrname, sep, strval );
1101*0Sstevel@tonic-gate     }
1102*0Sstevel@tonic-gate }
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate static int
1106*0Sstevel@tonic-gate write_ldif_value( char *type, char *value, unsigned long vallen,
1107*0Sstevel@tonic-gate 	unsigned long ldifoptions )
1108*0Sstevel@tonic-gate {
1109*0Sstevel@tonic-gate     char	*ldif;
1110*0Sstevel@tonic-gate     static int	wrote_version = 0;
1111*0Sstevel@tonic-gate 
1112*0Sstevel@tonic-gate     if ( write_ldif_version && !wrote_version ) {
1113*0Sstevel@tonic-gate 	char	versionbuf[ 64 ];
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 	wrote_version = 1;
1116*0Sstevel@tonic-gate 	sprintf( versionbuf, "%d", LDIF_VERSION_ONE );
1117*0Sstevel@tonic-gate 	write_ldif_value( "version", versionbuf, strlen( versionbuf ), 0 );
1118*0Sstevel@tonic-gate     }
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate     if ( !fold ) {
1121*0Sstevel@tonic-gate 	ldifoptions |= LDIF_OPT_NOWRAP;
1122*0Sstevel@tonic-gate     }
1123*0Sstevel@tonic-gate     if ( minimize_base64 ) {
1124*0Sstevel@tonic-gate 	ldifoptions |= LDIF_OPT_MINIMAL_ENCODING;
1125*0Sstevel@tonic-gate     }
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate     if (( ldif = ldif_type_and_value_with_options( type, value, (int)vallen,
1128*0Sstevel@tonic-gate 	    ldifoptions )) == NULL ) {
1129*0Sstevel@tonic-gate 	return( -1 );
1130*0Sstevel@tonic-gate     }
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate     fputs( ldif, stdout );
1133*0Sstevel@tonic-gate     free( ldif );
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate     return( 0 );
1136*0Sstevel@tonic-gate }
1137*0Sstevel@tonic-gate 
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate static char *
1140*0Sstevel@tonic-gate sortresult2string(unsigned long result)
1141*0Sstevel@tonic-gate {
1142*0Sstevel@tonic-gate 	/*
1143*0Sstevel@tonic-gate             success                   (0), -- results are sorted
1144*0Sstevel@tonic-gate             operationsError           (1), -- server internal failure
1145*0Sstevel@tonic-gate             timeLimitExceeded         (3), -- timelimit reached before
1146*0Sstevel@tonic-gate                                            -- sorting was completed
1147*0Sstevel@tonic-gate             strongAuthRequired        (8), -- refused to return sorted
1148*0Sstevel@tonic-gate                                            -- results via insecure
1149*0Sstevel@tonic-gate                                            -- protocol
1150*0Sstevel@tonic-gate             adminLimitExceeded       (11), -- too many matching entries
1151*0Sstevel@tonic-gate                                            -- for the server to sort
1152*0Sstevel@tonic-gate             noSuchAttribute          (16), -- unrecognized attribute
1153*0Sstevel@tonic-gate                                            -- type in sort key
1154*0Sstevel@tonic-gate             inappropriateMatching    (18), -- unrecognized or inappro-
1155*0Sstevel@tonic-gate                                            -- priate matching rule in
1156*0Sstevel@tonic-gate                                            -- sort key
1157*0Sstevel@tonic-gate             insufficientAccessRights (50), -- refused to return sorted
1158*0Sstevel@tonic-gate                                            -- results to this client
1159*0Sstevel@tonic-gate             busy                     (51), -- too busy to process
1160*0Sstevel@tonic-gate             unwillingToPerform       (53), -- unable to sort
1161*0Sstevel@tonic-gate             other                    (80)
1162*0Sstevel@tonic-gate 	*/
1163*0Sstevel@tonic-gate 
1164*0Sstevel@tonic-gate 	switch (result) {
1165*0Sstevel@tonic-gate 	case 0: return (gettext("success"));
1166*0Sstevel@tonic-gate 	case 1: return (gettext("operations error"));
1167*0Sstevel@tonic-gate 	case 3: return (gettext("time limit exceeded"));
1168*0Sstevel@tonic-gate 	case 8: return (gettext("strong auth required"));
1169*0Sstevel@tonic-gate 	case 11: return (gettext("admin limit exceeded"));
1170*0Sstevel@tonic-gate 	case 16: return (gettext("no such attribute"));
1171*0Sstevel@tonic-gate 	case 18: return (gettext("unrecognized or inappropriate matching rule"));
1172*0Sstevel@tonic-gate 	case 50: return (gettext("insufficient access rights"));
1173*0Sstevel@tonic-gate 	case 51: return (gettext("too busy"));
1174*0Sstevel@tonic-gate 	case 53: return (gettext("unable to sort"));
1175*0Sstevel@tonic-gate 	case 80:
1176*0Sstevel@tonic-gate         default: return (gettext("Er...Other ?"));
1177*0Sstevel@tonic-gate 	}
1178*0Sstevel@tonic-gate }
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate 
1181*0Sstevel@tonic-gate static void
1182*0Sstevel@tonic-gate parse_and_display_reference( LDAP *ld, LDAPMessage *ref )
1183*0Sstevel@tonic-gate {
1184*0Sstevel@tonic-gate     int		i;
1185*0Sstevel@tonic-gate     char	**refs;
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate     if ( ldap_parse_reference( ld, ref, &refs, NULL, 0 ) != LDAP_SUCCESS ) {
1188*0Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_parse_reference",
1189*0Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
1190*0Sstevel@tonic-gate     } else if ( refs != NULL && refs[ 0 ] != NULL ) {
1191*0Sstevel@tonic-gate 	fputs( gettext("Unfollowed continuation reference(s):\n"), stderr );
1192*0Sstevel@tonic-gate 	for ( i = 0; refs[ i ] != NULL; ++i ) {
1193*0Sstevel@tonic-gate 	    fprintf( stderr, "    %s\n", refs[ i ] );
1194*0Sstevel@tonic-gate 	}
1195*0Sstevel@tonic-gate 	ldap_value_free( refs );
1196*0Sstevel@tonic-gate     }
1197*0Sstevel@tonic-gate }
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 
1200*0Sstevel@tonic-gate /*possible operations a client can invoke -- copied from ldaprot.h */
1201*0Sstevel@tonic-gate 
1202*0Sstevel@tonic-gate #ifndef LDAP_REQ_BIND
1203*0Sstevel@tonic-gate #define LDAP_REQ_BIND                   0x60L   /* application + constructed */
1204*0Sstevel@tonic-gate #define LDAP_REQ_UNBIND                 0x42L   /* application + primitive   */
1205*0Sstevel@tonic-gate #define LDAP_REQ_SEARCH                 0x63L   /* application + constructed */
1206*0Sstevel@tonic-gate #define LDAP_REQ_MODIFY                 0x66L   /* application + constructed */
1207*0Sstevel@tonic-gate #define LDAP_REQ_ADD                    0x68L   /* application + constructed */
1208*0Sstevel@tonic-gate #define LDAP_REQ_DELETE                 0x4aL   /* application + primitive   */
1209*0Sstevel@tonic-gate #define LDAP_REQ_RENAME                 0x6cL   /* application + constructed */
1210*0Sstevel@tonic-gate #define LDAP_REQ_COMPARE                0x6eL   /* application + constructed */
1211*0Sstevel@tonic-gate #define LDAP_REQ_ABANDON                0x50L   /* application + primitive   */
1212*0Sstevel@tonic-gate #define LDAP_REQ_EXTENDED               0x77L   /* application + constructed */
1213*0Sstevel@tonic-gate #endif /* LDAP_REQ_BIND */
1214*0Sstevel@tonic-gate 
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate struct ldapsearch_type2str {
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate     int         ldst2s_type;    /* message type */
1220*0Sstevel@tonic-gate     char        *ldst2s_string; /* descriptive string */
1221*0Sstevel@tonic-gate };
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1224*0Sstevel@tonic-gate static struct ldapsearch_type2str ldapsearch_msgtypes[] = {
1225*0Sstevel@tonic-gate 
1226*0Sstevel@tonic-gate     /* results: */
1227*0Sstevel@tonic-gate     { LDAP_RES_BIND,                    NULL },
1228*0Sstevel@tonic-gate     { LDAP_RES_SEARCH_REFERENCE,        NULL },
1229*0Sstevel@tonic-gate     { LDAP_RES_SEARCH_ENTRY,            NULL },
1230*0Sstevel@tonic-gate     { LDAP_RES_SEARCH_RESULT,           NULL },
1231*0Sstevel@tonic-gate     { LDAP_RES_MODIFY,                  NULL },
1232*0Sstevel@tonic-gate     { LDAP_RES_ADD,                     NULL },
1233*0Sstevel@tonic-gate     { LDAP_RES_DELETE,                  NULL },
1234*0Sstevel@tonic-gate     { LDAP_RES_MODDN,                   NULL },
1235*0Sstevel@tonic-gate     { LDAP_RES_COMPARE,                 NULL },
1236*0Sstevel@tonic-gate     { LDAP_RES_EXTENDED,                NULL },
1237*0Sstevel@tonic-gate     /* requests: */
1238*0Sstevel@tonic-gate     { LDAP_REQ_BIND,                    NULL },
1239*0Sstevel@tonic-gate     { LDAP_REQ_UNBIND,                  NULL },
1240*0Sstevel@tonic-gate     { LDAP_REQ_SEARCH,                  NULL },
1241*0Sstevel@tonic-gate     { LDAP_REQ_MODIFY,                  NULL },
1242*0Sstevel@tonic-gate     { LDAP_REQ_ADD,                     NULL },
1243*0Sstevel@tonic-gate     { LDAP_REQ_DELETE,                  NULL },
1244*0Sstevel@tonic-gate     { LDAP_REQ_RENAME,                  NULL },
1245*0Sstevel@tonic-gate     { LDAP_REQ_COMPARE,                 NULL },
1246*0Sstevel@tonic-gate     { LDAP_REQ_ABANDON,                 NULL },
1247*0Sstevel@tonic-gate     { LDAP_REQ_EXTENDED,                NULL },
1248*0Sstevel@tonic-gate 
1249*0Sstevel@tonic-gate };
1250*0Sstevel@tonic-gate #else
1251*0Sstevel@tonic-gate static struct ldapsearch_type2str ldapsearch_msgtypes[] = {
1252*0Sstevel@tonic-gate 
1253*0Sstevel@tonic-gate     /* results: */
1254*0Sstevel@tonic-gate     { LDAP_RES_BIND,                    "bind result" },
1255*0Sstevel@tonic-gate     { LDAP_RES_SEARCH_REFERENCE,        "continuation reference" },
1256*0Sstevel@tonic-gate     { LDAP_RES_SEARCH_ENTRY,            "entry" },
1257*0Sstevel@tonic-gate     { LDAP_RES_SEARCH_RESULT,           "search result" },
1258*0Sstevel@tonic-gate     { LDAP_RES_MODIFY,                  "modify result" },
1259*0Sstevel@tonic-gate     { LDAP_RES_ADD,                     "add result" },
1260*0Sstevel@tonic-gate     { LDAP_RES_DELETE,                  "delete result" },
1261*0Sstevel@tonic-gate     { LDAP_RES_MODDN,                   "rename result" },
1262*0Sstevel@tonic-gate     { LDAP_RES_COMPARE,                 "compare result" },
1263*0Sstevel@tonic-gate     { LDAP_RES_EXTENDED,                "extended operation result" },
1264*0Sstevel@tonic-gate     /* requests: */
1265*0Sstevel@tonic-gate     { LDAP_REQ_BIND,                    "bind request" },
1266*0Sstevel@tonic-gate     { LDAP_REQ_UNBIND,                  "unbind request" },
1267*0Sstevel@tonic-gate     { LDAP_REQ_SEARCH,                  "search request" },
1268*0Sstevel@tonic-gate     { LDAP_REQ_MODIFY,                  "modify request" },
1269*0Sstevel@tonic-gate     { LDAP_REQ_ADD,                     "add request" },
1270*0Sstevel@tonic-gate     { LDAP_REQ_DELETE,                  "delete request" },
1271*0Sstevel@tonic-gate     { LDAP_REQ_RENAME,                  "rename request" },
1272*0Sstevel@tonic-gate     { LDAP_REQ_COMPARE,                 "compare request" },
1273*0Sstevel@tonic-gate     { LDAP_REQ_ABANDON,                 "abandon request" },
1274*0Sstevel@tonic-gate     { LDAP_REQ_EXTENDED,                "extended request" },
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate };
1277*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1278*0Sstevel@tonic-gate 
1279*0Sstevel@tonic-gate 
1280*0Sstevel@tonic-gate #define LDAPSEARCHTOOL_NUMTYPES (sizeof(ldapsearch_msgtypes) \
1281*0Sstevel@tonic-gate 				 / sizeof(struct ldapsearch_type2str))
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1284*0Sstevel@tonic-gate static void
1285*0Sstevel@tonic-gate fill_ldapsearch_msgtypes( void )
1286*0Sstevel@tonic-gate {
1287*0Sstevel@tonic-gate     int		i = 0;
1288*0Sstevel@tonic-gate     if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
1289*0Sstevel@tonic-gate 	!= NULL)
1290*0Sstevel@tonic-gate 		return;
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate     /* results: */
1293*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1294*0Sstevel@tonic-gate 			"bind result");
1295*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1296*0Sstevel@tonic-gate 			"continuation reference");
1297*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1298*0Sstevel@tonic-gate 			"entry");
1299*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1300*0Sstevel@tonic-gate     			"search result");
1301*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1302*0Sstevel@tonic-gate     			"modify result");
1303*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1304*0Sstevel@tonic-gate     			"add result");
1305*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1306*0Sstevel@tonic-gate     			"delete result");
1307*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1308*0Sstevel@tonic-gate     			"rename result");
1309*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1310*0Sstevel@tonic-gate     			"compare result");
1311*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1312*0Sstevel@tonic-gate     			"extended operation result");
1313*0Sstevel@tonic-gate     /* requests: */
1314*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1315*0Sstevel@tonic-gate     			"bind request");
1316*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1317*0Sstevel@tonic-gate     			"unbind request");
1318*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1319*0Sstevel@tonic-gate     			"search request");
1320*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1321*0Sstevel@tonic-gate     			"modify request");
1322*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1323*0Sstevel@tonic-gate     			"add request");
1324*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1325*0Sstevel@tonic-gate     			"delete request");
1326*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1327*0Sstevel@tonic-gate     			"rename request");
1328*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1329*0Sstevel@tonic-gate     			"compare request");
1330*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1331*0Sstevel@tonic-gate     			"abandon request");
1332*0Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1333*0Sstevel@tonic-gate 			"extended request");
1334*0Sstevel@tonic-gate }
1335*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1336*0Sstevel@tonic-gate 
1337*0Sstevel@tonic-gate /*
1338*0Sstevel@tonic-gate  * Return a descriptive string given an LDAP result message type (tag).
1339*0Sstevel@tonic-gate  */
1340*0Sstevel@tonic-gate static char *
1341*0Sstevel@tonic-gate msgtype2str( int msgtype )
1342*0Sstevel@tonic-gate {
1343*0Sstevel@tonic-gate     char        *s = gettext("unknown");
1344*0Sstevel@tonic-gate     int         i;
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1347*0Sstevel@tonic-gate     /* Make sure ldapsearch_msgtypes is initialized */
1348*0Sstevel@tonic-gate     if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
1349*0Sstevel@tonic-gate 	== NULL)
1350*0Sstevel@tonic-gate 		(void) fill_ldapsearch_msgtypes();
1351*0Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate     for ( i = 0; i < LDAPSEARCHTOOL_NUMTYPES; ++i ) {
1354*0Sstevel@tonic-gate 	if ( msgtype == ldapsearch_msgtypes[ i ].ldst2s_type ) {
1355*0Sstevel@tonic-gate 	    s = ldapsearch_msgtypes[ i ].ldst2s_string;
1356*0Sstevel@tonic-gate 	}
1357*0Sstevel@tonic-gate     }
1358*0Sstevel@tonic-gate     return( s );
1359*0Sstevel@tonic-gate }
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 
1362*0Sstevel@tonic-gate /*
1363*0Sstevel@tonic-gate  * Return a descriptive string given a Persistent Search change type
1364*0Sstevel@tonic-gate  */
1365*0Sstevel@tonic-gate static char *
1366*0Sstevel@tonic-gate changetype_num2string( int chgtype )
1367*0Sstevel@tonic-gate {
1368*0Sstevel@tonic-gate     char	*s = gettext("unknown");
1369*0Sstevel@tonic-gate 
1370*0Sstevel@tonic-gate     switch( chgtype ) {
1371*0Sstevel@tonic-gate     case LDAP_CHANGETYPE_ADD:
1372*0Sstevel@tonic-gate 	s = gettext("add");
1373*0Sstevel@tonic-gate 	break;
1374*0Sstevel@tonic-gate     case LDAP_CHANGETYPE_DELETE:
1375*0Sstevel@tonic-gate 	s = gettext("delete");
1376*0Sstevel@tonic-gate 	break;
1377*0Sstevel@tonic-gate     case LDAP_CHANGETYPE_MODIFY:
1378*0Sstevel@tonic-gate 	s = gettext("modify");
1379*0Sstevel@tonic-gate 	break;
1380*0Sstevel@tonic-gate     case LDAP_CHANGETYPE_MODDN:
1381*0Sstevel@tonic-gate 	s = gettext("moddn");
1382*0Sstevel@tonic-gate 	break;
1383*0Sstevel@tonic-gate     }
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate     return( s );
1386*0Sstevel@tonic-gate }
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate /* returns a null teminated charrary */
1389*0Sstevel@tonic-gate static char  **get_effectiverights_attrlist(char * optarg) {
1390*0Sstevel@tonic-gate 
1391*0Sstevel@tonic-gate 	char * tmp_str = strdup(optarg);
1392*0Sstevel@tonic-gate 	char ** retArray = NULL;
1393*0Sstevel@tonic-gate 	int i = 0;
1394*0Sstevel@tonic-gate 
1395*0Sstevel@tonic-gate 	retArray = ldap_str2charray( tmp_str, " "); /* takes copies */
1396*0Sstevel@tonic-gate 
1397*0Sstevel@tonic-gate 	free(tmp_str);
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 	/* Oops - somebody left this debug message in for the
1400*0Sstevel@tonic-gate 	   getEffectiveRights control
1401*0Sstevel@tonic-gate 	   fprintf(stderr, "attrlist: "); */
1402*0Sstevel@tonic-gate 	i = 0;
1403*0Sstevel@tonic-gate 	while( retArray[i] != NULL ) {
1404*0Sstevel@tonic-gate 
1405*0Sstevel@tonic-gate 		fprintf(stderr,"%s ", retArray[i]);
1406*0Sstevel@tonic-gate 		i++;
1407*0Sstevel@tonic-gate 	}
1408*0Sstevel@tonic-gate 	fprintf(stderr, "\n");
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate 	return(retArray);
1411*0Sstevel@tonic-gate 
1412*0Sstevel@tonic-gate }
1413