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 mech SASL mechanism\n"
70 " -Z Start TLS request (-ZZ to require successful response)\n"
76 static int docompare LDAP_P((
80 struct berval *bvalue,
83 LDAPControl **cctrls));
85 static char *prog = NULL;
86 static char *binddn = NULL;
87 static struct berval passwd = { 0, NULL };
88 static char *ldaphost = NULL;
89 static char *ldapuri = NULL;
90 static int ldapport = 0;
91 #ifdef HAVE_CYRUS_SASL
92 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
93 static char *sasl_realm = NULL;
94 static char *sasl_authc_id = NULL;
95 static char *sasl_authz_id = NULL;
96 static char *sasl_mech = NULL;
97 static char *sasl_secprops = NULL;
99 static int use_tls = 0;
100 static int verbose, not;
103 main( int argc, char **argv )
105 char *compdn = NULL, *attrs = NULL;
107 int rc, i, manageDSAit, quiet;
108 int referrals, debug;
109 int authmethod, version, want_bindpw;
111 struct berval bvalue = { 0, NULL };
113 debug = verbose = not = referrals =
114 manageDSAit = want_bindpw = quiet = 0;
120 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
122 while (( i = getopt( argc, argv,
123 "Cd:D:h:H:IkKMnO:p:P:qQR:U:vw:WxX:Y:zZ")) != EOF )
132 debug |= atoi( optarg );
134 case 'D': /* bind DN */
135 if( binddn != NULL ) {
136 fprintf( stderr, "%s: -D previously specified\n", prog );
139 binddn = strdup( optarg );
141 case 'h': /* ldap host */
142 if( ldapuri != NULL ) {
143 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
146 if( ldaphost != NULL ) {
147 fprintf( stderr, "%s: -h previously specified\n", prog );
150 ldaphost = strdup( optarg );
152 case 'H': /* ldap URI */
153 if( ldaphost != NULL ) {
154 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
158 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
161 if( ldapuri != NULL ) {
162 fprintf( stderr, "%s: -H previously specified\n", prog );
165 ldapuri = strdup( optarg );
168 #ifdef HAVE_CYRUS_SASL
169 if( version == LDAP_VERSION2 ) {
170 fprintf( stderr, "%s: -I incompatible with version %d\n",
174 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
175 fprintf( stderr, "%s: incompatible previous "
176 "authentication choice\n",
180 authmethod = LDAP_AUTH_SASL;
181 version = LDAP_VERSION3;
182 sasl_flags = LDAP_SASL_INTERACTIVE;
185 fprintf( stderr, "%s: was not compiled with SASL support\n",
187 return( EXIT_FAILURE );
189 case 'k': /* kerberos bind */
190 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
191 if( version > LDAP_VERSION2 ) {
192 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
197 if( authmethod != -1 ) {
198 fprintf( stderr, "%s: -k incompatible with previous "
199 "authentication choice\n", prog );
203 authmethod = LDAP_AUTH_KRBV4;
205 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
209 case 'K': /* kerberos bind, part one only */
210 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
211 if( version > LDAP_VERSION2 ) {
212 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
216 if( authmethod != -1 ) {
217 fprintf( stderr, "%s: incompatible with previous "
218 "authentication choice\n", prog );
222 authmethod = LDAP_AUTH_KRBV41;
224 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
225 return( EXIT_FAILURE );
229 /* enable Manage DSA IT */
230 if( version == LDAP_VERSION2 ) {
231 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
236 version = LDAP_VERSION3;
238 case 'n': /* print compares, don't actually do them */
242 #ifdef HAVE_CYRUS_SASL
243 if( sasl_secprops != NULL ) {
244 fprintf( stderr, "%s: -O previously specified\n", prog );
247 if( version == LDAP_VERSION2 ) {
248 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
252 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
253 fprintf( stderr, "%s: incompatible previous "
254 "authentication choice\n", prog );
257 authmethod = LDAP_AUTH_SASL;
258 version = LDAP_VERSION3;
259 sasl_secprops = strdup( optarg );
261 fprintf( stderr, "%s: not compiled with SASL support\n",
263 return( EXIT_FAILURE );
268 fprintf( stderr, "%s: -p previously specified\n", prog );
271 ldapport = atoi( optarg );
274 switch( atoi(optarg) ) {
276 if( version == LDAP_VERSION3 ) {
277 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
281 version = LDAP_VERSION2;
284 if( version == LDAP_VERSION2 ) {
285 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
289 version = LDAP_VERSION3;
292 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
295 return( EXIT_FAILURE );
298 #ifdef HAVE_CYRUS_SASL
299 if( version == LDAP_VERSION2 ) {
300 fprintf( stderr, "%s: -Q incompatible with version %d\n",
304 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
305 fprintf( stderr, "%s: incompatible previous "
306 "authentication choice\n",
310 authmethod = LDAP_AUTH_SASL;
311 version = LDAP_VERSION3;
312 sasl_flags = LDAP_SASL_QUIET;
315 fprintf( stderr, "%s: not compiled with SASL support\n",
317 return( EXIT_FAILURE );
320 #ifdef HAVE_CYRUS_SASL
321 if( sasl_realm != NULL ) {
322 fprintf( stderr, "%s: -R previously specified\n", prog );
325 if( version == LDAP_VERSION2 ) {
326 fprintf( stderr, "%s: -R incompatible with version %d\n",
330 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
331 fprintf( stderr, "%s: incompatible previous "
332 "authentication choice\n",
336 authmethod = LDAP_AUTH_SASL;
337 version = LDAP_VERSION3;
338 sasl_realm = strdup( optarg );
340 fprintf( stderr, "%s: not compiled with SASL support\n",
342 return( EXIT_FAILURE );
346 #ifdef HAVE_CYRUS_SASL
347 if( sasl_authc_id != NULL ) {
348 fprintf( stderr, "%s: -U previously specified\n", prog );
351 if( version == LDAP_VERSION2 ) {
352 fprintf( stderr, "%s: -U incompatible with version %d\n",
356 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
357 fprintf( stderr, "%s: incompatible previous "
358 "authentication choice\n",
362 authmethod = LDAP_AUTH_SASL;
363 version = LDAP_VERSION3;
364 sasl_authc_id = strdup( optarg );
366 fprintf( stderr, "%s: not compiled with SASL support\n",
368 return( EXIT_FAILURE );
371 case 'v': /* verbose mode */
374 case 'w': /* password */
375 passwd.bv_val = strdup( optarg );
379 for( p = optarg; *p != '\0'; p++ ) {
383 passwd.bv_len = strlen( passwd.bv_val );
389 #ifdef HAVE_CYRUS_SASL
390 if( sasl_mech != NULL ) {
391 fprintf( stderr, "%s: -Y previously specified\n", prog );
394 if( version == LDAP_VERSION2 ) {
395 fprintf( stderr, "%s: -Y incompatible with version %d\n",
399 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
400 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
403 authmethod = LDAP_AUTH_SASL;
404 version = LDAP_VERSION3;
405 sasl_mech = strdup( optarg );
407 fprintf( stderr, "%s: not compiled with SASL support\n",
409 return( EXIT_FAILURE );
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
414 fprintf( stderr, "%s: incompatible with previous "
415 "authentication choice\n", prog );
418 authmethod = LDAP_AUTH_SIMPLE;
421 #ifdef HAVE_CYRUS_SASL
422 if( sasl_authz_id != NULL ) {
423 fprintf( stderr, "%s: -X previously specified\n", prog );
426 if( version == LDAP_VERSION2 ) {
427 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
431 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
432 fprintf( stderr, "%s: -X incompatible with "
433 "authentication choice\n", prog );
436 authmethod = LDAP_AUTH_SASL;
437 version = LDAP_VERSION3;
438 sasl_authz_id = strdup( optarg );
440 fprintf( stderr, "%s: not compiled with SASL support\n",
442 return( EXIT_FAILURE );
450 if( version == LDAP_VERSION2 ) {
451 fprintf( stderr, "%s: -Z incompatible with version %d\n",
455 version = LDAP_VERSION3;
458 fprintf( stderr, "%s: not compiled with TLS support\n",
460 return( EXIT_FAILURE );
464 fprintf( stderr, "%s: unrecognized option -%c\n",
471 version = LDAP_VERSION3;
473 if (authmethod == -1 && version > LDAP_VERSION2) {
474 #ifdef HAVE_CYRUS_SASL
475 authmethod = LDAP_AUTH_SASL;
477 authmethod = LDAP_AUTH_SIMPLE;
481 if ( argc - optind != 2 ) {
485 compdn = argv[optind++];
486 attrs = argv[optind++];
488 /* user passed in only 2 args, the last one better be in
489 * the form attr:value or attr::b64value
491 sep = strchr(attrs, ':');
498 bvalue.bv_val = strdup( sep );
499 bvalue.bv_len = strlen( bvalue.bv_val );
502 /* it's base64 encoded. */
503 bvalue.bv_val = malloc( strlen( &sep[1] ));
504 bvalue.bv_len = lutil_b64_pton( &sep[1],
505 bvalue.bv_val, strlen( &sep[1] ));
507 if (bvalue.bv_len == -1) {
508 fprintf(stderr, "base64 decode error\n");
514 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
515 != LBER_OPT_SUCCESS )
518 "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
520 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
521 != LDAP_OPT_SUCCESS )
524 "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
530 (void) SIGNAL( SIGPIPE, SIG_IGN );
533 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
535 fprintf( stderr, "ldap_init( %s, %d )\n",
536 ldaphost != NULL ? ldaphost : "<DEFAULT>",
540 ld = ldap_init( ldaphost, ldapport );
542 perror("ldapsearch: ldap_init");
548 fprintf( stderr, "ldap_initialize( %s )\n",
549 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
552 rc = ldap_initialize( &ld, ldapuri );
553 if( rc != LDAP_SUCCESS ) {
555 "Could not create LDAP session handle (%d): %s\n",
556 rc, ldap_err2string(rc) );
563 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
564 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
566 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
567 referrals ? "on" : "off" );
571 if (version == -1 ) {
572 version = LDAP_VERSION3;
575 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
576 != LDAP_OPT_SUCCESS )
578 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
583 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
584 ldap_perror( ld, "ldap_start_tls" );
591 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
592 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
595 if ( authmethod == LDAP_AUTH_SASL ) {
596 #ifdef HAVE_CYRUS_SASL
599 if( sasl_secprops != NULL ) {
600 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
601 (void *) sasl_secprops );
603 if( rc != LDAP_OPT_SUCCESS ) {
605 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
611 defaults = lutil_sasl_defaults( ld,
618 rc = ldap_sasl_interactive_bind_s( ld, binddn,
619 sasl_mech, NULL, NULL,
620 sasl_flags, lutil_sasl_interact, defaults );
622 if( rc != LDAP_SUCCESS ) {
623 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
627 fprintf( stderr, "%s: not compiled with SASL support\n",
632 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
634 ldap_perror( ld, "ldap_bind" );
642 LDAPControl *ctrls[2];
646 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
647 c.ldctl_value.bv_val = NULL;
648 c.ldctl_value.bv_len = 0;
649 c.ldctl_iscritical = manageDSAit > 1;
651 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
653 if( err != LDAP_OPT_SUCCESS ) {
654 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
655 c.ldctl_iscritical ? "critical " : "" );
656 if( c.ldctl_iscritical ) {
663 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
664 compdn, attrs, sep );
667 rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
669 free( bvalue.bv_val );
677 static int docompare(
681 struct berval *bvalue,
683 LDAPControl **sctrls,
684 LDAPControl **cctrls )
692 rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
696 ldap_perror( ld, "ldap_result" );
700 /* if we were told to be quiet, use the return value. */
702 if ( rc == LDAP_COMPARE_TRUE ) {
705 } else if ( rc == LDAP_COMPARE_FALSE ) {
709 ldap_perror( ld, "ldap_compare" );