1N/A/*
1N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A
1N/A/* ldapsearch.c - generic program to search LDAP */
1N/A
1N/A#include "ldaptool.h"
1N/A#include "fileurl.h"
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A#include <locale.h>
1N/A#include "solaris-int.h"
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A#define VLV_PARAM_SEP ':'
1N/A
1N/A#ifndef SOLARIS_LDAP_CMD
1N/A#define gettext(s) s
1N/A#endif
1N/A
1N/Astatic void usage( void );
1N/Astatic int dosearch( LDAP *ld, char *base, int scope, char **attrs,
1N/A int attrsonly, char *filtpatt, char *value);
1N/Astatic void write_string_attr_value( char *attrname, char *strval,
1N/A unsigned long opts );
1N/A#define LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME 0x01
1N/Astatic int write_ldif_value( char *type, char *value, unsigned long vallen,
1N/A unsigned long ldifoptions );
1N/Astatic void print_entry( LDAP *ld, LDAPMessage *entry, int attrsonly );
1N/Astatic void options_callback( int option, char *optarg );
1N/Astatic void parse_and_display_reference( LDAP *ld, LDAPMessage *ref );
1N/Astatic char *sortresult2string(unsigned long result);
1N/Astatic char *changetype_num2string( int chgtype );
1N/Astatic char *msgtype2str( int msgtype );
1N/Astatic char **get_effectiverights_attrlist(char * optarg);
1N/A
1N/A#ifdef SOLARIS_LDAP_CMD
1N/Astatic void fill_ldapsearch_msgtypes( void );
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A/*
1N/A * Prefix used in names of pseudo attributes added to the entry LDIF
1N/A * output if we receive an entryChangeNotification control with an entry
1N/A * (requested using persistent search).
1N/A */
1N/A#define LDAPTOOL_PSEARCH_ATTR_PREFIX "persistentSearch-"
1N/A
1N/A
1N/Astatic void
1N/Ausage( void )
1N/A{
1N/A fprintf( stderr, gettext("usage: %s -b basedn [options] filter [attributes...]\n"), ldaptool_progname );
1N/A fprintf( stderr, gettext(" %s -b basedn [options] -f file [attributes...]\nwhere:\n"), ldaptool_progname );
1N/A fprintf( stderr, gettext(" basedn\tbase dn for search\n") );
1N/A fprintf( stderr, gettext("\t\t(if the environment variable LDAP_BASEDN is set,\n") );
1N/A fprintf( stderr, gettext("\t\tthen the -b flag is not required)\n") );
1N/A fprintf( stderr, gettext(" filter\tRFC-2254 compliant LDAP search filter\n") );
1N/A fprintf( stderr, gettext(" file\tfile containing a sequence of LDAP search filters to use\n") );
1N/A fprintf( stderr, gettext(" attributes\twhitespace-separated list of attributes to retrieve\n") );
1N/A fprintf( stderr, gettext("\t\t(if no attribute list is given, all are retrieved)\n") );
1N/A fprintf( stderr, gettext("options:\n") );
1N/A ldaptool_common_usage( 0 );
1N/A#if defined( XP_WIN32 )
1N/A fprintf( stderr, gettext(" -t\t\twrite values to files in temp directory.\n") );
1N/A#else
1N/A fprintf( stderr, gettext(" -t\t\twrite values to files in /tmp\n") );
1N/A#endif
1N/A fprintf( stderr, gettext(" -U\t\tproduce file URLs in conjunction with -t\n") );
1N/A fprintf( stderr, gettext(" -e\t\tminimize base-64 encoding of values\n") );
1N/A fprintf( stderr, gettext(" -u\t\tinclude User Friendly entry names in the output\n") );
1N/A#ifndef HAVE_SASL_OPTIONS
1N/A fprintf( stderr, gettext(" -o\t\tprint entries using old format (default is LDIF)\n") );
1N/A#endif
1N/A fprintf( stderr, gettext(" -T\t\tdon't fold (wrap) long lines (default is to fold)\n") );
1N/A fprintf( stderr, gettext(" -1\t\tomit leading \"version: %d\" line in LDIF output\n"), LDIF_VERSION_ONE );
1N/A fprintf( stderr, gettext(" -A\t\tretrieve attribute names only (no values)\n") );
1N/A fprintf( stderr, gettext(" -B\t\tprint non-ASCII values and use old output format (attr=value)\n") );
1N/A fprintf( stderr, gettext(" -x\t\tperforming sorting on server\n") );
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A fprintf( stderr, gettext(" -r\t\tprint entries using old format (default is LDIF)\n") );
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A fprintf( stderr, gettext(" -F sep\tprint `sep' instead of `%s' between attribute names\n"), LDAPTOOL_DEFSEP );
1N/A fprintf( stderr, gettext(" \tand values\n") );
1N/A fprintf( stderr, gettext(" -S attr\tsort the results by attribute `attr'\n") );
1N/A fprintf( stderr, gettext(" -s scope\tone of base, one, or sub (default is sub)\n") );
1N/A fprintf( stderr, gettext(" -a deref\tone of never, always, search, or find (default: never)\n") );
1N/A fprintf( stderr, gettext(" \t(alias dereferencing)\n") );
1N/A fprintf( stderr, gettext(" -l timelim\ttime limit (in seconds) for search (default is no limit)\n") );
1N/A fprintf( stderr, gettext(" -z sizelim\tsize limit (in entries) for search (default is no limit)\n") );
1N/A fprintf( stderr, gettext(" -C ps:changetype[:changesonly[:entrychgcontrols]]\n") );
1N/A fprintf( stderr, gettext("\t\tchangetypes are add,delete,modify,moddn,any\n") );
1N/A fprintf( stderr, gettext("\t\tchangesonly and entrychgcontrols are boolean values\n") );
1N/A fprintf( stderr, gettext("\t\t(default is 1)\n") );
1N/A 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 );
1N/A fprintf( stderr, gettext("\t\t'after' are the number of entries surrounding 'index.'\n"));
1N/A fprintf( stderr, gettext("\t\t'count' is the content count, 'value' is the search value.\n"));
1N/A#ifndef SOLARIS_LDAP_CMD
1N/A fprintf( stderr, gettext(" -c authzid\tspecifies the getEffectiveRights control authzid\n"));
1N/A fprintf( stderr, gettext("\t\t eg. dn:uid=bjensen,dc=example,dc=com\n"));
1N/A fprintf( stderr, gettext("\t\t A value of \"\" means \"the authorization id for the operation\".\n"));
1N/A fprintf( stderr, gettext("\t\t A value of \"dn:\" means \"anonymous\"\n"));
1N/A fprintf( stderr, gettext("\t\t (The aclRights operational attribute must be requested)\n"));
1N/A fprintf( stderr, gettext(" -X attrlist\tspecifies the getEffectiveRights control specific attribute list.\n"));
1N/A fprintf( stderr, gettext("\t\t eg. \"nsroledn userPassword\"\n"));
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A exit( LDAP_PARAM_ERROR );
1N/A}
1N/A
1N/Astatic char *base = NULL;
1N/Astatic char *sep = LDAPTOOL_DEFSEP;
1N/Astatic char **sortattr = NULL;
1N/Astatic char *vlv_value = NULL;
1N/Astatic int sortsize = 0;
1N/Astatic int *skipsortattr = NULL;
1N/Astatic int includeufn, allow_binary, vals2tmp, ldif, scope, deref;
1N/Astatic int attrsonly, timelimit, sizelimit, server_sort, fold;
1N/Astatic int minimize_base64, produce_file_urls;
1N/Astatic int use_vlv = 0, vlv_before, vlv_after, vlv_index, vlv_count;
1N/Astatic int use_psearch=0;
1N/Astatic int write_ldif_version = 1;
1N/A#ifndef SOLARIS_LDAP_CMD
1N/Astatic char *get_effectiverights_control_target_dn = NULL; /* -c */
1N/Astatic char **get_effectiverights_control_attrlist = NULL; /* -X */
1N/Astatic int do_effective_rights_control = 0;
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A/* Persistent search variables */
1N/Astatic int chgtype=0, changesonly=1, return_echg_ctls=1;
1N/A
1N/A
1N/Aint
1N/Amain( int argc, char **argv )
1N/A{
1N/A char *filtpattern, **attrs;
1N/A int rc, optind, i, first, free_filtpattern;
1N/A LDAP *ld;
1N/A
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A char *locale = setlocale(LC_ALL, "");
1N/A textdomain(TEXT_DOMAIN);
1N/A ldaptool_require_binddn = 0;
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A free_filtpattern = 0;
1N/A deref = LDAP_DEREF_NEVER;
1N/A allow_binary = vals2tmp = attrsonly = 0;
1N/A minimize_base64 = produce_file_urls = 0;
1N/A ldif = 1;
1N/A fold = 1;
1N/A sizelimit = timelimit = 0;
1N/A scope = LDAP_SCOPE_SUBTREE;
1N/A server_sort = 0;
1N/A
1N/A
1N/A#ifdef notdef
1N/A#ifdef HPUX11
1N/A#ifndef __LP64__
1N/A _main( argc, argv);
1N/A#endif /* __LP64_ */
1N/A#endif /* HPUX11 */
1N/A#endif
1N/A
1N/A
1N/A ldaptool_reset_control_array( ldaptool_request_ctrls );
1N/A#ifdef HAVE_SASL_OPTIONS
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A optind = ldaptool_process_args( argc, argv, "ABLTU1etuxra:b:F:G:l:S:s:z:C:",
1N/A 0, options_callback );
1N/A#else
1N/A optind = ldaptool_process_args( argc, argv, "ABLTU1etuxa:b:F:G:l:S:s:z:C:c:",
1N/A 0, options_callback );
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A#else
1N/A optind = ldaptool_process_args( argc, argv, "ABLTU1eotuxa:b:F:G:l:S:s:z:C:c:",
1N/A 0, options_callback );
1N/A#endif /* HAVE_SASL_OPTIONS */
1N/A
1N/A if ( optind == -1 ) {
1N/A usage();
1N/A }
1N/A
1N/A if ( base == NULL ) {
1N/A if (( base = getenv( "LDAP_BASEDN" )) == NULL ) {
1N/A usage();
1N/A }
1N/A }
1N/A if ( sortattr ) {
1N/A for ( sortsize = 0; sortattr[sortsize] != NULL; sortsize++ ) {
1N/A ; /* NULL */
1N/A }
1N/A sortsize++; /* add in the final NULL field */
1N/A skipsortattr = (int *) malloc( sortsize * sizeof(int *) );
1N/A if ( skipsortattr == NULL ) {
1N/A fprintf( stderr, gettext("Out of memory\n") );
1N/A exit( LDAP_NO_MEMORY );
1N/A }
1N/A memset( (char *) skipsortattr, 0, sortsize * sizeof(int *) );
1N/A } else if ( server_sort ) {
1N/A server_sort = 0; /* ignore this option if no sortattrs were given */
1N/A }
1N/A
1N/A if ( argc - optind < 1 ) {
1N/A if ( ldaptool_fp == NULL ) {
1N/A usage();
1N/A }
1N/A attrs = NULL;
1N/A filtpattern = "%s";
1N/A } else { /* there are additional args (filter + attrs) */
1N/A if ( ldaptool_fp == NULL || strstr( argv[ optind ], "%s" ) != NULL ) {
1N/A filtpattern = ldaptool_local2UTF8( argv[ optind ] );
1N/A /* since local2UTF8 always allocates something, we should free it */
1N/A free_filtpattern = 1;
1N/A ++optind;
1N/A } else {
1N/A filtpattern = "%s";
1N/A }
1N/A
1N/A if ( argv[ optind ] == NULL ) {
1N/A attrs = NULL;
1N/A } else if ( sortattr == NULL || *sortattr == '\0' || server_sort) {
1N/A attrs = &argv[ optind ];
1N/A } else {
1N/A attrs = ldap_charray_dup( &argv[ optind ] );
1N/A if ( attrs == NULL ) {
1N/A fprintf( stderr, gettext("Out of memory\n") );
1N/A exit( LDAP_NO_MEMORY );
1N/A }
1N/A for ( i = 0; i < (sortsize - 1); i++ ) {
1N/A if ( !ldap_charray_inlist( attrs, sortattr[i] ) ) {
1N/A if ( ldap_charray_add( &attrs, sortattr[i] ) != 0 ) {
1N/A fprintf( stderr, gettext("Out of memory\n") );
1N/A exit( LDAP_NO_MEMORY );
1N/A }
1N/A /*
1N/A * attribute in the search list only for the purpose of
1N/A * sorting
1N/A */
1N/A skipsortattr[i] = 1;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A
1N/A ld = ldaptool_ldap_init( 0 );
1N/A
1N/A if ( !ldaptool_not ) {
1N/A ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
1N/A ldap_set_option( ld, LDAP_OPT_TIMELIMIT, &timelimit );
1N/A ldap_set_option( ld, LDAP_OPT_SIZELIMIT, &sizelimit );
1N/A }
1N/A
1N/A ldaptool_bind( ld );
1N/A
1N/A if ( ldaptool_verbose ) {
1N/A printf( gettext("filter pattern: %s\nreturning: "), filtpattern );
1N/A if ( attrs == NULL ) {
1N/A printf( gettext("ALL") );
1N/A } else {
1N/A for ( i = 0; attrs[ i ] != NULL; ++i ) {
1N/A printf( "%s ", attrs[ i ] );
1N/A }
1N/A }
1N/A putchar( '\n' );
1N/A }
1N/A
1N/A if ( ldaptool_fp == NULL ) {
1N/A char *conv;
1N/A
1N/A conv = ldaptool_local2UTF8( base );
1N/A rc = dosearch( ld, conv, scope, attrs, attrsonly, filtpattern, "" );
1N/A if( conv != NULL )
1N/A free( conv );
1N/A } else {
1N/A int done = 0;
1N/A
1N/A rc = LDAP_SUCCESS;
1N/A first = 1;
1N/A while ( rc == LDAP_SUCCESS && !done ) {
1N/A char *linep = NULL;
1N/A int increment = 0;
1N/A int c, index;
1N/A
1N/A /* allocate initial block of memory */
1N/A if ((linep = (char *)malloc(BUFSIZ)) == NULL) {
1N/A fprintf( stderr, gettext("Out of memory\n") );
1N/A exit( LDAP_NO_MEMORY );
1N/A }
1N/A increment++;
1N/A index = 0;
1N/A while ((c = fgetc( ldaptool_fp )) != '\n' && c != EOF) {
1N/A
1N/A /* check if we will overflow the buffer */
1N/A if ((c != EOF) && (index == ((increment * BUFSIZ) -1))) {
1N/A
1N/A /* if we did, add another BUFSIZ worth of bytes */
1N/A if ((linep = (char *)
1N/A realloc(linep, (increment + 1) * BUFSIZ)) == NULL) {
1N/A fprintf( stderr, gettext("Out of memory\n") );
1N/A exit( LDAP_NO_MEMORY );
1N/A }
1N/A increment++;
1N/A }
1N/A linep[index++] = c;
1N/A }
1N/A
1N/A if (c == EOF) {
1N/A done = 1;
1N/A break;
1N/A }
1N/A
1N/A linep[index] = '\0';
1N/A
1N/A if ( !first ) {
1N/A putchar( '\n' );
1N/A } else {
1N/A first = 0;
1N/A }
1N/A rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
1N/A linep );
1N/A free (linep);
1N/A }
1N/A }
1N/A
1N/A ldaptool_cleanup( ld );
1N/A if (free_filtpattern != 0 && filtpattern != NULL) {
1N/A free (filtpattern);
1N/A }
1N/A
1N/A /* check for and report output error */
1N/A fflush( stdout );
1N/A rc = ldaptool_check_ferror( stdout, rc,
1N/A gettext("output error (output might be incomplete)") );
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Aoptions_callback( int option, char *optarg )
1N/A{
1N/A char *s, *temp_arg, *ps_ptr, *ps_arg;
1N/A
1N/A switch( option ) {
1N/A case 'u': /* include UFN */
1N/A ++includeufn;
1N/A break;
1N/A case 't': /* write attribute values to /tmp files */
1N/A ++vals2tmp;
1N/A break;
1N/A case 'U': /* produce file URLs in conjunction with -t */
1N/A ++produce_file_urls;
1N/A break;
1N/A case 'e': /* minimize base-64 encoding of values */
1N/A ++minimize_base64;
1N/A break;
1N/A case 'A': /* retrieve attribute names only -- no values */
1N/A ++attrsonly;
1N/A break;
1N/A case 'L': /* print entries in LDIF format -- now the default */
1N/A break;
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A case 'r': /* print entries in the old format */
1N/A ldif = 0;
1N/A break;
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A#ifdef HAVE_SASL_OPTIONS
1N/A#ifdef HAVE_SASL_OPTIONS_2
1N/A case 'o': /* print entries using old ldapsearch format */
1N/A ldif = 0;
1N/A break;
1N/A#endif
1N/A#else
1N/A case 'o': /* print entries using old ldapsearch format */
1N/A ldif = 0;
1N/A break;
1N/A#endif
1N/A case 'B': /* allow binary values to be printed, use old format */
1N/A ++allow_binary;
1N/A ldif = 0;
1N/A break;
1N/A case '1': /* omit leading "version: #" line from LDIF output */
1N/A write_ldif_version = 0;
1N/A break;
1N/A case 's': /* search scope */
1N/A if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
1N/A scope = LDAP_SCOPE_BASE;
1N/A } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
1N/A scope = LDAP_SCOPE_ONELEVEL;
1N/A } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
1N/A scope = LDAP_SCOPE_SUBTREE;
1N/A } else {
1N/A fprintf( stderr, gettext("scope should be base, one, or sub\n") );
1N/A usage();
1N/A }
1N/A break;
1N/A
1N/A case 'a': /* set alias deref option */
1N/A if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
1N/A deref = LDAP_DEREF_NEVER;
1N/A } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
1N/A deref = LDAP_DEREF_SEARCHING;
1N/A } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
1N/A deref = LDAP_DEREF_FINDING;
1N/A } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
1N/A deref = LDAP_DEREF_ALWAYS;
1N/A } else {
1N/A fprintf( stderr, gettext("alias deref should be never, search, find, or always\n") );
1N/A usage();
1N/A }
1N/A break;
1N/A
1N/A case 'F': /* field separator */
1N/A sep = strdup( optarg );
1N/A ldif = 0;
1N/A break;
1N/A#ifndef SOLARIS_LDAP_CMD
1N/A case 'c':
1N/A if ( optarg && optarg[0] == '\0' ) {
1N/A /* -c ""
1N/A means "This user"
1N/A */
1N/A get_effectiverights_control_target_dn = NULL;
1N/A do_effective_rights_control = 1;
1N/A }else if ( strlen(optarg) < 3 || (strncasecmp(optarg, "dn:", 3) != 0) ) {
1N/A fprintf(stderr, gettext("-c wrong format--should be \"\" or \"dn:...\".\n"
1N/A "\"dn:\" means anonymous user."));
1N/A usage();
1N/A } else {
1N/A get_effectiverights_control_target_dn = strdup(optarg);
1N/A do_effective_rights_control = 1;
1N/A }
1N/A break;
1N/A case 'X':
1N/A get_effectiverights_control_attrlist = get_effectiverights_attrlist(optarg);
1N/A do_effective_rights_control = 1;
1N/A break;
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A case 'b': /* searchbase */
1N/A base = strdup( optarg );
1N/A break;
1N/A case 'l': /* time limit */
1N/A timelimit = atoi( optarg );
1N/A break;
1N/A case 'x': /* server sorting requested */
1N/A server_sort = 1;
1N/A break;
1N/A case 'z': /* size limit */
1N/A sizelimit = atoi( optarg );
1N/A break;
1N/A case 'S': /* sort attribute */
1N/A ldap_charray_add( &sortattr, strdup( optarg ) );
1N/A break;
1N/A case 'T': /* don't fold lines */
1N/A fold = 0;
1N/A break;
1N/A case 'G': /* do the virtual list setup */
1N/A use_vlv++;
1N/A s = strchr(optarg, VLV_PARAM_SEP );
1N/A
1N/A if (s != NULL)
1N/A {
1N/A vlv_before = atoi(optarg);
1N/A s++;
1N/A vlv_after = atoi( s );
1N/A s = strchr(s, VLV_PARAM_SEP );
1N/A if (s != NULL)
1N/A {
1N/A s++;
1N/A /* below is a small set of logic to implement the following cases
1N/A * -G23:23:wilber
1N/A * -G23:23:"wilber:wright"
1N/A * -G23:23:'wilber'
1N/A * -G23:23:wilber wright
1N/A * all of the above are before, after, value - NOTE: a colon not in a quoted
1N/A * string will break the parser!!!!
1N/A * -G23:23:45:600
1N/A * above is index, count encoding
1N/A */
1N/A
1N/A if (*s == '\'' || *s == '"')
1N/A {
1N/A vlv_value = strdup( s );
1N/A }
1N/A else
1N/A {
1N/A if (strchr( s, VLV_PARAM_SEP ))
1N/A {
1N/A /* we have an index + count option */
1N/A vlv_index = atoi( s );
1N/A vlv_count = atoi( strchr( s, VLV_PARAM_SEP) + 1);
1N/A }
1N/A else
1N/A {
1N/A /* we don't have a quote surrounding the assertion value
1N/A * do we need to???
1N/A */
1N/A vlv_value = strdup( s );
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A fprintf( stderr,gettext("Illegal 'after' parameter for virtual list\n") );
1N/A exit( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A }
1N/A else
1N/A {
1N/A fprintf( stderr,gettext("Illegal 'before' parameter for virtual list\n") );
1N/A exit( LDAP_PARAM_ERROR );
1N/A }
1N/A break;
1N/A case 'C':
1N/A use_psearch++;
1N/A if ( (ps_arg = strdup( optarg)) == NULL ) {
1N/A perror ("strdup");
1N/A exit (LDAP_NO_MEMORY);
1N/A }
1N/A
1N/A ps_ptr=strtok(ps_arg, ":");
1N/A if (ps_ptr == NULL || (strcasecmp(ps_ptr, "ps")) ) {
1N/A fprintf (stderr, gettext("Invalid argument for -C\n"));
1N/A usage();
1N/A }
1N/A if (NULL != (ps_ptr=strtok(NULL, ":"))) {
1N/A if ( (temp_arg = strdup( ps_ptr )) == NULL ) {
1N/A perror ("strdup");
1N/A exit (LDAP_NO_MEMORY);
1N/A }
1N/A } else {
1N/A fprintf (stderr, gettext("Invalid argument for -C\n"));
1N/A usage();
1N/A }
1N/A if (NULL != (ps_ptr=strtok(NULL, ":"))) {
1N/A if ( (changesonly = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
1N/A fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
1N/A usage();
1N/A }
1N/A }
1N/A if (NULL != (ps_ptr=strtok(NULL, ":"))) {
1N/A if ( (return_echg_ctls = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) {
1N/A fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr);
1N/A usage();
1N/A }
1N/A }
1N/A
1N/A /* Now parse the temp_arg and build chgtype as
1N/A * the changetypes are encountered */
1N/A
1N/A if ((ps_ptr = strtok( temp_arg, "," )) == NULL) {
1N/A usage();
1N/A } else {
1N/A while ( ps_ptr ) {
1N/A if ((strcasecmp(ps_ptr, "add"))==0)
1N/A chgtype |= LDAP_CHANGETYPE_ADD;
1N/A else if ((strcasecmp(ps_ptr, "delete"))==0)
1N/A chgtype |= LDAP_CHANGETYPE_DELETE;
1N/A else if ((strcasecmp(ps_ptr, "modify"))==0)
1N/A chgtype |= LDAP_CHANGETYPE_MODIFY;
1N/A else if ((strcasecmp(ps_ptr, "moddn"))==0)
1N/A chgtype |= LDAP_CHANGETYPE_MODDN;
1N/A else if ((strcasecmp(ps_ptr, "any"))==0)
1N/A chgtype = LDAP_CHANGETYPE_ANY;
1N/A else {
1N/A fprintf(stderr, gettext("Unknown changetype: %s\n"), ps_ptr);
1N/A usage();
1N/A }
1N/A ps_ptr = strtok( NULL, "," );
1N/A }
1N/A }
1N/A break;
1N/A default:
1N/A usage();
1N/A break;
1N/A }
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Adosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
1N/A LDAP *ld;
1N/A char *base;
1N/A int scope;
1N/A char **attrs;
1N/A int attrsonly;
1N/A char *filtpatt;
1N/A char *value;
1N/A{
1N/A char **refs = NULL, filter[ BUFSIZ ], *filterp = NULL;
1N/A int rc, first, matches;
1N/A LDAPMessage *res, *e;
1N/A LDAPControl *ldctrl;
1N/A LDAPControl **ctrl_response_array = NULL;
1N/A LDAPVirtualList vlv_data;
1N/A int msgid = 0;
1N/A int length = 0;
1N/A int mallocd_filter = 0;
1N/A
1N/A if ( strstr( filtpatt, "%s" ) == NULL ) { /* no need to sprintf() */
1N/A filterp = filtpatt;
1N/A } else {
1N/A length = strlen( filtpatt ) + strlen ( value ) +1;
1N/A if ( length > BUFSIZ ) {
1N/A if ((filterp = (char *)
1N/A malloc ( length )) == NULL) {
1N/A perror( gettext("filter and/or pattern too long?") );
1N/A exit (LDAP_PARAM_ERROR);
1N/A }
1N/A mallocd_filter = 1;
1N/A } else {
1N/A filterp = filter;
1N/A }
1N/A
1N/A#ifdef HAVE_SNPRINTF
1N/A if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {
1N/A perror( gettext("snprintf filter (filter and/or pattern too long?)") );
1N/A exit( LDAP_PARAM_ERROR );
1N/A }
1N/A#else
1N/A sprintf( filterp, filtpatt, value );
1N/A#endif
1N/A }
1N/A
1N/A if ( *filterp == '\0' ) { /* treat empty filter is a shortcut for oc=* */
1N/A if (mallocd_filter) {
1N/A free(filterp);
1N/A mallocd_filter = 0;
1N/A }
1N/A filterp = "(objectclass=*)";
1N/A }
1N/A
1N/A if ( ldaptool_verbose ) {
1N/A /*
1N/A * Display the filter that will be used. Add surrounding parens.
1N/A * if they are missing.
1N/A */
1N/A if ( '(' == *filterp ) {
1N/A printf( "filter is: %s\n", filterp );
1N/A } else {
1N/A printf( "filter is: (%s)\n", filterp );
1N/A }
1N/A }
1N/A
1N/A if ( ldaptool_not ) {
1N/A if (mallocd_filter) free(filterp);
1N/A return( LDAP_SUCCESS );
1N/A }
1N/A
1N/A if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
1N/A ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
1N/A }
1N/A
1N/A if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
1N/A ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
1N/A }
1N/A
1N/A#ifndef SOLARIS_LDAP_CMD
1N/A if ( do_effective_rights_control ) {
1N/A if ((ldctrl = ldaptool_create_geteffectiveRights_control(ld,
1N/A get_effectiverights_control_target_dn,
1N/A (const char**) get_effectiverights_control_attrlist)) !=NULL) {
1N/A ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
1N/A }
1N/A }
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A if (use_psearch) {
1N/A if ( ldap_create_persistentsearch_control( ld, chgtype,
1N/A changesonly, return_echg_ctls,
1N/A 1, &ldctrl ) != LDAP_SUCCESS )
1N/A {
1N/A ldap_perror( ld, "ldap_create_persistentsearch_control" );
1N/A return (1);
1N/A }
1N/A ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
1N/A }
1N/A
1N/A
1N/A if (server_sort) {
1N/A /* First make a sort key list from the attribute list we have */
1N/A LDAPsortkey **keylist = NULL;
1N/A int i = 0;
1N/A char *sortattrs = NULL;
1N/A char *s = NULL;
1N/A int string_length = 0;
1N/A
1N/A /* Count the sort strings */
1N/A for (i = 0; i < sortsize - 1 ; i++) {
1N/A string_length += strlen(sortattr[i]) + 1;
1N/A }
1N/A
1N/A sortattrs = (char *) malloc(string_length + 1);
1N/A if (NULL == sortattrs) {
1N/A fprintf( stderr, gettext("Out of memory\n") );
1N/A exit( LDAP_NO_MEMORY );
1N/A }
1N/A
1N/A s = sortattrs;
1N/A for (i = 0; i < sortsize - 1 ; i++) {
1N/A memcpy(s, sortattr[i], strlen(sortattr[i]));
1N/A s += strlen(sortattr[i]);
1N/A *s++ = ' ';
1N/A }
1N/A
1N/A sortattrs[string_length] = '\0';
1N/A
1N/A ldap_create_sort_keylist(&keylist,sortattrs);
1N/A free(sortattrs);
1N/A sortattrs = NULL;
1N/A
1N/A /* Then make a control for the sort attributes we have */
1N/A rc = ldap_create_sort_control(ld,keylist,0,&ldctrl);
1N/A ldap_free_sort_keylist(keylist);
1N/A if ( rc != LDAP_SUCCESS ) {
1N/A if (mallocd_filter) free(filterp);
1N/A return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP ));
1N/A }
1N/A
1N/A ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
1N/A
1N/A }
1N/A /* remember server side sorting must be available for vlv!!!! */
1N/A
1N/A if (use_vlv)
1N/A {
1N/A vlv_data.ldvlist_before_count = vlv_before;
1N/A vlv_data.ldvlist_after_count = vlv_after;
1N/A if ( ldaptool_verbose ) {
1N/A printf( gettext("vlv data %lu, %lu, "),
1N/A vlv_data.ldvlist_before_count,
1N/A vlv_data.ldvlist_after_count
1N/A );
1N/A }
1N/A if (vlv_value)
1N/A {
1N/A vlv_data.ldvlist_attrvalue = vlv_value;
1N/A vlv_data.ldvlist_size = 0;
1N/A vlv_data.ldvlist_index = 0;
1N/A if ( ldaptool_verbose ) {
1N/A printf( "%s, 0, 0\n", vlv_data.ldvlist_attrvalue);
1N/A }
1N/A }
1N/A else
1N/A {
1N/A vlv_data.ldvlist_attrvalue = NULL;
1N/A vlv_data.ldvlist_size = vlv_count;
1N/A vlv_data.ldvlist_index = vlv_index;
1N/A if ( ldaptool_verbose ) {
1N/A printf( "(null), %lu, %lu\n", vlv_data.ldvlist_size, vlv_data.ldvlist_index );
1N/A }
1N/A }
1N/A
1N/A if ( rc != LDAP_SUCCESS ) {
1N/A if (mallocd_filter) free(filterp);
1N/A return( ldaptool_print_lderror( ld, "ldap_create_sort_control",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP ));
1N/A }
1N/A if (LDAP_SUCCESS != (rc = ldap_create_virtuallist_control(ld,
1N/A &vlv_data, &ldctrl)))
1N/A {
1N/A if (mallocd_filter) free(filterp);
1N/A return( ldaptool_print_lderror( ld,
1N/A "ldap_create_virtuallist_control",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP ));
1N/A }
1N/A
1N/A ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);
1N/A
1N/A }
1N/A
1N/A if ( ldap_search_ext( ld, base, scope, filterp, attrs, attrsonly,
1N/A ldaptool_request_ctrls, NULL, NULL, -1, &msgid )
1N/A != LDAP_SUCCESS ) {
1N/A if (mallocd_filter) free(filterp);
1N/A return( ldaptool_print_lderror( ld, "ldap_search",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP ));
1N/A }
1N/A
1N/A
1N/A matches = 0;
1N/A first = 1;
1N/A if ( sortattr && !server_sort ) {
1N/A rc = ldap_result( ld, LDAP_RES_ANY, 1, NULL, &res );
1N/A } else {
1N/A while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res )) !=
1N/A LDAP_RES_SEARCH_RESULT && rc != -1 ) {
1N/A if ( rc != LDAP_RES_SEARCH_ENTRY ) {
1N/A if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1N/A parse_and_display_reference( ld, res );
1N/A } else if ( rc == LDAP_RES_EXTENDED
1N/A && ldap_msgid( res ) == LDAP_RES_UNSOLICITED ) {
1N/A ldaptool_print_extended_response( ld, res,
1N/A gettext("Unsolicited response") );
1N/A } else {
1N/A fprintf( stderr, gettext("%s: ignoring LDAP response message"
1N/A " type 0x%x (%s)\n"),
1N/A ldaptool_progname, rc, msgtype2str( rc ));
1N/A }
1N/A ldap_msgfree( res );
1N/A continue;
1N/A }
1N/A matches++;
1N/A e = ldap_first_entry( ld, res );
1N/A if ( !first ) {
1N/A putchar( '\n' );
1N/A } else {
1N/A first = 0;
1N/A }
1N/A print_entry( ld, e, attrsonly );
1N/A ldap_msgfree( res );
1N/A }
1N/A }
1N/A if ( rc == -1 ) {
1N/A if (mallocd_filter) free(filterp);
1N/A return( ldaptool_print_lderror( ld, "ldap_result",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP ));
1N/A }
1N/A
1N/A if ( ldap_parse_result( ld, res, &rc, NULL, NULL, &refs,
1N/A &ctrl_response_array, 0 ) != LDAP_SUCCESS ) {
1N/A ldaptool_print_lderror( ld, "ldap_parse_result",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP );
1N/A } else if ( rc != LDAP_SUCCESS ) {
1N/A ldaptool_print_lderror( ld, "ldap_search",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP );
1N/A }
1N/A /* Parse the returned sort control */
1N/A if (server_sort) {
1N/A unsigned long result = 0;
1N/A char *attribute;
1N/A
1N/A if ( LDAP_SUCCESS != ldap_parse_sort_control(ld,ctrl_response_array,&result,&attribute) ) {
1N/A ldaptool_print_lderror(ld, "ldap_parse_sort_control",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP );
1N/A ldap_controls_free(ctrl_response_array);
1N/A ldap_msgfree(res);
1N/A if (mallocd_filter) free(filterp);
1N/A return ( ldap_get_lderrno( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A if (0 == result) {
1N/A if ( ldaptool_verbose ) {
1N/A printf( gettext("Server indicated results sorted OK\n"));
1N/A }
1N/A } else {
1N/A if (NULL != attribute) {
1N/A printf(gettext("Server reported sorting error %ld: %s, attribute in error\"%s\"\n"),result,sortresult2string(result),attribute);
1N/A } else {
1N/A printf(gettext("Server reported sorting error %ld: %s\n"),result,sortresult2string(result));
1N/A }
1N/A }
1N/A
1N/A }
1N/A
1N/A if (use_vlv)
1N/A {
1N/A unsigned long vpos, vcount;
1N/A int vresult;
1N/A if ( LDAP_SUCCESS != ldap_parse_virtuallist_control(ld,ctrl_response_array,&vpos, &vcount,&vresult) ) {
1N/A ldaptool_print_lderror( ld, "ldap_parse_virtuallist_control",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP );
1N/A ldap_controls_free(ctrl_response_array);
1N/A ldap_msgfree(res);
1N/A if (mallocd_filter) free(filterp);
1N/A return ( ldap_get_lderrno( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A if (0 == vresult) {
1N/A if ( ldaptool_verbose ) {
1N/A printf( gettext("Server indicated virtual list positioning OK\n"));
1N/A }
1N/A printf(gettext("index %lu content count %lu\n"), vpos, vcount);
1N/A
1N/A } else {
1N/A printf(gettext("Server reported sorting error %d: %s\n"),vresult,sortresult2string(vresult));
1N/A
1N/A }
1N/A
1N/A }
1N/A
1N/A ldap_controls_free(ctrl_response_array);
1N/A
1N/A if ( sortattr != NULL && !server_sort) {
1N/A
1N/A (void) ldap_multisort_entries( ld, &res,
1N/A ( *sortattr == NULL ) ? NULL : sortattr,
1N/A (LDAP_CMP_CALLBACK *)strcasecmp );
1N/A matches = 0;
1N/A first = 1;
1N/A for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
1N/A e = ldap_next_entry( ld, e ) ) {
1N/A matches++;
1N/A if ( !first ) {
1N/A putchar( '\n' );
1N/A } else {
1N/A first = 0;
1N/A }
1N/A print_entry( ld, e, attrsonly );
1N/A }
1N/A }
1N/A
1N/A if ( ldaptool_verbose ) {
1N/A printf( gettext("%d matches\n"), matches );
1N/A }
1N/A
1N/A if ( refs != NULL ) {
1N/A ldaptool_print_referrals( refs );
1N/A ldap_value_free( refs );
1N/A }
1N/A
1N/A if (mallocd_filter) free(filterp);
1N/A
1N/A ldap_msgfree( res );
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Aprint_entry( ld, entry, attrsonly )
1N/A LDAP *ld;
1N/A LDAPMessage *entry;
1N/A int attrsonly;
1N/A{
1N/A char *a, *dn, *ufn, tmpfname[ BUFSIZ ];
1N/A int i, notascii;
1N/A BerElement *ber;
1N/A struct berval **bvals;
1N/A FILE *tmpfp;
1N/A#if defined( XP_WIN32 )
1N/A char mode[20] = "w+b";
1N/A#else
1N/A char mode[20] = "w";
1N/A#endif
1N/A
1N/A dn = ldap_get_dn( ld, entry );
1N/A write_string_attr_value( "dn", dn, LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
1N/A if ( includeufn ) {
1N/A ufn = ldap_dn2ufn( dn );
1N/A write_string_attr_value( "ufn", ufn,
1N/A LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME );
1N/A free( ufn );
1N/A }
1N/A ldap_memfree( dn );
1N/A
1N/A if ( use_psearch ) {
1N/A LDAPControl **ectrls;
1N/A int chgtype, chgnumpresent;
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A ber_int_t chgnum;
1N/A#else
1N/A long chgnum;
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A char *prevdn, longbuf[ 128 ];
1N/A
1N/A if ( ldap_get_entry_controls( ld, entry, &ectrls ) == LDAP_SUCCESS ) {
1N/A if ( ldap_parse_entrychange_control( ld, ectrls, &chgtype,
1N/A &prevdn, &chgnumpresent, &chgnum ) == LDAP_SUCCESS ) {
1N/A write_string_attr_value(
1N/A LDAPTOOL_PSEARCH_ATTR_PREFIX "changeType",
1N/A changetype_num2string( chgtype ), 0 );
1N/A if ( chgnumpresent ) {
1N/A sprintf( longbuf, "%d", chgnum );
1N/A write_string_attr_value(
1N/A LDAPTOOL_PSEARCH_ATTR_PREFIX "changeNumber",
1N/A longbuf, 0 );
1N/A }
1N/A if ( NULL != prevdn ) {
1N/A write_string_attr_value(
1N/A LDAPTOOL_PSEARCH_ATTR_PREFIX "previousDN",
1N/A prevdn, 0 );
1N/A ldap_memfree( prevdn );
1N/A }
1N/A }
1N/A ldap_controls_free (ectrls);
1N/A }
1N/A }
1N/A
1N/A for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1N/A a = ldap_next_attribute( ld, entry, ber ) ) {
1N/A if ( ldap_charray_inlist(sortattr, a) && /* in the list*/
1N/A skipsortattr[ldap_charray_position(sortattr, a)] ) {/* and skip it*/
1N/A continue; /* so skip it! */
1N/A }
1N/A if ( attrsonly ) {
1N/A if ( ldif ) {
1N/A write_ldif_value( a, "", 0, 0 );
1N/A } else {
1N/A printf( "%s\n", a );
1N/A }
1N/A } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1N/A for ( i = 0; bvals[i] != NULL; i++ ) {
1N/A if ( vals2tmp ) {
1N/A#ifdef HAVE_SNPRINTF
1N/A if ( snprintf( tmpfname, sizeof(tmpfname),
1N/A "%s/ldapsearch-%s-XXXXXX",
1N/A ldaptool_get_tmp_dir(), a ) < 0 ) {
1N/A perror( gettext("snprintf tmpfname (attribute name too long?)") );
1N/A exit( LDAP_PARAM_ERROR );
1N/A }
1N/A#else
1N/A sprintf( tmpfname, "%s/ldapsearch-%s-XXXXXX",
1N/A ldaptool_get_tmp_dir(), a );
1N/A#endif
1N/A tmpfp = NULL;
1N/A
1N/A if ( LDAPTOOL_MKTEMP( tmpfname ) == NULL ) {
1N/A perror( tmpfname );
1N/A } else if (( tmpfp = ldaptool_open_file( tmpfname, mode)) == NULL ) {
1N/A perror( tmpfname );
1N/A } else if ( bvals[ i ]->bv_len > 0 &&
1N/A fwrite( bvals[ i ]->bv_val,
1N/A bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
1N/A perror( tmpfname );
1N/A } else if ( ldif ) {
1N/A if ( produce_file_urls ) {
1N/A char *url;
1N/A
1N/A if ( ldaptool_path2fileurl( tmpfname, &url ) !=
1N/A LDAPTOOL_FILEURL_SUCCESS ) {
1N/A perror( "ldaptool_path2fileurl" );
1N/A } else {
1N/A write_ldif_value( a, url, strlen( url ),
1N/A LDIF_OPT_VALUE_IS_URL );
1N/A free( url );
1N/A }
1N/A } else {
1N/A write_ldif_value( a, tmpfname, strlen( tmpfname ),
1N/A 0 );
1N/A }
1N/A } else {
1N/A printf( "%s%s%s\n", a, sep, tmpfname );
1N/A }
1N/A
1N/A if ( tmpfp != NULL ) {
1N/A fclose( tmpfp );
1N/A }
1N/A } else {
1N/A notascii = 0;
1N/A if ( !ldif && !allow_binary ) {
1N/A notascii = !ldaptool_berval_is_ascii( bvals[i] );
1N/A }
1N/A
1N/A if ( ldif ) {
1N/A write_ldif_value( a, bvals[ i ]->bv_val,
1N/A bvals[ i ]->bv_len, 0 );
1N/A } else {
1N/A printf( "%s%s%s\n", a, sep,
1N/A notascii ? gettext("NOT ASCII") : bvals[ i ]->bv_val );
1N/A }
1N/A }
1N/A }
1N/A ber_bvecfree( bvals );
1N/A }
1N/A ldap_memfree( a );
1N/A }
1N/A
1N/A if ( ldap_get_lderrno( ld, NULL, NULL ) != LDAP_SUCCESS ) {
1N/A ldaptool_print_lderror( ld, "ldap_first_attribute/ldap_next_attribute",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP );
1N/A }
1N/A
1N/A if ( ber != NULL ) {
1N/A ber_free( ber, 0 );
1N/A }
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Awrite_string_attr_value( char *attrname, char *strval, unsigned long opts )
1N/A{
1N/A if ( strval == NULL ) {
1N/A strval = "";
1N/A }
1N/A if ( ldif ) {
1N/A write_ldif_value( attrname, strval, strlen( strval ), 0 );
1N/A } else if ( 0 != ( opts & LDAPTOOL_WRITEVALOPT_SUPPRESS_NAME )) {
1N/A printf( "%s\n", strval );
1N/A } else {
1N/A printf( "%s%s%s\n", attrname, sep, strval );
1N/A }
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Awrite_ldif_value( char *type, char *value, unsigned long vallen,
1N/A unsigned long ldifoptions )
1N/A{
1N/A char *ldif;
1N/A static int wrote_version = 0;
1N/A
1N/A if ( write_ldif_version && !wrote_version ) {
1N/A char versionbuf[ 64 ];
1N/A
1N/A wrote_version = 1;
1N/A sprintf( versionbuf, "%d", LDIF_VERSION_ONE );
1N/A write_ldif_value( "version", versionbuf, strlen( versionbuf ), 0 );
1N/A }
1N/A
1N/A if ( !fold ) {
1N/A ldifoptions |= LDIF_OPT_NOWRAP;
1N/A }
1N/A if ( minimize_base64 ) {
1N/A ldifoptions |= LDIF_OPT_MINIMAL_ENCODING;
1N/A }
1N/A
1N/A if (( ldif = ldif_type_and_value_with_options( type, value, (int)vallen,
1N/A ldifoptions )) == NULL ) {
1N/A return( -1 );
1N/A }
1N/A
1N/A fputs( ldif, stdout );
1N/A free( ldif );
1N/A
1N/A return( 0 );
1N/A}
1N/A
1N/A
1N/Astatic char *
1N/Asortresult2string(unsigned long result)
1N/A{
1N/A /*
1N/A success (0), -- results are sorted
1N/A operationsError (1), -- server internal failure
1N/A timeLimitExceeded (3), -- timelimit reached before
1N/A -- sorting was completed
1N/A strongAuthRequired (8), -- refused to return sorted
1N/A -- results via insecure
1N/A -- protocol
1N/A adminLimitExceeded (11), -- too many matching entries
1N/A -- for the server to sort
1N/A noSuchAttribute (16), -- unrecognized attribute
1N/A -- type in sort key
1N/A inappropriateMatching (18), -- unrecognized or inappro-
1N/A -- priate matching rule in
1N/A -- sort key
1N/A insufficientAccessRights (50), -- refused to return sorted
1N/A -- results to this client
1N/A busy (51), -- too busy to process
1N/A unwillingToPerform (53), -- unable to sort
1N/A other (80)
1N/A */
1N/A
1N/A switch (result) {
1N/A case 0: return (gettext("success"));
1N/A case 1: return (gettext("operations error"));
1N/A case 3: return (gettext("time limit exceeded"));
1N/A case 8: return (gettext("strong auth required"));
1N/A case 11: return (gettext("admin limit exceeded"));
1N/A case 16: return (gettext("no such attribute"));
1N/A case 18: return (gettext("unrecognized or inappropriate matching rule"));
1N/A case 50: return (gettext("insufficient access rights"));
1N/A case 51: return (gettext("too busy"));
1N/A case 53: return (gettext("unable to sort"));
1N/A case 80:
1N/A default: return (gettext("Er...Other ?"));
1N/A }
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Aparse_and_display_reference( LDAP *ld, LDAPMessage *ref )
1N/A{
1N/A int i;
1N/A char **refs;
1N/A
1N/A if ( ldap_parse_reference( ld, ref, &refs, NULL, 0 ) != LDAP_SUCCESS ) {
1N/A ldaptool_print_lderror( ld, "ldap_parse_reference",
1N/A LDAPTOOL_CHECK4SSL_IF_APPROP );
1N/A } else if ( refs != NULL && refs[ 0 ] != NULL ) {
1N/A fputs( gettext("Unfollowed continuation reference(s):\n"), stderr );
1N/A for ( i = 0; refs[ i ] != NULL; ++i ) {
1N/A fprintf( stderr, " %s\n", refs[ i ] );
1N/A }
1N/A ldap_value_free( refs );
1N/A }
1N/A}
1N/A
1N/A
1N/A/*possible operations a client can invoke -- copied from ldaprot.h */
1N/A
1N/A#ifndef LDAP_REQ_BIND
1N/A#define LDAP_REQ_BIND 0x60L /* application + constructed */
1N/A#define LDAP_REQ_UNBIND 0x42L /* application + primitive */
1N/A#define LDAP_REQ_SEARCH 0x63L /* application + constructed */
1N/A#define LDAP_REQ_MODIFY 0x66L /* application + constructed */
1N/A#define LDAP_REQ_ADD 0x68L /* application + constructed */
1N/A#define LDAP_REQ_DELETE 0x4aL /* application + primitive */
1N/A#define LDAP_REQ_RENAME 0x6cL /* application + constructed */
1N/A#define LDAP_REQ_COMPARE 0x6eL /* application + constructed */
1N/A#define LDAP_REQ_ABANDON 0x50L /* application + primitive */
1N/A#define LDAP_REQ_EXTENDED 0x77L /* application + constructed */
1N/A#endif /* LDAP_REQ_BIND */
1N/A
1N/A
1N/A
1N/Astruct ldapsearch_type2str {
1N/A
1N/A int ldst2s_type; /* message type */
1N/A char *ldst2s_string; /* descriptive string */
1N/A};
1N/A
1N/A#ifdef SOLARIS_LDAP_CMD
1N/Astatic struct ldapsearch_type2str ldapsearch_msgtypes[] = {
1N/A
1N/A /* results: */
1N/A { LDAP_RES_BIND, NULL },
1N/A { LDAP_RES_SEARCH_REFERENCE, NULL },
1N/A { LDAP_RES_SEARCH_ENTRY, NULL },
1N/A { LDAP_RES_SEARCH_RESULT, NULL },
1N/A { LDAP_RES_MODIFY, NULL },
1N/A { LDAP_RES_ADD, NULL },
1N/A { LDAP_RES_DELETE, NULL },
1N/A { LDAP_RES_MODDN, NULL },
1N/A { LDAP_RES_COMPARE, NULL },
1N/A { LDAP_RES_EXTENDED, NULL },
1N/A /* requests: */
1N/A { LDAP_REQ_BIND, NULL },
1N/A { LDAP_REQ_UNBIND, NULL },
1N/A { LDAP_REQ_SEARCH, NULL },
1N/A { LDAP_REQ_MODIFY, NULL },
1N/A { LDAP_REQ_ADD, NULL },
1N/A { LDAP_REQ_DELETE, NULL },
1N/A { LDAP_REQ_RENAME, NULL },
1N/A { LDAP_REQ_COMPARE, NULL },
1N/A { LDAP_REQ_ABANDON, NULL },
1N/A { LDAP_REQ_EXTENDED, NULL },
1N/A
1N/A};
1N/A#else
1N/Astatic struct ldapsearch_type2str ldapsearch_msgtypes[] = {
1N/A
1N/A /* results: */
1N/A { LDAP_RES_BIND, "bind result" },
1N/A { LDAP_RES_SEARCH_REFERENCE, "continuation reference" },
1N/A { LDAP_RES_SEARCH_ENTRY, "entry" },
1N/A { LDAP_RES_SEARCH_RESULT, "search result" },
1N/A { LDAP_RES_MODIFY, "modify result" },
1N/A { LDAP_RES_ADD, "add result" },
1N/A { LDAP_RES_DELETE, "delete result" },
1N/A { LDAP_RES_MODDN, "rename result" },
1N/A { LDAP_RES_COMPARE, "compare result" },
1N/A { LDAP_RES_EXTENDED, "extended operation result" },
1N/A /* requests: */
1N/A { LDAP_REQ_BIND, "bind request" },
1N/A { LDAP_REQ_UNBIND, "unbind request" },
1N/A { LDAP_REQ_SEARCH, "search request" },
1N/A { LDAP_REQ_MODIFY, "modify request" },
1N/A { LDAP_REQ_ADD, "add request" },
1N/A { LDAP_REQ_DELETE, "delete request" },
1N/A { LDAP_REQ_RENAME, "rename request" },
1N/A { LDAP_REQ_COMPARE, "compare request" },
1N/A { LDAP_REQ_ABANDON, "abandon request" },
1N/A { LDAP_REQ_EXTENDED, "extended request" },
1N/A
1N/A};
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A
1N/A#define LDAPSEARCHTOOL_NUMTYPES (sizeof(ldapsearch_msgtypes) \
1N/A / sizeof(struct ldapsearch_type2str))
1N/A
1N/A#ifdef SOLARIS_LDAP_CMD
1N/Astatic void
1N/Afill_ldapsearch_msgtypes( void )
1N/A{
1N/A int i = 0;
1N/A if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
1N/A != NULL)
1N/A return;
1N/A
1N/A /* results: */
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "bind result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "continuation reference");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "entry");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "search result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "modify result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "add result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "delete result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "rename result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "compare result");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "extended operation result");
1N/A /* requests: */
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "bind request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "unbind request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "search request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "modify request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "add request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "delete request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "rename request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "compare request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "abandon request");
1N/A ldapsearch_msgtypes[i++].ldst2s_string = gettext(
1N/A "extended request");
1N/A}
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A/*
1N/A * Return a descriptive string given an LDAP result message type (tag).
1N/A */
1N/Astatic char *
1N/Amsgtype2str( int msgtype )
1N/A{
1N/A char *s = gettext("unknown");
1N/A int i;
1N/A
1N/A#ifdef SOLARIS_LDAP_CMD
1N/A /* Make sure ldapsearch_msgtypes is initialized */
1N/A if (ldapsearch_msgtypes[LDAPSEARCHTOOL_NUMTYPES - 1].ldst2s_string
1N/A == NULL)
1N/A (void) fill_ldapsearch_msgtypes();
1N/A#endif /* SOLARIS_LDAP_CMD */
1N/A
1N/A for ( i = 0; i < LDAPSEARCHTOOL_NUMTYPES; ++i ) {
1N/A if ( msgtype == ldapsearch_msgtypes[ i ].ldst2s_type ) {
1N/A s = ldapsearch_msgtypes[ i ].ldst2s_string;
1N/A }
1N/A }
1N/A return( s );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Return a descriptive string given a Persistent Search change type
1N/A */
1N/Astatic char *
1N/Achangetype_num2string( int chgtype )
1N/A{
1N/A char *s = gettext("unknown");
1N/A
1N/A switch( chgtype ) {
1N/A case LDAP_CHANGETYPE_ADD:
1N/A s = gettext("add");
1N/A break;
1N/A case LDAP_CHANGETYPE_DELETE:
1N/A s = gettext("delete");
1N/A break;
1N/A case LDAP_CHANGETYPE_MODIFY:
1N/A s = gettext("modify");
1N/A break;
1N/A case LDAP_CHANGETYPE_MODDN:
1N/A s = gettext("moddn");
1N/A break;
1N/A }
1N/A
1N/A return( s );
1N/A}
1N/A
1N/A/* returns a null teminated charrary */
1N/Astatic char **get_effectiverights_attrlist(char * optarg) {
1N/A
1N/A char * tmp_str = strdup(optarg);
1N/A char ** retArray = NULL;
1N/A int i = 0;
1N/A
1N/A retArray = ldap_str2charray( tmp_str, " "); /* takes copies */
1N/A
1N/A free(tmp_str);
1N/A
1N/A /* Oops - somebody left this debug message in for the
1N/A getEffectiveRights control
1N/A fprintf(stderr, "attrlist: "); */
1N/A i = 0;
1N/A while( retArray[i] != NULL ) {
1N/A
1N/A fprintf(stderr,"%s ", retArray[i]);
1N/A i++;
1N/A }
1N/A fprintf(stderr, "\n");
1N/A
1N/A return(retArray);
1N/A
1N/A}