2 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
13 #include <ac/signal.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"
37 usage( const char *s )
40 "usage: %s [options] DN <attr:value|attr::b64value>\n"
42 " DN\tDistinguished Name\n"
43 " attr\tassertion attribute\n"
44 " value\tassertion value\n"
45 " b64value\tbase64 encoding of assertion value\n"
48 " -d level set LDAP debugging level to `level'\n"
49 " -D binddn bind DN\n"
50 " -h host LDAP server\n"
51 " -H URI LDAP Uniform Resource Indentifier(s)\n"
52 " -I use SASL Interactive mode\n"
53 " -k use Kerberos authentication\n"
54 " -K like -k, but do only step 1 of the Kerberos bind\n"
55 " -M enable Manage DSA IT control (-MM to make critical)\n"
56 " -n show what would be done but don't actually compare\n"
57 " -O props SASL security properties\n"
58 " -p port port on LDAP server\n"
59 " -P version procotol version (default: 3)\n"
60 " -z Quiet mode, don't print anything, use return values\n"
61 " -Q use SASL Quiet mode\n"
62 " -R realm SASL realm\n"
63 " -U authcid SASL authentication identity\n"
64 " -v run in verbose mode (diagnostics to standard output)\n"
65 " -w passwd bind passwd (for simple authentication)\n"
66 " -W prompt for bind passwd\n"
67 " -x Simple authentication\n"
68 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
69 " -y file Read passwd from file\n"
70 " -Y mech SASL mechanism\n"
71 " -Z Start TLS request (-ZZ to require successful response)\n"
77 static int docompare LDAP_P((
81 struct berval *bvalue,
84 LDAPControl **cctrls));
86 static char *prog = NULL;
87 static char *binddn = NULL;
88 static struct berval passwd = { 0, NULL };
89 static char *ldaphost = NULL;
90 static char *ldapuri = NULL;
91 static int ldapport = 0;
92 #ifdef HAVE_CYRUS_SASL
93 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
94 static char *sasl_realm = NULL;
95 static char *sasl_authc_id = NULL;
96 static char *sasl_authz_id = NULL;
97 static char *sasl_mech = NULL;
98 static char *sasl_secprops = NULL;
100 static int use_tls = 0;
101 static int verbose, not;
104 main( int argc, char **argv )
106 char *compdn = NULL, *attrs = NULL;
108 int rc, i, manageDSAit, quiet;
109 int referrals, debug;
110 int authmethod, version, want_bindpw;
112 struct berval bvalue = { 0, NULL };
113 char *pw_file = NULL;
115 debug = verbose = not = referrals =
116 manageDSAit = want_bindpw = quiet = 0;
122 prog = lutil_progname( "ldapcompare", argc, argv );
124 while (( i = getopt( argc, argv,
125 "Cd:D:h:H:IkKMnO:p:P:qQR:U:vw:WxX:y:Y:zZ")) != EOF )
134 debug |= atoi( optarg );
136 case 'D': /* bind DN */
137 if( binddn != NULL ) {
138 fprintf( stderr, "%s: -D previously specified\n", prog );
141 binddn = strdup( optarg );
143 case 'h': /* ldap host */
144 if( ldapuri != NULL ) {
145 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
148 if( ldaphost != NULL ) {
149 fprintf( stderr, "%s: -h previously specified\n", prog );
152 ldaphost = strdup( optarg );
154 case 'H': /* ldap URI */
155 if( ldaphost != NULL ) {
156 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
160 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
163 if( ldapuri != NULL ) {
164 fprintf( stderr, "%s: -H previously specified\n", prog );
167 ldapuri = strdup( optarg );
170 #ifdef HAVE_CYRUS_SASL
171 if( version == LDAP_VERSION2 ) {
172 fprintf( stderr, "%s: -I incompatible with version %d\n",
176 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
177 fprintf( stderr, "%s: incompatible previous "
178 "authentication choice\n",
182 authmethod = LDAP_AUTH_SASL;
183 version = LDAP_VERSION3;
184 sasl_flags = LDAP_SASL_INTERACTIVE;
187 fprintf( stderr, "%s: was not compiled with SASL support\n",
189 return( EXIT_FAILURE );
191 case 'k': /* kerberos bind */
192 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
193 if( version > LDAP_VERSION2 ) {
194 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
199 if( authmethod != -1 ) {
200 fprintf( stderr, "%s: -k incompatible with previous "
201 "authentication choice\n", prog );
205 authmethod = LDAP_AUTH_KRBV4;
207 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
211 case 'K': /* kerberos bind, part one only */
212 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
213 if( version > LDAP_VERSION2 ) {
214 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
218 if( authmethod != -1 ) {
219 fprintf( stderr, "%s: incompatible with previous "
220 "authentication choice\n", prog );
224 authmethod = LDAP_AUTH_KRBV41;
226 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
227 return( EXIT_FAILURE );
231 /* enable Manage DSA IT */
232 if( version == LDAP_VERSION2 ) {
233 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
238 version = LDAP_VERSION3;
240 case 'n': /* print compares, don't actually do them */
244 #ifdef HAVE_CYRUS_SASL
245 if( sasl_secprops != NULL ) {
246 fprintf( stderr, "%s: -O previously specified\n", prog );
249 if( version == LDAP_VERSION2 ) {
250 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
254 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
255 fprintf( stderr, "%s: incompatible previous "
256 "authentication choice\n", prog );
259 authmethod = LDAP_AUTH_SASL;
260 version = LDAP_VERSION3;
261 sasl_secprops = strdup( optarg );
263 fprintf( stderr, "%s: not compiled with SASL support\n",
265 return( EXIT_FAILURE );
270 fprintf( stderr, "%s: -p previously specified\n", prog );
273 ldapport = atoi( optarg );
276 switch( atoi(optarg) ) {
278 if( version == LDAP_VERSION3 ) {
279 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
283 version = LDAP_VERSION2;
286 if( version == LDAP_VERSION2 ) {
287 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
291 version = LDAP_VERSION3;
294 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
297 return( EXIT_FAILURE );
300 #ifdef HAVE_CYRUS_SASL
301 if( version == LDAP_VERSION2 ) {
302 fprintf( stderr, "%s: -Q incompatible with version %d\n",
306 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
307 fprintf( stderr, "%s: incompatible previous "
308 "authentication choice\n",
312 authmethod = LDAP_AUTH_SASL;
313 version = LDAP_VERSION3;
314 sasl_flags = LDAP_SASL_QUIET;
317 fprintf( stderr, "%s: not compiled with SASL support\n",
319 return( EXIT_FAILURE );
322 #ifdef HAVE_CYRUS_SASL
323 if( sasl_realm != NULL ) {
324 fprintf( stderr, "%s: -R previously specified\n", prog );
327 if( version == LDAP_VERSION2 ) {
328 fprintf( stderr, "%s: -R incompatible with version %d\n",
332 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
333 fprintf( stderr, "%s: incompatible previous "
334 "authentication choice\n",
338 authmethod = LDAP_AUTH_SASL;
339 version = LDAP_VERSION3;
340 sasl_realm = strdup( optarg );
342 fprintf( stderr, "%s: not compiled with SASL support\n",
344 return( EXIT_FAILURE );
348 #ifdef HAVE_CYRUS_SASL
349 if( sasl_authc_id != NULL ) {
350 fprintf( stderr, "%s: -U previously specified\n", prog );
353 if( version == LDAP_VERSION2 ) {
354 fprintf( stderr, "%s: -U incompatible with version %d\n",
358 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
359 fprintf( stderr, "%s: incompatible previous "
360 "authentication choice\n",
364 authmethod = LDAP_AUTH_SASL;
365 version = LDAP_VERSION3;
366 sasl_authc_id = strdup( optarg );
368 fprintf( stderr, "%s: not compiled with SASL support\n",
370 return( EXIT_FAILURE );
373 case 'v': /* verbose mode */
376 case 'w': /* password */
377 passwd.bv_val = strdup( optarg );
381 for( p = optarg; *p != '\0'; p++ ) {
385 passwd.bv_len = strlen( passwd.bv_val );
394 #ifdef HAVE_CYRUS_SASL
395 if( sasl_mech != NULL ) {
396 fprintf( stderr, "%s: -Y previously specified\n", prog );
399 if( version == LDAP_VERSION2 ) {
400 fprintf( stderr, "%s: -Y incompatible with version %d\n",
404 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
405 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
408 authmethod = LDAP_AUTH_SASL;
409 version = LDAP_VERSION3;
410 sasl_mech = strdup( optarg );
412 fprintf( stderr, "%s: not compiled with SASL support\n",
414 return( EXIT_FAILURE );
418 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
419 fprintf( stderr, "%s: incompatible with previous "
420 "authentication choice\n", prog );
423 authmethod = LDAP_AUTH_SIMPLE;
426 #ifdef HAVE_CYRUS_SASL
427 if( sasl_authz_id != NULL ) {
428 fprintf( stderr, "%s: -X previously specified\n", prog );
431 if( version == LDAP_VERSION2 ) {
432 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
436 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
437 fprintf( stderr, "%s: -X incompatible with "
438 "authentication choice\n", prog );
441 authmethod = LDAP_AUTH_SASL;
442 version = LDAP_VERSION3;
443 sasl_authz_id = strdup( optarg );
445 fprintf( stderr, "%s: not compiled with SASL support\n",
447 return( EXIT_FAILURE );
455 if( version == LDAP_VERSION2 ) {
456 fprintf( stderr, "%s: -Z incompatible with version %d\n",
460 version = LDAP_VERSION3;
463 fprintf( stderr, "%s: not compiled with TLS support\n",
465 return( EXIT_FAILURE );
469 fprintf( stderr, "%s: unrecognized option -%c\n",
476 version = LDAP_VERSION3;
478 if (authmethod == -1 && version > LDAP_VERSION2) {
479 #ifdef HAVE_CYRUS_SASL
480 authmethod = LDAP_AUTH_SASL;
482 authmethod = LDAP_AUTH_SIMPLE;
486 if ( argc - optind != 2 ) {
490 compdn = argv[optind++];
491 attrs = argv[optind++];
493 /* user passed in only 2 args, the last one better be in
494 * the form attr:value or attr::b64value
496 sep = strchr(attrs, ':');
503 bvalue.bv_val = strdup( sep );
504 bvalue.bv_len = strlen( bvalue.bv_val );
507 /* it's base64 encoded. */
508 bvalue.bv_val = malloc( strlen( &sep[1] ));
509 bvalue.bv_len = lutil_b64_pton( &sep[1],
510 bvalue.bv_val, strlen( &sep[1] ));
512 if (bvalue.bv_len == -1) {
513 fprintf(stderr, "base64 decode error\n");
519 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
520 != LBER_OPT_SUCCESS )
523 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
525 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
526 != LDAP_OPT_SUCCESS )
529 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
535 (void) SIGNAL( SIGPIPE, SIG_IGN );
538 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
540 fprintf( stderr, "ldap_init( %s, %d )\n",
541 ldaphost != NULL ? ldaphost : "<DEFAULT>",
545 ld = ldap_init( ldaphost, ldapport );
547 perror("ldapcompare: ldap_init");
553 fprintf( stderr, "ldap_initialize( %s )\n",
554 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
557 rc = ldap_initialize( &ld, ldapuri );
558 if( rc != LDAP_SUCCESS ) {
560 "Could not create LDAP session handle (%d): %s\n",
561 rc, ldap_err2string(rc) );
568 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
569 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
571 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
572 referrals ? "on" : "off" );
576 if (version == -1 ) {
577 version = LDAP_VERSION3;
580 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
581 != LDAP_OPT_SUCCESS )
583 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
588 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
589 ldap_perror( ld, "ldap_start_tls" );
595 if ( pw_file || want_bindpw ) {
597 rc = lutil_get_filed_password( pw_file, &passwd );
598 if( rc ) return EXIT_FAILURE;
600 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
601 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
605 if ( authmethod == LDAP_AUTH_SASL ) {
606 #ifdef HAVE_CYRUS_SASL
609 if( sasl_secprops != NULL ) {
610 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
611 (void *) sasl_secprops );
613 if( rc != LDAP_OPT_SUCCESS ) {
615 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
621 defaults = lutil_sasl_defaults( ld,
628 rc = ldap_sasl_interactive_bind_s( ld, binddn,
629 sasl_mech, NULL, NULL,
630 sasl_flags, lutil_sasl_interact, defaults );
632 if( rc != LDAP_SUCCESS ) {
633 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
637 fprintf( stderr, "%s: not compiled with SASL support\n",
642 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
644 ldap_perror( ld, "ldap_bind" );
652 LDAPControl *ctrls[2];
656 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
657 c.ldctl_value.bv_val = NULL;
658 c.ldctl_value.bv_len = 0;
659 c.ldctl_iscritical = manageDSAit > 1;
661 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
663 if( err != LDAP_OPT_SUCCESS ) {
664 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
665 c.ldctl_iscritical ? "critical " : "" );
666 if( c.ldctl_iscritical ) {
673 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
674 compdn, attrs, sep );
677 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
679 free( bvalue.bv_val );
687 static int docompare(
691 struct berval *bvalue,
693 LDAPControl **sctrls,
694 LDAPControl **cctrls )
702 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
706 ldap_perror( ld, "ldap_result" );
710 /* if we were told to be quiet, use the return value. */
712 if ( rc == LDAP_COMPARE_TRUE ) {
715 } else if ( rc == LDAP_COMPARE_FALSE ) {
719 ldap_perror( ld, "ldap_compare" );