3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
18 #include <ac/unistd.h>
22 #include "lutil_ldap.h"
23 #include "ldap_defaults.h"
25 static int verbose = 0;
31 "Issue LDAP Who am I? operation to request user's authzid\n\n"
32 "usage: %s [options]\n"
35 " -d level set LDAP debugging level to `level'\n"
36 " -D binddn bind DN\n"
37 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
38 " [!]manageDSAit (alternate form, see -M)\n"
40 " -f file read operations from `file'\n"
41 " -h host LDAP server(s)\n"
42 " -H URI LDAP Uniform Resource Indentifier(s)\n"
43 " -I use SASL Interactive mode\n"
44 " -n show what would be done but don't actually do it\n"
45 " -O props SASL security properties\n"
46 " -p port port on LDAP server\n"
47 " -Q use SASL Quiet mode\n"
48 " -R realm SASL realm\n"
49 " -U authcid SASL authentication identity\n"
50 " -v run in verbose mode (diagnostics to standard output)\n"
51 " -w passwd bind passwd (for simple authentication)\n"
52 " -W prompt for bind passwd\n"
53 " -x Simple authentication\n"
54 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
55 " -y file Read passwd from file\n"
56 " -Y mech SASL mechanism\n"
57 " -Z Start TLS request (-ZZ to require successful response)\n"
64 main( int argc, char *argv[] )
68 char *ldaphost = NULL;
74 struct berval passwd = { 0, NULL };
85 #ifdef HAVE_CYRUS_SASL
86 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
87 char *sasl_realm = NULL;
88 char *sasl_authc_id = NULL;
89 char *sasl_authz_id = NULL;
90 char *sasl_mech = NULL;
91 char *sasl_secprops = NULL;
96 int manageDSAit=0, noop=0;
97 char *control, *cvalue;
100 int id, code = LDAP_OTHER;
102 char *matcheddn = NULL, *text = NULL, **refs = NULL;
104 struct berval *retdata = NULL;
106 prog = lutil_progname( "ldapwhoami", argc, argv );
108 while( (i = getopt( argc, argv,
109 "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:y:Y:Z" )) != EOF )
112 case 'E': /* whoami controls */
113 if( version == LDAP_VERSION2 ) {
114 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
119 /* should be extended to support comma separated list of
120 * [!]key[=value] parameters, e.g. -E !foo,bar=567
125 if( optarg[0] == '!' ) {
130 control = strdup( optarg );
131 if ( (cvalue = strchr( control, '=' )) != NULL ) {
134 fprintf( stderr, "Invalid whoami control name: %s\n", control );
138 /* Common Options (including options we don't use) */
143 debug |= atoi( optarg );
145 case 'D': /* bind DN */
146 if( binddn != NULL ) {
147 fprintf( stderr, "%s: -D previously specified\n", prog );
150 binddn = strdup( optarg );
152 case 'e': /* general controls */
153 if( version == LDAP_VERSION2 ) {
154 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
159 /* should be extended to support comma separated list of
160 * [!]key[=value] parameters, e.g. -e !foo,bar=567
165 if( optarg[0] == '!' ) {
170 control = strdup( optarg );
171 if ( (cvalue = strchr( control, '=' )) != NULL ) {
175 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
176 if( cvalue != NULL ) {
177 fprintf( stderr, "manageDSAit: no control value expected" );
182 manageDSAit = 1 + crit;
186 } else if ( strcasecmp( control, "noop" ) == 0 ) {
187 if( cvalue != NULL ) {
188 fprintf( stderr, "noop: no control value expected" );
198 fprintf( stderr, "Invalid general control name: %s\n", control );
202 case 'h': /* ldap host */
203 if( ldapuri != NULL ) {
204 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
207 if( ldaphost != NULL ) {
208 fprintf( stderr, "%s: -h previously specified\n", prog );
211 ldaphost = strdup( optarg );
213 case 'H': /* ldap URI */
214 if( ldaphost != NULL ) {
215 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
219 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
222 if( ldapuri != NULL ) {
223 fprintf( stderr, "%s: -H previously specified\n", prog );
226 ldapuri = strdup( optarg );
229 #ifdef HAVE_CYRUS_SASL
230 if( version == LDAP_VERSION2 ) {
231 fprintf( stderr, "%s: -I incompatible with version %d\n",
235 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
236 fprintf( stderr, "%s: incompatible previous "
237 "authentication choice\n",
241 authmethod = LDAP_AUTH_SASL;
242 version = LDAP_VERSION3;
243 sasl_flags = LDAP_SASL_INTERACTIVE;
246 fprintf( stderr, "%s: was not compiled with SASL support\n",
248 return( EXIT_FAILURE );
250 case 'k': /* kerberos bind */
251 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
252 if( version > LDAP_VERSION2 ) {
253 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
258 if( authmethod != -1 ) {
259 fprintf( stderr, "%s: -k incompatible with previous "
260 "authentication choice\n", prog );
264 authmethod = LDAP_AUTH_KRBV4;
266 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
270 case 'K': /* kerberos bind, part one only */
271 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
272 if( version > LDAP_VERSION2 ) {
273 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
277 if( authmethod != -1 ) {
278 fprintf( stderr, "%s: incompatible with previous "
279 "authentication choice\n", prog );
283 authmethod = LDAP_AUTH_KRBV41;
285 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
286 return( EXIT_FAILURE );
289 case 'n': /* print deletes, don't actually do them */
293 #ifdef HAVE_CYRUS_SASL
294 if( sasl_secprops != NULL ) {
295 fprintf( stderr, "%s: -O previously specified\n", prog );
298 if( version == LDAP_VERSION2 ) {
299 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
303 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
304 fprintf( stderr, "%s: incompatible previous "
305 "authentication choice\n", prog );
308 authmethod = LDAP_AUTH_SASL;
309 version = LDAP_VERSION3;
310 sasl_secprops = strdup( optarg );
312 fprintf( stderr, "%s: not compiled with SASL support\n",
314 return( EXIT_FAILURE );
319 fprintf( stderr, "%s: -p previously specified\n", prog );
322 ldapport = atoi( optarg );
325 switch( atoi(optarg) ) {
327 if( version == LDAP_VERSION3 ) {
328 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
332 version = LDAP_VERSION2;
335 if( version == LDAP_VERSION2 ) {
336 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
340 version = LDAP_VERSION3;
343 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
346 return( EXIT_FAILURE );
349 #ifdef HAVE_CYRUS_SASL
350 if( version == LDAP_VERSION2 ) {
351 fprintf( stderr, "%s: -Q incompatible with version %d\n",
355 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
356 fprintf( stderr, "%s: incompatible previous "
357 "authentication choice\n",
361 authmethod = LDAP_AUTH_SASL;
362 version = LDAP_VERSION3;
363 sasl_flags = LDAP_SASL_QUIET;
366 fprintf( stderr, "%s: not compiled with SASL support\n",
368 return( EXIT_FAILURE );
371 #ifdef HAVE_CYRUS_SASL
372 if( sasl_realm != NULL ) {
373 fprintf( stderr, "%s: -R previously specified\n", prog );
376 if( version == LDAP_VERSION2 ) {
377 fprintf( stderr, "%s: -R incompatible with version %d\n",
381 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
382 fprintf( stderr, "%s: incompatible previous "
383 "authentication choice\n",
387 authmethod = LDAP_AUTH_SASL;
388 version = LDAP_VERSION3;
389 sasl_realm = strdup( optarg );
391 fprintf( stderr, "%s: not compiled with SASL support\n",
393 return( EXIT_FAILURE );
397 #ifdef HAVE_CYRUS_SASL
398 if( sasl_authc_id != NULL ) {
399 fprintf( stderr, "%s: -U previously specified\n", prog );
402 if( version == LDAP_VERSION2 ) {
403 fprintf( stderr, "%s: -U incompatible with version %d\n",
407 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
408 fprintf( stderr, "%s: incompatible previous "
409 "authentication choice\n",
413 authmethod = LDAP_AUTH_SASL;
414 version = LDAP_VERSION3;
415 sasl_authc_id = strdup( optarg );
417 fprintf( stderr, "%s: not compiled with SASL support\n",
419 return( EXIT_FAILURE );
422 case 'v': /* verbose mode */
425 case 'w': /* password */
426 passwd.bv_val = strdup( optarg );
430 for( p = optarg; *p != '\0'; p++ ) {
434 passwd.bv_len = strlen( passwd.bv_val );
443 #ifdef HAVE_CYRUS_SASL
444 if( sasl_mech != NULL ) {
445 fprintf( stderr, "%s: -Y previously specified\n", prog );
448 if( version == LDAP_VERSION2 ) {
449 fprintf( stderr, "%s: -Y incompatible with version %d\n",
453 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
454 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
457 authmethod = LDAP_AUTH_SASL;
458 version = LDAP_VERSION3;
459 sasl_mech = strdup( optarg );
461 fprintf( stderr, "%s: not compiled with SASL support\n",
463 return( EXIT_FAILURE );
467 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
468 fprintf( stderr, "%s: incompatible with previous "
469 "authentication choice\n", prog );
472 authmethod = LDAP_AUTH_SIMPLE;
475 #ifdef HAVE_CYRUS_SASL
476 if( sasl_authz_id != NULL ) {
477 fprintf( stderr, "%s: -X previously specified\n", prog );
480 if( version == LDAP_VERSION2 ) {
481 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
485 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
486 fprintf( stderr, "%s: -X incompatible with "
487 "authentication choice\n", prog );
490 authmethod = LDAP_AUTH_SASL;
491 version = LDAP_VERSION3;
492 sasl_authz_id = strdup( optarg );
494 fprintf( stderr, "%s: not compiled with SASL support\n",
496 return( EXIT_FAILURE );
501 if( version == LDAP_VERSION2 ) {
502 fprintf( stderr, "%s: -Z incompatible with version %d\n",
506 version = LDAP_VERSION3;
509 fprintf( stderr, "%s: not compiled with TLS support\n",
511 return( EXIT_FAILURE );
517 fprintf( stderr, "%s: unrecognized option -%c\n",
523 if (authmethod == -1) {
524 #ifdef HAVE_CYRUS_SASL
525 authmethod = LDAP_AUTH_SASL;
527 authmethod = LDAP_AUTH_SIMPLE;
531 if( argc - optind > 1 ) {
533 } else if ( argc - optind == 1 ) {
534 user = strdup( argv[optind] );
539 if ( pw_file || want_bindpw ) {
541 rc = lutil_get_filed_password( pw_file, &passwd );
542 if( rc ) return EXIT_FAILURE;
544 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
545 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
550 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
551 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
553 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
554 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
559 (void) SIGNAL( SIGPIPE, SIG_IGN );
562 /* connect to server */
563 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
565 fprintf( stderr, "ldap_init( %s, %d )\n",
566 ldaphost != NULL ? ldaphost : "<DEFAULT>",
570 ld = ldap_init( ldaphost, ldapport );
572 perror("ldapwhoami: ldap_init");
578 fprintf( stderr, "ldap_initialize( %s )\n",
579 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
582 rc = ldap_initialize( &ld, ldapuri );
583 if( rc != LDAP_SUCCESS ) {
584 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
585 rc, ldap_err2string(rc) );
591 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
592 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
594 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
595 referrals ? "on" : "off" );
600 version = LDAP_VERSION3;
601 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
603 if(rc != LDAP_OPT_SUCCESS ) {
604 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
608 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
609 ldap_perror( ld, "ldap_start_tls" );
611 return( EXIT_FAILURE );
615 if ( authmethod == LDAP_AUTH_SASL ) {
616 #ifdef HAVE_CYRUS_SASL
619 if( sasl_secprops != NULL ) {
620 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
621 (void *) sasl_secprops );
623 if( rc != LDAP_OPT_SUCCESS ) {
625 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
627 return( EXIT_FAILURE );
631 defaults = lutil_sasl_defaults( ld,
638 rc = ldap_sasl_interactive_bind_s( ld, binddn,
639 sasl_mech, NULL, NULL,
640 sasl_flags, lutil_sasl_interact, defaults );
642 if( rc != LDAP_SUCCESS ) {
643 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
644 return( EXIT_FAILURE );
647 fprintf( stderr, "%s: not compiled with SASL support\n",
649 return( EXIT_FAILURE );
653 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
655 ldap_perror( ld, "ldap_bind" );
656 return( EXIT_FAILURE );
665 if ( manageDSAit || noop ) {
668 LDAPControl *ctrls[3];
673 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
674 c1.ldctl_value.bv_val = NULL;
675 c1.ldctl_value.bv_len = 0;
676 c1.ldctl_iscritical = manageDSAit > 1;
683 c2.ldctl_oid = LDAP_CONTROL_NOOP;
684 c2.ldctl_value.bv_val = NULL;
685 c2.ldctl_value.bv_len = 0;
686 c2.ldctl_iscritical = noop > 1;
689 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
691 if( err != LDAP_OPT_SUCCESS ) {
692 fprintf( stderr, "Could not set %scontrols\n",
693 (c1.ldctl_iscritical || c2.ldctl_iscritical)
694 ? "critical " : "" );
695 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
701 rc = ldap_extended_operation( ld,
702 LDAP_EXOP_X_WHO_AM_I, NULL,
705 if( rc != LDAP_SUCCESS ) {
706 ldap_perror( ld, "ldap_extended_operation" );
711 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
713 ldap_perror( ld, "ldappasswd: ldap_result" );
717 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
719 if( rc != LDAP_SUCCESS ) {
720 ldap_perror( ld, "ldap_parse_result" );
724 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
726 if( rc != LDAP_SUCCESS ) {
727 ldap_perror( ld, "ldap_parse_result" );
731 if( retdata != NULL ) {
732 if( retdata->bv_len == 0 ) {
733 printf("anonymous\n" );
735 printf("%s\n", retdata->bv_val );
739 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
740 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
742 if( text && *text ) {
743 printf( "Additional info: %s\n", text );
746 if( matcheddn && *matcheddn ) {
747 printf( "Matched DN: %s\n", matcheddn );
752 for( i=0; refs[i]; i++ ) {
753 printf("Referral: %s\n", refs[i] );
759 ber_memfree( matcheddn );
760 ber_memvfree( (void **) refs );
761 ber_memfree( retoid );
762 ber_bvfree( retdata );
765 /* disconnect from server */
768 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;