10Sstevel@tonic-gate /*
2*8097SSreedhar.Chalamalasetti@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
80Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
90Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
100Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
130Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
140Sstevel@tonic-gate * implied. See the License for the specific language governing
150Sstevel@tonic-gate * rights and limitations under the License.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
180Sstevel@tonic-gate * March 31, 1998.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
210Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
220Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
230Sstevel@tonic-gate * Rights Reserved.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * Contributor(s):
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /* ldapsearch.c - generic program to search LDAP */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include "ldaptool.h"
310Sstevel@tonic-gate #include "fileurl.h"
320Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include "solaris-int.h"
350Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #define VLV_PARAM_SEP ':'
380Sstevel@tonic-gate
390Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
400Sstevel@tonic-gate #define gettext(s) s
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate
430Sstevel@tonic-gate static void usage( void );
440Sstevel@tonic-gate static int dosearch( LDAP *ld, char *base, int scope, char **attrs,
450Sstevel@tonic-gate int attrsonly, char *filtpatt, char *value);
460Sstevel@tonic-gate static void write_string_attr_value( char *attrname, char *strval,
470Sstevel@tonic-gate unsigned long opts );
480Sstevel@tonic-gate #define LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME 0x01
490Sstevel@tonic-gate static int write_ldif_value( char *type, char *value, unsigned long vallen,
500Sstevel@tonic-gate unsigned long ldifoptions );
510Sstevel@tonic-gate static void print_entry( LDAP *ld, LDAPMessage *entry, int attrsonly );
520Sstevel@tonic-gate static void options_callback( int option, char *optarg );
530Sstevel@tonic-gate static void parse_and_display_reference( LDAP *ld, LDAPMessage *ref );
540Sstevel@tonic-gate static char *sortresult2string(unsigned long result);
550Sstevel@tonic-gate static char *changetype_num2string( int chgtype );
560Sstevel@tonic-gate static char *msgtype2str( int msgtype );
570Sstevel@tonic-gate static char **get_effectiverights_attrlist(char * optarg);
580Sstevel@tonic-gate
590Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
600Sstevel@tonic-gate static void fill_ldapsearch_msgtypes( void );
610Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Prefix used in names of pseudo attributes added to the entry LDIF
650Sstevel@tonic-gate * output if we receive an entryChangeNotification control with an entry
660Sstevel@tonic-gate * (requested using persistent search).
670Sstevel@tonic-gate */
680Sstevel@tonic-gate #define LDAPTOOL_PSEARCH_ATTR_PREFIX "persistentSearch-"
690Sstevel@tonic-gate
700Sstevel@tonic-gate
710Sstevel@tonic-gate static void
usage(void)720Sstevel@tonic-gate usage( void )
730Sstevel@tonic-gate {
740Sstevel@tonic-gate fprintf( stderr, gettext("usage: %s -b basedn [options] filter [attributes...]\n"), ldaptool_progname );
750Sstevel@tonic-gate fprintf( stderr, gettext(" %s -b basedn [options] -f file [attributes...]\nwhere:\n"), ldaptool_progname );
760Sstevel@tonic-gate fprintf( stderr, gettext(" basedn\tbase dn for search\n") );
770Sstevel@tonic-gate fprintf( stderr, gettext("\t\t(if the environment variable LDAP_BASEDN is set,\n") );
780Sstevel@tonic-gate fprintf( stderr, gettext("\t\tthen the -b flag is not required)\n") );
790Sstevel@tonic-gate fprintf( stderr, gettext(" filter\tRFC-2254 compliant LDAP search filter\n") );
800Sstevel@tonic-gate fprintf( stderr, gettext(" file\tfile containing a sequence of LDAP search filters to use\n") );
810Sstevel@tonic-gate fprintf( stderr, gettext(" attributes\twhitespace-separated list of attributes to retrieve\n") );
820Sstevel@tonic-gate fprintf( stderr, gettext("\t\t(if no attribute list is given, all are retrieved)\n") );
830Sstevel@tonic-gate fprintf( stderr, gettext("options:\n") );
840Sstevel@tonic-gate ldaptool_common_usage( 0 );
850Sstevel@tonic-gate #if defined( XP_WIN32 )
860Sstevel@tonic-gate fprintf( stderr, gettext(" -t\t\twrite values to files in temp directory.\n") );
870Sstevel@tonic-gate #else
880Sstevel@tonic-gate fprintf( stderr, gettext(" -t\t\twrite values to files in /tmp\n") );
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate fprintf( stderr, gettext(" -U\t\tproduce file URLs in conjunction with -t\n") );
910Sstevel@tonic-gate fprintf( stderr, gettext(" -e\t\tminimize base-64 encoding of values\n") );
920Sstevel@tonic-gate fprintf( stderr, gettext(" -u\t\tinclude User Friendly entry names in the output\n") );
930Sstevel@tonic-gate #ifndef HAVE_SASL_OPTIONS
940Sstevel@tonic-gate fprintf( stderr, gettext(" -o\t\tprint entries using old format (default is LDIF)\n") );
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate fprintf( stderr, gettext(" -T\t\tdon't fold (wrap) long lines (default is to fold)\n") );
970Sstevel@tonic-gate fprintf( stderr, gettext(" -1\t\tomit leading \"version: %d\" line in LDIF output\n"), LDIF_VERSION_ONE );
980Sstevel@tonic-gate fprintf( stderr, gettext(" -A\t\tretrieve attribute names only (no values)\n") );
990Sstevel@tonic-gate fprintf( stderr, gettext(" -B\t\tprint non-ASCII values and use old output format (attr=value)\n") );
1000Sstevel@tonic-gate fprintf( stderr, gettext(" -x\t\tperforming sorting on server\n") );
1010Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1020Sstevel@tonic-gate fprintf( stderr, gettext(" -r\t\tprint entries using old format (default is LDIF)\n") );
1030Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
1040Sstevel@tonic-gate fprintf( stderr, gettext(" -F sep\tprint `sep' instead of `%s' between attribute names\n"), LDAPTOOL_DEFSEP );
1050Sstevel@tonic-gate fprintf( stderr, gettext(" \tand values\n") );
1060Sstevel@tonic-gate fprintf( stderr, gettext(" -S attr\tsort the results by attribute `attr'\n") );
1070Sstevel@tonic-gate fprintf( stderr, gettext(" -s scope\tone of base, one, or sub (default is sub)\n") );
1080Sstevel@tonic-gate fprintf( stderr, gettext(" -a deref\tone of never, always, search, or find (default: never)\n") );
1090Sstevel@tonic-gate fprintf( stderr, gettext(" \t(alias dereferencing)\n") );
1100Sstevel@tonic-gate fprintf( stderr, gettext(" -l timelim\ttime limit (in seconds) for search (default is no limit)\n") );
1110Sstevel@tonic-gate fprintf( stderr, gettext(" -z sizelim\tsize limit (in entries) for search (default is no limit)\n") );
1120Sstevel@tonic-gate fprintf( stderr, gettext(" -C ps:changetype[:changesonly[:entrychgcontrols]]\n") );
1130Sstevel@tonic-gate fprintf( stderr, gettext("\t\tchangetypes are add,delete,modify,moddn,any\n") );
1140Sstevel@tonic-gate fprintf( stderr, gettext("\t\tchangesonly and entrychgcontrols are boolean values\n") );
1150Sstevel@tonic-gate fprintf( stderr, gettext("\t\t(default is 1)\n") );
1160Sstevel@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 );
1170Sstevel@tonic-gate fprintf( stderr, gettext("\t\t'after' are the number of entries surrounding 'index.'\n"));
1180Sstevel@tonic-gate fprintf( stderr, gettext("\t\t'count' is the content count, 'value' is the search value.\n"));
1190Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
1200Sstevel@tonic-gate fprintf( stderr, gettext(" -c authzid\tspecifies the getEffectiveRights control authzid\n"));
1210Sstevel@tonic-gate fprintf( stderr, gettext("\t\t eg. dn:uid=bjensen,dc=example,dc=com\n"));
1220Sstevel@tonic-gate fprintf( stderr, gettext("\t\t A value of \"\" means \"the authorization id for the operation\".\n"));
1230Sstevel@tonic-gate fprintf( stderr, gettext("\t\t A value of \"dn:\" means \"anonymous\"\n"));
1240Sstevel@tonic-gate fprintf( stderr, gettext("\t\t (The aclRights operational attribute must be requested)\n"));
1250Sstevel@tonic-gate fprintf( stderr, gettext(" -X attrlist\tspecifies the getEffectiveRights control specific attribute list.\n"));
1260Sstevel@tonic-gate fprintf( stderr, gettext("\t\t eg. \"nsroledn userPassword\"\n"));
1270Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate exit( LDAP_PARAM_ERROR );
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate static char *base = NULL;
1330Sstevel@tonic-gate static char *sep = LDAPTOOL_DEFSEP;
1340Sstevel@tonic-gate static char **sortattr = NULL;
1350Sstevel@tonic-gate static char *vlv_value = NULL;
1360Sstevel@tonic-gate static int sortsize = 0;
1370Sstevel@tonic-gate static int *skipsortattr = NULL;
1380Sstevel@tonic-gate static int includeufn, allow_binary, vals2tmp, ldif, scope, deref;
1390Sstevel@tonic-gate static int attrsonly, timelimit, sizelimit, server_sort, fold;
1400Sstevel@tonic-gate static int minimize_base64, produce_file_urls;
1410Sstevel@tonic-gate static int use_vlv = 0, vlv_before, vlv_after, vlv_index, vlv_count;
1420Sstevel@tonic-gate static int use_psearch=0;
1430Sstevel@tonic-gate static int write_ldif_version = 1;
1440Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
1450Sstevel@tonic-gate static char *get_effectiverights_control_target_dn = NULL; /* -c */
1460Sstevel@tonic-gate static char **get_effectiverights_control_attrlist = NULL; /* -X */
1470Sstevel@tonic-gate static int do_effective_rights_control = 0;
1480Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /* Persistent search variables */
1510Sstevel@tonic-gate static int chgtype=0, changesonly=1, return_echg_ctls=1;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate int
main(int argc,char ** argv)1550Sstevel@tonic-gate main( int argc, char **argv )
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate char *filtpattern, **attrs;
1580Sstevel@tonic-gate int rc, optind, i, first, free_filtpattern;
1590Sstevel@tonic-gate LDAP *ld;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1620Sstevel@tonic-gate char *locale = setlocale(LC_ALL, "");
1630Sstevel@tonic-gate textdomain(TEXT_DOMAIN);
1640Sstevel@tonic-gate ldaptool_require_binddn = 0;
1650Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate free_filtpattern = 0;
1680Sstevel@tonic-gate deref = LDAP_DEREF_NEVER;
1690Sstevel@tonic-gate allow_binary = vals2tmp = attrsonly = 0;
1700Sstevel@tonic-gate minimize_base64 = produce_file_urls = 0;
1710Sstevel@tonic-gate ldif = 1;
1720Sstevel@tonic-gate fold = 1;
1730Sstevel@tonic-gate sizelimit = timelimit = 0;
1740Sstevel@tonic-gate scope = LDAP_SCOPE_SUBTREE;
1750Sstevel@tonic-gate server_sort = 0;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate #ifdef notdef
1790Sstevel@tonic-gate #ifdef HPUX11
1800Sstevel@tonic-gate #ifndef __LP64__
1810Sstevel@tonic-gate _main( argc, argv);
1820Sstevel@tonic-gate #endif /* __LP64_ */
1830Sstevel@tonic-gate #endif /* HPUX11 */
1840Sstevel@tonic-gate #endif
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate ldaptool_reset_control_array( ldaptool_request_ctrls );
1880Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS
1890Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
1900Sstevel@tonic-gate optind = ldaptool_process_args( argc, argv, "ABLTU1etuxra:b:F:G:l:S:s:z:C:",
1910Sstevel@tonic-gate 0, options_callback );
1920Sstevel@tonic-gate #else
1930Sstevel@tonic-gate optind = ldaptool_process_args( argc, argv, "ABLTU1etuxa:b:F:G:l:S:s:z:C:c:",
1940Sstevel@tonic-gate 0, options_callback );
1950Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
1960Sstevel@tonic-gate #else
1970Sstevel@tonic-gate optind = ldaptool_process_args( argc, argv, "ABLTU1eotuxa:b:F:G:l:S:s:z:C:c:",
1980Sstevel@tonic-gate 0, options_callback );
1990Sstevel@tonic-gate #endif /* HAVE_SASL_OPTIONS */
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if ( optind == -1 ) {
2020Sstevel@tonic-gate usage();
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if ( base == NULL ) {
2060Sstevel@tonic-gate if (( base = getenv( "LDAP_BASEDN" )) == NULL ) {
2070Sstevel@tonic-gate usage();
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate if ( sortattr ) {
2110Sstevel@tonic-gate for ( sortsize = 0; sortattr[sortsize] != NULL; sortsize++ ) {
2120Sstevel@tonic-gate ; /* NULL */
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate sortsize++; /* add in the final NULL field */
2150Sstevel@tonic-gate skipsortattr = (int *) malloc( sortsize * sizeof(int *) );
2160Sstevel@tonic-gate if ( skipsortattr == NULL ) {
2170Sstevel@tonic-gate fprintf( stderr, gettext("Out of memory\n") );
2180Sstevel@tonic-gate exit( LDAP_NO_MEMORY );
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate memset( (char *) skipsortattr, 0, sortsize * sizeof(int *) );
2210Sstevel@tonic-gate } else if ( server_sort ) {
2220Sstevel@tonic-gate server_sort = 0; /* ignore this option if no sortattrs were given */
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if ( argc - optind < 1 ) {
2260Sstevel@tonic-gate if ( ldaptool_fp == NULL ) {
2270Sstevel@tonic-gate usage();
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate attrs = NULL;
2300Sstevel@tonic-gate filtpattern = "%s";
2310Sstevel@tonic-gate } else { /* there are additional args (filter + attrs) */
2320Sstevel@tonic-gate if ( ldaptool_fp == NULL || strstr( argv[ optind ], "%s" ) != NULL ) {
2330Sstevel@tonic-gate filtpattern = ldaptool_local2UTF8( argv[ optind ] );
2340Sstevel@tonic-gate /* since local2UTF8 always allocates something, we should free it */
2350Sstevel@tonic-gate free_filtpattern = 1;
2360Sstevel@tonic-gate ++optind;
2370Sstevel@tonic-gate } else {
2380Sstevel@tonic-gate filtpattern = "%s";
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate if ( argv[ optind ] == NULL ) {
2420Sstevel@tonic-gate attrs = NULL;
2430Sstevel@tonic-gate } else if ( sortattr == NULL || *sortattr == '\0' || server_sort) {
2440Sstevel@tonic-gate attrs = &argv[ optind ];
2450Sstevel@tonic-gate } else {
2460Sstevel@tonic-gate attrs = ldap_charray_dup( &argv[ optind ] );
2470Sstevel@tonic-gate if ( attrs == NULL ) {
2480Sstevel@tonic-gate fprintf( stderr, gettext("Out of memory\n") );
2490Sstevel@tonic-gate exit( LDAP_NO_MEMORY );
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate for ( i = 0; i < (sortsize - 1); i++ ) {
2520Sstevel@tonic-gate if ( !ldap_charray_inlist( attrs, sortattr[i] ) ) {
2530Sstevel@tonic-gate if ( ldap_charray_add( &attrs, sortattr[i] ) != 0 ) {
2540Sstevel@tonic-gate fprintf( stderr, gettext("Out of memory\n") );
2550Sstevel@tonic-gate exit( LDAP_NO_MEMORY );
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate * attribute in the search list only for the purpose of
2590Sstevel@tonic-gate * sorting
2600Sstevel@tonic-gate */
2610Sstevel@tonic-gate skipsortattr[i] = 1;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate ld = ldaptool_ldap_init( 0 );
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate if ( !ldaptool_not ) {
2700Sstevel@tonic-gate ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
2710Sstevel@tonic-gate ldap_set_option( ld, LDAP_OPT_TIMELIMIT, &timelimit );
2720Sstevel@tonic-gate ldap_set_option( ld, LDAP_OPT_SIZELIMIT, &sizelimit );
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate ldaptool_bind( ld );
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate if ( ldaptool_verbose ) {
2780Sstevel@tonic-gate printf( gettext("filter pattern: %s\nreturning: "), filtpattern );
2790Sstevel@tonic-gate if ( attrs == NULL ) {
2800Sstevel@tonic-gate printf( gettext("ALL") );
2810Sstevel@tonic-gate } else {
2820Sstevel@tonic-gate for ( i = 0; attrs[ i ] != NULL; ++i ) {
2830Sstevel@tonic-gate printf( "%s ", attrs[ i ] );
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate putchar( '\n' );
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate if ( ldaptool_fp == NULL ) {
2900Sstevel@tonic-gate char *conv;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate conv = ldaptool_local2UTF8( base );
2930Sstevel@tonic-gate rc = dosearch( ld, conv, scope, attrs, attrsonly, filtpattern, "" );
2940Sstevel@tonic-gate if( conv != NULL )
2950Sstevel@tonic-gate free( conv );
2960Sstevel@tonic-gate } else {
2970Sstevel@tonic-gate int done = 0;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate rc = LDAP_SUCCESS;
3000Sstevel@tonic-gate first = 1;
3010Sstevel@tonic-gate while ( rc == LDAP_SUCCESS && !done ) {
3020Sstevel@tonic-gate char *linep = NULL;
3030Sstevel@tonic-gate int increment = 0;
3040Sstevel@tonic-gate int c, index;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate /* allocate initial block of memory */
3070Sstevel@tonic-gate if ((linep = (char *)malloc(BUFSIZ)) == NULL) {
3080Sstevel@tonic-gate fprintf( stderr, gettext("Out of memory\n") );
3090Sstevel@tonic-gate exit( LDAP_NO_MEMORY );
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate increment++;
3120Sstevel@tonic-gate index = 0;
3130Sstevel@tonic-gate while ((c = fgetc( ldaptool_fp )) != '\n' && c != EOF) {
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate /* check if we will overflow the buffer */
3160Sstevel@tonic-gate if ((c != EOF) && (index == ((increment * BUFSIZ) -1))) {
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate /* if we did, add another BUFSIZ worth of bytes */
3190Sstevel@tonic-gate if ((linep = (char *)
3200Sstevel@tonic-gate realloc(linep, (increment + 1) * BUFSIZ)) == NULL) {
3210Sstevel@tonic-gate fprintf( stderr, gettext("Out of memory\n") );
3220Sstevel@tonic-gate exit( LDAP_NO_MEMORY );
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate increment++;
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate linep[index++] = c;
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate if (c == EOF) {
3300Sstevel@tonic-gate done = 1;
3310Sstevel@tonic-gate break;
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate linep[index] = '\0';
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate if ( !first ) {
3370Sstevel@tonic-gate putchar( '\n' );
3380Sstevel@tonic-gate } else {
3390Sstevel@tonic-gate first = 0;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
3420Sstevel@tonic-gate linep );
3430Sstevel@tonic-gate free (linep);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate ldaptool_cleanup( ld );
3480Sstevel@tonic-gate if (free_filtpattern != 0 && filtpattern != NULL) {
3490Sstevel@tonic-gate free (filtpattern);
3500Sstevel@tonic-gate }
351*8097SSreedhar.Chalamalasetti@Sun.COM
352*8097SSreedhar.Chalamalasetti@Sun.COM /* check for and report output error */
353*8097SSreedhar.Chalamalasetti@Sun.COM fflush( stdout );
354*8097SSreedhar.Chalamalasetti@Sun.COM rc = ldaptool_check_ferror( stdout, rc,
355*8097SSreedhar.Chalamalasetti@Sun.COM gettext("output error (output might be incomplete)") );
3560Sstevel@tonic-gate return( rc );
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate static void
options_callback(int option,char * optarg)3610Sstevel@tonic-gate options_callback( int option, char *optarg )
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate char *s, *temp_arg, *ps_ptr, *ps_arg;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate switch( option ) {
3660Sstevel@tonic-gate case 'u': /* include UFN */
3670Sstevel@tonic-gate ++includeufn;
3680Sstevel@tonic-gate break;
3690Sstevel@tonic-gate case 't': /* write attribute values to /tmp files */
3700Sstevel@tonic-gate ++vals2tmp;
3710Sstevel@tonic-gate break;
3720Sstevel@tonic-gate case 'U': /* produce file URLs in conjunction with -t */
3730Sstevel@tonic-gate ++produce_file_urls;
3740Sstevel@tonic-gate break;
3750Sstevel@tonic-gate case 'e': /* minimize base-64 encoding of values */
3760Sstevel@tonic-gate ++minimize_base64;
3770Sstevel@tonic-gate break;
3780Sstevel@tonic-gate case 'A': /* retrieve attribute names only -- no values */
3790Sstevel@tonic-gate ++attrsonly;
3800Sstevel@tonic-gate break;
3810Sstevel@tonic-gate case 'L': /* print entries in LDIF format -- now the default */
3820Sstevel@tonic-gate break;
3830Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
3840Sstevel@tonic-gate case 'r': /* print entries in the old format */
3850Sstevel@tonic-gate ldif = 0;
3860Sstevel@tonic-gate break;
3870Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
3880Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS
3890Sstevel@tonic-gate #ifdef HAVE_SASL_OPTIONS_2
3900Sstevel@tonic-gate case 'o': /* print entries using old ldapsearch format */
3910Sstevel@tonic-gate ldif = 0;
3920Sstevel@tonic-gate break;
3930Sstevel@tonic-gate #endif
3940Sstevel@tonic-gate #else
3950Sstevel@tonic-gate case 'o': /* print entries using old ldapsearch format */
3960Sstevel@tonic-gate ldif = 0;
3970Sstevel@tonic-gate break;
3980Sstevel@tonic-gate #endif
3990Sstevel@tonic-gate case 'B': /* allow binary values to be printed, use old format */
4000Sstevel@tonic-gate ++allow_binary;
4010Sstevel@tonic-gate ldif = 0;
4020Sstevel@tonic-gate break;
4030Sstevel@tonic-gate case '1': /* omit leading "version: #" line from LDIF output */
4040Sstevel@tonic-gate write_ldif_version = 0;
4050Sstevel@tonic-gate break;
4060Sstevel@tonic-gate case 's': /* search scope */
4070Sstevel@tonic-gate if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
4080Sstevel@tonic-gate scope = LDAP_SCOPE_BASE;
4090Sstevel@tonic-gate } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
4100Sstevel@tonic-gate scope = LDAP_SCOPE_ONELEVEL;
4110Sstevel@tonic-gate } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
4120Sstevel@tonic-gate scope = LDAP_SCOPE_SUBTREE;
4130Sstevel@tonic-gate } else {
4140Sstevel@tonic-gate fprintf( stderr, gettext("scope should be base, one, or sub\n") );
4150Sstevel@tonic-gate usage();
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate break;
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate case 'a': /* set alias deref option */
4200Sstevel@tonic-gate if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
4210Sstevel@tonic-gate deref = LDAP_DEREF_NEVER;
4220Sstevel@tonic-gate } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
4230Sstevel@tonic-gate deref = LDAP_DEREF_SEARCHING;
4240Sstevel@tonic-gate } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
4250Sstevel@tonic-gate deref = LDAP_DEREF_FINDING;
4260Sstevel@tonic-gate } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
4270Sstevel@tonic-gate deref = LDAP_DEREF_ALWAYS;
4280Sstevel@tonic-gate } else {
4290Sstevel@tonic-gate fprintf( stderr, gettext("alias deref should be never, search, find, or always\n") );
4300Sstevel@tonic-gate usage();
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate break;
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate case 'F': /* field separator */
4350Sstevel@tonic-gate sep = strdup( optarg );
4360Sstevel@tonic-gate ldif = 0;
4370Sstevel@tonic-gate break;
4380Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
4390Sstevel@tonic-gate case 'c':
4400Sstevel@tonic-gate if ( optarg && optarg[0] == '\0' ) {
4410Sstevel@tonic-gate /* -c ""
4420Sstevel@tonic-gate means "This user"
4430Sstevel@tonic-gate */
4440Sstevel@tonic-gate get_effectiverights_control_target_dn = NULL;
4450Sstevel@tonic-gate do_effective_rights_control = 1;
4460Sstevel@tonic-gate }else if ( strlen(optarg) < 3 || (strncasecmp(optarg, "dn:", 3) != 0) ) {
4470Sstevel@tonic-gate fprintf(stderr, gettext("-c wrong format--should be \"\" or \"dn:...\".\n"
4480Sstevel@tonic-gate "\"dn:\" means anonymous user."));
4490Sstevel@tonic-gate usage();
4500Sstevel@tonic-gate } else {
4510Sstevel@tonic-gate get_effectiverights_control_target_dn = strdup(optarg);
4520Sstevel@tonic-gate do_effective_rights_control = 1;
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate break;
4550Sstevel@tonic-gate case 'X':
4560Sstevel@tonic-gate get_effectiverights_control_attrlist = get_effectiverights_attrlist(optarg);
4570Sstevel@tonic-gate do_effective_rights_control = 1;
4580Sstevel@tonic-gate break;
4590Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
4600Sstevel@tonic-gate case 'b': /* searchbase */
4610Sstevel@tonic-gate base = strdup( optarg );
4620Sstevel@tonic-gate break;
4630Sstevel@tonic-gate case 'l': /* time limit */
4640Sstevel@tonic-gate timelimit = atoi( optarg );
4650Sstevel@tonic-gate break;
4660Sstevel@tonic-gate case 'x': /* server sorting requested */
4670Sstevel@tonic-gate server_sort = 1;
4680Sstevel@tonic-gate break;
4690Sstevel@tonic-gate case 'z': /* size limit */
4700Sstevel@tonic-gate sizelimit = atoi( optarg );
4710Sstevel@tonic-gate break;
4720Sstevel@tonic-gate case 'S': /* sort attribute */
4730Sstevel@tonic-gate ldap_charray_add( &sortattr, strdup( optarg ) );
4740Sstevel@tonic-gate break;
4750Sstevel@tonic-gate case 'T': /* don't fold lines */
4760Sstevel@tonic-gate fold = 0;
4770Sstevel@tonic-gate break;
4780Sstevel@tonic-gate case 'G': /* do the virtual list setup */
4790Sstevel@tonic-gate use_vlv++;
4800Sstevel@tonic-gate s = strchr(optarg, VLV_PARAM_SEP );
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate if (s != NULL)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate vlv_before = atoi(optarg);
4850Sstevel@tonic-gate s++;
4860Sstevel@tonic-gate vlv_after = atoi( s );
4870Sstevel@tonic-gate s = strchr(s, VLV_PARAM_SEP );
4880Sstevel@tonic-gate if (s != NULL)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate s++;
4910Sstevel@tonic-gate /* below is a small set of logic to implement the following cases
4920Sstevel@tonic-gate * -G23:23:wilber
4930Sstevel@tonic-gate * -G23:23:"wilber:wright"
4940Sstevel@tonic-gate * -G23:23:'wilber'
4950Sstevel@tonic-gate * -G23:23:wilber wright
4960Sstevel@tonic-gate * all of the above are before, after, value - NOTE: a colon not in a quoted
4970Sstevel@tonic-gate * string will break the parser!!!!
4980Sstevel@tonic-gate * -G23:23:45:600
4990Sstevel@tonic-gate * above is index, count encoding
5000Sstevel@tonic-gate */
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate if (*s == '\'' || *s == '"')
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate vlv_value = strdup( s );
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate else
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate if (strchr( s, VLV_PARAM_SEP ))
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate /* we have an index + count option */
5110Sstevel@tonic-gate vlv_index = atoi( s );
5120Sstevel@tonic-gate vlv_count = atoi( strchr( s, VLV_PARAM_SEP) + 1);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate else
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate /* we don't have a quote surrounding the assertion value
5170Sstevel@tonic-gate * do we need to???
5180Sstevel@tonic-gate */
5190Sstevel@tonic-gate vlv_value = strdup( s );
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate else
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate fprintf( stderr,gettext("Illegal 'after' paramater for virtual list\n") );
5260Sstevel@tonic-gate exit( LDAP_PARAM_ERROR );
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate else
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate fprintf( stderr,gettext("Illegal 'before' paramater for virtual list\n") );
5330Sstevel@tonic-gate exit( LDAP_PARAM_ERROR );
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate break;
5360Sstevel@tonic-gate case 'C':
5370Sstevel@tonic-gate use_psearch++;
5380Sstevel@tonic-gate if ( (ps_arg = strdup( optarg)) == NULL ) {
5390Sstevel@tonic-gate perror ("strdup");
5400Sstevel@tonic-gate exit (LDAP_NO_MEMORY);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate ps_ptr=strtok(ps_arg, ":");
5440Sstevel@tonic-gate if (ps_ptr == NULL || (strcasecmp(ps_ptr, "ps")) ) {
5450Sstevel@tonic-gate fprintf (stderr, gettext("Invalid argument for -C\n"));
5460Sstevel@tonic-gate usage();
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate if (NULL != (ps_ptr=strtok(NULL, ":"))) {
5490Sstevel@tonic-gate if ( (temp_arg = strdup( ps_ptr )) == NULL ) {
5500Sstevel@tonic-gate perror ("strdup");
5510Sstevel@tonic-gate exit (LDAP_NO_MEMORY);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate } else {
5540Sstevel@tonic-gate fprintf (stderr, gettext("Invalid argument for -C\n"));
5550Sstevel@tonic-gate usage();
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate if (NULL != (ps_ptr=strtok(NULL, ":"))) {
5580Sstevel@tonic-gate if ( (changesonly = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
5590Sstevel@tonic-gate fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
5600Sstevel@tonic-gate usage();
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate if (NULL != (ps_ptr=strtok(NULL, ":"))) {
5640Sstevel@tonic-gate if ( (return_echg_ctls = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
5650Sstevel@tonic-gate fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
5660Sstevel@tonic-gate usage();
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate /* Now parse the temp_arg and build chgtype as
5710Sstevel@tonic-gate * the changetypes are encountered */
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate if ((ps_ptr = strtok( temp_arg, "," )) == NULL) {
5740Sstevel@tonic-gate usage();
5750Sstevel@tonic-gate } else {
5760Sstevel@tonic-gate while ( ps_ptr ) {
5770Sstevel@tonic-gate if ((strcasecmp(ps_ptr, "add"))==0)
5780Sstevel@tonic-gate chgtype |= LDAP_CHANGETYPE_ADD;
5790Sstevel@tonic-gate else if ((strcasecmp(ps_ptr, "delete"))==0)
5800Sstevel@tonic-gate chgtype |= LDAP_CHANGETYPE_DELETE;
5810Sstevel@tonic-gate else if ((strcasecmp(ps_ptr, "modify"))==0)
5820Sstevel@tonic-gate chgtype |= LDAP_CHANGETYPE_MODIFY;
5830Sstevel@tonic-gate else if ((strcasecmp(ps_ptr, "moddn"))==0)
5840Sstevel@tonic-gate chgtype |= LDAP_CHANGETYPE_MODDN;
5850Sstevel@tonic-gate else if ((strcasecmp(ps_ptr, "any"))==0)
5860Sstevel@tonic-gate chgtype = LDAP_CHANGETYPE_ANY;
5870Sstevel@tonic-gate else {
5880Sstevel@tonic-gate fprintf(stderr, gettext("Unknown changetype: %s\n"), ps_ptr);
5890Sstevel@tonic-gate usage();
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate ps_ptr = strtok( NULL, "," );
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate break;
5950Sstevel@tonic-gate default:
5960Sstevel@tonic-gate usage();
5970Sstevel@tonic-gate break;
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate static int
dosearch(ld,base,scope,attrs,attrsonly,filtpatt,value)6030Sstevel@tonic-gate dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
6040Sstevel@tonic-gate LDAP *ld;
6050Sstevel@tonic-gate char *base;
6060Sstevel@tonic-gate int scope;
6070Sstevel@tonic-gate char **attrs;
6080Sstevel@tonic-gate int attrsonly;
6090Sstevel@tonic-gate char *filtpatt;
6100Sstevel@tonic-gate char *value;
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate char **refs = NULL, filter[ BUFSIZ ], *filterp = NULL;
6130Sstevel@tonic-gate int rc, first, matches;
6140Sstevel@tonic-gate LDAPMessage *res, *e;
6150Sstevel@tonic-gate LDAPControl *ldctrl;
6160Sstevel@tonic-gate LDAPControl **ctrl_response_array = NULL;
6170Sstevel@tonic-gate LDAPVirtualList vlv_data;
6180Sstevel@tonic-gate int msgid = 0;
6190Sstevel@tonic-gate int length = 0;
6200Sstevel@tonic-gate int mallocd_filter = 0;
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate if ( strstr( filtpatt, "%s" ) == NULL ) { /* no need to sprintf() */
6230Sstevel@tonic-gate filterp = filtpatt;
6240Sstevel@tonic-gate } else {
6250Sstevel@tonic-gate length = strlen( filtpatt ) + strlen ( value ) +1;
6260Sstevel@tonic-gate if ( length > BUFSIZ ) {
6270Sstevel@tonic-gate if ((filterp = (char *)
6280Sstevel@tonic-gate malloc ( length )) == NULL) {
6290Sstevel@tonic-gate perror( gettext("filter and/or pattern too long?") );
6300Sstevel@tonic-gate exit (LDAP_PARAM_ERROR);
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate mallocd_filter = 1;
6330Sstevel@tonic-gate } else {
6340Sstevel@tonic-gate filterp = filter;
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate #ifdef HAVE_SNPRINTF
6380Sstevel@tonic-gate if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {
6390Sstevel@tonic-gate perror( gettext("snprintf filter (filter and/or pattern too long?)") );
6400Sstevel@tonic-gate exit( LDAP_PARAM_ERROR );
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate #else
6430Sstevel@tonic-gate sprintf( filterp, filtpatt, value );
6440Sstevel@tonic-gate #endif
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate if ( *filterp == '\0' ) { /* treat empty filter is a shortcut for oc=* */
6480Sstevel@tonic-gate if (mallocd_filter) {
6490Sstevel@tonic-gate free(filterp);
6500Sstevel@tonic-gate mallocd_filter = 0;
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate filterp = "(objectclass=*)";
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate if ( ldaptool_verbose ) {
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate * Display the filter that will be used. Add surrounding parens.
6580Sstevel@tonic-gate * if they are missing.
6590Sstevel@tonic-gate */
6600Sstevel@tonic-gate if ( '(' == *filterp ) {
6610Sstevel@tonic-gate printf( "filter is: %s\n", filterp );
6620Sstevel@tonic-gate } else {
6630Sstevel@tonic-gate printf( "filter is: (%s)\n", filterp );
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate if ( ldaptool_not ) {
6680Sstevel@tonic-gate if (mallocd_filter) free(filterp);
6690Sstevel@tonic-gate return( LDAP_SUCCESS );
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
6730Sstevel@tonic-gate ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
6770Sstevel@tonic-gate ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate #ifndef SOLARIS_LDAP_CMD
6810Sstevel@tonic-gate if ( do_effective_rights_control ) {
6820Sstevel@tonic-gate if ((ldctrl = ldaptool_create_geteffectiveRights_control(ld,
6830Sstevel@tonic-gate get_effectiverights_control_target_dn,
6840Sstevel@tonic-gate (const char**) get_effectiverights_control_attrlist)) !=NULL) {
6850Sstevel@tonic-gate ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate if (use_psearch) {
6910Sstevel@tonic-gate if ( ldap_create_persistentsearch_control( ld, chgtype,
6920Sstevel@tonic-gate changesonly, return_echg_ctls,
6930Sstevel@tonic-gate 1, &ldctrl ) != LDAP_SUCCESS )
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate ldap_perror( ld, "ldap_create_persistentsearch_control" );
6960Sstevel@tonic-gate return (1);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate if (server_sort) {
7030Sstevel@tonic-gate /* First make a sort key list from the attribute list we have */
7040Sstevel@tonic-gate LDAPsortkey **keylist = NULL;
7050Sstevel@tonic-gate int i = 0;
7060Sstevel@tonic-gate char *sortattrs = NULL;
7070Sstevel@tonic-gate char *s = NULL;
7080Sstevel@tonic-gate int string_length = 0;
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate /* Count the sort strings */
7110Sstevel@tonic-gate for (i = 0; i < sortsize - 1 ; i++) {
7120Sstevel@tonic-gate string_length += strlen(sortattr[i]) + 1;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate sortattrs = (char *) malloc(string_length + 1);
7160Sstevel@tonic-gate if (NULL == sortattrs) {
7170Sstevel@tonic-gate fprintf( stderr, gettext("Out of memory\n") );
7180Sstevel@tonic-gate exit( LDAP_NO_MEMORY );
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate s = sortattrs;
7220Sstevel@tonic-gate for (i = 0; i < sortsize - 1 ; i++) {
7230Sstevel@tonic-gate memcpy(s, sortattr[i], strlen(sortattr[i]));
7240Sstevel@tonic-gate s += strlen(sortattr[i]);
7250Sstevel@tonic-gate *s++ = ' ';
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate sortattrs[string_length] = '\0';
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate ldap_create_sort_keylist(&keylist,sortattrs);
7310Sstevel@tonic-gate free(sortattrs);
7320Sstevel@tonic-gate sortattrs = NULL;
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate /* Then make a control for the sort attributes we have */
7350Sstevel@tonic-gate rc = ldap_create_sort_control(ld,keylist,0,&ldctrl);
7360Sstevel@tonic-gate ldap_free_sort_keylist(keylist);
7370Sstevel@tonic-gate if ( rc != LDAP_SUCCESS ) {
7380Sstevel@tonic-gate if (mallocd_filter) free(filterp);
7390Sstevel@tonic-gate return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
7400Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP ));
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate /* remember server side sorting must be available for vlv!!!! */
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate if (use_vlv)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate vlv_data.ldvlist_before_count = vlv_before;
7510Sstevel@tonic-gate vlv_data.ldvlist_after_count = vlv_after;
7520Sstevel@tonic-gate if ( ldaptool_verbose ) {
7530Sstevel@tonic-gate printf( gettext("vlv data %lu, %lu, "),
7540Sstevel@tonic-gate vlv_data.ldvlist_before_count,
7550Sstevel@tonic-gate vlv_data.ldvlist_after_count
7560Sstevel@tonic-gate );
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate if (vlv_value)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate vlv_data.ldvlist_attrvalue = vlv_value;
7610Sstevel@tonic-gate vlv_data.ldvlist_size = 0;
7620Sstevel@tonic-gate vlv_data.ldvlist_index = 0;
7630Sstevel@tonic-gate if ( ldaptool_verbose ) {
7640Sstevel@tonic-gate printf( "%s, 0, 0\n", vlv_data.ldvlist_attrvalue);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate else
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate vlv_data.ldvlist_attrvalue = NULL;
7700Sstevel@tonic-gate vlv_data.ldvlist_size = vlv_count;
7710Sstevel@tonic-gate vlv_data.ldvlist_index = vlv_index;
7720Sstevel@tonic-gate if ( ldaptool_verbose ) {
7730Sstevel@tonic-gate printf( "(null), %lu, %lu\n", vlv_data.ldvlist_size, vlv_data.ldvlist_index );
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate if ( rc != LDAP_SUCCESS ) {
7780Sstevel@tonic-gate if (mallocd_filter) free(filterp);
7790Sstevel@tonic-gate return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
7800Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP ));
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate if (LDAP_SUCCESS != (rc = ldap_create_virtuallist_control(ld,
7830Sstevel@tonic-gate &vlv_data, &ldctrl)))
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate if (mallocd_filter) free(filterp);
7860Sstevel@tonic-gate return( ldaptool_print_lderror( ld,
7870Sstevel@tonic-gate "ldap_create_virtuallist_control",
7880Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP ));
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate if ( ldap_search_ext( ld, base, scope, filterp, attrs, attrsonly,
7960Sstevel@tonic-gate ldaptool_request_ctrls, NULL, NULL, -1, &msgid )
7970Sstevel@tonic-gate != LDAP_SUCCESS ) {
7980Sstevel@tonic-gate if (mallocd_filter) free(filterp);
7990Sstevel@tonic-gate return( ldaptool_print_lderror( ld, "ldap_search",
8000Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP ));
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate matches = 0;
8050Sstevel@tonic-gate first = 1;
8060Sstevel@tonic-gate if ( sortattr && !server_sort ) {
8070Sstevel@tonic-gate rc = ldap_result( ld, LDAP_RES_ANY, 1, NULL, &res );
8080Sstevel@tonic-gate } else {
8090Sstevel@tonic-gate while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res )) !=
8100Sstevel@tonic-gate LDAP_RES_SEARCH_RESULT && rc != -1 ) {
8110Sstevel@tonic-gate if ( rc != LDAP_RES_SEARCH_ENTRY ) {
8120Sstevel@tonic-gate if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
8130Sstevel@tonic-gate parse_and_display_reference( ld, res );
8140Sstevel@tonic-gate } else if ( rc == LDAP_RES_EXTENDED
8150Sstevel@tonic-gate && ldap_msgid( res ) == LDAP_RES_UNSOLICITED ) {
8160Sstevel@tonic-gate ldaptool_print_extended_response( ld, res,
8170Sstevel@tonic-gate gettext("Unsolicited response") );
8180Sstevel@tonic-gate } else {
8190Sstevel@tonic-gate fprintf( stderr, gettext("%s: ignoring LDAP response message"
8200Sstevel@tonic-gate " type 0x%x (%s)\n"),
8210Sstevel@tonic-gate ldaptool_progname, rc, msgtype2str( rc ));
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate ldap_msgfree( res );
8240Sstevel@tonic-gate continue;
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate matches++;
8270Sstevel@tonic-gate e = ldap_first_entry( ld, res );
8280Sstevel@tonic-gate if ( !first ) {
8290Sstevel@tonic-gate putchar( '\n' );
8300Sstevel@tonic-gate } else {
8310Sstevel@tonic-gate first = 0;
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate print_entry( ld, e, attrsonly );
8340Sstevel@tonic-gate ldap_msgfree( res );
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate if ( rc == -1 ) {
8380Sstevel@tonic-gate if (mallocd_filter) free(filterp);
8390Sstevel@tonic-gate return( ldaptool_print_lderror( ld, "ldap_result",
8400Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP ));
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate
8430Sstevel@tonic-gate if ( ldap_parse_result( ld, res, &rc, NULL, NULL, &refs,
8440Sstevel@tonic-gate &ctrl_response_array, 0 ) != LDAP_SUCCESS ) {
8450Sstevel@tonic-gate ldaptool_print_lderror( ld, "ldap_parse_result",
8460Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP );
8470Sstevel@tonic-gate } else if ( rc != LDAP_SUCCESS ) {
8480Sstevel@tonic-gate ldaptool_print_lderror( ld, "ldap_search",
8490Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP );
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate /* Parse the returned sort control */
8520Sstevel@tonic-gate if (server_sort) {
8530Sstevel@tonic-gate unsigned long result = 0;
8540Sstevel@tonic-gate char *attribute;
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate if ( LDAP_SUCCESS != ldap_parse_sort_control(ld,ctrl_response_array,&result,&attribute) ) {
8570Sstevel@tonic-gate ldaptool_print_lderror(ld, "ldap_parse_sort_control",
8580Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP );
8590Sstevel@tonic-gate ldap_controls_free(ctrl_response_array);
8600Sstevel@tonic-gate ldap_msgfree(res);
8610Sstevel@tonic-gate if (mallocd_filter) free(filterp);
8620Sstevel@tonic-gate return ( ldap_get_lderrno( ld, NULL, NULL ) );
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate if (0 == result) {
8660Sstevel@tonic-gate if ( ldaptool_verbose ) {
8670Sstevel@tonic-gate printf( gettext("Server indicated results sorted OK\n"));
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate } else {
8700Sstevel@tonic-gate if (NULL != attribute) {
8710Sstevel@tonic-gate printf(gettext("Server reported sorting error %ld: %s, attribute in error\"%s\"\n"),result,sortresult2string(result),attribute);
8720Sstevel@tonic-gate } else {
8730Sstevel@tonic-gate printf(gettext("Server reported sorting error %ld: %s\n"),result,sortresult2string(result));
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate
8790Sstevel@tonic-gate if (use_vlv)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate unsigned long vpos, vcount;
8820Sstevel@tonic-gate int vresult;
8830Sstevel@tonic-gate if ( LDAP_SUCCESS != ldap_parse_virtuallist_control(ld,ctrl_response_array,&vpos, &vcount,&vresult) ) {
8840Sstevel@tonic-gate ldaptool_print_lderror( ld, "ldap_parse_virtuallist_control",
8850Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP );
8860Sstevel@tonic-gate ldap_controls_free(ctrl_response_array);
8870Sstevel@tonic-gate ldap_msgfree(res);
8880Sstevel@tonic-gate if (mallocd_filter) free(filterp);
8890Sstevel@tonic-gate return ( ldap_get_lderrno( ld, NULL, NULL ) );
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate if (0 == vresult) {
8930Sstevel@tonic-gate if ( ldaptool_verbose ) {
8940Sstevel@tonic-gate printf( gettext("Server indicated virtual list positioning OK\n"));
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate printf(gettext("index %lu content count %lu\n"), vpos, vcount);
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate } else {
8990Sstevel@tonic-gate printf(gettext("Server reported sorting error %d: %s\n"),vresult,sortresult2string(vresult));
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate ldap_controls_free(ctrl_response_array);
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate if ( sortattr != NULL && !server_sort) {
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate (void) ldap_multisort_entries( ld, &res,
9100Sstevel@tonic-gate ( *sortattr == NULL ) ? NULL : sortattr,
9110Sstevel@tonic-gate (LDAP_CMP_CALLBACK *)strcasecmp );
9120Sstevel@tonic-gate matches = 0;
9130Sstevel@tonic-gate first = 1;
9140Sstevel@tonic-gate for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
9150Sstevel@tonic-gate e = ldap_next_entry( ld, e ) ) {
9160Sstevel@tonic-gate matches++;
9170Sstevel@tonic-gate if ( !first ) {
9180Sstevel@tonic-gate putchar( '\n' );
9190Sstevel@tonic-gate } else {
9200Sstevel@tonic-gate first = 0;
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate print_entry( ld, e, attrsonly );
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate if ( ldaptool_verbose ) {
9270Sstevel@tonic-gate printf( gettext("%d matches\n"), matches );
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate if ( refs != NULL ) {
9310Sstevel@tonic-gate ldaptool_print_referrals( refs );
9320Sstevel@tonic-gate ldap_value_free( refs );
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate if (mallocd_filter) free(filterp);
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate ldap_msgfree( res );
9380Sstevel@tonic-gate return( rc );
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate static void
print_entry(ld,entry,attrsonly)9430Sstevel@tonic-gate print_entry( ld, entry, attrsonly )
9440Sstevel@tonic-gate LDAP *ld;
9450Sstevel@tonic-gate LDAPMessage *entry;
9460Sstevel@tonic-gate int attrsonly;
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate char *a, *dn, *ufn, tmpfname[ BUFSIZ ];
9490Sstevel@tonic-gate int i, notascii;
9500Sstevel@tonic-gate BerElement *ber;
9510Sstevel@tonic-gate struct berval **bvals;
9520Sstevel@tonic-gate FILE *tmpfp;
9530Sstevel@tonic-gate #if defined( XP_WIN32 )
9540Sstevel@tonic-gate char mode[20] = "w+b";
9550Sstevel@tonic-gate #else
9560Sstevel@tonic-gate char mode[20] = "w";
9570Sstevel@tonic-gate #endif
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate dn = ldap_get_dn( ld, entry );
9600Sstevel@tonic-gate write_string_attr_value( "dn", dn, LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
9610Sstevel@tonic-gate if ( includeufn ) {
9620Sstevel@tonic-gate ufn = ldap_dn2ufn( dn );
9630Sstevel@tonic-gate write_string_attr_value( "ufn", ufn,
9640Sstevel@tonic-gate LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
9650Sstevel@tonic-gate free( ufn );
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate ldap_memfree( dn );
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate if ( use_psearch ) {
9700Sstevel@tonic-gate LDAPControl **ectrls;
9710Sstevel@tonic-gate int chgtype, chgnumpresent;
9720Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
9730Sstevel@tonic-gate ber_int_t chgnum;
9740Sstevel@tonic-gate #else
9750Sstevel@tonic-gate long chgnum;
9760Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
9770Sstevel@tonic-gate char *prevdn, longbuf[ 128 ];
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate if ( ldap_get_entry_controls( ld, entry, &ectrls ) == LDAP_SUCCESS ) {
9800Sstevel@tonic-gate if ( ldap_parse_entrychange_control( ld, ectrls, &chgtype,
9810Sstevel@tonic-gate &prevdn, &chgnumpresent, &chgnum ) == LDAP_SUCCESS ) {
9820Sstevel@tonic-gate write_string_attr_value(
9830Sstevel@tonic-gate LDAPTOOL_PSEARCH_ATTR_PREFIX "changeType",
9840Sstevel@tonic-gate changetype_num2string( chgtype ), 0 );
9850Sstevel@tonic-gate if ( chgnumpresent ) {
9860Sstevel@tonic-gate sprintf( longbuf, "%d", chgnum );
9870Sstevel@tonic-gate write_string_attr_value(
9880Sstevel@tonic-gate LDAPTOOL_PSEARCH_ATTR_PREFIX "changeNumber",
9890Sstevel@tonic-gate longbuf, 0 );
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate if ( NULL != prevdn ) {
9920Sstevel@tonic-gate write_string_attr_value(
9930Sstevel@tonic-gate LDAPTOOL_PSEARCH_ATTR_PREFIX "previousDN",
9940Sstevel@tonic-gate prevdn, 0 );
9950Sstevel@tonic-gate ldap_memfree( prevdn );
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate ldap_controls_free (ectrls);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
10030Sstevel@tonic-gate a = ldap_next_attribute( ld, entry, ber ) ) {
10040Sstevel@tonic-gate if ( ldap_charray_inlist(sortattr, a) && /* in the list*/
10050Sstevel@tonic-gate skipsortattr[ldap_charray_position(sortattr, a)] ) {/* and skip it*/
10060Sstevel@tonic-gate continue; /* so skip it! */
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate if ( attrsonly ) {
10090Sstevel@tonic-gate if ( ldif ) {
10100Sstevel@tonic-gate write_ldif_value( a, "", 0, 0 );
10110Sstevel@tonic-gate } else {
10120Sstevel@tonic-gate printf( "%s\n", a );
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
10150Sstevel@tonic-gate for ( i = 0; bvals[i] != NULL; i++ ) {
10160Sstevel@tonic-gate if ( vals2tmp ) {
10170Sstevel@tonic-gate #ifdef HAVE_SNPRINTF
10180Sstevel@tonic-gate if ( snprintf( tmpfname, sizeof(tmpfname),
10190Sstevel@tonic-gate "%s/ldapsearch-%s-XXXXXX",
10200Sstevel@tonic-gate ldaptool_get_tmp_dir(), a ) < 0 ) {
10210Sstevel@tonic-gate perror( gettext("snprintf tmpfname (attribute name too long?)") );
10220Sstevel@tonic-gate exit( LDAP_PARAM_ERROR );
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate #else
10250Sstevel@tonic-gate sprintf( tmpfname, "%s/ldapsearch-%s-XXXXXX",
10260Sstevel@tonic-gate ldaptool_get_tmp_dir(), a );
10270Sstevel@tonic-gate #endif
10280Sstevel@tonic-gate tmpfp = NULL;
10290Sstevel@tonic-gate
10300Sstevel@tonic-gate if ( LDAPTOOL_MKTEMP( tmpfname ) == NULL ) {
10310Sstevel@tonic-gate perror( tmpfname );
10320Sstevel@tonic-gate } else if (( tmpfp = ldaptool_open_file( tmpfname, mode)) == NULL ) {
10330Sstevel@tonic-gate perror( tmpfname );
10340Sstevel@tonic-gate } else if ( bvals[ i ]->bv_len > 0 &&
10350Sstevel@tonic-gate fwrite( bvals[ i ]->bv_val,
10360Sstevel@tonic-gate bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
10370Sstevel@tonic-gate perror( tmpfname );
10380Sstevel@tonic-gate } else if ( ldif ) {
10390Sstevel@tonic-gate if ( produce_file_urls ) {
10400Sstevel@tonic-gate char *url;
10410Sstevel@tonic-gate
10420Sstevel@tonic-gate if ( ldaptool_path2fileurl( tmpfname, &url ) !=
10430Sstevel@tonic-gate LDAPTOOL_FILEURL_SUCCESS ) {
10440Sstevel@tonic-gate perror( "ldaptool_path2fileurl" );
10450Sstevel@tonic-gate } else {
10460Sstevel@tonic-gate write_ldif_value( a, url, strlen( url ),
10470Sstevel@tonic-gate LDIF_OPT_VALUE_IS_URL );
10480Sstevel@tonic-gate free( url );
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate } else {
10510Sstevel@tonic-gate write_ldif_value( a, tmpfname, strlen( tmpfname ),
10520Sstevel@tonic-gate 0 );
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate } else {
10550Sstevel@tonic-gate printf( "%s%s%s\n", a, sep, tmpfname );
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate if ( tmpfp != NULL ) {
10590Sstevel@tonic-gate fclose( tmpfp );
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate } else {
10620Sstevel@tonic-gate notascii = 0;
10630Sstevel@tonic-gate if ( !ldif && !allow_binary ) {
10640Sstevel@tonic-gate notascii = !ldaptool_berval_is_ascii( bvals[i] );
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate if ( ldif ) {
10680Sstevel@tonic-gate write_ldif_value( a, bvals[ i ]->bv_val,
10690Sstevel@tonic-gate bvals[ i ]->bv_len, 0 );
10700Sstevel@tonic-gate } else {
10710Sstevel@tonic-gate printf( "%s%s%s\n", a, sep,
10720Sstevel@tonic-gate notascii ? gettext("NOT ASCII") : bvals[ i ]->bv_val );
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate ber_bvecfree( bvals );
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate ldap_memfree( a );
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate
10810Sstevel@tonic-gate if ( ldap_get_lderrno( ld, NULL, NULL ) != LDAP_SUCCESS ) {
10820Sstevel@tonic-gate ldaptool_print_lderror( ld, "ldap_first_attribute/ldap_next_attribute",
10830Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP );
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate if ( ber != NULL ) {
10870Sstevel@tonic-gate ber_free( ber, 0 );
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate static void
write_string_attr_value(char * attrname,char * strval,unsigned long opts)10930Sstevel@tonic-gate write_string_attr_value( char *attrname, char *strval, unsigned long opts )
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate if ( strval == NULL ) {
10960Sstevel@tonic-gate strval = "";
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate if ( ldif ) {
10990Sstevel@tonic-gate write_ldif_value( attrname, strval, strlen( strval ), 0 );
11000Sstevel@tonic-gate } else if ( 0 != ( opts & LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME )) {
11010Sstevel@tonic-gate printf( "%s\n", strval );
11020Sstevel@tonic-gate } else {
11030Sstevel@tonic-gate printf( "%s%s%s\n", attrname, sep, strval );
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate
11080Sstevel@tonic-gate static int
write_ldif_value(char * type,char * value,unsigned long vallen,unsigned long ldifoptions)11090Sstevel@tonic-gate write_ldif_value( char *type, char *value, unsigned long vallen,
11100Sstevel@tonic-gate unsigned long ldifoptions )
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate char *ldif;
11130Sstevel@tonic-gate static int wrote_version = 0;
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate if ( write_ldif_version && !wrote_version ) {
11160Sstevel@tonic-gate char versionbuf[ 64 ];
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate wrote_version = 1;
11190Sstevel@tonic-gate sprintf( versionbuf, "%d", LDIF_VERSION_ONE );
11200Sstevel@tonic-gate write_ldif_value( "version", versionbuf, strlen( versionbuf ), 0 );
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate
11230Sstevel@tonic-gate if ( !fold ) {
11240Sstevel@tonic-gate ldifoptions |= LDIF_OPT_NOWRAP;
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate if ( minimize_base64 ) {
11270Sstevel@tonic-gate ldifoptions |= LDIF_OPT_MINIMAL_ENCODING;
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate if (( ldif = ldif_type_and_value_with_options( type, value, (int)vallen,
11310Sstevel@tonic-gate ldifoptions )) == NULL ) {
11320Sstevel@tonic-gate return( -1 );
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate fputs( ldif, stdout );
11360Sstevel@tonic-gate free( ldif );
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate return( 0 );
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate static char *
sortresult2string(unsigned long result)11430Sstevel@tonic-gate sortresult2string(unsigned long result)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate /*
11460Sstevel@tonic-gate success (0), -- results are sorted
11470Sstevel@tonic-gate operationsError (1), -- server internal failure
11480Sstevel@tonic-gate timeLimitExceeded (3), -- timelimit reached before
11490Sstevel@tonic-gate -- sorting was completed
11500Sstevel@tonic-gate strongAuthRequired (8), -- refused to return sorted
11510Sstevel@tonic-gate -- results via insecure
11520Sstevel@tonic-gate -- protocol
11530Sstevel@tonic-gate adminLimitExceeded (11), -- too many matching entries
11540Sstevel@tonic-gate -- for the server to sort
11550Sstevel@tonic-gate noSuchAttribute (16), -- unrecognized attribute
11560Sstevel@tonic-gate -- type in sort key
11570Sstevel@tonic-gate inappropriateMatching (18), -- unrecognized or inappro-
11580Sstevel@tonic-gate -- priate matching rule in
11590Sstevel@tonic-gate -- sort key
11600Sstevel@tonic-gate insufficientAccessRights (50), -- refused to return sorted
11610Sstevel@tonic-gate -- results to this client
11620Sstevel@tonic-gate busy (51), -- too busy to process
11630Sstevel@tonic-gate unwillingToPerform (53), -- unable to sort
11640Sstevel@tonic-gate other (80)
11650Sstevel@tonic-gate */
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate switch (result) {
11680Sstevel@tonic-gate case 0: return (gettext("success"));
11690Sstevel@tonic-gate case 1: return (gettext("operations error"));
11700Sstevel@tonic-gate case 3: return (gettext("time limit exceeded"));
11710Sstevel@tonic-gate case 8: return (gettext("strong auth required"));
11720Sstevel@tonic-gate case 11: return (gettext("admin limit exceeded"));
11730Sstevel@tonic-gate case 16: return (gettext("no such attribute"));
11740Sstevel@tonic-gate case 18: return (gettext("unrecognized or inappropriate matching rule"));
11750Sstevel@tonic-gate case 50: return (gettext("insufficient access rights"));
11760Sstevel@tonic-gate case 51: return (gettext("too busy"));
11770Sstevel@tonic-gate case 53: return (gettext("unable to sort"));
11780Sstevel@tonic-gate case 80:
11790Sstevel@tonic-gate default: return (gettext("Er...Other ?"));
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate static void
parse_and_display_reference(LDAP * ld,LDAPMessage * ref)11850Sstevel@tonic-gate parse_and_display_reference( LDAP *ld, LDAPMessage *ref )
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate int i;
11880Sstevel@tonic-gate char **refs;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate if ( ldap_parse_reference( ld, ref, &refs, NULL, 0 ) != LDAP_SUCCESS ) {
11910Sstevel@tonic-gate ldaptool_print_lderror( ld, "ldap_parse_reference",
11920Sstevel@tonic-gate LDAPTOOL_CHECK4SSL_IF_APPROP );
11930Sstevel@tonic-gate } else if ( refs != NULL && refs[ 0 ] != NULL ) {
11940Sstevel@tonic-gate fputs( gettext("Unfollowed continuation reference(s):\n"), stderr );
11950Sstevel@tonic-gate for ( i = 0; refs[ i ] != NULL; ++i ) {
11960Sstevel@tonic-gate fprintf( stderr, " %s\n", refs[ i ] );
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate ldap_value_free( refs );
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate /*possible operations a client can invoke -- copied from ldaprot.h */
12040Sstevel@tonic-gate
12050Sstevel@tonic-gate #ifndef LDAP_REQ_BIND
12060Sstevel@tonic-gate #define LDAP_REQ_BIND 0x60L /* application + constructed */
12070Sstevel@tonic-gate #define LDAP_REQ_UNBIND 0x42L /* application + primitive */
12080Sstevel@tonic-gate #define LDAP_REQ_SEARCH 0x63L /* application + constructed */
12090Sstevel@tonic-gate #define LDAP_REQ_MODIFY 0x66L /* application + constructed */
12100Sstevel@tonic-gate #define LDAP_REQ_ADD 0x68L /* application + constructed */
12110Sstevel@tonic-gate #define LDAP_REQ_DELETE 0x4aL /* application + primitive */
12120Sstevel@tonic-gate #define LDAP_REQ_RENAME 0x6cL /* application + constructed */
12130Sstevel@tonic-gate #define LDAP_REQ_COMPARE 0x6eL /* application + constructed */
12140Sstevel@tonic-gate #define LDAP_REQ_ABANDON 0x50L /* application + primitive */
12150Sstevel@tonic-gate #define LDAP_REQ_EXTENDED 0x77L /* application + constructed */
12160Sstevel@tonic-gate #endif /* LDAP_REQ_BIND */
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate struct ldapsearch_type2str {
12210Sstevel@tonic-gate
12220Sstevel@tonic-gate int ldst2s_type; /* message type */
12230Sstevel@tonic-gate char *ldst2s_string; /* descriptive string */
12240Sstevel@tonic-gate };
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
12270Sstevel@tonic-gate static struct ldapsearch_type2str ldapsearch_msgtypes[] = {
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate /* results: */
12300Sstevel@tonic-gate { LDAP_RES_BIND, NULL },
12310Sstevel@tonic-gate { LDAP_RES_SEARCH_REFERENCE, NULL },
12320Sstevel@tonic-gate { LDAP_RES_SEARCH_ENTRY, NULL },
12330Sstevel@tonic-gate { LDAP_RES_SEARCH_RESULT, NULL },
12340Sstevel@tonic-gate { LDAP_RES_MODIFY, NULL },
12350Sstevel@tonic-gate { LDAP_RES_ADD, NULL },
12360Sstevel@tonic-gate { LDAP_RES_DELETE, NULL },
12370Sstevel@tonic-gate { LDAP_RES_MODDN, NULL },
12380Sstevel@tonic-gate { LDAP_RES_COMPARE, NULL },
12390Sstevel@tonic-gate { LDAP_RES_EXTENDED, NULL },
12400Sstevel@tonic-gate /* requests: */
12410Sstevel@tonic-gate { LDAP_REQ_BIND, NULL },
12420Sstevel@tonic-gate { LDAP_REQ_UNBIND, NULL },
12430Sstevel@tonic-gate { LDAP_REQ_SEARCH, NULL },
12440Sstevel@tonic-gate { LDAP_REQ_MODIFY, NULL },
12450Sstevel@tonic-gate { LDAP_REQ_ADD, NULL },
12460Sstevel@tonic-gate { LDAP_REQ_DELETE, NULL },
12470Sstevel@tonic-gate { LDAP_REQ_RENAME, NULL },
12480Sstevel@tonic-gate { LDAP_REQ_COMPARE, NULL },
12490Sstevel@tonic-gate { LDAP_REQ_ABANDON, NULL },
12500Sstevel@tonic-gate { LDAP_REQ_EXTENDED, NULL },
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate };
12530Sstevel@tonic-gate #else
12540Sstevel@tonic-gate static struct ldapsearch_type2str ldapsearch_msgtypes[] = {
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate /* results: */
12570Sstevel@tonic-gate { LDAP_RES_BIND, "bind result" },
12580Sstevel@tonic-gate { LDAP_RES_SEARCH_REFERENCE, "continuation reference" },
12590Sstevel@tonic-gate { LDAP_RES_SEARCH_ENTRY, "entry" },
12600Sstevel@tonic-gate { LDAP_RES_SEARCH_RESULT, "search result" },
12610Sstevel@tonic-gate { LDAP_RES_MODIFY, "modify result" },
12620Sstevel@tonic-gate { LDAP_RES_ADD, "add result" },
12630Sstevel@tonic-gate { LDAP_RES_DELETE, "delete result" },
12640Sstevel@tonic-gate { LDAP_RES_MODDN, "rename result" },
12650Sstevel@tonic-gate { LDAP_RES_COMPARE, "compare result" },
12660Sstevel@tonic-gate { LDAP_RES_EXTENDED, "extended operation result" },
12670Sstevel@tonic-gate /* requests: */
12680Sstevel@tonic-gate { LDAP_REQ_BIND, "bind request" },
12690Sstevel@tonic-gate { LDAP_REQ_UNBIND, "unbind request" },
12700Sstevel@tonic-gate { LDAP_REQ_SEARCH, "search request" },
12710Sstevel@tonic-gate { LDAP_REQ_MODIFY, "modify request" },
12720Sstevel@tonic-gate { LDAP_REQ_ADD, "add request" },
12730Sstevel@tonic-gate { LDAP_REQ_DELETE, "delete request" },
12740Sstevel@tonic-gate { LDAP_REQ_RENAME, "rename request" },
12750Sstevel@tonic-gate { LDAP_REQ_COMPARE, "compare request" },
12760Sstevel@tonic-gate { LDAP_REQ_ABANDON, "abandon request" },
12770Sstevel@tonic-gate { LDAP_REQ_EXTENDED, "extended request" },
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate };
12800Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate
12830Sstevel@tonic-gate #define LDAPSEARCHTOOL_NUMTYPES (sizeof(ldapsearch_msgtypes) \
12840Sstevel@tonic-gate / sizeof(struct ldapsearch_type2str))
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
12870Sstevel@tonic-gate static void
fill_ldapsearch_msgtypes(void)12880Sstevel@tonic-gate fill_ldapsearch_msgtypes( void )
12890Sstevel@tonic-gate {
12900Sstevel@tonic-gate int i = 0;
12910Sstevel@tonic-gate if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
12920Sstevel@tonic-gate != NULL)
12930Sstevel@tonic-gate return;
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate /* results: */
12960Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
12970Sstevel@tonic-gate "bind result");
12980Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
12990Sstevel@tonic-gate "continuation reference");
13000Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13010Sstevel@tonic-gate "entry");
13020Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13030Sstevel@tonic-gate "search result");
13040Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13050Sstevel@tonic-gate "modify result");
13060Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13070Sstevel@tonic-gate "add result");
13080Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13090Sstevel@tonic-gate "delete result");
13100Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13110Sstevel@tonic-gate "rename result");
13120Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13130Sstevel@tonic-gate "compare result");
13140Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13150Sstevel@tonic-gate "extended operation result");
13160Sstevel@tonic-gate /* requests: */
13170Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13180Sstevel@tonic-gate "bind request");
13190Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13200Sstevel@tonic-gate "unbind request");
13210Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13220Sstevel@tonic-gate "search request");
13230Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13240Sstevel@tonic-gate "modify request");
13250Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13260Sstevel@tonic-gate "add request");
13270Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13280Sstevel@tonic-gate "delete request");
13290Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13300Sstevel@tonic-gate "rename request");
13310Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13320Sstevel@tonic-gate "compare request");
13330Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13340Sstevel@tonic-gate "abandon request");
13350Sstevel@tonic-gate ldapsearch_msgtypes[i++].ldst2s_string = gettext(
13360Sstevel@tonic-gate "extended request");
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate * Return a descriptive string given an LDAP result message type (tag).
13420Sstevel@tonic-gate */
13430Sstevel@tonic-gate static char *
msgtype2str(int msgtype)13440Sstevel@tonic-gate msgtype2str( int msgtype )
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate char *s = gettext("unknown");
13470Sstevel@tonic-gate int i;
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate #ifdef SOLARIS_LDAP_CMD
13500Sstevel@tonic-gate /* Make sure ldapsearch_msgtypes is initialized */
13510Sstevel@tonic-gate if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
13520Sstevel@tonic-gate == NULL)
13530Sstevel@tonic-gate (void) fill_ldapsearch_msgtypes();
13540Sstevel@tonic-gate #endif /* SOLARIS_LDAP_CMD */
13550Sstevel@tonic-gate
13560Sstevel@tonic-gate for ( i = 0; i < LDAPSEARCHTOOL_NUMTYPES; ++i ) {
13570Sstevel@tonic-gate if ( msgtype == ldapsearch_msgtypes[ i ].ldst2s_type ) {
13580Sstevel@tonic-gate s = ldapsearch_msgtypes[ i ].ldst2s_string;
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate return( s );
13620Sstevel@tonic-gate }
13630Sstevel@tonic-gate
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate * Return a descriptive string given a Persistent Search change type
13670Sstevel@tonic-gate */
13680Sstevel@tonic-gate static char *
changetype_num2string(int chgtype)13690Sstevel@tonic-gate changetype_num2string( int chgtype )
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate char *s = gettext("unknown");
13720Sstevel@tonic-gate
13730Sstevel@tonic-gate switch( chgtype ) {
13740Sstevel@tonic-gate case LDAP_CHANGETYPE_ADD:
13750Sstevel@tonic-gate s = gettext("add");
13760Sstevel@tonic-gate break;
13770Sstevel@tonic-gate case LDAP_CHANGETYPE_DELETE:
13780Sstevel@tonic-gate s = gettext("delete");
13790Sstevel@tonic-gate break;
13800Sstevel@tonic-gate case LDAP_CHANGETYPE_MODIFY:
13810Sstevel@tonic-gate s = gettext("modify");
13820Sstevel@tonic-gate break;
13830Sstevel@tonic-gate case LDAP_CHANGETYPE_MODDN:
13840Sstevel@tonic-gate s = gettext("moddn");
13850Sstevel@tonic-gate break;
13860Sstevel@tonic-gate }
13870Sstevel@tonic-gate
13880Sstevel@tonic-gate return( s );
13890Sstevel@tonic-gate }
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate /* returns a null teminated charrary */
get_effectiverights_attrlist(char * optarg)13920Sstevel@tonic-gate static char **get_effectiverights_attrlist(char * optarg) {
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate char * tmp_str = strdup(optarg);
13950Sstevel@tonic-gate char ** retArray = NULL;
13960Sstevel@tonic-gate int i = 0;
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate retArray = ldap_str2charray( tmp_str, " "); /* takes copies */
13990Sstevel@tonic-gate
14000Sstevel@tonic-gate free(tmp_str);
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate /* Oops - somebody left this debug message in for the
14030Sstevel@tonic-gate getEffectiveRights control
14040Sstevel@tonic-gate fprintf(stderr, "attrlist: "); */
14050Sstevel@tonic-gate i = 0;
14060Sstevel@tonic-gate while( retArray[i] != NULL ) {
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate fprintf(stderr,"%s ", retArray[i]);
14090Sstevel@tonic-gate i++;
14100Sstevel@tonic-gate }
14110Sstevel@tonic-gate fprintf(stderr, "\n");
14120Sstevel@tonic-gate
14130Sstevel@tonic-gate return(retArray);
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate }
1416