Home | History | Annotate | Line # | Download | only in tools
ldapcompare.c revision 1.1
      1  1.1  lukem /* ldapcompare.c -- LDAP compare tool */
      2  1.1  lukem /* $OpenLDAP: pkg/ldap/clients/tools/ldapcompare.c,v 1.43.2.4 2008/02/11 23:26:38 kurt Exp $ */
      3  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  1.1  lukem  *
      5  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      6  1.1  lukem  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
      7  1.1  lukem  * Portions Copyright 1998-2001 Net Boolean Incorporated.
      8  1.1  lukem  * All rights reserved.
      9  1.1  lukem  *
     10  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     12  1.1  lukem  * Public License.
     13  1.1  lukem  *
     14  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     15  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     16  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     17  1.1  lukem  */
     18  1.1  lukem /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
     19  1.1  lukem  * All rights reserved.
     20  1.1  lukem  *
     21  1.1  lukem  * Redistribution and use in source and binary forms are permitted
     22  1.1  lukem  * provided that this notice is preserved and that due credit is given
     23  1.1  lukem  * to the University of Michigan at Ann Arbor.  The name of the
     24  1.1  lukem  * University may not be used to endorse or promote products derived
     25  1.1  lukem  * from this software without specific prior written permission.  This
     26  1.1  lukem  * software is provided ``as is'' without express or implied warranty.
     27  1.1  lukem  */
     28  1.1  lukem /* Portions Copyright 2002, F5 Networks, Inc, All rights reserved.
     29  1.1  lukem  * This software is not subject to any license of F5 Networks.
     30  1.1  lukem  * This is free software; you can redistribute and use it
     31  1.1  lukem  * under the same terms as OpenLDAP itself.
     32  1.1  lukem  */
     33  1.1  lukem /* ACKNOWLEDGEMENTS:
     34  1.1  lukem  * This work was originally developed by Jeff Costlow (F5 Networks)
     35  1.1  lukem  * based, in part, on existing LDAP tools and adapted for inclusion
     36  1.1  lukem  * into OpenLDAP Software by Kurt D. Zeilenga.
     37  1.1  lukem  */
     38  1.1  lukem 
     39  1.1  lukem #include "portable.h"
     40  1.1  lukem 
     41  1.1  lukem #include <stdio.h>
     42  1.1  lukem 
     43  1.1  lukem #include <ac/stdlib.h>
     44  1.1  lukem 
     45  1.1  lukem #include <ac/ctype.h>
     46  1.1  lukem #include <ac/string.h>
     47  1.1  lukem #include <ac/unistd.h>
     48  1.1  lukem #include <ac/errno.h>
     49  1.1  lukem #include <ac/socket.h>
     50  1.1  lukem #include <ac/time.h>
     51  1.1  lukem #include <sys/stat.h>
     52  1.1  lukem 
     53  1.1  lukem #ifdef HAVE_FCNTL_H
     54  1.1  lukem #include <fcntl.h>
     55  1.1  lukem #endif
     56  1.1  lukem #ifdef HAVE_SYS_TYPES_H
     57  1.1  lukem #include <sys/types.h>
     58  1.1  lukem #endif
     59  1.1  lukem #ifdef HAVE_IO_H
     60  1.1  lukem #include <io.h>
     61  1.1  lukem #endif
     62  1.1  lukem 
     63  1.1  lukem #include <ldap.h>
     64  1.1  lukem 
     65  1.1  lukem #include "lutil.h"
     66  1.1  lukem #include "lutil_ldap.h"
     67  1.1  lukem #include "ldap_defaults.h"
     68  1.1  lukem 
     69  1.1  lukem #include "common.h"
     70  1.1  lukem 
     71  1.1  lukem 
     72  1.1  lukem static int quiet = 0;
     73  1.1  lukem 
     74  1.1  lukem 
     75  1.1  lukem void
     76  1.1  lukem usage( void )
     77  1.1  lukem {
     78  1.1  lukem 	fprintf( stderr, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
     79  1.1  lukem 	fprintf( stderr, _("where:\n"));
     80  1.1  lukem 	fprintf( stderr, _("  DN\tDistinguished Name\n"));
     81  1.1  lukem 	fprintf( stderr, _("  attr\tassertion attribute\n"));
     82  1.1  lukem 	fprintf( stderr, _("  value\tassertion value\n"));
     83  1.1  lukem 	fprintf( stderr, _("  b64value\tbase64 encoding of assertion value\n"));
     84  1.1  lukem 
     85  1.1  lukem 	fprintf( stderr, _("Compare options:\n"));
     86  1.1  lukem 	fprintf( stderr, _("  -E [!]<ext>[=<extparam>] compare extensions (! indicates criticality)\n"));
     87  1.1  lukem 	fprintf( stderr, _("             !dontUseCopy                (Don't Use Copy)\n"));
     88  1.1  lukem 	fprintf( stderr, _("  -z         Quiet mode,"
     89  1.1  lukem 		" don't print anything, use return values\n"));
     90  1.1  lukem 	tool_common_usage();
     91  1.1  lukem 	exit( EXIT_FAILURE );
     92  1.1  lukem }
     93  1.1  lukem 
     94  1.1  lukem static int docompare LDAP_P((
     95  1.1  lukem 	LDAP *ld,
     96  1.1  lukem 	char *dn,
     97  1.1  lukem 	char *attr,
     98  1.1  lukem 	struct berval *bvalue,
     99  1.1  lukem 	int quiet,
    100  1.1  lukem 	LDAPControl **sctrls,
    101  1.1  lukem 	LDAPControl **cctrls));
    102  1.1  lukem 
    103  1.1  lukem 
    104  1.1  lukem const char options[] = "z"
    105  1.1  lukem 	"Cd:D:e:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
    106  1.1  lukem 
    107  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    108  1.1  lukem int dontUseCopy = 0;
    109  1.1  lukem #endif
    110  1.1  lukem 
    111  1.1  lukem int
    112  1.1  lukem handle_private_option( int i )
    113  1.1  lukem {
    114  1.1  lukem 	char	*control, *cvalue;
    115  1.1  lukem 	int		crit;
    116  1.1  lukem 
    117  1.1  lukem 	switch ( i ) {
    118  1.1  lukem 	case 'E': /* compare extensions */
    119  1.1  lukem 		if( protocol == LDAP_VERSION2 ) {
    120  1.1  lukem 			fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
    121  1.1  lukem 				prog, protocol );
    122  1.1  lukem 			exit( EXIT_FAILURE );
    123  1.1  lukem 		}
    124  1.1  lukem 
    125  1.1  lukem 		/* should be extended to support comma separated list of
    126  1.1  lukem 		 *	[!]key[=value] parameters, e.g.  -E !foo,bar=567
    127  1.1  lukem 		 */
    128  1.1  lukem 
    129  1.1  lukem 		crit = 0;
    130  1.1  lukem 		cvalue = NULL;
    131  1.1  lukem 		if( optarg[0] == '!' ) {
    132  1.1  lukem 			crit = 1;
    133  1.1  lukem 			optarg++;
    134  1.1  lukem 		}
    135  1.1  lukem 
    136  1.1  lukem 		control = ber_strdup( optarg );
    137  1.1  lukem 		if ( (cvalue = strchr( control, '=' )) != NULL ) {
    138  1.1  lukem 			*cvalue++ = '\0';
    139  1.1  lukem 		}
    140  1.1  lukem 
    141  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    142  1.1  lukem 		if ( strcasecmp( control, "dontUseCopy" ) == 0 ) {
    143  1.1  lukem 			if( dontUseCopy ) {
    144  1.1  lukem 				fprintf( stderr,
    145  1.1  lukem 					_("dontUseCopy control previously specified\n"));
    146  1.1  lukem 				exit( EXIT_FAILURE );
    147  1.1  lukem 			}
    148  1.1  lukem 			if( cvalue != NULL ) {
    149  1.1  lukem 				fprintf( stderr,
    150  1.1  lukem 					_("dontUseCopy: no control value expected\n") );
    151  1.1  lukem 				usage();
    152  1.1  lukem 			}
    153  1.1  lukem 			if( !crit ) {
    154  1.1  lukem 				fprintf( stderr,
    155  1.1  lukem 					_("dontUseCopy: critical flag required\n") );
    156  1.1  lukem 				usage();
    157  1.1  lukem 			}
    158  1.1  lukem 
    159  1.1  lukem 			dontUseCopy = 1 + crit;
    160  1.1  lukem 		} else
    161  1.1  lukem #endif
    162  1.1  lukem 		{
    163  1.1  lukem 			fprintf( stderr,
    164  1.1  lukem 				_("Invalid compare extension name: %s\n"), control );
    165  1.1  lukem 			usage();
    166  1.1  lukem 		}
    167  1.1  lukem 		break;
    168  1.1  lukem 
    169  1.1  lukem 	case 'z':
    170  1.1  lukem 		quiet = 1;
    171  1.1  lukem 		break;
    172  1.1  lukem 
    173  1.1  lukem 	default:
    174  1.1  lukem 		return 0;
    175  1.1  lukem 	}
    176  1.1  lukem 	return 1;
    177  1.1  lukem }
    178  1.1  lukem 
    179  1.1  lukem 
    180  1.1  lukem int
    181  1.1  lukem main( int argc, char **argv )
    182  1.1  lukem {
    183  1.1  lukem 	char		*compdn = NULL, *attrs = NULL;
    184  1.1  lukem 	char		*sep;
    185  1.1  lukem 	int		rc;
    186  1.1  lukem 	LDAP		*ld = NULL;
    187  1.1  lukem 	struct berval	bvalue = { 0, NULL };
    188  1.1  lukem 	int		i = 0;
    189  1.1  lukem 	LDAPControl	c[1];
    190  1.1  lukem 
    191  1.1  lukem 
    192  1.1  lukem 	tool_init( TOOL_COMPARE );
    193  1.1  lukem 	prog = lutil_progname( "ldapcompare", argc, argv );
    194  1.1  lukem 
    195  1.1  lukem 	tool_args( argc, argv );
    196  1.1  lukem 
    197  1.1  lukem 	if ( argc - optind != 2 ) {
    198  1.1  lukem 		usage();
    199  1.1  lukem 	}
    200  1.1  lukem 
    201  1.1  lukem 	compdn = argv[optind++];
    202  1.1  lukem 	attrs = argv[optind++];
    203  1.1  lukem 
    204  1.1  lukem 	/* user passed in only 2 args, the last one better be in
    205  1.1  lukem 	 * the form attr:value or attr::b64value
    206  1.1  lukem 	 */
    207  1.1  lukem 	sep = strchr(attrs, ':');
    208  1.1  lukem 	if (!sep) {
    209  1.1  lukem 		usage();
    210  1.1  lukem 	}
    211  1.1  lukem 
    212  1.1  lukem 	*sep++='\0';
    213  1.1  lukem 	if ( *sep != ':' ) {
    214  1.1  lukem 		bvalue.bv_val = strdup( sep );
    215  1.1  lukem 		bvalue.bv_len = strlen( bvalue.bv_val );
    216  1.1  lukem 
    217  1.1  lukem 	} else {
    218  1.1  lukem 		/* it's base64 encoded. */
    219  1.1  lukem 		bvalue.bv_val = malloc( strlen( &sep[1] ));
    220  1.1  lukem 		bvalue.bv_len = lutil_b64_pton( &sep[1],
    221  1.1  lukem 			(unsigned char *) bvalue.bv_val, strlen( &sep[1] ));
    222  1.1  lukem 
    223  1.1  lukem 		if (bvalue.bv_len == (ber_len_t)-1) {
    224  1.1  lukem 			fprintf(stderr, _("base64 decode error\n"));
    225  1.1  lukem 			exit(-1);
    226  1.1  lukem 		}
    227  1.1  lukem 	}
    228  1.1  lukem 
    229  1.1  lukem 	ld = tool_conn_setup( 0, 0 );
    230  1.1  lukem 
    231  1.1  lukem 	if ( pw_file || want_bindpw ) {
    232  1.1  lukem 		if ( pw_file ) {
    233  1.1  lukem 			rc = lutil_get_filed_password( pw_file, &passwd );
    234  1.1  lukem 			if( rc ) return EXIT_FAILURE;
    235  1.1  lukem 		} else {
    236  1.1  lukem 			passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
    237  1.1  lukem 			passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
    238  1.1  lukem 		}
    239  1.1  lukem 	}
    240  1.1  lukem 
    241  1.1  lukem 	tool_bind( ld );
    242  1.1  lukem 
    243  1.1  lukem 	if ( 0
    244  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    245  1.1  lukem 		|| dontUseCopy
    246  1.1  lukem #endif
    247  1.1  lukem 		)
    248  1.1  lukem 	{
    249  1.1  lukem #ifdef LDAP_CONTROL_DONTUSECOPY
    250  1.1  lukem 		if ( dontUseCopy ) {
    251  1.1  lukem 			c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
    252  1.1  lukem 			c[i].ldctl_value.bv_val = NULL;
    253  1.1  lukem 			c[i].ldctl_value.bv_len = 0;
    254  1.1  lukem 			c[i].ldctl_iscritical = dontUseCopy > 1;
    255  1.1  lukem 			i++;
    256  1.1  lukem 		}
    257  1.1  lukem #endif
    258  1.1  lukem 	}
    259  1.1  lukem 
    260  1.1  lukem 	tool_server_controls( ld, c, i );
    261  1.1  lukem 
    262  1.1  lukem 	if ( verbose ) {
    263  1.1  lukem 		fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
    264  1.1  lukem 			compdn, attrs, sep );
    265  1.1  lukem 	}
    266  1.1  lukem 
    267  1.1  lukem 	rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
    268  1.1  lukem 
    269  1.1  lukem 	free( bvalue.bv_val );
    270  1.1  lukem 
    271  1.1  lukem 	tool_unbind( ld );
    272  1.1  lukem 	tool_destroy();
    273  1.1  lukem 	return rc;
    274  1.1  lukem }
    275  1.1  lukem 
    276  1.1  lukem 
    277  1.1  lukem static int docompare(
    278  1.1  lukem 	LDAP *ld,
    279  1.1  lukem 	char *dn,
    280  1.1  lukem 	char *attr,
    281  1.1  lukem 	struct berval *bvalue,
    282  1.1  lukem 	int quiet,
    283  1.1  lukem 	LDAPControl **sctrls,
    284  1.1  lukem 	LDAPControl **cctrls )
    285  1.1  lukem {
    286  1.1  lukem 	int		rc, msgid, code;
    287  1.1  lukem 	LDAPMessage	*res;
    288  1.1  lukem 	char		*matcheddn;
    289  1.1  lukem 	char		*text;
    290  1.1  lukem 	char		**refs;
    291  1.1  lukem 	LDAPControl **ctrls = NULL;
    292  1.1  lukem 
    293  1.1  lukem 	if ( dont ) {
    294  1.1  lukem 		return LDAP_SUCCESS;
    295  1.1  lukem 	}
    296  1.1  lukem 
    297  1.1  lukem 	rc = ldap_compare_ext( ld, dn, attr, bvalue,
    298  1.1  lukem 		sctrls, cctrls, &msgid );
    299  1.1  lukem 	if ( rc == -1 ) {
    300  1.1  lukem 		return( rc );
    301  1.1  lukem 	}
    302  1.1  lukem 
    303  1.1  lukem 	for ( ; ; ) {
    304  1.1  lukem 		struct timeval	tv;
    305  1.1  lukem 
    306  1.1  lukem 		tv.tv_sec = 0;
    307  1.1  lukem 		tv.tv_usec = 100000;
    308  1.1  lukem 
    309  1.1  lukem 		if ( tool_check_abandon( ld, msgid ) ) {
    310  1.1  lukem 			return LDAP_CANCELLED;
    311  1.1  lukem 		}
    312  1.1  lukem 
    313  1.1  lukem 		rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
    314  1.1  lukem 		if ( rc < 0 ) {
    315  1.1  lukem 			tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
    316  1.1  lukem 			return rc;
    317  1.1  lukem 		}
    318  1.1  lukem 
    319  1.1  lukem 		if ( rc != 0 ) {
    320  1.1  lukem 			break;
    321  1.1  lukem 		}
    322  1.1  lukem 	}
    323  1.1  lukem 
    324  1.1  lukem 	rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 1 );
    325  1.1  lukem 
    326  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
    327  1.1  lukem 		fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
    328  1.1  lukem 			prog, ldap_err2string( rc ), rc );
    329  1.1  lukem 		return rc;
    330  1.1  lukem 	}
    331  1.1  lukem 
    332  1.1  lukem 	if ( !quiet && ( verbose || ( code != LDAP_SUCCESS && code != LDAP_COMPARE_TRUE && code != LDAP_COMPARE_FALSE )||
    333  1.1  lukem 		(matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) )
    334  1.1  lukem 	{
    335  1.1  lukem 		printf( _("Compare Result: %s (%d)\n"),
    336  1.1  lukem 			ldap_err2string( code ), code );
    337  1.1  lukem 
    338  1.1  lukem 		if( text && *text ) {
    339  1.1  lukem 			printf( _("Additional info: %s\n"), text );
    340  1.1  lukem 		}
    341  1.1  lukem 
    342  1.1  lukem 		if( matcheddn && *matcheddn ) {
    343  1.1  lukem 			printf( _("Matched DN: %s\n"), matcheddn );
    344  1.1  lukem 		}
    345  1.1  lukem 
    346  1.1  lukem 		if( refs ) {
    347  1.1  lukem 			int i;
    348  1.1  lukem 			for( i=0; refs[i]; i++ ) {
    349  1.1  lukem 				printf(_("Referral: %s\n"), refs[i] );
    350  1.1  lukem 			}
    351  1.1  lukem 		}
    352  1.1  lukem 	}
    353  1.1  lukem 
    354  1.1  lukem 	/* if we were told to be quiet, use the return value. */
    355  1.1  lukem 	if ( !quiet ) {
    356  1.1  lukem 		if ( code == LDAP_COMPARE_TRUE ) {
    357  1.1  lukem 			printf(_("TRUE\n"));
    358  1.1  lukem 		} else if ( code == LDAP_COMPARE_FALSE ) {
    359  1.1  lukem 			printf(_("FALSE\n"));
    360  1.1  lukem 		} else {
    361  1.1  lukem 			printf(_("UNDEFINED\n"));
    362  1.1  lukem 		}
    363  1.1  lukem 	}
    364  1.1  lukem 
    365  1.1  lukem 	if ( ctrls ) {
    366  1.1  lukem 		tool_print_ctrls( ld, ctrls );
    367  1.1  lukem 		ldap_controls_free( ctrls );
    368  1.1  lukem 	}
    369  1.1  lukem 
    370  1.1  lukem 	ber_memfree( text );
    371  1.1  lukem 	ber_memfree( matcheddn );
    372  1.1  lukem 	ber_memvfree( (void **) refs );
    373  1.1  lukem 
    374  1.1  lukem 	return( code );
    375  1.1  lukem }
    376  1.1  lukem 
    377