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 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
53 " [!]manageDSAit (alternate form, see -M)\n"
55 " -h host LDAP server\n"
56 " -H URI LDAP Uniform Resource Indentifier(s)\n"
57 " -I use SASL Interactive mode\n"
58 " -k use Kerberos authentication\n"
59 " -K like -k, but do only step 1 of the Kerberos bind\n"
60 " -M enable Manage DSA IT control (-MM to make critical)\n"
61 " -n show what would be done but don't actually compare\n"
62 " -O props SASL security properties\n"
63 " -p port port on LDAP server\n"
64 " -P version procotol version (default: 3)\n"
65 " -z Quiet mode, don't print anything, use return values\n"
66 " -Q use SASL Quiet mode\n"
67 " -R realm SASL realm\n"
68 " -U authcid SASL authentication identity\n"
69 " -v run in verbose mode (diagnostics to standard output)\n"
70 " -w passwd bind passwd (for simple authentication)\n"
71 " -W prompt for bind passwd\n"
72 " -x Simple authentication\n"
73 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
74 " -y file Read passwd from file\n"
75 " -Y mech SASL mechanism\n"
76 " -Z Start TLS request (-ZZ to require successful response)\n"
82 static int docompare LDAP_P((
86 struct berval *bvalue,
89 LDAPControl **cctrls));
91 static char *prog = NULL;
92 static char *binddn = NULL;
93 static struct berval passwd = { 0, NULL };
94 static char *ldaphost = NULL;
95 static char *ldapuri = NULL;
96 static int ldapport = 0;
97 #ifdef HAVE_CYRUS_SASL
98 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
99 static char *sasl_realm = NULL;
100 static char *sasl_authc_id = NULL;
101 static char *sasl_authz_id = NULL;
102 static char *sasl_mech = NULL;
103 static char *sasl_secprops = NULL;
105 static int use_tls = 0;
106 static int verbose, not;
109 main( int argc, char **argv )
111 char *compdn = NULL, *attrs = NULL;
113 int rc, i, crit, manageDSAit, noop, quiet;
114 int referrals, debug;
115 int authmethod, version, want_bindpw;
117 struct berval bvalue = { 0, NULL };
118 char *pw_file = NULL;
119 char *control, *cvalue;
120 char *authzid = NULL;
122 debug = verbose = not = referrals = noop =
123 manageDSAit = want_bindpw = quiet = 0;
129 prog = lutil_progname( "ldapcompare", argc, argv );
131 while (( i = getopt( argc, argv,
132 "Cd:D:e:h:H:IkKMnO:p:P:qQR:U:vw:WxX:y:Y:zZ")) != EOF )
136 case 'E': /* compare controls */
137 if( version == LDAP_VERSION2 ) {
138 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
143 /* should be extended to support comma separated list of
144 * [!]key[=value] parameters, e.g. -E !foo,bar=567
149 if( optarg[0] == '!' ) {
154 control = strdup( optarg );
155 if ( (cvalue = strchr( control, '=' )) != NULL ) {
158 fprintf( stderr, "Invalid compare control name: %s\n", control );
168 debug |= atoi( optarg );
170 case 'D': /* bind DN */
171 if( binddn != NULL ) {
172 fprintf( stderr, "%s: -D previously specified\n", prog );
175 binddn = strdup( optarg );
178 case 'e': /* general controls */
179 if( version == LDAP_VERSION2 ) {
180 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
185 /* should be extended to support comma separated list of
186 * [!]key[=value] parameters, e.g. -e !foo,bar=567
191 if( optarg[0] == '!' ) {
196 control = strdup( optarg );
197 if ( (cvalue = strchr( control, '=' )) != NULL ) {
201 if ( strcasecmp( control, "authzid" ) == 0 ) {
202 if( authzid != NULL ) {
203 fprintf( stderr, "authzid control previously specified");
206 if( cvalue == NULL ) {
207 fprintf( stderr, "authzid: control value expected" );
212 fprintf( stderr, "authzid: must be marked critical" );
217 assert( authzid == NULL );
221 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
223 fprintf( stderr, "manageDSAit control previously specified");
226 if( cvalue != NULL ) {
227 fprintf( stderr, "manageDSAit: no control value expected" );
232 manageDSAit = 1 + crit;
235 } else if ( strcasecmp( control, "noop" ) == 0 ) {
237 fprintf( stderr, "noop control previously specified");
240 if( cvalue != NULL ) {
241 fprintf( stderr, "noop: no control value expected" );
250 fprintf( stderr, "Invalid general control name: %s\n", control );
254 case 'h': /* ldap host */
255 if( ldapuri != NULL ) {
256 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
259 if( ldaphost != NULL ) {
260 fprintf( stderr, "%s: -h previously specified\n", prog );
263 ldaphost = strdup( optarg );
265 case 'H': /* ldap URI */
266 if( ldaphost != NULL ) {
267 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
271 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
274 if( ldapuri != NULL ) {
275 fprintf( stderr, "%s: -H previously specified\n", prog );
278 ldapuri = strdup( optarg );
281 #ifdef HAVE_CYRUS_SASL
282 if( version == LDAP_VERSION2 ) {
283 fprintf( stderr, "%s: -I incompatible with version %d\n",
287 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
288 fprintf( stderr, "%s: incompatible previous "
289 "authentication choice\n",
293 authmethod = LDAP_AUTH_SASL;
294 version = LDAP_VERSION3;
295 sasl_flags = LDAP_SASL_INTERACTIVE;
298 fprintf( stderr, "%s: was not compiled with SASL support\n",
300 return( EXIT_FAILURE );
302 case 'k': /* kerberos bind */
303 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
304 if( version > LDAP_VERSION2 ) {
305 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
310 if( authmethod != -1 ) {
311 fprintf( stderr, "%s: -k incompatible with previous "
312 "authentication choice\n", prog );
316 authmethod = LDAP_AUTH_KRBV4;
318 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
322 case 'K': /* kerberos bind, part one only */
323 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
324 if( version > LDAP_VERSION2 ) {
325 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
329 if( authmethod != -1 ) {
330 fprintf( stderr, "%s: incompatible with previous "
331 "authentication choice\n", prog );
335 authmethod = LDAP_AUTH_KRBV41;
337 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
338 return( EXIT_FAILURE );
342 /* enable Manage DSA IT */
343 if( version == LDAP_VERSION2 ) {
344 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
349 version = LDAP_VERSION3;
351 case 'n': /* print compares, don't actually do them */
355 #ifdef HAVE_CYRUS_SASL
356 if( sasl_secprops != NULL ) {
357 fprintf( stderr, "%s: -O previously specified\n", prog );
360 if( version == LDAP_VERSION2 ) {
361 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
365 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
366 fprintf( stderr, "%s: incompatible previous "
367 "authentication choice\n", prog );
370 authmethod = LDAP_AUTH_SASL;
371 version = LDAP_VERSION3;
372 sasl_secprops = strdup( optarg );
374 fprintf( stderr, "%s: not compiled with SASL support\n",
376 return( EXIT_FAILURE );
381 fprintf( stderr, "%s: -p previously specified\n", prog );
384 ldapport = atoi( optarg );
387 switch( atoi(optarg) ) {
389 if( version == LDAP_VERSION3 ) {
390 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
394 version = LDAP_VERSION2;
397 if( version == LDAP_VERSION2 ) {
398 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
402 version = LDAP_VERSION3;
405 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
408 return( EXIT_FAILURE );
411 #ifdef HAVE_CYRUS_SASL
412 if( version == LDAP_VERSION2 ) {
413 fprintf( stderr, "%s: -Q incompatible with version %d\n",
417 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
418 fprintf( stderr, "%s: incompatible previous "
419 "authentication choice\n",
423 authmethod = LDAP_AUTH_SASL;
424 version = LDAP_VERSION3;
425 sasl_flags = LDAP_SASL_QUIET;
428 fprintf( stderr, "%s: not compiled with SASL support\n",
430 return( EXIT_FAILURE );
433 #ifdef HAVE_CYRUS_SASL
434 if( sasl_realm != NULL ) {
435 fprintf( stderr, "%s: -R previously specified\n", prog );
438 if( version == LDAP_VERSION2 ) {
439 fprintf( stderr, "%s: -R incompatible with version %d\n",
443 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
444 fprintf( stderr, "%s: incompatible previous "
445 "authentication choice\n",
449 authmethod = LDAP_AUTH_SASL;
450 version = LDAP_VERSION3;
451 sasl_realm = strdup( optarg );
453 fprintf( stderr, "%s: not compiled with SASL support\n",
455 return( EXIT_FAILURE );
459 #ifdef HAVE_CYRUS_SASL
460 if( sasl_authc_id != NULL ) {
461 fprintf( stderr, "%s: -U previously specified\n", prog );
464 if( version == LDAP_VERSION2 ) {
465 fprintf( stderr, "%s: -U incompatible with version %d\n",
469 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
470 fprintf( stderr, "%s: incompatible previous "
471 "authentication choice\n",
475 authmethod = LDAP_AUTH_SASL;
476 version = LDAP_VERSION3;
477 sasl_authc_id = strdup( optarg );
479 fprintf( stderr, "%s: not compiled with SASL support\n",
481 return( EXIT_FAILURE );
484 case 'v': /* verbose mode */
487 case 'w': /* password */
488 passwd.bv_val = strdup( optarg );
492 for( p = optarg; *p != '\0'; p++ ) {
496 passwd.bv_len = strlen( passwd.bv_val );
505 #ifdef HAVE_CYRUS_SASL
506 if( sasl_mech != NULL ) {
507 fprintf( stderr, "%s: -Y previously specified\n", prog );
510 if( version == LDAP_VERSION2 ) {
511 fprintf( stderr, "%s: -Y incompatible with version %d\n",
515 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
516 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
519 authmethod = LDAP_AUTH_SASL;
520 version = LDAP_VERSION3;
521 sasl_mech = strdup( optarg );
523 fprintf( stderr, "%s: not compiled with SASL support\n",
525 return( EXIT_FAILURE );
529 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
530 fprintf( stderr, "%s: incompatible with previous "
531 "authentication choice\n", prog );
534 authmethod = LDAP_AUTH_SIMPLE;
537 #ifdef HAVE_CYRUS_SASL
538 if( sasl_authz_id != NULL ) {
539 fprintf( stderr, "%s: -X previously specified\n", prog );
542 if( version == LDAP_VERSION2 ) {
543 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
547 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
548 fprintf( stderr, "%s: -X incompatible with "
549 "authentication choice\n", prog );
552 authmethod = LDAP_AUTH_SASL;
553 version = LDAP_VERSION3;
554 sasl_authz_id = strdup( optarg );
556 fprintf( stderr, "%s: not compiled with SASL support\n",
558 return( EXIT_FAILURE );
566 if( version == LDAP_VERSION2 ) {
567 fprintf( stderr, "%s: -Z incompatible with version %d\n",
571 version = LDAP_VERSION3;
574 fprintf( stderr, "%s: not compiled with TLS support\n",
576 return( EXIT_FAILURE );
580 fprintf( stderr, "%s: unrecognized option -%c\n",
587 version = LDAP_VERSION3;
589 if (authmethod == -1 && version > LDAP_VERSION2) {
590 #ifdef HAVE_CYRUS_SASL
591 authmethod = LDAP_AUTH_SASL;
593 authmethod = LDAP_AUTH_SIMPLE;
597 if ( argc - optind != 2 ) {
601 compdn = argv[optind++];
602 attrs = argv[optind++];
604 /* user passed in only 2 args, the last one better be in
605 * the form attr:value or attr::b64value
607 sep = strchr(attrs, ':');
614 bvalue.bv_val = strdup( sep );
615 bvalue.bv_len = strlen( bvalue.bv_val );
618 /* it's base64 encoded. */
619 bvalue.bv_val = malloc( strlen( &sep[1] ));
620 bvalue.bv_len = lutil_b64_pton( &sep[1],
621 bvalue.bv_val, strlen( &sep[1] ));
623 if (bvalue.bv_len == -1) {
624 fprintf(stderr, "base64 decode error\n");
630 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
631 != LBER_OPT_SUCCESS )
634 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
636 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
637 != LDAP_OPT_SUCCESS )
640 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
646 (void) SIGNAL( SIGPIPE, SIG_IGN );
649 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
651 fprintf( stderr, "ldap_init( %s, %d )\n",
652 ldaphost != NULL ? ldaphost : "<DEFAULT>",
656 ld = ldap_init( ldaphost, ldapport );
658 perror("ldapcompare: ldap_init");
664 fprintf( stderr, "ldap_initialize( %s )\n",
665 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
668 rc = ldap_initialize( &ld, ldapuri );
669 if( rc != LDAP_SUCCESS ) {
671 "Could not create LDAP session handle (%d): %s\n",
672 rc, ldap_err2string(rc) );
679 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
680 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
682 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
683 referrals ? "on" : "off" );
687 if (version == -1 ) {
688 version = LDAP_VERSION3;
691 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
692 != LDAP_OPT_SUCCESS )
694 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
699 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
700 ldap_perror( ld, "ldap_start_tls" );
706 if ( pw_file || want_bindpw ) {
708 rc = lutil_get_filed_password( pw_file, &passwd );
709 if( rc ) return EXIT_FAILURE;
711 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
712 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
716 if ( authmethod == LDAP_AUTH_SASL ) {
717 #ifdef HAVE_CYRUS_SASL
720 if( sasl_secprops != NULL ) {
721 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
722 (void *) sasl_secprops );
724 if( rc != LDAP_OPT_SUCCESS ) {
726 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
732 defaults = lutil_sasl_defaults( ld,
739 rc = ldap_sasl_interactive_bind_s( ld, binddn,
740 sasl_mech, NULL, NULL,
741 sasl_flags, lutil_sasl_interact, defaults );
743 if( rc != LDAP_SUCCESS ) {
744 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
748 fprintf( stderr, "%s: not compiled with SASL support\n",
753 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
755 ldap_perror( ld, "ldap_bind" );
760 if ( authzid || manageDSAit || noop ) {
761 int err, crit=0, i=0;
763 LDAPControl *ctrls[4];
766 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
767 c[i].ldctl_value.bv_val = authzid;
768 c[i].ldctl_value.bv_len = strlen( authzid );
769 c[i].ldctl_iscritical = 1;
771 if( c[i].ldctl_iscritical ) crit++;
777 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
778 c[i].ldctl_value.bv_val = NULL;
779 c[i].ldctl_value.bv_len = 0;
780 c[i].ldctl_iscritical = manageDSAit > 1;
782 if( c[i].ldctl_iscritical ) crit++;
788 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
789 c[i].ldctl_value.bv_val = NULL;
790 c[i].ldctl_value.bv_len = 0;
791 c[i].ldctl_iscritical = noop > 1;
793 if( c[i].ldctl_iscritical ) crit++;
798 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
800 if( err != LDAP_OPT_SUCCESS ) {
801 fprintf( stderr, "Could not set %scontrols\n",
802 crit ? "critical " : "" );
810 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
811 compdn, attrs, sep );
814 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
816 free( bvalue.bv_val );
824 static int docompare(
828 struct berval *bvalue,
830 LDAPControl **sctrls,
831 LDAPControl **cctrls )
839 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
843 ldap_perror( ld, "ldap_result" );
847 /* if we were told to be quiet, use the return value. */
849 if ( rc == LDAP_COMPARE_TRUE ) {
852 } else if ( rc == LDAP_COMPARE_FALSE ) {
856 ldap_perror( ld, "ldap_compare" );