2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
44 fprintf( stderr, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
45 fprintf( stderr, _("where:\n"));
46 fprintf( stderr, _(" DN\tDistinguished Name\n"));
47 fprintf( stderr, _(" attr\tassertion attribute\n"));
48 fprintf( stderr, _(" value\tassertion value\n"));
49 fprintf( stderr, _(" b64value\tbase64 encoding of assertion value\n"));
51 fprintf( stderr, _("Compare options:\n"));
52 fprintf( stderr, _(" -z Quiet mode, don't print anything, use return values\n"));
57 static int docompare LDAP_P((
61 struct berval *bvalue,
64 LDAPControl **cctrls));
67 const char options[] = "z"
68 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
71 handle_private_option( int i )
75 char *control, *cvalue;
77 case 'E': /* compare controls */
78 if( protocol == LDAP_VERSION2 ) {
79 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
84 /* should be extended to support comma separated list of
85 * [!]key[=value] parameters, e.g. -E !foo,bar=567
90 if( optarg[0] == '!' ) {
95 control = strdup( optarg );
96 if ( (cvalue = strchr( control, '=' )) != NULL ) {
99 fprintf( stderr, _("Invalid compare control name: %s\n"), control );
115 main( int argc, char **argv )
117 char *compdn = NULL, *attrs = NULL;
121 struct berval bvalue = { 0, NULL };
124 prog = lutil_progname( "ldapcompare", argc, argv );
126 tool_args( argc, argv );
128 if ( argc - optind != 2 ) {
132 compdn = argv[optind++];
133 attrs = argv[optind++];
135 /* user passed in only 2 args, the last one better be in
136 * the form attr:value or attr::b64value
138 sep = strchr(attrs, ':');
145 bvalue.bv_val = strdup( sep );
146 bvalue.bv_len = strlen( bvalue.bv_val );
149 /* it's base64 encoded. */
150 bvalue.bv_val = malloc( strlen( &sep[1] ));
151 bvalue.bv_len = lutil_b64_pton( &sep[1],
152 bvalue.bv_val, strlen( &sep[1] ));
154 if (bvalue.bv_len == (ber_len_t)-1) {
155 fprintf(stderr, _("base64 decode error\n"));
160 ld = tool_conn_setup( 0, 0 );
162 if ( pw_file || want_bindpw ) {
164 rc = lutil_get_filed_password( pw_file, &passwd );
165 if( rc ) return EXIT_FAILURE;
167 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
168 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
174 if ( authzid || manageDSAit || noop )
175 tool_server_controls( ld, NULL, 0 );
178 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
179 compdn, attrs, sep );
182 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
184 free( bvalue.bv_val );
192 static int docompare(
196 struct berval *bvalue,
198 LDAPControl **sctrls,
199 LDAPControl **cctrls )
207 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
211 ldap_perror( ld, "ldap_result" );
215 /* if we were told to be quiet, use the return value. */
217 if ( rc == LDAP_COMPARE_TRUE ) {
220 } else if ( rc == LDAP_COMPARE_FALSE ) {
222 printf(_("FALSE\n"));
224 ldap_perror( ld, "ldap_compare" );