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"
46 "usage: %s [options] DN <attr:value|attr::b64value>\n"
48 " DN\tDistinguished Name\n"
49 " attr\tassertion attribute\n"
50 " value\tassertion value\n"
51 " b64value\tbase64 encoding of assertion value\n"
54 " -z Quiet mode, don't print anything, use return values\n"
60 static int docompare LDAP_P((
64 struct berval *bvalue,
67 LDAPControl **cctrls));
70 const char options[] = "z"
71 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
74 handle_private_option( int i )
78 char *control, *cvalue;
80 case 'E': /* compare controls */
81 if( protocol == LDAP_VERSION2 ) {
82 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
87 /* should be extended to support comma separated list of
88 * [!]key[=value] parameters, e.g. -E !foo,bar=567
93 if( optarg[0] == '!' ) {
98 control = strdup( optarg );
99 if ( (cvalue = strchr( control, '=' )) != NULL ) {
102 fprintf( stderr, "Invalid compare control name: %s\n", control );
118 main( int argc, char **argv )
120 char *compdn = NULL, *attrs = NULL;
124 struct berval bvalue = { 0, NULL };
126 prog = lutil_progname( "ldapcompare", argc, argv );
128 tool_args( argc, argv );
130 if ( argc - optind != 2 ) {
134 compdn = argv[optind++];
135 attrs = argv[optind++];
137 /* user passed in only 2 args, the last one better be in
138 * the form attr:value or attr::b64value
140 sep = strchr(attrs, ':');
147 bvalue.bv_val = strdup( sep );
148 bvalue.bv_len = strlen( bvalue.bv_val );
151 /* it's base64 encoded. */
152 bvalue.bv_val = malloc( strlen( &sep[1] ));
153 bvalue.bv_len = lutil_b64_pton( &sep[1],
154 bvalue.bv_val, strlen( &sep[1] ));
156 if (bvalue.bv_len == (ber_len_t)-1) {
157 fprintf(stderr, "base64 decode error\n");
165 ld = tool_conn_setup( 0, 0 );
167 if ( pw_file || want_bindpw ) {
169 rc = lutil_get_filed_password( pw_file, &passwd );
170 if( rc ) return EXIT_FAILURE;
172 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
173 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
179 if ( authzid || manageDSAit || noop )
180 tool_server_controls( ld, NULL, 0 );
183 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
184 compdn, attrs, sep );
187 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
189 free( bvalue.bv_val );
197 static int docompare(
201 struct berval *bvalue,
203 LDAPControl **sctrls,
204 LDAPControl **cctrls )
212 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
216 ldap_perror( ld, "ldap_result" );
220 /* if we were told to be quiet, use the return value. */
222 if ( rc == LDAP_COMPARE_TRUE ) {
225 } else if ( rc == LDAP_COMPARE_FALSE ) {
229 ldap_perror( ld, "ldap_compare" );