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,"
53 " don't print anything, use return values\n"));
58 static int docompare LDAP_P((
62 struct berval *bvalue,
65 LDAPControl **cctrls));
68 const char options[] = "z"
69 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
72 handle_private_option( int i )
76 char *control, *cvalue;
78 case 'E': /* compare controls */
79 if( protocol == LDAP_VERSION2 ) {
80 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
85 /* should be extended to support comma separated list of
86 * [!]key[=value] parameters, e.g. -E !foo,bar=567
91 if( optarg[0] == '!' ) {
96 control = strdup( optarg );
97 if ( (cvalue = strchr( control, '=' )) != NULL ) {
100 fprintf( stderr, _("Invalid compare control name: %s\n"), control );
116 main( int argc, char **argv )
118 char *compdn = NULL, *attrs = NULL;
122 struct berval bvalue = { 0, NULL };
125 prog = lutil_progname( "ldapcompare", argc, argv );
127 tool_args( argc, argv );
129 if ( argc - optind != 2 ) {
133 compdn = argv[optind++];
134 attrs = argv[optind++];
136 /* user passed in only 2 args, the last one better be in
137 * the form attr:value or attr::b64value
139 sep = strchr(attrs, ':');
146 bvalue.bv_val = strdup( sep );
147 bvalue.bv_len = strlen( bvalue.bv_val );
150 /* it's base64 encoded. */
151 bvalue.bv_val = malloc( strlen( &sep[1] ));
152 bvalue.bv_len = lutil_b64_pton( &sep[1],
153 bvalue.bv_val, strlen( &sep[1] ));
155 if (bvalue.bv_len == (ber_len_t)-1) {
156 fprintf(stderr, _("base64 decode error\n"));
161 ld = tool_conn_setup( 0, 0 );
163 if ( pw_file || want_bindpw ) {
165 rc = lutil_get_filed_password( pw_file, &passwd );
166 if( rc ) return EXIT_FAILURE;
168 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
169 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
175 if ( authzid || manageDSAit || noop )
176 tool_server_controls( ld, NULL, 0 );
179 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
180 compdn, attrs, sep );
183 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
185 free( bvalue.bv_val );
193 static int docompare(
197 struct berval *bvalue,
199 LDAPControl **sctrls,
200 LDAPControl **cctrls )
208 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
212 ldap_perror( ld, "ldap_result" );
216 /* if we were told to be quiet, use the return value. */
218 if ( rc == LDAP_COMPARE_TRUE ) {
221 } else if ( rc == LDAP_COMPARE_FALSE ) {
223 printf(_("FALSE\n"));
225 ldap_perror( ld, "ldap_compare" );