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>
33 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
38 #define _OLV_APP "ldapcompare"
40 #include "ol_version.h"
49 "usage: %s [options] DN <attr:value|attr::b64value>\n"
51 " DN\tDistinguished Name\n"
52 " attr\tassertion attribute\n"
53 " value\tassertion value\n"
54 " b64value\tbase64 encoding of assertion value\n"
57 " -z Quiet mode, don't print anything, use return values\n"
63 static int docompare LDAP_P((
67 struct berval *bvalue,
70 LDAPControl **cctrls));
73 const char options[] = "z"
74 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
77 handle_private_option( int i )
81 char *control, *cvalue;
83 case 'E': /* compare controls */
84 if( protocol == LDAP_VERSION2 ) {
85 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
90 /* should be extended to support comma separated list of
91 * [!]key[=value] parameters, e.g. -E !foo,bar=567
96 if( optarg[0] == '!' ) {
101 control = strdup( optarg );
102 if ( (cvalue = strchr( control, '=' )) != NULL ) {
105 fprintf( stderr, "Invalid compare control name: %s\n", control );
121 main( int argc, char **argv )
123 char *compdn = NULL, *attrs = NULL;
127 struct berval bvalue = { 0, NULL };
129 prog = lutil_progname( "ldapcompare", argc, argv );
131 tool_args( argc, argv );
133 if ( argc - optind != 2 ) {
137 compdn = argv[optind++];
138 attrs = argv[optind++];
140 /* user passed in only 2 args, the last one better be in
141 * the form attr:value or attr::b64value
143 sep = strchr(attrs, ':');
150 bvalue.bv_val = strdup( sep );
151 bvalue.bv_len = strlen( bvalue.bv_val );
154 /* it's base64 encoded. */
155 bvalue.bv_val = malloc( strlen( &sep[1] ));
156 bvalue.bv_len = lutil_b64_pton( &sep[1],
157 bvalue.bv_val, strlen( &sep[1] ));
159 if (bvalue.bv_len == (ber_len_t)-1) {
160 fprintf(stderr, "base64 decode error\n");
168 ld = tool_conn_setup( 0, 0 );
170 if ( pw_file || want_bindpw ) {
172 rc = lutil_get_filed_password( pw_file, &passwd );
173 if( rc ) return EXIT_FAILURE;
175 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
176 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
182 if ( authzid || manageDSAit || noop )
183 tool_server_controls( ld, NULL, 0 );
186 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
187 compdn, attrs, sep );
190 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
192 free( bvalue.bv_val );
200 static int docompare(
204 struct berval *bvalue,
206 LDAPControl **sctrls,
207 LDAPControl **cctrls )
215 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
219 ldap_perror( ld, "ldap_result" );
223 /* if we were told to be quiet, use the return value. */
225 if ( rc == LDAP_COMPARE_TRUE ) {
228 } else if ( rc == LDAP_COMPARE_FALSE ) {
232 ldap_perror( ld, "ldap_compare" );