2 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
34 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
38 usage( const char *s )
41 "usage: %s [options] DN <attr:value|attr::b64value>\n"
43 " DN\tDistinguished Name\n"
44 " attr\tassertion attribute\n"
45 " value\tassertion value\n"
46 " b64value\tbase64 encoding of assertion value\n"
49 " -d level set LDAP debugging level to `level'\n"
50 " -D binddn bind DN\n"
51 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
52 " [!]manageDSAit (alternate form, see -M)\n"
54 " -h host LDAP server\n"
55 " -H URI LDAP Uniform Resource Indentifier(s)\n"
56 " -I use SASL Interactive mode\n"
57 " -k use Kerberos authentication\n"
58 " -K like -k, but do only step 1 of the Kerberos bind\n"
59 " -M enable Manage DSA IT control (-MM to make critical)\n"
60 " -n show what would be done but don't actually compare\n"
61 " -O props SASL security properties\n"
62 " -p port port on LDAP server\n"
63 " -P version procotol version (default: 3)\n"
64 " -z Quiet mode, don't print anything, use return values\n"
65 " -Q use SASL Quiet mode\n"
66 " -R realm SASL realm\n"
67 " -U authcid SASL authentication identity\n"
68 " -v run in verbose mode (diagnostics to standard output)\n"
69 " -w passwd bind passwd (for simple authentication)\n"
70 " -W prompt for bind passwd\n"
71 " -x Simple authentication\n"
72 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
73 " -y file Read passwd from file\n"
74 " -Y mech SASL mechanism\n"
75 " -Z Start TLS request (-ZZ to require successful response)\n"
81 static int docompare LDAP_P((
85 struct berval *bvalue,
88 LDAPControl **cctrls));
90 static char *prog = NULL;
91 static char *binddn = NULL;
92 static struct berval passwd = { 0, NULL };
93 static char *ldaphost = NULL;
94 static char *ldapuri = NULL;
95 static int ldapport = 0;
96 #ifdef HAVE_CYRUS_SASL
97 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
98 static char *sasl_realm = NULL;
99 static char *sasl_authc_id = NULL;
100 static char *sasl_authz_id = NULL;
101 static char *sasl_mech = NULL;
102 static char *sasl_secprops = NULL;
104 static int use_tls = 0;
105 static int verbose, not;
108 main( int argc, char **argv )
110 char *compdn = NULL, *attrs = NULL;
112 int rc, i, crit, manageDSAit, noop, quiet;
113 int referrals, debug;
114 int authmethod, version, want_bindpw;
116 struct berval bvalue = { 0, NULL };
117 char *pw_file = NULL;
118 char *control, *cvalue;
120 debug = verbose = not = referrals = noop =
121 manageDSAit = want_bindpw = quiet = 0;
127 prog = lutil_progname( "ldapcompare", argc, argv );
129 while (( i = getopt( argc, argv,
130 "Cd:D:e:h:H:IkKMnO:p:P:qQR:U:vw:WxX:y:Y:zZ")) != EOF )
134 case 'E': /* compare controls */
135 if( version == LDAP_VERSION2 ) {
136 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
141 /* should be extended to support comma separated list of
142 * [!]key[=value] parameters, e.g. -E !foo,bar=567
147 if( optarg[0] == '!' ) {
152 control = strdup( optarg );
153 if ( (cvalue = strchr( control, '=' )) != NULL ) {
156 fprintf( stderr, "Invalid compare control name: %s\n", control );
166 debug |= atoi( optarg );
168 case 'D': /* bind DN */
169 if( binddn != NULL ) {
170 fprintf( stderr, "%s: -D previously specified\n", prog );
173 binddn = strdup( optarg );
176 case 'e': /* general controls */
177 if( version == LDAP_VERSION2 ) {
178 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
183 /* should be extended to support comma separated list of
184 * [!]key[=value] parameters, e.g. -e !foo,bar=567
189 if( optarg[0] == '!' ) {
194 control = strdup( optarg );
195 if ( (cvalue = strchr( control, '=' )) != NULL ) {
199 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
201 fprintf( stderr, "manageDSAit control previously specified");
204 if( cvalue != NULL ) {
205 fprintf( stderr, "manageDSAit: no control value expected" );
210 manageDSAit = 1 + crit;
214 } else if ( strcasecmp( control, "noop" ) == 0 ) {
216 fprintf( stderr, "noop control previously specified");
219 if( cvalue != NULL ) {
220 fprintf( stderr, "noop: no control value expected" );
230 fprintf( stderr, "Invalid general control name: %s\n", control );
234 case 'h': /* ldap host */
235 if( ldapuri != NULL ) {
236 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
239 if( ldaphost != NULL ) {
240 fprintf( stderr, "%s: -h previously specified\n", prog );
243 ldaphost = strdup( optarg );
245 case 'H': /* ldap URI */
246 if( ldaphost != NULL ) {
247 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
251 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
254 if( ldapuri != NULL ) {
255 fprintf( stderr, "%s: -H previously specified\n", prog );
258 ldapuri = strdup( optarg );
261 #ifdef HAVE_CYRUS_SASL
262 if( version == LDAP_VERSION2 ) {
263 fprintf( stderr, "%s: -I incompatible with version %d\n",
267 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
268 fprintf( stderr, "%s: incompatible previous "
269 "authentication choice\n",
273 authmethod = LDAP_AUTH_SASL;
274 version = LDAP_VERSION3;
275 sasl_flags = LDAP_SASL_INTERACTIVE;
278 fprintf( stderr, "%s: was not compiled with SASL support\n",
280 return( EXIT_FAILURE );
282 case 'k': /* kerberos bind */
283 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
284 if( version > LDAP_VERSION2 ) {
285 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
290 if( authmethod != -1 ) {
291 fprintf( stderr, "%s: -k incompatible with previous "
292 "authentication choice\n", prog );
296 authmethod = LDAP_AUTH_KRBV4;
298 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
302 case 'K': /* kerberos bind, part one only */
303 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
304 if( version > LDAP_VERSION2 ) {
305 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
309 if( authmethod != -1 ) {
310 fprintf( stderr, "%s: incompatible with previous "
311 "authentication choice\n", prog );
315 authmethod = LDAP_AUTH_KRBV41;
317 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
318 return( EXIT_FAILURE );
322 /* enable Manage DSA IT */
323 if( version == LDAP_VERSION2 ) {
324 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
329 version = LDAP_VERSION3;
331 case 'n': /* print compares, don't actually do them */
335 #ifdef HAVE_CYRUS_SASL
336 if( sasl_secprops != NULL ) {
337 fprintf( stderr, "%s: -O previously specified\n", prog );
340 if( version == LDAP_VERSION2 ) {
341 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
345 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
346 fprintf( stderr, "%s: incompatible previous "
347 "authentication choice\n", prog );
350 authmethod = LDAP_AUTH_SASL;
351 version = LDAP_VERSION3;
352 sasl_secprops = strdup( optarg );
354 fprintf( stderr, "%s: not compiled with SASL support\n",
356 return( EXIT_FAILURE );
361 fprintf( stderr, "%s: -p previously specified\n", prog );
364 ldapport = atoi( optarg );
367 switch( atoi(optarg) ) {
369 if( version == LDAP_VERSION3 ) {
370 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
374 version = LDAP_VERSION2;
377 if( version == LDAP_VERSION2 ) {
378 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
382 version = LDAP_VERSION3;
385 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
388 return( EXIT_FAILURE );
391 #ifdef HAVE_CYRUS_SASL
392 if( version == LDAP_VERSION2 ) {
393 fprintf( stderr, "%s: -Q incompatible with version %d\n",
397 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
398 fprintf( stderr, "%s: incompatible previous "
399 "authentication choice\n",
403 authmethod = LDAP_AUTH_SASL;
404 version = LDAP_VERSION3;
405 sasl_flags = LDAP_SASL_QUIET;
408 fprintf( stderr, "%s: not compiled with SASL support\n",
410 return( EXIT_FAILURE );
413 #ifdef HAVE_CYRUS_SASL
414 if( sasl_realm != NULL ) {
415 fprintf( stderr, "%s: -R previously specified\n", prog );
418 if( version == LDAP_VERSION2 ) {
419 fprintf( stderr, "%s: -R incompatible with version %d\n",
423 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
424 fprintf( stderr, "%s: incompatible previous "
425 "authentication choice\n",
429 authmethod = LDAP_AUTH_SASL;
430 version = LDAP_VERSION3;
431 sasl_realm = strdup( optarg );
433 fprintf( stderr, "%s: not compiled with SASL support\n",
435 return( EXIT_FAILURE );
439 #ifdef HAVE_CYRUS_SASL
440 if( sasl_authc_id != NULL ) {
441 fprintf( stderr, "%s: -U previously specified\n", prog );
444 if( version == LDAP_VERSION2 ) {
445 fprintf( stderr, "%s: -U incompatible with version %d\n",
449 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
450 fprintf( stderr, "%s: incompatible previous "
451 "authentication choice\n",
455 authmethod = LDAP_AUTH_SASL;
456 version = LDAP_VERSION3;
457 sasl_authc_id = strdup( optarg );
459 fprintf( stderr, "%s: not compiled with SASL support\n",
461 return( EXIT_FAILURE );
464 case 'v': /* verbose mode */
467 case 'w': /* password */
468 passwd.bv_val = strdup( optarg );
472 for( p = optarg; *p != '\0'; p++ ) {
476 passwd.bv_len = strlen( passwd.bv_val );
485 #ifdef HAVE_CYRUS_SASL
486 if( sasl_mech != NULL ) {
487 fprintf( stderr, "%s: -Y previously specified\n", prog );
490 if( version == LDAP_VERSION2 ) {
491 fprintf( stderr, "%s: -Y incompatible with version %d\n",
495 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
496 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
499 authmethod = LDAP_AUTH_SASL;
500 version = LDAP_VERSION3;
501 sasl_mech = strdup( optarg );
503 fprintf( stderr, "%s: not compiled with SASL support\n",
505 return( EXIT_FAILURE );
509 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
510 fprintf( stderr, "%s: incompatible with previous "
511 "authentication choice\n", prog );
514 authmethod = LDAP_AUTH_SIMPLE;
517 #ifdef HAVE_CYRUS_SASL
518 if( sasl_authz_id != NULL ) {
519 fprintf( stderr, "%s: -X previously specified\n", prog );
522 if( version == LDAP_VERSION2 ) {
523 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
527 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
528 fprintf( stderr, "%s: -X incompatible with "
529 "authentication choice\n", prog );
532 authmethod = LDAP_AUTH_SASL;
533 version = LDAP_VERSION3;
534 sasl_authz_id = strdup( optarg );
536 fprintf( stderr, "%s: not compiled with SASL support\n",
538 return( EXIT_FAILURE );
546 if( version == LDAP_VERSION2 ) {
547 fprintf( stderr, "%s: -Z incompatible with version %d\n",
551 version = LDAP_VERSION3;
554 fprintf( stderr, "%s: not compiled with TLS support\n",
556 return( EXIT_FAILURE );
560 fprintf( stderr, "%s: unrecognized option -%c\n",
567 version = LDAP_VERSION3;
569 if (authmethod == -1 && version > LDAP_VERSION2) {
570 #ifdef HAVE_CYRUS_SASL
571 authmethod = LDAP_AUTH_SASL;
573 authmethod = LDAP_AUTH_SIMPLE;
577 if ( argc - optind != 2 ) {
581 compdn = argv[optind++];
582 attrs = argv[optind++];
584 /* user passed in only 2 args, the last one better be in
585 * the form attr:value or attr::b64value
587 sep = strchr(attrs, ':');
594 bvalue.bv_val = strdup( sep );
595 bvalue.bv_len = strlen( bvalue.bv_val );
598 /* it's base64 encoded. */
599 bvalue.bv_val = malloc( strlen( &sep[1] ));
600 bvalue.bv_len = lutil_b64_pton( &sep[1],
601 bvalue.bv_val, strlen( &sep[1] ));
603 if (bvalue.bv_len == -1) {
604 fprintf(stderr, "base64 decode error\n");
610 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
611 != LBER_OPT_SUCCESS )
614 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
616 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
617 != LDAP_OPT_SUCCESS )
620 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
626 (void) SIGNAL( SIGPIPE, SIG_IGN );
629 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
631 fprintf( stderr, "ldap_init( %s, %d )\n",
632 ldaphost != NULL ? ldaphost : "<DEFAULT>",
636 ld = ldap_init( ldaphost, ldapport );
638 perror("ldapcompare: ldap_init");
644 fprintf( stderr, "ldap_initialize( %s )\n",
645 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
648 rc = ldap_initialize( &ld, ldapuri );
649 if( rc != LDAP_SUCCESS ) {
651 "Could not create LDAP session handle (%d): %s\n",
652 rc, ldap_err2string(rc) );
659 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
660 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
662 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
663 referrals ? "on" : "off" );
667 if (version == -1 ) {
668 version = LDAP_VERSION3;
671 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
672 != LDAP_OPT_SUCCESS )
674 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
679 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
680 ldap_perror( ld, "ldap_start_tls" );
686 if ( pw_file || want_bindpw ) {
688 rc = lutil_get_filed_password( pw_file, &passwd );
689 if( rc ) return EXIT_FAILURE;
691 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
692 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
696 if ( authmethod == LDAP_AUTH_SASL ) {
697 #ifdef HAVE_CYRUS_SASL
700 if( sasl_secprops != NULL ) {
701 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
702 (void *) sasl_secprops );
704 if( rc != LDAP_OPT_SUCCESS ) {
706 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
712 defaults = lutil_sasl_defaults( ld,
719 rc = ldap_sasl_interactive_bind_s( ld, binddn,
720 sasl_mech, NULL, NULL,
721 sasl_flags, lutil_sasl_interact, defaults );
723 if( rc != LDAP_SUCCESS ) {
724 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
728 fprintf( stderr, "%s: not compiled with SASL support\n",
733 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
735 ldap_perror( ld, "ldap_bind" );
740 if ( manageDSAit || noop ) {
743 LDAPControl *ctrls[3];
748 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
749 c1.ldctl_value.bv_val = NULL;
750 c1.ldctl_value.bv_len = 0;
751 c1.ldctl_iscritical = manageDSAit > 1;
758 c2.ldctl_oid = LDAP_CONTROL_NOOP;
759 c2.ldctl_value.bv_val = NULL;
760 c2.ldctl_value.bv_len = 0;
761 c2.ldctl_iscritical = noop > 1;
764 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
766 if( err != LDAP_OPT_SUCCESS ) {
767 fprintf( stderr, "Could not set %scontrols\n",
768 (c1.ldctl_iscritical || c2.ldctl_iscritical)
769 ? "critical " : "" );
770 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
777 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
778 compdn, attrs, sep );
781 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
783 free( bvalue.bv_val );
791 static int docompare(
795 struct berval *bvalue,
797 LDAPControl **sctrls,
798 LDAPControl **cctrls )
806 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
810 ldap_perror( ld, "ldap_result" );
814 /* if we were told to be quiet, use the return value. */
816 if ( rc == LDAP_COMPARE_TRUE ) {
819 } else if ( rc == LDAP_COMPARE_FALSE ) {
823 ldap_perror( ld, "ldap_compare" );