1 /* ldapcompare.c -- LDAP compare tool */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 1998-2001 Net Boolean Incorporated.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
18 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms are permitted
22 * provided that this notice is preserved and that due credit is given
23 * to the University of Michigan at Ann Arbor. The name of the
24 * University may not be used to endorse or promote products derived
25 * from this software without specific prior written permission. This
26 * software is provided ``as is'' without express or implied warranty.
28 /* Portions Copyright 2002, F5 Networks, Inc, All rights reserved.
29 * This software is not subject to any license of F5 Networks.
30 * This is free software; you can redistribute and use it
31 * under the same terms as OpenLDAP itself.
34 * This work was originally developed by Jeff Costlow (F5 Networks)
35 * based, in part, on existing LDAP tools and adapted for inclusion
36 * into OpenLDAP Software by Kurt D. Zeilenga.
43 #include <ac/stdlib.h>
46 #include <ac/string.h>
47 #include <ac/unistd.h>
55 #ifdef HAVE_SYS_TYPES_H
56 #include <sys/types.h>
65 #include "lutil_ldap.h"
66 #include "ldap_defaults.h"
77 fprintf( stderr, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
78 fprintf( stderr, _("where:\n"));
79 fprintf( stderr, _(" DN\tDistinguished Name\n"));
80 fprintf( stderr, _(" attr\tassertion attribute\n"));
81 fprintf( stderr, _(" value\tassertion value\n"));
82 fprintf( stderr, _(" b64value\tbase64 encoding of assertion value\n"));
84 fprintf( stderr, _("Compare options:\n"));
85 #ifdef LDAP_CONTROL_DONTUSECOPY
86 fprintf( stderr, _(" -E [!]<ext>[=<extparam>] compare extensions (! indicates criticality)\n"));
87 fprintf( stderr, _(" !dontUseCopy (Don't Use Copy)\n"));
89 fprintf( stderr, _(" -z Quiet mode,"
90 " don't print anything, use return values\n"));
95 static int docompare LDAP_P((
99 struct berval *bvalue,
101 LDAPControl **sctrls,
102 LDAPControl **cctrls));
105 const char options[] = "z"
106 "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
108 #ifdef LDAP_CONTROL_DONTUSECOPY
113 handle_private_option( int i )
115 char *control, *cvalue;
119 case 'E': /* compare extensions */
120 if( protocol == LDAP_VERSION2 ) {
121 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
123 exit( EXIT_FAILURE );
126 /* should be extended to support comma separated list of
127 * [!]key[=value] parameters, e.g. -E !foo,bar=567
132 if( optarg[0] == '!' ) {
137 control = ber_strdup( optarg );
138 if ( (cvalue = strchr( control, '=' )) != NULL ) {
142 #ifdef LDAP_CONTROL_DONTUSECOPY
143 if ( strcasecmp( control, "dontUseCopy" ) == 0 ) {
146 _("dontUseCopy control previously specified\n"));
147 exit( EXIT_FAILURE );
149 if( cvalue != NULL ) {
151 _("dontUseCopy: no control value expected\n") );
156 _("dontUseCopy: critical flag required\n") );
160 dontUseCopy = 1 + crit;
165 _("Invalid compare extension name: %s\n"), control );
182 main( int argc, char **argv )
184 char *compdn = NULL, *attrs = NULL;
188 struct berval bvalue = { 0, NULL };
190 tool_init( TOOL_COMPARE );
191 prog = lutil_progname( "ldapcompare", argc, argv );
193 tool_args( argc, argv );
195 if ( argc - optind != 2 ) {
199 compdn = argv[optind++];
200 attrs = argv[optind++];
202 /* user passed in only 2 args, the last one better be in
203 * the form attr:value or attr::b64value
205 sep = strchr(attrs, ':');
212 bvalue.bv_val = strdup( sep );
213 bvalue.bv_len = strlen( bvalue.bv_val );
216 /* it's base64 encoded. */
217 bvalue.bv_val = malloc( strlen( &sep[1] ));
218 bvalue.bv_len = lutil_b64_pton( &sep[1],
219 (unsigned char *) bvalue.bv_val, strlen( &sep[1] ));
221 if (bvalue.bv_len == (ber_len_t)-1) {
222 fprintf(stderr, _("base64 decode error\n"));
227 ld = tool_conn_setup( 0, 0 );
229 if ( pw_file || want_bindpw ) {
231 rc = lutil_get_filed_password( pw_file, &passwd );
232 if( rc ) return EXIT_FAILURE;
234 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
235 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
241 if ( assertion || authzid || manageDSAit || noop
242 #ifdef LDAP_CONTROL_DONTUSECOPY
251 #ifdef LDAP_CONTROL_DONTUSECOPY
253 c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
254 c[i].ldctl_value.bv_val = NULL;
255 c[i].ldctl_value.bv_len = 0;
256 c[i].ldctl_iscritical = dontUseCopy > 1;
261 tool_server_controls( ld, c, i );
265 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
266 compdn, attrs, sep );
269 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
271 free( bvalue.bv_val );
279 static int docompare(
283 struct berval *bvalue,
285 LDAPControl **sctrls,
286 LDAPControl **cctrls )
298 rc = ldap_compare_ext( ld, dn, attr, bvalue,
299 sctrls, cctrls, &msgid );
310 if ( tool_check_abandon( ld, msgid ) ) {
311 return LDAP_CANCELLED;
314 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
316 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
325 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
327 if( rc != LDAP_SUCCESS ) {
328 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
329 prog, ldap_err2string( rc ), rc );
333 if ( !quiet && ( verbose || ( code != LDAP_SUCCESS && code != LDAP_COMPARE_TRUE && code != LDAP_COMPARE_FALSE )||
334 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) )
336 printf( _("Compare Result: %s (%d)\n"),
337 ldap_err2string( code ), code );
339 if( text && *text ) {
340 printf( _("Additional info: %s\n"), text );
343 if( matcheddn && *matcheddn ) {
344 printf( _("Matched DN: %s\n"), matcheddn );
349 for( i=0; refs[i]; i++ ) {
350 printf(_("Referral: %s\n"), refs[i] );
356 ber_memfree( matcheddn );
357 ber_memvfree( (void **) refs );
359 /* if we were told to be quiet, use the return value. */
361 if ( code == LDAP_COMPARE_TRUE ) {
363 } else if ( code == LDAP_COMPARE_FALSE ) {
364 printf(_("FALSE\n"));
366 printf(_("UNDEFINED\n"));