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 )
133 case 'E': /* compare controls */
134 if( version == LDAP_VERSION2 ) {
135 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
140 /* should be extended to support comma separated list of
141 * [!]key[=value] parameters, e.g. -E !foo,bar=567
146 if( optarg[0] == '!' ) {
151 control = strdup( optarg );
152 if ( (cvalue = strchr( control, '=' )) != NULL ) {
155 fprintf( stderr, "Invalid compare control name: %s\n", control );
164 debug |= atoi( optarg );
166 case 'D': /* bind DN */
167 if( binddn != NULL ) {
168 fprintf( stderr, "%s: -D previously specified\n", prog );
171 binddn = strdup( optarg );
174 case 'e': /* general controls */
175 if( version == LDAP_VERSION2 ) {
176 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
181 /* should be extended to support comma separated list of
182 * [!]key[=value] parameters, e.g. -e !foo,bar=567
187 if( optarg[0] == '!' ) {
192 control = strdup( optarg );
193 if ( (cvalue = strchr( control, '=' )) != NULL ) {
197 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
198 if( cvalue != NULL ) {
199 fprintf( stderr, "manageDSAit: no control value expected" );
204 manageDSAit = 1 + crit;
208 } else if ( strcasecmp( control, "noop" ) == 0 ) {
209 if( cvalue != NULL ) {
210 fprintf( stderr, "noop: no control value expected" );
220 fprintf( stderr, "Invalid general control name: %s\n", control );
224 case 'h': /* ldap host */
225 if( ldapuri != NULL ) {
226 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
229 if( ldaphost != NULL ) {
230 fprintf( stderr, "%s: -h previously specified\n", prog );
233 ldaphost = strdup( optarg );
235 case 'H': /* ldap URI */
236 if( ldaphost != NULL ) {
237 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
241 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
244 if( ldapuri != NULL ) {
245 fprintf( stderr, "%s: -H previously specified\n", prog );
248 ldapuri = strdup( optarg );
251 #ifdef HAVE_CYRUS_SASL
252 if( version == LDAP_VERSION2 ) {
253 fprintf( stderr, "%s: -I incompatible with version %d\n",
257 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
258 fprintf( stderr, "%s: incompatible previous "
259 "authentication choice\n",
263 authmethod = LDAP_AUTH_SASL;
264 version = LDAP_VERSION3;
265 sasl_flags = LDAP_SASL_INTERACTIVE;
268 fprintf( stderr, "%s: was not compiled with SASL support\n",
270 return( EXIT_FAILURE );
272 case 'k': /* kerberos bind */
273 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
274 if( version > LDAP_VERSION2 ) {
275 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
280 if( authmethod != -1 ) {
281 fprintf( stderr, "%s: -k incompatible with previous "
282 "authentication choice\n", prog );
286 authmethod = LDAP_AUTH_KRBV4;
288 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
292 case 'K': /* kerberos bind, part one only */
293 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
294 if( version > LDAP_VERSION2 ) {
295 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
299 if( authmethod != -1 ) {
300 fprintf( stderr, "%s: incompatible with previous "
301 "authentication choice\n", prog );
305 authmethod = LDAP_AUTH_KRBV41;
307 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
308 return( EXIT_FAILURE );
312 /* enable Manage DSA IT */
313 if( version == LDAP_VERSION2 ) {
314 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
319 version = LDAP_VERSION3;
321 case 'n': /* print compares, don't actually do them */
325 #ifdef HAVE_CYRUS_SASL
326 if( sasl_secprops != NULL ) {
327 fprintf( stderr, "%s: -O previously specified\n", prog );
330 if( version == LDAP_VERSION2 ) {
331 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
335 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
336 fprintf( stderr, "%s: incompatible previous "
337 "authentication choice\n", prog );
340 authmethod = LDAP_AUTH_SASL;
341 version = LDAP_VERSION3;
342 sasl_secprops = strdup( optarg );
344 fprintf( stderr, "%s: not compiled with SASL support\n",
346 return( EXIT_FAILURE );
351 fprintf( stderr, "%s: -p previously specified\n", prog );
354 ldapport = atoi( optarg );
357 switch( atoi(optarg) ) {
359 if( version == LDAP_VERSION3 ) {
360 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
364 version = LDAP_VERSION2;
367 if( version == LDAP_VERSION2 ) {
368 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
372 version = LDAP_VERSION3;
375 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
378 return( EXIT_FAILURE );
381 #ifdef HAVE_CYRUS_SASL
382 if( version == LDAP_VERSION2 ) {
383 fprintf( stderr, "%s: -Q incompatible with version %d\n",
387 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
388 fprintf( stderr, "%s: incompatible previous "
389 "authentication choice\n",
393 authmethod = LDAP_AUTH_SASL;
394 version = LDAP_VERSION3;
395 sasl_flags = LDAP_SASL_QUIET;
398 fprintf( stderr, "%s: not compiled with SASL support\n",
400 return( EXIT_FAILURE );
403 #ifdef HAVE_CYRUS_SASL
404 if( sasl_realm != NULL ) {
405 fprintf( stderr, "%s: -R previously specified\n", prog );
408 if( version == LDAP_VERSION2 ) {
409 fprintf( stderr, "%s: -R incompatible with version %d\n",
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
414 fprintf( stderr, "%s: incompatible previous "
415 "authentication choice\n",
419 authmethod = LDAP_AUTH_SASL;
420 version = LDAP_VERSION3;
421 sasl_realm = strdup( optarg );
423 fprintf( stderr, "%s: not compiled with SASL support\n",
425 return( EXIT_FAILURE );
429 #ifdef HAVE_CYRUS_SASL
430 if( sasl_authc_id != NULL ) {
431 fprintf( stderr, "%s: -U previously specified\n", prog );
434 if( version == LDAP_VERSION2 ) {
435 fprintf( stderr, "%s: -U incompatible with version %d\n",
439 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
440 fprintf( stderr, "%s: incompatible previous "
441 "authentication choice\n",
445 authmethod = LDAP_AUTH_SASL;
446 version = LDAP_VERSION3;
447 sasl_authc_id = strdup( optarg );
449 fprintf( stderr, "%s: not compiled with SASL support\n",
451 return( EXIT_FAILURE );
454 case 'v': /* verbose mode */
457 case 'w': /* password */
458 passwd.bv_val = strdup( optarg );
462 for( p = optarg; *p != '\0'; p++ ) {
466 passwd.bv_len = strlen( passwd.bv_val );
475 #ifdef HAVE_CYRUS_SASL
476 if( sasl_mech != NULL ) {
477 fprintf( stderr, "%s: -Y previously specified\n", prog );
480 if( version == LDAP_VERSION2 ) {
481 fprintf( stderr, "%s: -Y incompatible with version %d\n",
485 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
486 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
489 authmethod = LDAP_AUTH_SASL;
490 version = LDAP_VERSION3;
491 sasl_mech = strdup( optarg );
493 fprintf( stderr, "%s: not compiled with SASL support\n",
495 return( EXIT_FAILURE );
499 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
500 fprintf( stderr, "%s: incompatible with previous "
501 "authentication choice\n", prog );
504 authmethod = LDAP_AUTH_SIMPLE;
507 #ifdef HAVE_CYRUS_SASL
508 if( sasl_authz_id != NULL ) {
509 fprintf( stderr, "%s: -X previously specified\n", prog );
512 if( version == LDAP_VERSION2 ) {
513 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
517 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
518 fprintf( stderr, "%s: -X incompatible with "
519 "authentication choice\n", prog );
522 authmethod = LDAP_AUTH_SASL;
523 version = LDAP_VERSION3;
524 sasl_authz_id = strdup( optarg );
526 fprintf( stderr, "%s: not compiled with SASL support\n",
528 return( EXIT_FAILURE );
536 if( version == LDAP_VERSION2 ) {
537 fprintf( stderr, "%s: -Z incompatible with version %d\n",
541 version = LDAP_VERSION3;
544 fprintf( stderr, "%s: not compiled with TLS support\n",
546 return( EXIT_FAILURE );
550 fprintf( stderr, "%s: unrecognized option -%c\n",
557 version = LDAP_VERSION3;
559 if (authmethod == -1 && version > LDAP_VERSION2) {
560 #ifdef HAVE_CYRUS_SASL
561 authmethod = LDAP_AUTH_SASL;
563 authmethod = LDAP_AUTH_SIMPLE;
567 if ( argc - optind != 2 ) {
571 compdn = argv[optind++];
572 attrs = argv[optind++];
574 /* user passed in only 2 args, the last one better be in
575 * the form attr:value or attr::b64value
577 sep = strchr(attrs, ':');
584 bvalue.bv_val = strdup( sep );
585 bvalue.bv_len = strlen( bvalue.bv_val );
588 /* it's base64 encoded. */
589 bvalue.bv_val = malloc( strlen( &sep[1] ));
590 bvalue.bv_len = lutil_b64_pton( &sep[1],
591 bvalue.bv_val, strlen( &sep[1] ));
593 if (bvalue.bv_len == -1) {
594 fprintf(stderr, "base64 decode error\n");
600 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
601 != LBER_OPT_SUCCESS )
604 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
606 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
607 != LDAP_OPT_SUCCESS )
610 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
616 (void) SIGNAL( SIGPIPE, SIG_IGN );
619 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
621 fprintf( stderr, "ldap_init( %s, %d )\n",
622 ldaphost != NULL ? ldaphost : "<DEFAULT>",
626 ld = ldap_init( ldaphost, ldapport );
628 perror("ldapcompare: ldap_init");
634 fprintf( stderr, "ldap_initialize( %s )\n",
635 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
638 rc = ldap_initialize( &ld, ldapuri );
639 if( rc != LDAP_SUCCESS ) {
641 "Could not create LDAP session handle (%d): %s\n",
642 rc, ldap_err2string(rc) );
649 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
650 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
652 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
653 referrals ? "on" : "off" );
657 if (version == -1 ) {
658 version = LDAP_VERSION3;
661 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
662 != LDAP_OPT_SUCCESS )
664 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
669 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
670 ldap_perror( ld, "ldap_start_tls" );
676 if ( pw_file || want_bindpw ) {
678 rc = lutil_get_filed_password( pw_file, &passwd );
679 if( rc ) return EXIT_FAILURE;
681 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
682 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
686 if ( authmethod == LDAP_AUTH_SASL ) {
687 #ifdef HAVE_CYRUS_SASL
690 if( sasl_secprops != NULL ) {
691 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
692 (void *) sasl_secprops );
694 if( rc != LDAP_OPT_SUCCESS ) {
696 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
702 defaults = lutil_sasl_defaults( ld,
709 rc = ldap_sasl_interactive_bind_s( ld, binddn,
710 sasl_mech, NULL, NULL,
711 sasl_flags, lutil_sasl_interact, defaults );
713 if( rc != LDAP_SUCCESS ) {
714 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
718 fprintf( stderr, "%s: not compiled with SASL support\n",
723 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
725 ldap_perror( ld, "ldap_bind" );
730 if ( manageDSAit || noop ) {
733 LDAPControl *ctrls[3];
738 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
739 c1.ldctl_value.bv_val = NULL;
740 c1.ldctl_value.bv_len = 0;
741 c1.ldctl_iscritical = manageDSAit > 1;
748 c2.ldctl_oid = LDAP_CONTROL_NOOP;
749 c2.ldctl_value.bv_val = NULL;
750 c2.ldctl_value.bv_len = 0;
751 c2.ldctl_iscritical = noop > 1;
754 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
756 if( err != LDAP_OPT_SUCCESS ) {
757 fprintf( stderr, "Could not set %scontrols\n",
758 (c1.ldctl_iscritical || c2.ldctl_iscritical)
759 ? "critical " : "" );
760 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
767 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
768 compdn, attrs, sep );
771 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
773 free( bvalue.bv_val );
781 static int docompare(
785 struct berval *bvalue,
787 LDAPControl **sctrls,
788 LDAPControl **cctrls )
796 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
800 ldap_perror( ld, "ldap_result" );
804 /* if we were told to be quiet, use the return value. */
806 if ( rc == LDAP_COMPARE_TRUE ) {
809 } else if ( rc == LDAP_COMPARE_FALSE ) {
813 ldap_perror( ld, "ldap_compare" );