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"
45 "usage: %s [options] DN <attr:value|attr::b64value>\n"
47 " DN\tDistinguished Name\n"
48 " attr\tassertion attribute\n"
49 " value\tassertion value\n"
50 " b64value\tbase64 encoding of assertion value\n"
53 " -z Quiet mode, don't print anything, use return values\n"
59 static int docompare LDAP_P((
63 struct berval *bvalue,
66 LDAPControl **cctrls));
69 const char options[] = "z"
70 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
73 handle_private_option( int i )
77 char *control, *cvalue;
79 case 'E': /* compare controls */
80 if( protocol == LDAP_VERSION2 ) {
81 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
86 /* should be extended to support comma separated list of
87 * [!]key[=value] parameters, e.g. -E !foo,bar=567
92 if( optarg[0] == '!' ) {
97 control = strdup( optarg );
98 if ( (cvalue = strchr( control, '=' )) != NULL ) {
101 fprintf( stderr, "Invalid compare control name: %s\n", control );
117 main( int argc, char **argv )
119 char *compdn = NULL, *attrs = NULL;
123 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 ) {
225 ldap_perror( ld, "ldap_compare" );