17c478bd9Sstevel@tonic-gate /*
2ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*55fea89dSDan Cross /*
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
11*55fea89dSDan Cross  *
127c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
16*55fea89dSDan Cross  *
177c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate  * March 31, 1998.
19*55fea89dSDan Cross  *
207c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate  * Rights Reserved.
24*55fea89dSDan Cross  *
25*55fea89dSDan Cross  * Contributor(s):
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /* ldapsearch.c - generic program to search LDAP */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "ldaptool.h"
317c478bd9Sstevel@tonic-gate #include "fileurl.h"
327c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include "solaris-int.h"
357c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define VLV_PARAM_SEP ':'
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
407c478bd9Sstevel@tonic-gate #define gettext(s) s
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static void usage( void );
44*55fea89dSDan Cross static int dosearch( LDAP *ld, char *base, int scope, char **attrs,
457c478bd9Sstevel@tonic-gate 			 int attrsonly, char *filtpatt, char *value);
467c478bd9Sstevel@tonic-gate static void write_string_attr_value( char *attrname, char *strval,
477c478bd9Sstevel@tonic-gate 	unsigned long opts );
487c478bd9Sstevel@tonic-gate #define LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME	0x01
497c478bd9Sstevel@tonic-gate static int write_ldif_value( char *type, char *value, unsigned long vallen,
507c478bd9Sstevel@tonic-gate 	unsigned long ldifoptions );
517c478bd9Sstevel@tonic-gate static void print_entry( LDAP *ld, LDAPMessage *entry, int attrsonly );
527c478bd9Sstevel@tonic-gate static void options_callback( int option, char *optarg );
537c478bd9Sstevel@tonic-gate static void parse_and_display_reference( LDAP *ld, LDAPMessage *ref );
547c478bd9Sstevel@tonic-gate static char *sortresult2string(unsigned long result);
557c478bd9Sstevel@tonic-gate static char *changetype_num2string( int chgtype );
567c478bd9Sstevel@tonic-gate static char *msgtype2str( int msgtype );
577c478bd9Sstevel@tonic-gate static char  **get_effectiverights_attrlist(char * optarg);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
607c478bd9Sstevel@tonic-gate static void fill_ldapsearch_msgtypes( void );
617c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Prefix used in names of pseudo attributes added to the entry LDIF
657c478bd9Sstevel@tonic-gate  * output if we receive an entryChangeNotification control with an entry
667c478bd9Sstevel@tonic-gate  * (requested using persistent search).
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define LDAPTOOL_PSEARCH_ATTR_PREFIX	"persistentSearch-"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static void
usage(void)727c478bd9Sstevel@tonic-gate usage( void )
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("usage: %s -b basedn [options] filter [attributes...]\n"), ldaptool_progname );
757c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("       %s -b basedn [options] -f file [attributes...]\nwhere:\n"), ldaptool_progname );
767c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    basedn\tbase dn for search\n") );
777c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t(if the environment variable LDAP_BASEDN is set,\n") );
787c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\tthen the -b flag is not required)\n") );
797c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    filter\tRFC-2254 compliant LDAP search filter\n") );
807c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    file\tfile containing a sequence of LDAP search filters to use\n") );
817c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    attributes\twhitespace-separated list of attributes to retrieve\n") );
827c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t(if no attribute list is given, all are retrieved)\n") );
837c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("options:\n") );
847c478bd9Sstevel@tonic-gate     ldaptool_common_usage( 0 );
857c478bd9Sstevel@tonic-gate #if defined( XP_WIN32 )
867c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -t\t\twrite values to files in temp directory.\n") );
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -t\t\twrite values to files in /tmp\n") );
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -U\t\tproduce file URLs in conjunction with -t\n") );
917c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -e\t\tminimize base-64 encoding of values\n") );
927c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -u\t\tinclude User Friendly entry names in the output\n") );
937c478bd9Sstevel@tonic-gate #ifndef HAVE_SASL_OPTIONS
947c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -o\t\tprint entries using old format (default is LDIF)\n") );
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -T\t\tdon't fold (wrap) long lines (default is to fold)\n") );
977c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -1\t\tomit leading \"version: %d\" line in LDIF output\n"), LDIF_VERSION_ONE );
987c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -A\t\tretrieve attribute names only (no values)\n") );
997c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -B\t\tprint non-ASCII values and use old output format (attr=value)\n") );
1007c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -x\t\tperforming sorting on server\n") );
1017c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1027c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -r\t\tprint entries using old format (default is LDIF)\n") );
1037c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1047c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -F sep\tprint `sep' instead of `%s' between attribute names\n"), LDAPTOOL_DEFSEP );
1057c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("          \tand values\n") );
1067c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -S attr\tsort the results by attribute `attr'\n") );
1077c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -s scope\tone of base, one, or sub (default is sub)\n") );
1087c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -a deref\tone of never, always, search, or find (default: never)\n") );
1097c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("            \t(alias dereferencing)\n") );
1107c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -l timelim\ttime limit (in seconds) for search (default is no limit)\n") );
1117c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -z sizelim\tsize limit (in entries) for search (default is no limit)\n") );
1127c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -C ps:changetype[:changesonly[:entrychgcontrols]]\n") );
1137c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\tchangetypes are add,delete,modify,moddn,any\n") );
1147c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\tchangesonly and  entrychgcontrols are boolean values\n") );
1157c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t(default is 1)\n") );
1167c478bd9Sstevel@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 );
1177c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t'after' are the number of entries surrounding 'index.'\n"));
1187c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t'count' is the content count, 'value' is the search value.\n"));
1197c478bd9Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
1207c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("    -c authzid\tspecifies the getEffectiveRights control authzid\n"));
1217c478bd9Sstevel@tonic-gate 	fprintf( stderr, gettext("\t\t eg. dn:uid=bjensen,dc=example,dc=com\n"));
1227c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t A value of \"\" means \"the authorization id for the operation\".\n"));
1237c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t A value of \"dn:\" means \"anonymous\"\n"));
1247c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t (The aclRights operational attribute must be requested)\n"));
1257c478bd9Sstevel@tonic-gate 	fprintf( stderr, gettext("    -X attrlist\tspecifies the getEffectiveRights control specific attribute list.\n"));
1267c478bd9Sstevel@tonic-gate     fprintf( stderr, gettext("\t\t eg. \"nsroledn userPassword\"\n"));
1277c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate     exit( LDAP_PARAM_ERROR );
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static char	*base = NULL;
1337c478bd9Sstevel@tonic-gate static char	*sep = LDAPTOOL_DEFSEP;
1347c478bd9Sstevel@tonic-gate static char	**sortattr = NULL;
1357c478bd9Sstevel@tonic-gate static char     *vlv_value = NULL;
1367c478bd9Sstevel@tonic-gate static int	sortsize = 0;
1377c478bd9Sstevel@tonic-gate static int	*skipsortattr = NULL;
1387c478bd9Sstevel@tonic-gate static int	includeufn, allow_binary, vals2tmp, ldif, scope, deref;
1397c478bd9Sstevel@tonic-gate static int	attrsonly, timelimit, sizelimit, server_sort, fold;
1407c478bd9Sstevel@tonic-gate static int	minimize_base64, produce_file_urls;
1417c478bd9Sstevel@tonic-gate static int	use_vlv = 0, vlv_before, vlv_after, vlv_index, vlv_count;
1427c478bd9Sstevel@tonic-gate static int	use_psearch=0;
1437c478bd9Sstevel@tonic-gate static int	write_ldif_version = 1;
1447c478bd9Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
1457c478bd9Sstevel@tonic-gate static char *get_effectiverights_control_target_dn = NULL; /* -c */
1467c478bd9Sstevel@tonic-gate static char **get_effectiverights_control_attrlist = NULL;  /* -X */
1477c478bd9Sstevel@tonic-gate static int	do_effective_rights_control = 0;
1487c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* Persistent search variables */
1517c478bd9Sstevel@tonic-gate static int	chgtype=0, changesonly=1, return_echg_ctls=1;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1557c478bd9Sstevel@tonic-gate main( int argc, char **argv )
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate     char		*filtpattern, **attrs;
1587c478bd9Sstevel@tonic-gate     int			rc, optind, i, first, free_filtpattern;
1597c478bd9Sstevel@tonic-gate     LDAP		*ld;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1627c478bd9Sstevel@tonic-gate     char *locale = setlocale(LC_ALL, "");
1637c478bd9Sstevel@tonic-gate     textdomain(TEXT_DOMAIN);
1647c478bd9Sstevel@tonic-gate     ldaptool_require_binddn = 0;
1657c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate     free_filtpattern = 0;
1687c478bd9Sstevel@tonic-gate     deref = LDAP_DEREF_NEVER;
1697c478bd9Sstevel@tonic-gate     allow_binary = vals2tmp = attrsonly = 0;
1707c478bd9Sstevel@tonic-gate     minimize_base64 = produce_file_urls = 0;
1717c478bd9Sstevel@tonic-gate     ldif = 1;
1727c478bd9Sstevel@tonic-gate     fold = 1;
1737c478bd9Sstevel@tonic-gate     sizelimit = timelimit = 0;
1747c478bd9Sstevel@tonic-gate     scope = LDAP_SCOPE_SUBTREE;
1757c478bd9Sstevel@tonic-gate 	server_sort = 0;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #ifdef notdef
1797c478bd9Sstevel@tonic-gate #ifdef HPUX11
1807c478bd9Sstevel@tonic-gate #ifndef __LP64__
1817c478bd9Sstevel@tonic-gate 	_main( argc, argv);
1827c478bd9Sstevel@tonic-gate #endif /* __LP64_ */
1837c478bd9Sstevel@tonic-gate #endif /* HPUX11 */
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate     ldaptool_reset_control_array( ldaptool_request_ctrls );
1887c478bd9Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS
1897c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1907c478bd9Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "ABLTU1etuxra:b:F:G:l:S:s:z:C:",
1917c478bd9Sstevel@tonic-gate         0, options_callback );
1927c478bd9Sstevel@tonic-gate #else
1937c478bd9Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "ABLTU1etuxa:b:F:G:l:S:s:z:C:c:",
1947c478bd9Sstevel@tonic-gate         0, options_callback );
1957c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
1967c478bd9Sstevel@tonic-gate #else
1977c478bd9Sstevel@tonic-gate     optind = ldaptool_process_args( argc, argv, "ABLTU1eotuxa:b:F:G:l:S:s:z:C:c:",
1987c478bd9Sstevel@tonic-gate         0, options_callback );
1997c478bd9Sstevel@tonic-gate #endif	/* HAVE_SASL_OPTIONS */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate     if ( optind == -1 ) {
2027c478bd9Sstevel@tonic-gate 	usage();
2037c478bd9Sstevel@tonic-gate     }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate     if ( base == NULL ) {
2067c478bd9Sstevel@tonic-gate 	if (( base = getenv( "LDAP_BASEDN" )) == NULL ) {
2077c478bd9Sstevel@tonic-gate 	    usage();
2087c478bd9Sstevel@tonic-gate 	}
209*55fea89dSDan Cross     }
2107c478bd9Sstevel@tonic-gate     if ( sortattr ) {
2117c478bd9Sstevel@tonic-gate 	for ( sortsize = 0; sortattr[sortsize] != NULL; sortsize++ ) {
2127c478bd9Sstevel@tonic-gate 	    ;       /* NULL */
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	sortsize++;             /* add in the final NULL field */
2157c478bd9Sstevel@tonic-gate 	skipsortattr = (int *) malloc( sortsize * sizeof(int *) );
2167c478bd9Sstevel@tonic-gate 	if ( skipsortattr == NULL ) {
2177c478bd9Sstevel@tonic-gate     		fprintf( stderr, gettext("Out of memory\n") );
2187c478bd9Sstevel@tonic-gate 		exit( LDAP_NO_MEMORY );
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	memset( (char *) skipsortattr, 0, sortsize * sizeof(int *) );
2217c478bd9Sstevel@tonic-gate     } else if ( server_sort ) {
2227c478bd9Sstevel@tonic-gate 	server_sort = 0;   /* ignore this option if no sortattrs were given */
2237c478bd9Sstevel@tonic-gate     }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate     if ( argc - optind < 1 ) {
2267c478bd9Sstevel@tonic-gate 	if ( ldaptool_fp == NULL ) {
2277c478bd9Sstevel@tonic-gate 	    usage();
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 	attrs = NULL;
2307c478bd9Sstevel@tonic-gate 	filtpattern = "%s";
2317c478bd9Sstevel@tonic-gate     } else {	/* there are additional args (filter + attrs) */
2327c478bd9Sstevel@tonic-gate 	if ( ldaptool_fp == NULL || strstr( argv[ optind ], "%s" ) != NULL ) {
2337c478bd9Sstevel@tonic-gate 	    filtpattern = ldaptool_local2UTF8( argv[ optind ] );
2347c478bd9Sstevel@tonic-gate 	    /* since local2UTF8 always allocates something, we should free it */
2357c478bd9Sstevel@tonic-gate 	    free_filtpattern = 1;
2367c478bd9Sstevel@tonic-gate 	    ++optind;
2377c478bd9Sstevel@tonic-gate 	} else {
2387c478bd9Sstevel@tonic-gate 	    filtpattern = "%s";
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if ( argv[ optind ] == NULL ) {
2427c478bd9Sstevel@tonic-gate 	    attrs = NULL;
2433718f283SToomas Soome 	} else if ( sortattr == NULL || *sortattr == NULL || server_sort) {
2447c478bd9Sstevel@tonic-gate 	    attrs = &argv[ optind ];
2457c478bd9Sstevel@tonic-gate 	} else {
2467c478bd9Sstevel@tonic-gate 	    attrs = ldap_charray_dup( &argv[ optind ] );
2477c478bd9Sstevel@tonic-gate 	    if ( attrs == NULL ) {
2487c478bd9Sstevel@tonic-gate 		fprintf( stderr, gettext("Out of memory\n") );
2497c478bd9Sstevel@tonic-gate 		exit( LDAP_NO_MEMORY );
2507c478bd9Sstevel@tonic-gate 	    }
2517c478bd9Sstevel@tonic-gate 	    for ( i = 0; i < (sortsize - 1); i++ ) {
2527c478bd9Sstevel@tonic-gate                 if ( !ldap_charray_inlist( attrs, sortattr[i] ) ) {
2537c478bd9Sstevel@tonic-gate                     if ( ldap_charray_add( &attrs, sortattr[i] ) != 0 ) {
2547c478bd9Sstevel@tonic-gate     			fprintf( stderr, gettext("Out of memory\n") );
2557c478bd9Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
2567c478bd9Sstevel@tonic-gate 		    }
2577c478bd9Sstevel@tonic-gate                     /*
2587c478bd9Sstevel@tonic-gate                      * attribute in the search list only for the purpose of
2597c478bd9Sstevel@tonic-gate                      * sorting
2607c478bd9Sstevel@tonic-gate                      */
2617c478bd9Sstevel@tonic-gate                     skipsortattr[i] = 1;
2627c478bd9Sstevel@tonic-gate                 }
2637c478bd9Sstevel@tonic-gate 	    }
2647c478bd9Sstevel@tonic-gate         }
2657c478bd9Sstevel@tonic-gate     }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate     ld = ldaptool_ldap_init( 0 );
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate     if ( !ldaptool_not ) {
2707c478bd9Sstevel@tonic-gate 	ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
2717c478bd9Sstevel@tonic-gate 	ldap_set_option( ld, LDAP_OPT_TIMELIMIT, &timelimit );
2727c478bd9Sstevel@tonic-gate 	ldap_set_option( ld, LDAP_OPT_SIZELIMIT, &sizelimit );
2737c478bd9Sstevel@tonic-gate     }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate     ldaptool_bind( ld );
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate     if ( ldaptool_verbose ) {
2787c478bd9Sstevel@tonic-gate 	printf( gettext("filter pattern: %s\nreturning: "), filtpattern );
2797c478bd9Sstevel@tonic-gate 	if ( attrs == NULL ) {
2807c478bd9Sstevel@tonic-gate 	    printf( gettext("ALL") );
2817c478bd9Sstevel@tonic-gate 	} else {
2827c478bd9Sstevel@tonic-gate 	    for ( i = 0; attrs[ i ] != NULL; ++i ) {
2837c478bd9Sstevel@tonic-gate 		printf( "%s ", attrs[ i ] );
2847c478bd9Sstevel@tonic-gate 	    }
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	putchar( '\n' );
2877c478bd9Sstevel@tonic-gate     }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate     if ( ldaptool_fp == NULL ) {
2907c478bd9Sstevel@tonic-gate 	char *conv;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	conv = ldaptool_local2UTF8( base );
2937c478bd9Sstevel@tonic-gate 	rc = dosearch( ld, conv, scope, attrs, attrsonly, filtpattern, "" );
2947c478bd9Sstevel@tonic-gate 	if( conv != NULL )
2957c478bd9Sstevel@tonic-gate             free( conv );
2967c478bd9Sstevel@tonic-gate     } else {
2977c478bd9Sstevel@tonic-gate 	int done = 0;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	rc = LDAP_SUCCESS;
3007c478bd9Sstevel@tonic-gate 	first = 1;
3017c478bd9Sstevel@tonic-gate 	while ( rc == LDAP_SUCCESS && !done ) {
3027c478bd9Sstevel@tonic-gate 	    char *linep = NULL;
3037c478bd9Sstevel@tonic-gate 	    int   increment = 0;
3047c478bd9Sstevel@tonic-gate 	    int	  c, index;
305*55fea89dSDan Cross 
306*55fea89dSDan Cross 	    /* allocate initial block of memory */
3077c478bd9Sstevel@tonic-gate 	    if ((linep = (char *)malloc(BUFSIZ)) == NULL) {
3087c478bd9Sstevel@tonic-gate 	        fprintf( stderr, gettext("Out of memory\n") );
3097c478bd9Sstevel@tonic-gate 		exit( LDAP_NO_MEMORY );
3107c478bd9Sstevel@tonic-gate 	    }
3117c478bd9Sstevel@tonic-gate 	    increment++;
3127c478bd9Sstevel@tonic-gate 	    index = 0;
3137c478bd9Sstevel@tonic-gate 	    while ((c = fgetc( ldaptool_fp )) != '\n' && c != EOF) {
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	        /* check if we will overflow the buffer */
3167c478bd9Sstevel@tonic-gate 	        if ((c != EOF) && (index == ((increment * BUFSIZ) -1))) {
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		    /* if we did, add another BUFSIZ worth of bytes */
3197c478bd9Sstevel@tonic-gate 	   	    if ((linep = (char *)
3207c478bd9Sstevel@tonic-gate 			realloc(linep, (increment + 1) * BUFSIZ)) == NULL) {
3217c478bd9Sstevel@tonic-gate 			fprintf( stderr, gettext("Out of memory\n") );
3227c478bd9Sstevel@tonic-gate 			exit( LDAP_NO_MEMORY );
3237c478bd9Sstevel@tonic-gate 		    }
3247c478bd9Sstevel@tonic-gate 		    increment++;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		linep[index++] = c;
3277c478bd9Sstevel@tonic-gate 	    }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	    if (c == EOF) {
3307c478bd9Sstevel@tonic-gate 		done = 1;
3317c478bd9Sstevel@tonic-gate 		break;
3327c478bd9Sstevel@tonic-gate 	    }
333*55fea89dSDan Cross 
3347c478bd9Sstevel@tonic-gate 	    linep[index] = '\0';
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	    if ( !first ) {
3377c478bd9Sstevel@tonic-gate 		putchar( '\n' );
3387c478bd9Sstevel@tonic-gate 	    } else {
3397c478bd9Sstevel@tonic-gate 		first = 0;
3407c478bd9Sstevel@tonic-gate 	    }
3417c478bd9Sstevel@tonic-gate 	    rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
3427c478bd9Sstevel@tonic-gate 		    linep );
3437c478bd9Sstevel@tonic-gate 	    free (linep);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate     }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate     ldaptool_cleanup( ld );
3487c478bd9Sstevel@tonic-gate     if (free_filtpattern != 0 && filtpattern != NULL) {
3497c478bd9Sstevel@tonic-gate 	free (filtpattern);
3507c478bd9Sstevel@tonic-gate     }
351ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 
352ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India     /* check for and report output error */
353ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India     fflush( stdout );
354ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India     rc = ldaptool_check_ferror( stdout, rc,
355ffc33b84SSreedhar Chalamalasetti - Sun Microsystems - Bangalore India 		gettext("output error (output might be incomplete)") );
3567c478bd9Sstevel@tonic-gate     return( rc );
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static void
options_callback(int option,char * optarg)3617c478bd9Sstevel@tonic-gate options_callback( int option, char *optarg )
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate     char *s, *temp_arg, *ps_ptr, *ps_arg;
364*55fea89dSDan Cross 
3657c478bd9Sstevel@tonic-gate     switch( option ) {
3667c478bd9Sstevel@tonic-gate     case 'u':	/* include UFN */
3677c478bd9Sstevel@tonic-gate 	++includeufn;
3687c478bd9Sstevel@tonic-gate 	break;
3697c478bd9Sstevel@tonic-gate     case 't':	/* write attribute values to /tmp files */
3707c478bd9Sstevel@tonic-gate 	++vals2tmp;
3717c478bd9Sstevel@tonic-gate 	break;
3727c478bd9Sstevel@tonic-gate     case 'U':	/* produce file URLs in conjunction with -t */
3737c478bd9Sstevel@tonic-gate 	++produce_file_urls;
3747c478bd9Sstevel@tonic-gate 	break;
3757c478bd9Sstevel@tonic-gate     case 'e':	/* minimize base-64 encoding of values */
3767c478bd9Sstevel@tonic-gate 	++minimize_base64;
3777c478bd9Sstevel@tonic-gate 	break;
3787c478bd9Sstevel@tonic-gate     case 'A':	/* retrieve attribute names only -- no values */
3797c478bd9Sstevel@tonic-gate 	++attrsonly;
3807c478bd9Sstevel@tonic-gate 	break;
3817c478bd9Sstevel@tonic-gate     case 'L':       /* print entries in LDIF format -- now the default */
3827c478bd9Sstevel@tonic-gate 	break;
3837c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
3847c478bd9Sstevel@tonic-gate     case 'r':	/* print entries in the old format */
3857c478bd9Sstevel@tonic-gate 	ldif = 0;
3867c478bd9Sstevel@tonic-gate 	break;
3877c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
3887c478bd9Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS
3897c478bd9Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS_2
3907c478bd9Sstevel@tonic-gate     case 'o':	/* print entries using old ldapsearch format */
3917c478bd9Sstevel@tonic-gate 	ldif = 0;
3927c478bd9Sstevel@tonic-gate 	break;
3937c478bd9Sstevel@tonic-gate #endif
3947c478bd9Sstevel@tonic-gate #else
3957c478bd9Sstevel@tonic-gate     case 'o':	/* print entries using old ldapsearch format */
3967c478bd9Sstevel@tonic-gate 	ldif = 0;
3977c478bd9Sstevel@tonic-gate 	break;
3987c478bd9Sstevel@tonic-gate #endif
3997c478bd9Sstevel@tonic-gate     case 'B':	/* allow binary values to be printed, use old format */
4007c478bd9Sstevel@tonic-gate 	++allow_binary;
4017c478bd9Sstevel@tonic-gate 	ldif = 0;
4027c478bd9Sstevel@tonic-gate 	break;
4037c478bd9Sstevel@tonic-gate     case '1':	/* omit leading "version: #" line from LDIF output */
4047c478bd9Sstevel@tonic-gate 	write_ldif_version = 0;
4057c478bd9Sstevel@tonic-gate 	break;
4067c478bd9Sstevel@tonic-gate     case 's':	/* search scope */
4077c478bd9Sstevel@tonic-gate 	if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
4087c478bd9Sstevel@tonic-gate 	    scope = LDAP_SCOPE_BASE;
4097c478bd9Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
4107c478bd9Sstevel@tonic-gate 	    scope = LDAP_SCOPE_ONELEVEL;
4117c478bd9Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
4127c478bd9Sstevel@tonic-gate 	    scope = LDAP_SCOPE_SUBTREE;
4137c478bd9Sstevel@tonic-gate 	} else {
4147c478bd9Sstevel@tonic-gate 	    fprintf( stderr, gettext("scope should be base, one, or sub\n") );
4157c478bd9Sstevel@tonic-gate 	    usage();
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate     case 'a':	/* set alias deref option */
4207c478bd9Sstevel@tonic-gate 	if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
4217c478bd9Sstevel@tonic-gate 	    deref = LDAP_DEREF_NEVER;
4227c478bd9Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
4237c478bd9Sstevel@tonic-gate 	    deref = LDAP_DEREF_SEARCHING;
4247c478bd9Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
4257c478bd9Sstevel@tonic-gate 	    deref = LDAP_DEREF_FINDING;
4267c478bd9Sstevel@tonic-gate 	} else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
4277c478bd9Sstevel@tonic-gate 	    deref = LDAP_DEREF_ALWAYS;
4287c478bd9Sstevel@tonic-gate 	} else {
4297c478bd9Sstevel@tonic-gate 	    fprintf( stderr, gettext("alias deref should be never, search, find, or always\n") );
4307c478bd9Sstevel@tonic-gate 	    usage();
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate     case 'F':	/* field separator */
4357c478bd9Sstevel@tonic-gate 	sep = strdup( optarg );
4367c478bd9Sstevel@tonic-gate 	ldif = 0;
4377c478bd9Sstevel@tonic-gate 	break;
4387c478bd9Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
4397c478bd9Sstevel@tonic-gate 	case 'c':
4407c478bd9Sstevel@tonic-gate 		if ( optarg && optarg[0] == '\0' ) {
4417c478bd9Sstevel@tonic-gate 			/* -c ""
4427c478bd9Sstevel@tonic-gate 				means "This user"
4437c478bd9Sstevel@tonic-gate 			*/
4447c478bd9Sstevel@tonic-gate 			get_effectiverights_control_target_dn = NULL;
4457c478bd9Sstevel@tonic-gate 			do_effective_rights_control = 1;
4467c478bd9Sstevel@tonic-gate 		}else if ( strlen(optarg) < 3 || (strncasecmp(optarg, "dn:", 3) != 0) ) {
4477c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("-c wrong format--should be \"\" or \"dn:...\".\n"
4487c478bd9Sstevel@tonic-gate 				"\"dn:\" means anonymous user."));
4497c478bd9Sstevel@tonic-gate 			usage();
450*55fea89dSDan Cross 		} else {
4517c478bd9Sstevel@tonic-gate 			get_effectiverights_control_target_dn = strdup(optarg);
4527c478bd9Sstevel@tonic-gate 			do_effective_rights_control = 1;
453*55fea89dSDan Cross 		}
4547c478bd9Sstevel@tonic-gate 	break;
4557c478bd9Sstevel@tonic-gate 	case 'X':
4567c478bd9Sstevel@tonic-gate 		get_effectiverights_control_attrlist = get_effectiverights_attrlist(optarg);
4577c478bd9Sstevel@tonic-gate 		do_effective_rights_control = 1;
4587c478bd9Sstevel@tonic-gate 	break;
4597c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
4607c478bd9Sstevel@tonic-gate     case 'b':	/* searchbase */
4617c478bd9Sstevel@tonic-gate 	base = strdup( optarg );
4627c478bd9Sstevel@tonic-gate 	break;
4637c478bd9Sstevel@tonic-gate     case 'l':	/* time limit */
4647c478bd9Sstevel@tonic-gate 	timelimit = atoi( optarg );
4657c478bd9Sstevel@tonic-gate 	break;
4667c478bd9Sstevel@tonic-gate     case 'x':	/* server sorting requested */
4677c478bd9Sstevel@tonic-gate 	server_sort = 1;
4687c478bd9Sstevel@tonic-gate 	break;
4697c478bd9Sstevel@tonic-gate     case 'z':	/* size limit */
4707c478bd9Sstevel@tonic-gate 	sizelimit = atoi( optarg );
4717c478bd9Sstevel@tonic-gate 	break;
4727c478bd9Sstevel@tonic-gate     case 'S':	/* sort attribute */
4737c478bd9Sstevel@tonic-gate 	ldap_charray_add( &sortattr, strdup( optarg ) );
4747c478bd9Sstevel@tonic-gate 	break;
4757c478bd9Sstevel@tonic-gate     case 'T':	/* don't fold lines */
4767c478bd9Sstevel@tonic-gate 	fold = 0;
4777c478bd9Sstevel@tonic-gate 	break;
4787c478bd9Sstevel@tonic-gate     case 'G':  /* do the virtual list setup */
4797c478bd9Sstevel@tonic-gate 	use_vlv++;
4807c478bd9Sstevel@tonic-gate 	s = strchr(optarg, VLV_PARAM_SEP );
481*55fea89dSDan Cross 
4827c478bd9Sstevel@tonic-gate 	if (s != NULL)
4837c478bd9Sstevel@tonic-gate 	{
4847c478bd9Sstevel@tonic-gate 	    vlv_before = atoi(optarg);
4857c478bd9Sstevel@tonic-gate 	    s++;
4867c478bd9Sstevel@tonic-gate 	    vlv_after = atoi( s );
4877c478bd9Sstevel@tonic-gate 	    s = strchr(s, VLV_PARAM_SEP );
4887c478bd9Sstevel@tonic-gate 	    if (s != NULL)
4897c478bd9Sstevel@tonic-gate 	    {
4907c478bd9Sstevel@tonic-gate 		s++;
4917c478bd9Sstevel@tonic-gate 		/* below is a small set of logic to implement the following cases
4927c478bd9Sstevel@tonic-gate 		 * -G23:23:wilber
4937c478bd9Sstevel@tonic-gate 		 * -G23:23:"wilber:wright"
4947c478bd9Sstevel@tonic-gate 		 * -G23:23:'wilber'
4957c478bd9Sstevel@tonic-gate 		 * -G23:23:wilber wright
496*55fea89dSDan Cross 		 * all of the above are before, after, value -  NOTE: a colon not in a quoted
4977c478bd9Sstevel@tonic-gate 		 * string will break the parser!!!!
4987c478bd9Sstevel@tonic-gate 		 * -G23:23:45:600
4997c478bd9Sstevel@tonic-gate 		 * above is index, count encoding
5007c478bd9Sstevel@tonic-gate 		 */
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		if (*s == '\'' || *s == '"')
5037c478bd9Sstevel@tonic-gate 		{
5047c478bd9Sstevel@tonic-gate 		    vlv_value = strdup( s );
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 		else
5077c478bd9Sstevel@tonic-gate 		{
5087c478bd9Sstevel@tonic-gate 		    if (strchr( s, VLV_PARAM_SEP ))
5097c478bd9Sstevel@tonic-gate 		    {
5107c478bd9Sstevel@tonic-gate 			/* we have an index + count option */
5117c478bd9Sstevel@tonic-gate 			vlv_index = atoi( s );
5127c478bd9Sstevel@tonic-gate 			vlv_count = atoi( strchr( s, VLV_PARAM_SEP) + 1);
5137c478bd9Sstevel@tonic-gate 		    }
5147c478bd9Sstevel@tonic-gate 		    else
5157c478bd9Sstevel@tonic-gate 		    {
516*55fea89dSDan Cross 			/* we don't have a quote surrounding the assertion value
517*55fea89dSDan Cross 			 * do we need to???
5187c478bd9Sstevel@tonic-gate 			 */
5197c478bd9Sstevel@tonic-gate 			vlv_value = strdup( s );
5207c478bd9Sstevel@tonic-gate 		    }
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 	    }
5237c478bd9Sstevel@tonic-gate 	    else
5247c478bd9Sstevel@tonic-gate 	    {
5257c478bd9Sstevel@tonic-gate 		fprintf( stderr,gettext("Illegal 'after' paramater for virtual list\n") );
5267c478bd9Sstevel@tonic-gate 		exit( LDAP_PARAM_ERROR );
5277c478bd9Sstevel@tonic-gate 	    }
528*55fea89dSDan Cross 
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	else
5317c478bd9Sstevel@tonic-gate 	{
5327c478bd9Sstevel@tonic-gate 	    fprintf( stderr,gettext("Illegal 'before' paramater for virtual list\n") );
5337c478bd9Sstevel@tonic-gate 	    exit( LDAP_PARAM_ERROR );
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	break;
5367c478bd9Sstevel@tonic-gate     case 'C':
5377c478bd9Sstevel@tonic-gate 	use_psearch++;
5387c478bd9Sstevel@tonic-gate 	if ( (ps_arg = strdup( optarg)) == NULL ) {
5397c478bd9Sstevel@tonic-gate 	    perror ("strdup");
5407c478bd9Sstevel@tonic-gate 	    exit (LDAP_NO_MEMORY);
5417c478bd9Sstevel@tonic-gate 	}
542*55fea89dSDan Cross 
5437c478bd9Sstevel@tonic-gate 	ps_ptr=strtok(ps_arg, ":");
5447c478bd9Sstevel@tonic-gate 	if (ps_ptr == NULL || (strcasecmp(ps_ptr, "ps")) ) {
5457c478bd9Sstevel@tonic-gate 	    fprintf (stderr, gettext("Invalid argument for -C\n"));
5467c478bd9Sstevel@tonic-gate 	    usage();
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 	if (NULL != (ps_ptr=strtok(NULL, ":"))) {
5497c478bd9Sstevel@tonic-gate 	    if ( (temp_arg = strdup( ps_ptr )) == NULL ) {
5507c478bd9Sstevel@tonic-gate 	        perror ("strdup");
5517c478bd9Sstevel@tonic-gate 	    	exit (LDAP_NO_MEMORY);
552*55fea89dSDan Cross 	    }
5537c478bd9Sstevel@tonic-gate 	} else {
5547c478bd9Sstevel@tonic-gate 	    fprintf (stderr, gettext("Invalid argument for -C\n"));
5557c478bd9Sstevel@tonic-gate 	    usage();
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	if (NULL != (ps_ptr=strtok(NULL, ":"))) {
5587c478bd9Sstevel@tonic-gate 	    if ( (changesonly = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
5597c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
5607c478bd9Sstevel@tonic-gate 		usage();
5617c478bd9Sstevel@tonic-gate 	    }
562*55fea89dSDan Cross 	}
5637c478bd9Sstevel@tonic-gate 	if (NULL != (ps_ptr=strtok(NULL, ":"))) {
5647c478bd9Sstevel@tonic-gate 	    if ( (return_echg_ctls = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
5657c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
5667c478bd9Sstevel@tonic-gate 		usage();
5677c478bd9Sstevel@tonic-gate 	    }
568*55fea89dSDan Cross 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/* Now parse the temp_arg and build chgtype as
5717c478bd9Sstevel@tonic-gate 	 * the changetypes are encountered */
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if ((ps_ptr = strtok( temp_arg, "," )) == NULL) {
5747c478bd9Sstevel@tonic-gate 		usage();
5757c478bd9Sstevel@tonic-gate 	} else {
5767c478bd9Sstevel@tonic-gate 	    while ( ps_ptr ) {
577*55fea89dSDan Cross 		if ((strcasecmp(ps_ptr, "add"))==0)
5787c478bd9Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_ADD;
579*55fea89dSDan Cross 		else if ((strcasecmp(ps_ptr, "delete"))==0)
5807c478bd9Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_DELETE;
581*55fea89dSDan Cross 		else if ((strcasecmp(ps_ptr, "modify"))==0)
5827c478bd9Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_MODIFY;
583*55fea89dSDan Cross 		else if ((strcasecmp(ps_ptr, "moddn"))==0)
5847c478bd9Sstevel@tonic-gate 		    chgtype |= LDAP_CHANGETYPE_MODDN;
585*55fea89dSDan Cross 		else if ((strcasecmp(ps_ptr, "any"))==0)
5867c478bd9Sstevel@tonic-gate 		    chgtype = LDAP_CHANGETYPE_ANY;
5877c478bd9Sstevel@tonic-gate 		else {
5887c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Unknown changetype: %s\n"), ps_ptr);
5897c478bd9Sstevel@tonic-gate 			usage();
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		ps_ptr = strtok( NULL, "," );
5927c478bd9Sstevel@tonic-gate 	    }
5937c478bd9Sstevel@tonic-gate 	  }
5947c478bd9Sstevel@tonic-gate 	break;
5957c478bd9Sstevel@tonic-gate     default:
5967c478bd9Sstevel@tonic-gate 	usage();
5977c478bd9Sstevel@tonic-gate 	break;
5987c478bd9Sstevel@tonic-gate     }
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate static int
dosearch(ld,base,scope,attrs,attrsonly,filtpatt,value)6037c478bd9Sstevel@tonic-gate dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
6047c478bd9Sstevel@tonic-gate     LDAP	*ld;
6057c478bd9Sstevel@tonic-gate     char	*base;
6067c478bd9Sstevel@tonic-gate     int		scope;
6077c478bd9Sstevel@tonic-gate     char	**attrs;
6087c478bd9Sstevel@tonic-gate     int		attrsonly;
6097c478bd9Sstevel@tonic-gate     char	*filtpatt;
6107c478bd9Sstevel@tonic-gate     char	*value;
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate     char		**refs = NULL, filter[ BUFSIZ ], *filterp = NULL;
6137c478bd9Sstevel@tonic-gate     int			rc, first, matches;
6147c478bd9Sstevel@tonic-gate     LDAPMessage		*res, *e;
6157c478bd9Sstevel@tonic-gate     LDAPControl		*ldctrl;
6167c478bd9Sstevel@tonic-gate     LDAPControl		**ctrl_response_array = NULL;
6177c478bd9Sstevel@tonic-gate     LDAPVirtualList	vlv_data;
6187c478bd9Sstevel@tonic-gate     int			msgid = 0;
6197c478bd9Sstevel@tonic-gate     int			length = 0;
6207c478bd9Sstevel@tonic-gate     int			mallocd_filter = 0;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate     if ( strstr( filtpatt, "%s" ) == NULL ) {	/* no need to sprintf() */
6237c478bd9Sstevel@tonic-gate 	    filterp = filtpatt;
6247c478bd9Sstevel@tonic-gate     } else {
6257c478bd9Sstevel@tonic-gate 	length = strlen( filtpatt ) + strlen ( value ) +1;
6267c478bd9Sstevel@tonic-gate 	if ( length > BUFSIZ ) {
6277c478bd9Sstevel@tonic-gate 	    if ((filterp = (char *)
6287c478bd9Sstevel@tonic-gate 		 malloc ( length )) == NULL) {
6297c478bd9Sstevel@tonic-gate 		perror( gettext("filter and/or pattern too long?") );
6307c478bd9Sstevel@tonic-gate 		exit (LDAP_PARAM_ERROR);
6317c478bd9Sstevel@tonic-gate 	    }
6327c478bd9Sstevel@tonic-gate 	    mallocd_filter = 1;
6337c478bd9Sstevel@tonic-gate 	} else {
6347c478bd9Sstevel@tonic-gate 	    filterp = filter;
6357c478bd9Sstevel@tonic-gate 	}
636*55fea89dSDan Cross 
6377c478bd9Sstevel@tonic-gate #ifdef HAVE_SNPRINTF
6387c478bd9Sstevel@tonic-gate 	if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {
6397c478bd9Sstevel@tonic-gate 	    perror( gettext("snprintf filter (filter and/or pattern too long?)") );
6407c478bd9Sstevel@tonic-gate 	    exit( LDAP_PARAM_ERROR );
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate #else
6437c478bd9Sstevel@tonic-gate 	sprintf( filterp, filtpatt, value );
6447c478bd9Sstevel@tonic-gate #endif
6457c478bd9Sstevel@tonic-gate     }
646*55fea89dSDan Cross 
6477c478bd9Sstevel@tonic-gate     if ( *filterp == '\0' ) {	/* treat empty filter is a shortcut for oc=* */
6487c478bd9Sstevel@tonic-gate 	if (mallocd_filter) {
6497c478bd9Sstevel@tonic-gate 	    free(filterp);
6507c478bd9Sstevel@tonic-gate 	    mallocd_filter = 0;
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	filterp = "(objectclass=*)";
6537c478bd9Sstevel@tonic-gate     }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate     if ( ldaptool_verbose ) {
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * Display the filter that will be used.  Add surrounding parens.
6587c478bd9Sstevel@tonic-gate 	 * if they are missing.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if ( '(' == *filterp ) {
6617c478bd9Sstevel@tonic-gate 	    printf( "filter is: %s\n", filterp );
6627c478bd9Sstevel@tonic-gate 	} else {
6637c478bd9Sstevel@tonic-gate 	    printf( "filter is: (%s)\n", filterp );
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate     }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate     if ( ldaptool_not ) {
6687c478bd9Sstevel@tonic-gate 	if (mallocd_filter) free(filterp);
6697c478bd9Sstevel@tonic-gate 	return( LDAP_SUCCESS );
6707c478bd9Sstevel@tonic-gate     }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
6737c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6747c478bd9Sstevel@tonic-gate     }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
6777c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6787c478bd9Sstevel@tonic-gate     }
679*55fea89dSDan Cross 
6807c478bd9Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
6817c478bd9Sstevel@tonic-gate     if ( do_effective_rights_control ) {
6827c478bd9Sstevel@tonic-gate         if ((ldctrl = ldaptool_create_geteffectiveRights_control(ld,
6837c478bd9Sstevel@tonic-gate 							       get_effectiverights_control_target_dn,
6847c478bd9Sstevel@tonic-gate 							       (const char**)	get_effectiverights_control_attrlist)) !=NULL) {
6857c478bd9Sstevel@tonic-gate 	    ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6867c478bd9Sstevel@tonic-gate         }
6877c478bd9Sstevel@tonic-gate     }
6887c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate     if (use_psearch) {
6917c478bd9Sstevel@tonic-gate 	if ( ldap_create_persistentsearch_control( ld, chgtype,
692*55fea89dSDan Cross                 changesonly, return_echg_ctls,
6937c478bd9Sstevel@tonic-gate 		1, &ldctrl ) != LDAP_SUCCESS )
6947c478bd9Sstevel@tonic-gate 	{
6957c478bd9Sstevel@tonic-gate 		ldap_perror( ld, "ldap_create_persistentsearch_control" );
6967c478bd9Sstevel@tonic-gate 		return (1);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6997c478bd9Sstevel@tonic-gate     }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate     if (server_sort) {
7037c478bd9Sstevel@tonic-gate 	/* First make a sort key list from the attribute list we have */
7047c478bd9Sstevel@tonic-gate 	LDAPsortkey **keylist = NULL;
7057c478bd9Sstevel@tonic-gate 	int i = 0;
7067c478bd9Sstevel@tonic-gate 	char *sortattrs = NULL;
7077c478bd9Sstevel@tonic-gate 	char *s = NULL;
7087c478bd9Sstevel@tonic-gate 	int string_length = 0;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/* Count the sort strings */
7117c478bd9Sstevel@tonic-gate 	for (i = 0; i < sortsize - 1 ; i++) {
7127c478bd9Sstevel@tonic-gate 	    string_length += strlen(sortattr[i]) + 1;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	sortattrs = (char *) malloc(string_length + 1);
7167c478bd9Sstevel@tonic-gate 	if (NULL == sortattrs) {
7177c478bd9Sstevel@tonic-gate 	    fprintf( stderr, gettext("Out of memory\n") );
7187c478bd9Sstevel@tonic-gate 	    exit( LDAP_NO_MEMORY );
7197c478bd9Sstevel@tonic-gate 	}
720*55fea89dSDan Cross 
7217c478bd9Sstevel@tonic-gate 	s = sortattrs;
7227c478bd9Sstevel@tonic-gate 	for (i = 0; i < sortsize - 1 ; i++) {
7237c478bd9Sstevel@tonic-gate 	    memcpy(s, sortattr[i], strlen(sortattr[i]));
7247c478bd9Sstevel@tonic-gate 	    s += strlen(sortattr[i]);
7257c478bd9Sstevel@tonic-gate 	    *s++ = ' ';
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	sortattrs[string_length] = '\0';
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	ldap_create_sort_keylist(&keylist,sortattrs);
7317c478bd9Sstevel@tonic-gate 	free(sortattrs);
7327c478bd9Sstevel@tonic-gate 	sortattrs = NULL;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* Then make a control for the sort attributes we have */
7357c478bd9Sstevel@tonic-gate 	rc = ldap_create_sort_control(ld,keylist,0,&ldctrl);
7367c478bd9Sstevel@tonic-gate 	ldap_free_sort_keylist(keylist);
7377c478bd9Sstevel@tonic-gate 	if ( rc != LDAP_SUCCESS ) {
7387c478bd9Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
7397c478bd9Sstevel@tonic-gate 	    return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
7407c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate     }
7467c478bd9Sstevel@tonic-gate     /* remember server side sorting must be available for vlv!!!! */
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate     if (use_vlv)
7497c478bd9Sstevel@tonic-gate     {
7507c478bd9Sstevel@tonic-gate 	vlv_data.ldvlist_before_count = vlv_before;
7517c478bd9Sstevel@tonic-gate 	vlv_data.ldvlist_after_count = vlv_after;
7527c478bd9Sstevel@tonic-gate 	if ( ldaptool_verbose ) {
753*55fea89dSDan Cross 	    printf( gettext("vlv data %lu, %lu, "),
7547c478bd9Sstevel@tonic-gate 		    vlv_data.ldvlist_before_count,
755*55fea89dSDan Cross 		    vlv_data.ldvlist_after_count
7567c478bd9Sstevel@tonic-gate 		);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	if (vlv_value)
7597c478bd9Sstevel@tonic-gate 	{
7607c478bd9Sstevel@tonic-gate 	    vlv_data.ldvlist_attrvalue = vlv_value;
7617c478bd9Sstevel@tonic-gate 	    vlv_data.ldvlist_size = 0;
7627c478bd9Sstevel@tonic-gate 	    vlv_data.ldvlist_index = 0;
7637c478bd9Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
7647c478bd9Sstevel@tonic-gate 		printf( "%s, 0, 0\n", vlv_data.ldvlist_attrvalue);
7657c478bd9Sstevel@tonic-gate 	    }
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	else
7687c478bd9Sstevel@tonic-gate 	{
7697c478bd9Sstevel@tonic-gate 	    vlv_data.ldvlist_attrvalue = NULL;
7707c478bd9Sstevel@tonic-gate 	    vlv_data.ldvlist_size = vlv_count;
7717c478bd9Sstevel@tonic-gate 	    vlv_data.ldvlist_index = vlv_index;
7727c478bd9Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
7737c478bd9Sstevel@tonic-gate 		printf( "(null), %lu, %lu\n", vlv_data.ldvlist_size, vlv_data.ldvlist_index );
7747c478bd9Sstevel@tonic-gate 	    }
7757c478bd9Sstevel@tonic-gate 	}
776*55fea89dSDan Cross 
7777c478bd9Sstevel@tonic-gate 	if ( rc != LDAP_SUCCESS ) {
7787c478bd9Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
7797c478bd9Sstevel@tonic-gate 	    return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
7807c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	if (LDAP_SUCCESS != (rc = ldap_create_virtuallist_control(ld,
7837c478bd9Sstevel@tonic-gate 		&vlv_data, &ldctrl)))
7847c478bd9Sstevel@tonic-gate 	{
7857c478bd9Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
7867c478bd9Sstevel@tonic-gate 	    return( ldaptool_print_lderror( ld,
7877c478bd9Sstevel@tonic-gate 		"ldap_create_virtuallist_control",
7887c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
792*55fea89dSDan Cross 
7937c478bd9Sstevel@tonic-gate     }
7947c478bd9Sstevel@tonic-gate 
795*55fea89dSDan Cross     if ( ldap_search_ext( ld, base, scope, filterp, attrs, attrsonly,
7967c478bd9Sstevel@tonic-gate 	    ldaptool_request_ctrls, NULL, NULL, -1, &msgid )
7977c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
7987c478bd9Sstevel@tonic-gate 	if (mallocd_filter) free(filterp);
7997c478bd9Sstevel@tonic-gate 	return( ldaptool_print_lderror( ld, "ldap_search",
8007c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
8017c478bd9Sstevel@tonic-gate     }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate     matches = 0;
8057c478bd9Sstevel@tonic-gate     first = 1;
8067c478bd9Sstevel@tonic-gate     if ( sortattr && !server_sort ) {
8077c478bd9Sstevel@tonic-gate         rc = ldap_result( ld, LDAP_RES_ANY, 1, NULL, &res );
8087c478bd9Sstevel@tonic-gate     } else {
809*55fea89dSDan Cross         while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res )) !=
8107c478bd9Sstevel@tonic-gate 		LDAP_RES_SEARCH_RESULT && rc != -1 ) {
8117c478bd9Sstevel@tonic-gate 	    if ( rc != LDAP_RES_SEARCH_ENTRY ) {
8127c478bd9Sstevel@tonic-gate 		if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
8137c478bd9Sstevel@tonic-gate 		    parse_and_display_reference( ld, res );
8147c478bd9Sstevel@tonic-gate 		} else if ( rc == LDAP_RES_EXTENDED
8157c478bd9Sstevel@tonic-gate 			&& ldap_msgid( res ) == LDAP_RES_UNSOLICITED ) {
8167c478bd9Sstevel@tonic-gate 		    ldaptool_print_extended_response( ld, res,
8177c478bd9Sstevel@tonic-gate 			    gettext("Unsolicited response") );
8187c478bd9Sstevel@tonic-gate 		} else {
8197c478bd9Sstevel@tonic-gate 		    fprintf( stderr, gettext("%s: ignoring LDAP response message"
8207c478bd9Sstevel@tonic-gate 			    " type 0x%x (%s)\n"),
8217c478bd9Sstevel@tonic-gate 			    ldaptool_progname, rc, msgtype2str( rc ));
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 		ldap_msgfree( res );
8247c478bd9Sstevel@tonic-gate 		continue;
8257c478bd9Sstevel@tonic-gate 	    }
8267c478bd9Sstevel@tonic-gate 	    matches++;
8277c478bd9Sstevel@tonic-gate 	    e = ldap_first_entry( ld, res );
8287c478bd9Sstevel@tonic-gate 	    if ( !first ) {
8297c478bd9Sstevel@tonic-gate 	        putchar( '\n' );
8307c478bd9Sstevel@tonic-gate 	    } else {
8317c478bd9Sstevel@tonic-gate 	        first = 0;
8327c478bd9Sstevel@tonic-gate 	    }
8337c478bd9Sstevel@tonic-gate 	    print_entry( ld, e, attrsonly );
8347c478bd9Sstevel@tonic-gate 	    ldap_msgfree( res );
8357c478bd9Sstevel@tonic-gate         }
8367c478bd9Sstevel@tonic-gate     }
8377c478bd9Sstevel@tonic-gate     if ( rc == -1 ) {
8387c478bd9Sstevel@tonic-gate 	if (mallocd_filter) free(filterp);
8397c478bd9Sstevel@tonic-gate 	return( ldaptool_print_lderror( ld, "ldap_result",
8407c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP ));
8417c478bd9Sstevel@tonic-gate     }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate     if ( ldap_parse_result( ld, res, &rc, NULL, NULL, &refs,
8447c478bd9Sstevel@tonic-gate 	    &ctrl_response_array, 0 ) != LDAP_SUCCESS ) {
8457c478bd9Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_parse_result",
8467c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
847*55fea89dSDan Cross     } else if ( rc != LDAP_SUCCESS ) {
8487c478bd9Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_search",
8497c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
850*55fea89dSDan Cross     }
8517c478bd9Sstevel@tonic-gate     /* Parse the returned sort control */
8527c478bd9Sstevel@tonic-gate     if (server_sort) {
8537c478bd9Sstevel@tonic-gate 	unsigned long result = 0;
8547c478bd9Sstevel@tonic-gate 	char *attribute;
855*55fea89dSDan Cross 
8567c478bd9Sstevel@tonic-gate 	if ( LDAP_SUCCESS != ldap_parse_sort_control(ld,ctrl_response_array,&result,&attribute) ) {
8577c478bd9Sstevel@tonic-gate 	    ldaptool_print_lderror(ld, "ldap_parse_sort_control",
8587c478bd9Sstevel@tonic-gate 		    LDAPTOOL_CHECK4SSL_IF_APPROP );
8597c478bd9Sstevel@tonic-gate 	    ldap_controls_free(ctrl_response_array);
8607c478bd9Sstevel@tonic-gate 	    ldap_msgfree(res);
8617c478bd9Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
8627c478bd9Sstevel@tonic-gate 	    return ( ldap_get_lderrno( ld, NULL, NULL ) );
8637c478bd9Sstevel@tonic-gate 	}
864*55fea89dSDan Cross 
8657c478bd9Sstevel@tonic-gate 	if (0 == result) {
8667c478bd9Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
8677c478bd9Sstevel@tonic-gate 		printf( gettext("Server indicated results sorted OK\n"));
8687c478bd9Sstevel@tonic-gate 	    }
8697c478bd9Sstevel@tonic-gate 	} else {
8707c478bd9Sstevel@tonic-gate 	    if (NULL != attribute) {
8717c478bd9Sstevel@tonic-gate 		printf(gettext("Server reported sorting error %ld: %s, attribute in error\"%s\"\n"),result,sortresult2string(result),attribute);
8727c478bd9Sstevel@tonic-gate 	    } else {
8737c478bd9Sstevel@tonic-gate 		printf(gettext("Server reported sorting error %ld: %s\n"),result,sortresult2string(result));
8747c478bd9Sstevel@tonic-gate 	    }
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate     }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate     if (use_vlv)
8807c478bd9Sstevel@tonic-gate     {
8817c478bd9Sstevel@tonic-gate 	unsigned long vpos, vcount;
8827c478bd9Sstevel@tonic-gate 	int vresult;
8837c478bd9Sstevel@tonic-gate 	if ( LDAP_SUCCESS != ldap_parse_virtuallist_control(ld,ctrl_response_array,&vpos, &vcount,&vresult) ) {
8847c478bd9Sstevel@tonic-gate 	    ldaptool_print_lderror( ld, "ldap_parse_virtuallist_control",
8857c478bd9Sstevel@tonic-gate 		    LDAPTOOL_CHECK4SSL_IF_APPROP );
8867c478bd9Sstevel@tonic-gate 	    ldap_controls_free(ctrl_response_array);
8877c478bd9Sstevel@tonic-gate 	    ldap_msgfree(res);
8887c478bd9Sstevel@tonic-gate 	    if (mallocd_filter) free(filterp);
8897c478bd9Sstevel@tonic-gate 	    return ( ldap_get_lderrno( ld, NULL, NULL ) );
8907c478bd9Sstevel@tonic-gate 	}
891*55fea89dSDan Cross 
8927c478bd9Sstevel@tonic-gate 	if (0 == vresult) {
8937c478bd9Sstevel@tonic-gate 	    if ( ldaptool_verbose ) {
8947c478bd9Sstevel@tonic-gate 		printf( gettext("Server indicated virtual list positioning OK\n"));
8957c478bd9Sstevel@tonic-gate 	    }
8967c478bd9Sstevel@tonic-gate 	    printf(gettext("index %lu content count %lu\n"), vpos, vcount);
897*55fea89dSDan Cross 
8987c478bd9Sstevel@tonic-gate 	} else {
8997c478bd9Sstevel@tonic-gate 	    printf(gettext("Server reported sorting error %d: %s\n"),vresult,sortresult2string(vresult));
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate     }
904*55fea89dSDan Cross 
905*55fea89dSDan Cross     ldap_controls_free(ctrl_response_array);
906*55fea89dSDan Cross 
9077c478bd9Sstevel@tonic-gate     if ( sortattr != NULL && !server_sort) {
908*55fea89dSDan Cross 
9097c478bd9Sstevel@tonic-gate 	(void) ldap_multisort_entries( ld, &res,
910*55fea89dSDan Cross 				       ( *sortattr == NULL ) ? NULL : sortattr,
9117c478bd9Sstevel@tonic-gate 				       (LDAP_CMP_CALLBACK *)strcasecmp );
9127c478bd9Sstevel@tonic-gate 	matches = 0;
9137c478bd9Sstevel@tonic-gate 	first = 1;
9147c478bd9Sstevel@tonic-gate 	for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
9157c478bd9Sstevel@tonic-gate 	      e = ldap_next_entry( ld, e ) ) {
9167c478bd9Sstevel@tonic-gate 	    matches++;
9177c478bd9Sstevel@tonic-gate 	    if ( !first ) {
9187c478bd9Sstevel@tonic-gate 		putchar( '\n' );
9197c478bd9Sstevel@tonic-gate 	    } else {
9207c478bd9Sstevel@tonic-gate 		first = 0;
9217c478bd9Sstevel@tonic-gate 	    }
9227c478bd9Sstevel@tonic-gate 	    print_entry( ld, e, attrsonly );
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate     }
925*55fea89dSDan Cross 
9267c478bd9Sstevel@tonic-gate     if ( ldaptool_verbose ) {
9277c478bd9Sstevel@tonic-gate 	printf( gettext("%d matches\n"), matches );
9287c478bd9Sstevel@tonic-gate     }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate     if ( refs != NULL ) {
9317c478bd9Sstevel@tonic-gate 	ldaptool_print_referrals( refs );
9327c478bd9Sstevel@tonic-gate 	ldap_value_free( refs );
9337c478bd9Sstevel@tonic-gate     }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate     if (mallocd_filter) free(filterp);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate     ldap_msgfree( res );
9387c478bd9Sstevel@tonic-gate     return( rc );
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate static void
print_entry(ld,entry,attrsonly)9437c478bd9Sstevel@tonic-gate print_entry( ld, entry, attrsonly )
9447c478bd9Sstevel@tonic-gate     LDAP	*ld;
9457c478bd9Sstevel@tonic-gate     LDAPMessage	*entry;
9467c478bd9Sstevel@tonic-gate     int		attrsonly;
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate     char		*a, *dn, *ufn, tmpfname[ BUFSIZ ];
9497c478bd9Sstevel@tonic-gate     int			i, notascii;
9507c478bd9Sstevel@tonic-gate     BerElement		*ber;
9517c478bd9Sstevel@tonic-gate     struct berval	**bvals;
9527c478bd9Sstevel@tonic-gate     FILE		*tmpfp;
9537c478bd9Sstevel@tonic-gate #if defined( XP_WIN32 )
9547c478bd9Sstevel@tonic-gate 	char	mode[20] = "w+b";
9557c478bd9Sstevel@tonic-gate #else
9567c478bd9Sstevel@tonic-gate 	char	mode[20] = "w";
9577c478bd9Sstevel@tonic-gate #endif
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate     dn = ldap_get_dn( ld, entry );
9607c478bd9Sstevel@tonic-gate     write_string_attr_value( "dn", dn, LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
9617c478bd9Sstevel@tonic-gate     if ( includeufn ) {
9627c478bd9Sstevel@tonic-gate 	ufn = ldap_dn2ufn( dn );
9637c478bd9Sstevel@tonic-gate 	write_string_attr_value( "ufn", ufn,
9647c478bd9Sstevel@tonic-gate 			LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
9657c478bd9Sstevel@tonic-gate 	free( ufn );
9667c478bd9Sstevel@tonic-gate     }
9677c478bd9Sstevel@tonic-gate     ldap_memfree( dn );
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate     if ( use_psearch ) {
9707c478bd9Sstevel@tonic-gate 	LDAPControl	**ectrls;
9717c478bd9Sstevel@tonic-gate 	int		chgtype, chgnumpresent;
9727c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
9737c478bd9Sstevel@tonic-gate 	ber_int_t	chgnum;
9747c478bd9Sstevel@tonic-gate #else
9757c478bd9Sstevel@tonic-gate 	long		chgnum;
9767c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
9777c478bd9Sstevel@tonic-gate 	char		*prevdn, longbuf[ 128 ];
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	if ( ldap_get_entry_controls( ld, entry, &ectrls ) == LDAP_SUCCESS ) {
9807c478bd9Sstevel@tonic-gate 	    if ( ldap_parse_entrychange_control( ld, ectrls, &chgtype,
9817c478bd9Sstevel@tonic-gate 			&prevdn, &chgnumpresent, &chgnum ) == LDAP_SUCCESS ) {
9827c478bd9Sstevel@tonic-gate 		write_string_attr_value(
9837c478bd9Sstevel@tonic-gate 			LDAPTOOL_PSEARCH_ATTR_PREFIX "changeType",
9847c478bd9Sstevel@tonic-gate 			changetype_num2string( chgtype ), 0 );
9857c478bd9Sstevel@tonic-gate 		if ( chgnumpresent ) {
9867c478bd9Sstevel@tonic-gate 		    sprintf( longbuf, "%d", chgnum );
9877c478bd9Sstevel@tonic-gate 		    write_string_attr_value(
9887c478bd9Sstevel@tonic-gate 			    LDAPTOOL_PSEARCH_ATTR_PREFIX "changeNumber",
9897c478bd9Sstevel@tonic-gate 			    longbuf, 0 );
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 		if ( NULL != prevdn ) {
9927c478bd9Sstevel@tonic-gate 		    write_string_attr_value(
9937c478bd9Sstevel@tonic-gate 			    LDAPTOOL_PSEARCH_ATTR_PREFIX "previousDN",
9947c478bd9Sstevel@tonic-gate 			    prevdn, 0 );
9957c478bd9Sstevel@tonic-gate 		    ldap_memfree( prevdn );
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 	    }
9987c478bd9Sstevel@tonic-gate 	    ldap_controls_free (ectrls);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate     }
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
10037c478bd9Sstevel@tonic-gate 	    a = ldap_next_attribute( ld, entry, ber ) ) {
10047c478bd9Sstevel@tonic-gate 	if ( ldap_charray_inlist(sortattr, a) &&            /* in the list*/
10057c478bd9Sstevel@tonic-gate 	    skipsortattr[ldap_charray_position(sortattr, a)] ) {/* and skip it*/
10067c478bd9Sstevel@tonic-gate 	    continue;                                       /* so skip it! */
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 	if ( attrsonly ) {
10097c478bd9Sstevel@tonic-gate 	    if ( ldif ) {
10107c478bd9Sstevel@tonic-gate 		write_ldif_value( a, "", 0, 0 );
10117c478bd9Sstevel@tonic-gate 	    } else {
10127c478bd9Sstevel@tonic-gate 		printf( "%s\n", a );
10137c478bd9Sstevel@tonic-gate 	    }
10147c478bd9Sstevel@tonic-gate 	} else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
10157c478bd9Sstevel@tonic-gate 	    for ( i = 0; bvals[i] != NULL; i++ ) {
10167c478bd9Sstevel@tonic-gate 		if ( vals2tmp ) {
10177c478bd9Sstevel@tonic-gate #ifdef HAVE_SNPRINTF
10187c478bd9Sstevel@tonic-gate 		    if ( snprintf( tmpfname, sizeof(tmpfname),
10197c478bd9Sstevel@tonic-gate 			    "%s/ldapsearch-%s-XXXXXX",
10207c478bd9Sstevel@tonic-gate 			    ldaptool_get_tmp_dir(), a ) < 0 ) {
10217c478bd9Sstevel@tonic-gate 			perror( gettext("snprintf tmpfname (attribute name too long?)") );
10227c478bd9Sstevel@tonic-gate 			exit( LDAP_PARAM_ERROR );
10237c478bd9Sstevel@tonic-gate 		    }
10247c478bd9Sstevel@tonic-gate #else
10257c478bd9Sstevel@tonic-gate 		    sprintf( tmpfname, "%s/ldapsearch-%s-XXXXXX",
10267c478bd9Sstevel@tonic-gate 			    ldaptool_get_tmp_dir(), a );
10277c478bd9Sstevel@tonic-gate #endif
10287c478bd9Sstevel@tonic-gate 		    tmpfp = NULL;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		    if ( LDAPTOOL_MKTEMP( tmpfname ) == NULL ) {
10317c478bd9Sstevel@tonic-gate 			perror( tmpfname );
10327c478bd9Sstevel@tonic-gate 		    } else if (( tmpfp = ldaptool_open_file( tmpfname, mode)) == NULL ) {
10337c478bd9Sstevel@tonic-gate 			perror( tmpfname );
10347c478bd9Sstevel@tonic-gate 		    } else if ( bvals[ i ]->bv_len > 0 &&
10357c478bd9Sstevel@tonic-gate 			    fwrite( bvals[ i ]->bv_val,
10367c478bd9Sstevel@tonic-gate 			    bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
10377c478bd9Sstevel@tonic-gate 			perror( tmpfname );
10387c478bd9Sstevel@tonic-gate 		    } else if ( ldif ) {
10397c478bd9Sstevel@tonic-gate 			if ( produce_file_urls ) {
10407c478bd9Sstevel@tonic-gate 			    char	*url;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 			    if ( ldaptool_path2fileurl( tmpfname, &url ) !=
10437c478bd9Sstevel@tonic-gate 				LDAPTOOL_FILEURL_SUCCESS ) {
10447c478bd9Sstevel@tonic-gate 				    perror( "ldaptool_path2fileurl" );
10457c478bd9Sstevel@tonic-gate 				} else {
10467c478bd9Sstevel@tonic-gate 				    write_ldif_value( a, url, strlen( url ),
10477c478bd9Sstevel@tonic-gate 					    LDIF_OPT_VALUE_IS_URL );
10487c478bd9Sstevel@tonic-gate 				    free( url );
10497c478bd9Sstevel@tonic-gate 				}
10507c478bd9Sstevel@tonic-gate 			} else {
10517c478bd9Sstevel@tonic-gate 			    write_ldif_value( a, tmpfname, strlen( tmpfname ),
10527c478bd9Sstevel@tonic-gate 				    0 );
10537c478bd9Sstevel@tonic-gate 			}
10547c478bd9Sstevel@tonic-gate 		    } else {
10557c478bd9Sstevel@tonic-gate 			printf( "%s%s%s\n", a, sep, tmpfname );
10567c478bd9Sstevel@tonic-gate 		    }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		    if ( tmpfp != NULL ) {
10597c478bd9Sstevel@tonic-gate 			fclose( tmpfp );
10607c478bd9Sstevel@tonic-gate 		    }
10617c478bd9Sstevel@tonic-gate 		} else {
10627c478bd9Sstevel@tonic-gate 		    notascii = 0;
10637c478bd9Sstevel@tonic-gate 		    if ( !ldif && !allow_binary ) {
10647c478bd9Sstevel@tonic-gate 			notascii = !ldaptool_berval_is_ascii( bvals[i] );
10657c478bd9Sstevel@tonic-gate 		    }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 		    if ( ldif ) {
10687c478bd9Sstevel@tonic-gate 			write_ldif_value( a, bvals[ i ]->bv_val,
10697c478bd9Sstevel@tonic-gate 				bvals[ i ]->bv_len, 0 );
10707c478bd9Sstevel@tonic-gate 		    } else {
10717c478bd9Sstevel@tonic-gate 			printf( "%s%s%s\n", a, sep,
10727c478bd9Sstevel@tonic-gate 				notascii ? gettext("NOT ASCII") : bvals[ i ]->bv_val );
10737c478bd9Sstevel@tonic-gate 		    }
10747c478bd9Sstevel@tonic-gate 		}
10757c478bd9Sstevel@tonic-gate 	    }
10767c478bd9Sstevel@tonic-gate 	    ber_bvecfree( bvals );
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 	ldap_memfree( a );
10797c478bd9Sstevel@tonic-gate     }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate     if ( ldap_get_lderrno( ld, NULL, NULL ) != LDAP_SUCCESS ) {
10827c478bd9Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_first_attribute/ldap_next_attribute",
10837c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
10847c478bd9Sstevel@tonic-gate     }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate     if ( ber != NULL ) {
10877c478bd9Sstevel@tonic-gate 	ber_free( ber, 0 );
10887c478bd9Sstevel@tonic-gate     }
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate static void
write_string_attr_value(char * attrname,char * strval,unsigned long opts)10937c478bd9Sstevel@tonic-gate write_string_attr_value( char *attrname, char *strval, unsigned long opts )
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate     if ( strval == NULL ) {
10967c478bd9Sstevel@tonic-gate 	strval = "";
10977c478bd9Sstevel@tonic-gate     }
10987c478bd9Sstevel@tonic-gate     if ( ldif ) {
10997c478bd9Sstevel@tonic-gate 	write_ldif_value( attrname, strval, strlen( strval ), 0 );
11007c478bd9Sstevel@tonic-gate     } else if ( 0 != ( opts & LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME )) {
11017c478bd9Sstevel@tonic-gate 	printf( "%s\n", strval );
11027c478bd9Sstevel@tonic-gate     } else {
11037c478bd9Sstevel@tonic-gate 	printf( "%s%s%s\n", attrname, sep, strval );
11047c478bd9Sstevel@tonic-gate     }
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate static int
write_ldif_value(char * type,char * value,unsigned long vallen,unsigned long ldifoptions)11097c478bd9Sstevel@tonic-gate write_ldif_value( char *type, char *value, unsigned long vallen,
11107c478bd9Sstevel@tonic-gate 	unsigned long ldifoptions )
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate     char	*ldif;
11137c478bd9Sstevel@tonic-gate     static int	wrote_version = 0;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate     if ( write_ldif_version && !wrote_version ) {
11167c478bd9Sstevel@tonic-gate 	char	versionbuf[ 64 ];
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	wrote_version = 1;
11197c478bd9Sstevel@tonic-gate 	sprintf( versionbuf, "%d", LDIF_VERSION_ONE );
11207c478bd9Sstevel@tonic-gate 	write_ldif_value( "version", versionbuf, strlen( versionbuf ), 0 );
11217c478bd9Sstevel@tonic-gate     }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate     if ( !fold ) {
11247c478bd9Sstevel@tonic-gate 	ldifoptions |= LDIF_OPT_NOWRAP;
11257c478bd9Sstevel@tonic-gate     }
11267c478bd9Sstevel@tonic-gate     if ( minimize_base64 ) {
11277c478bd9Sstevel@tonic-gate 	ldifoptions |= LDIF_OPT_MINIMAL_ENCODING;
11287c478bd9Sstevel@tonic-gate     }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate     if (( ldif = ldif_type_and_value_with_options( type, value, (int)vallen,
11317c478bd9Sstevel@tonic-gate 	    ldifoptions )) == NULL ) {
11327c478bd9Sstevel@tonic-gate 	return( -1 );
11337c478bd9Sstevel@tonic-gate     }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate     fputs( ldif, stdout );
11367c478bd9Sstevel@tonic-gate     free( ldif );
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate     return( 0 );
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate static char *
sortresult2string(unsigned long result)11437c478bd9Sstevel@tonic-gate sortresult2string(unsigned long result)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 	/*
11467c478bd9Sstevel@tonic-gate             success                   (0), -- results are sorted
11477c478bd9Sstevel@tonic-gate             operationsError           (1), -- server internal failure
11487c478bd9Sstevel@tonic-gate             timeLimitExceeded         (3), -- timelimit reached before
11497c478bd9Sstevel@tonic-gate                                            -- sorting was completed
11507c478bd9Sstevel@tonic-gate             strongAuthRequired        (8), -- refused to return sorted
11517c478bd9Sstevel@tonic-gate                                            -- results via insecure
11527c478bd9Sstevel@tonic-gate                                            -- protocol
11537c478bd9Sstevel@tonic-gate             adminLimitExceeded       (11), -- too many matching entries
11547c478bd9Sstevel@tonic-gate                                            -- for the server to sort
11557c478bd9Sstevel@tonic-gate             noSuchAttribute          (16), -- unrecognized attribute
11567c478bd9Sstevel@tonic-gate                                            -- type in sort key
11577c478bd9Sstevel@tonic-gate             inappropriateMatching    (18), -- unrecognized or inappro-
11587c478bd9Sstevel@tonic-gate                                            -- priate matching rule in
11597c478bd9Sstevel@tonic-gate                                            -- sort key
11607c478bd9Sstevel@tonic-gate             insufficientAccessRights (50), -- refused to return sorted
11617c478bd9Sstevel@tonic-gate                                            -- results to this client
11627c478bd9Sstevel@tonic-gate             busy                     (51), -- too busy to process
11637c478bd9Sstevel@tonic-gate             unwillingToPerform       (53), -- unable to sort
11647c478bd9Sstevel@tonic-gate             other                    (80)
11657c478bd9Sstevel@tonic-gate 	*/
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	switch (result) {
11687c478bd9Sstevel@tonic-gate 	case 0: return (gettext("success"));
11697c478bd9Sstevel@tonic-gate 	case 1: return (gettext("operations error"));
11707c478bd9Sstevel@tonic-gate 	case 3: return (gettext("time limit exceeded"));
11717c478bd9Sstevel@tonic-gate 	case 8: return (gettext("strong auth required"));
11727c478bd9Sstevel@tonic-gate 	case 11: return (gettext("admin limit exceeded"));
11737c478bd9Sstevel@tonic-gate 	case 16: return (gettext("no such attribute"));
11747c478bd9Sstevel@tonic-gate 	case 18: return (gettext("unrecognized or inappropriate matching rule"));
11757c478bd9Sstevel@tonic-gate 	case 50: return (gettext("insufficient access rights"));
11767c478bd9Sstevel@tonic-gate 	case 51: return (gettext("too busy"));
11777c478bd9Sstevel@tonic-gate 	case 53: return (gettext("unable to sort"));
11787c478bd9Sstevel@tonic-gate 	case 80:
11797c478bd9Sstevel@tonic-gate         default: return (gettext("Er...Other ?"));
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate static void
parse_and_display_reference(LDAP * ld,LDAPMessage * ref)11857c478bd9Sstevel@tonic-gate parse_and_display_reference( LDAP *ld, LDAPMessage *ref )
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate     int		i;
11887c478bd9Sstevel@tonic-gate     char	**refs;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate     if ( ldap_parse_reference( ld, ref, &refs, NULL, 0 ) != LDAP_SUCCESS ) {
11917c478bd9Sstevel@tonic-gate 	ldaptool_print_lderror( ld, "ldap_parse_reference",
11927c478bd9Sstevel@tonic-gate 		LDAPTOOL_CHECK4SSL_IF_APPROP );
11937c478bd9Sstevel@tonic-gate     } else if ( refs != NULL && refs[ 0 ] != NULL ) {
11947c478bd9Sstevel@tonic-gate 	fputs( gettext("Unfollowed continuation reference(s):\n"), stderr );
11957c478bd9Sstevel@tonic-gate 	for ( i = 0; refs[ i ] != NULL; ++i ) {
11967c478bd9Sstevel@tonic-gate 	    fprintf( stderr, "    %s\n", refs[ i ] );
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 	ldap_value_free( refs );
11997c478bd9Sstevel@tonic-gate     }
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 
1203*55fea89dSDan Cross /*possible operations a client can invoke -- copied from ldaprot.h */
12047c478bd9Sstevel@tonic-gate 
1205*55fea89dSDan Cross #ifndef LDAP_REQ_BIND
1206*55fea89dSDan Cross #define LDAP_REQ_BIND                   0x60L   /* application + constructed */
1207*55fea89dSDan Cross #define LDAP_REQ_UNBIND                 0x42L   /* application + primitive   */
1208*55fea89dSDan Cross #define LDAP_REQ_SEARCH                 0x63L   /* application + constructed */
1209*55fea89dSDan Cross #define LDAP_REQ_MODIFY                 0x66L   /* application + constructed */
1210*55fea89dSDan Cross #define LDAP_REQ_ADD                    0x68L   /* application + constructed */
1211*55fea89dSDan Cross #define LDAP_REQ_DELETE                 0x4aL   /* application + primitive   */
1212*55fea89dSDan Cross #define LDAP_REQ_RENAME                 0x6cL   /* application + constructed */
1213*55fea89dSDan Cross #define LDAP_REQ_COMPARE                0x6eL   /* application + constructed */
1214*55fea89dSDan Cross #define LDAP_REQ_ABANDON                0x50L   /* application + primitive   */
1215*55fea89dSDan Cross #define LDAP_REQ_EXTENDED               0x77L   /* application + constructed */
1216*55fea89dSDan Cross #endif /* LDAP_REQ_BIND */
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate struct ldapsearch_type2str {
1221*55fea89dSDan Cross 
1222*55fea89dSDan Cross     int         ldst2s_type;    /* message type */
1223*55fea89dSDan Cross     char        *ldst2s_string; /* descriptive string */
1224*55fea89dSDan Cross };
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
12277c478bd9Sstevel@tonic-gate static struct ldapsearch_type2str ldapsearch_msgtypes[] = {
12287c478bd9Sstevel@tonic-gate 
1229*55fea89dSDan Cross     /* results: */
12307c478bd9Sstevel@tonic-gate     { LDAP_RES_BIND,                    NULL },
12317c478bd9Sstevel@tonic-gate     { LDAP_RES_SEARCH_REFERENCE,        NULL },
12327c478bd9Sstevel@tonic-gate     { LDAP_RES_SEARCH_ENTRY,            NULL },
12337c478bd9Sstevel@tonic-gate     { LDAP_RES_SEARCH_RESULT,           NULL },
12347c478bd9Sstevel@tonic-gate     { LDAP_RES_MODIFY,                  NULL },
1235*55fea89dSDan Cross     { LDAP_RES_ADD,                     NULL },
12367c478bd9Sstevel@tonic-gate     { LDAP_RES_DELETE,                  NULL },
12377c478bd9Sstevel@tonic-gate     { LDAP_RES_MODDN,                   NULL },
12387c478bd9Sstevel@tonic-gate     { LDAP_RES_COMPARE,                 NULL },
12397c478bd9Sstevel@tonic-gate     { LDAP_RES_EXTENDED,                NULL },
1240*55fea89dSDan Cross     /* requests: */
1241*55fea89dSDan Cross     { LDAP_REQ_BIND,                    NULL },
1242*55fea89dSDan Cross     { LDAP_REQ_UNBIND,                  NULL },
1243*55fea89dSDan Cross     { LDAP_REQ_SEARCH,                  NULL },
1244*55fea89dSDan Cross     { LDAP_REQ_MODIFY,                  NULL },
12457c478bd9Sstevel@tonic-gate     { LDAP_REQ_ADD,                     NULL },
1246*55fea89dSDan Cross     { LDAP_REQ_DELETE,                  NULL },
1247*55fea89dSDan Cross     { LDAP_REQ_RENAME,                  NULL },
1248*55fea89dSDan Cross     { LDAP_REQ_COMPARE,                 NULL },
1249*55fea89dSDan Cross     { LDAP_REQ_ABANDON,                 NULL },
12507c478bd9Sstevel@tonic-gate     { LDAP_REQ_EXTENDED,                NULL },
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate };
12537c478bd9Sstevel@tonic-gate #else
12547c478bd9Sstevel@tonic-gate static struct ldapsearch_type2str ldapsearch_msgtypes[] = {
1255*55fea89dSDan Cross 
1256*55fea89dSDan Cross     /* results: */
12577c478bd9Sstevel@tonic-gate     { LDAP_RES_BIND,                    "bind result" },
12587c478bd9Sstevel@tonic-gate     { LDAP_RES_SEARCH_REFERENCE,        "continuation reference" },
12597c478bd9Sstevel@tonic-gate     { LDAP_RES_SEARCH_ENTRY,            "entry" },
12607c478bd9Sstevel@tonic-gate     { LDAP_RES_SEARCH_RESULT,           "search result" },
12617c478bd9Sstevel@tonic-gate     { LDAP_RES_MODIFY,                  "modify result" },
1262*55fea89dSDan Cross     { LDAP_RES_ADD,                     "add result" },
12637c478bd9Sstevel@tonic-gate     { LDAP_RES_DELETE,                  "delete result" },
12647c478bd9Sstevel@tonic-gate     { LDAP_RES_MODDN,                   "rename result" },
12657c478bd9Sstevel@tonic-gate     { LDAP_RES_COMPARE,                 "compare result" },
12667c478bd9Sstevel@tonic-gate     { LDAP_RES_EXTENDED,                "extended operation result" },
1267*55fea89dSDan Cross     /* requests: */
1268*55fea89dSDan Cross     { LDAP_REQ_BIND,                    "bind request" },
1269*55fea89dSDan Cross     { LDAP_REQ_UNBIND,                  "unbind request" },
1270*55fea89dSDan Cross     { LDAP_REQ_SEARCH,                  "search request" },
1271*55fea89dSDan Cross     { LDAP_REQ_MODIFY,                  "modify request" },
12727c478bd9Sstevel@tonic-gate     { LDAP_REQ_ADD,                     "add request" },
1273*55fea89dSDan Cross     { LDAP_REQ_DELETE,                  "delete request" },
1274*55fea89dSDan Cross     { LDAP_REQ_RENAME,                  "rename request" },
1275*55fea89dSDan Cross     { LDAP_REQ_COMPARE,                 "compare request" },
1276*55fea89dSDan Cross     { LDAP_REQ_ABANDON,                 "abandon request" },
12777c478bd9Sstevel@tonic-gate     { LDAP_REQ_EXTENDED,                "extended request" },
1278*55fea89dSDan Cross 
12797c478bd9Sstevel@tonic-gate };
12807c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate #define LDAPSEARCHTOOL_NUMTYPES (sizeof(ldapsearch_msgtypes) \
12847c478bd9Sstevel@tonic-gate 				 / sizeof(struct ldapsearch_type2str))
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
12877c478bd9Sstevel@tonic-gate static void
fill_ldapsearch_msgtypes(void)12887c478bd9Sstevel@tonic-gate fill_ldapsearch_msgtypes( void )
12897c478bd9Sstevel@tonic-gate {
12907c478bd9Sstevel@tonic-gate     int		i = 0;
12917c478bd9Sstevel@tonic-gate     if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
12927c478bd9Sstevel@tonic-gate 	!= NULL)
12937c478bd9Sstevel@tonic-gate 		return;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate     /* results: */
12967c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
12977c478bd9Sstevel@tonic-gate 			"bind result");
12987c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
12997c478bd9Sstevel@tonic-gate 			"continuation reference");
13007c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13017c478bd9Sstevel@tonic-gate 			"entry");
13027c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13037c478bd9Sstevel@tonic-gate     			"search result");
13047c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13057c478bd9Sstevel@tonic-gate     			"modify result");
13067c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13077c478bd9Sstevel@tonic-gate     			"add result");
13087c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13097c478bd9Sstevel@tonic-gate     			"delete result");
13107c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13117c478bd9Sstevel@tonic-gate     			"rename result");
13127c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13137c478bd9Sstevel@tonic-gate     			"compare result");
13147c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13157c478bd9Sstevel@tonic-gate     			"extended operation result");
1316*55fea89dSDan Cross     /* requests: */
13177c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13187c478bd9Sstevel@tonic-gate     			"bind request");
13197c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13207c478bd9Sstevel@tonic-gate     			"unbind request");
13217c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13227c478bd9Sstevel@tonic-gate     			"search request");
13237c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13247c478bd9Sstevel@tonic-gate     			"modify request");
13257c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13267c478bd9Sstevel@tonic-gate     			"add request");
13277c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13287c478bd9Sstevel@tonic-gate     			"delete request");
13297c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13307c478bd9Sstevel@tonic-gate     			"rename request");
13317c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13327c478bd9Sstevel@tonic-gate     			"compare request");
13337c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13347c478bd9Sstevel@tonic-gate     			"abandon request");
13357c478bd9Sstevel@tonic-gate     ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13367c478bd9Sstevel@tonic-gate 			"extended request");
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate /*
1341*55fea89dSDan Cross  * Return a descriptive string given an LDAP result message type (tag).
1342*55fea89dSDan Cross  */
1343*55fea89dSDan Cross static char *
msgtype2str(int msgtype)13447c478bd9Sstevel@tonic-gate msgtype2str( int msgtype )
1345*55fea89dSDan Cross {
1346*55fea89dSDan Cross     char        *s = gettext("unknown");
13477c478bd9Sstevel@tonic-gate     int         i;
1348*55fea89dSDan Cross 
13497c478bd9Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
13507c478bd9Sstevel@tonic-gate     /* Make sure ldapsearch_msgtypes is initialized */
13517c478bd9Sstevel@tonic-gate     if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
13527c478bd9Sstevel@tonic-gate 	== NULL)
13537c478bd9Sstevel@tonic-gate 		(void) fill_ldapsearch_msgtypes();
13547c478bd9Sstevel@tonic-gate #endif	/* SOLARIS_LDAP_CMD */
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate     for ( i = 0; i < LDAPSEARCHTOOL_NUMTYPES; ++i ) {
13577c478bd9Sstevel@tonic-gate 	if ( msgtype == ldapsearch_msgtypes[ i ].ldst2s_type ) {
1358*55fea89dSDan Cross 	    s = ldapsearch_msgtypes[ i ].ldst2s_string;
1359*55fea89dSDan Cross 	}
1360*55fea89dSDan Cross     }
13617c478bd9Sstevel@tonic-gate     return( s );
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * Return a descriptive string given a Persistent Search change type
13677c478bd9Sstevel@tonic-gate  */
13687c478bd9Sstevel@tonic-gate static char *
changetype_num2string(int chgtype)13697c478bd9Sstevel@tonic-gate changetype_num2string( int chgtype )
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate     char	*s = gettext("unknown");
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate     switch( chgtype ) {
13747c478bd9Sstevel@tonic-gate     case LDAP_CHANGETYPE_ADD:
13757c478bd9Sstevel@tonic-gate 	s = gettext("add");
13767c478bd9Sstevel@tonic-gate 	break;
13777c478bd9Sstevel@tonic-gate     case LDAP_CHANGETYPE_DELETE:
13787c478bd9Sstevel@tonic-gate 	s = gettext("delete");
13797c478bd9Sstevel@tonic-gate 	break;
13807c478bd9Sstevel@tonic-gate     case LDAP_CHANGETYPE_MODIFY:
13817c478bd9Sstevel@tonic-gate 	s = gettext("modify");
13827c478bd9Sstevel@tonic-gate 	break;
13837c478bd9Sstevel@tonic-gate     case LDAP_CHANGETYPE_MODDN:
13847c478bd9Sstevel@tonic-gate 	s = gettext("moddn");
13857c478bd9Sstevel@tonic-gate 	break;
13867c478bd9Sstevel@tonic-gate     }
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate     return( s );
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate /* returns a null teminated charrary */
get_effectiverights_attrlist(char * optarg)13927c478bd9Sstevel@tonic-gate static char  **get_effectiverights_attrlist(char * optarg) {
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	char * tmp_str = strdup(optarg);
13957c478bd9Sstevel@tonic-gate 	char ** retArray = NULL;
13967c478bd9Sstevel@tonic-gate 	int i = 0;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	retArray = ldap_str2charray( tmp_str, " "); /* takes copies */
13997c478bd9Sstevel@tonic-gate 
1400*55fea89dSDan Cross 	free(tmp_str);
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	/* Oops - somebody left this debug message in for the
1403*55fea89dSDan Cross 	   getEffectiveRights control
14047c478bd9Sstevel@tonic-gate 	   fprintf(stderr, "attrlist: "); */
14057c478bd9Sstevel@tonic-gate 	i = 0;
14067c478bd9Sstevel@tonic-gate 	while( retArray[i] != NULL ) {
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 		fprintf(stderr,"%s ", retArray[i]);
14097c478bd9Sstevel@tonic-gate 		i++;
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	return(retArray);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate }
1416