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 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
39 " [!]manageDSAit (alternate form, see -M)\n"
41 " -f file read operations from `file'\n"
42 " -h host LDAP server(s)\n"
43 " -H URI LDAP Uniform Resource Indentifier(s)\n"
44 " -I use SASL Interactive mode\n"
45 " -n show what would be done but don't actually do it\n"
46 " -O props SASL security properties\n"
47 " -p port port on LDAP server\n"
48 " -Q use SASL Quiet mode\n"
49 " -R realm SASL realm\n"
50 " -U authcid SASL authentication identity\n"
51 " -v run in verbose mode (diagnostics to standard output)\n"
52 " -w passwd bind passwd (for simple authentication)\n"
53 " -W prompt for bind passwd\n"
54 " -x Simple authentication\n"
55 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
56 " -y file Read passwd from file\n"
57 " -Y mech SASL mechanism\n"
58 " -Z Start TLS request (-ZZ to require successful response)\n"
65 main( int argc, char *argv[] )
69 char *ldaphost = NULL;
75 struct berval passwd = { 0, NULL };
86 #ifdef HAVE_CYRUS_SASL
87 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
88 char *sasl_realm = NULL;
89 char *sasl_authc_id = NULL;
90 char *sasl_authz_id = NULL;
91 char *sasl_mech = NULL;
92 char *sasl_secprops = NULL;
100 char *control, *cvalue;
103 int id, code = LDAP_OTHER;
105 char *matcheddn = NULL, *text = NULL, **refs = NULL;
107 struct berval *retdata = NULL;
109 prog = lutil_progname( "ldapwhoami", argc, argv );
111 while( (i = getopt( argc, argv,
112 "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:y:Y:Z" )) != EOF )
115 case 'E': /* whoami controls */
116 if( version == LDAP_VERSION2 ) {
117 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
122 /* should be extended to support comma separated list of
123 * [!]key[=value] parameters, e.g. -E !foo,bar=567
128 if( optarg[0] == '!' ) {
133 control = strdup( optarg );
134 if ( (cvalue = strchr( control, '=' )) != NULL ) {
137 fprintf( stderr, "Invalid whoami control name: %s\n", control );
141 /* Common Options (including options we don't use) */
146 debug |= atoi( optarg );
148 case 'D': /* bind DN */
149 if( binddn != NULL ) {
150 fprintf( stderr, "%s: -D previously specified\n", prog );
153 binddn = strdup( optarg );
155 case 'e': /* general controls */
156 if( version == LDAP_VERSION2 ) {
157 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
162 /* should be extended to support comma separated list of
163 * [!]key[=value] parameters, e.g. -e !foo,bar=567
168 if( optarg[0] == '!' ) {
173 control = strdup( optarg );
174 if ( (cvalue = strchr( control, '=' )) != NULL ) {
178 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
180 fprintf( stderr, "manageDSAit control previously specified");
183 if( cvalue != NULL ) {
184 fprintf( stderr, "manageDSAit: no control value expected" );
189 manageDSAit = 1 + crit;
192 } else if ( strcasecmp( control, "noop" ) == 0 ) {
194 fprintf( stderr, "noop control previously specified");
197 if( cvalue != NULL ) {
198 fprintf( stderr, "noop: no control value expected" );
207 fprintf( stderr, "Invalid general control name: %s\n", control );
211 case 'h': /* ldap host */
212 if( ldapuri != NULL ) {
213 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
216 if( ldaphost != NULL ) {
217 fprintf( stderr, "%s: -h previously specified\n", prog );
220 ldaphost = strdup( optarg );
222 case 'H': /* ldap URI */
223 if( ldaphost != NULL ) {
224 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
228 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
231 if( ldapuri != NULL ) {
232 fprintf( stderr, "%s: -H previously specified\n", prog );
235 ldapuri = strdup( optarg );
238 #ifdef HAVE_CYRUS_SASL
239 if( version == LDAP_VERSION2 ) {
240 fprintf( stderr, "%s: -I incompatible with version %d\n",
244 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
245 fprintf( stderr, "%s: incompatible previous "
246 "authentication choice\n",
250 authmethod = LDAP_AUTH_SASL;
251 version = LDAP_VERSION3;
252 sasl_flags = LDAP_SASL_INTERACTIVE;
255 fprintf( stderr, "%s: was not compiled with SASL support\n",
257 return( EXIT_FAILURE );
259 case 'k': /* kerberos bind */
260 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
261 if( version > LDAP_VERSION2 ) {
262 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
267 if( authmethod != -1 ) {
268 fprintf( stderr, "%s: -k incompatible with previous "
269 "authentication choice\n", prog );
273 authmethod = LDAP_AUTH_KRBV4;
275 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
279 case 'K': /* kerberos bind, part one only */
280 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
281 if( version > LDAP_VERSION2 ) {
282 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
286 if( authmethod != -1 ) {
287 fprintf( stderr, "%s: incompatible with previous "
288 "authentication choice\n", prog );
292 authmethod = LDAP_AUTH_KRBV41;
294 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
295 return( EXIT_FAILURE );
298 case 'n': /* print deletes, don't actually do them */
302 #ifdef HAVE_CYRUS_SASL
303 if( sasl_secprops != NULL ) {
304 fprintf( stderr, "%s: -O previously specified\n", prog );
307 if( version == LDAP_VERSION2 ) {
308 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
312 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
313 fprintf( stderr, "%s: incompatible previous "
314 "authentication choice\n", prog );
317 authmethod = LDAP_AUTH_SASL;
318 version = LDAP_VERSION3;
319 sasl_secprops = strdup( optarg );
321 fprintf( stderr, "%s: not compiled with SASL support\n",
323 return( EXIT_FAILURE );
328 fprintf( stderr, "%s: -p previously specified\n", prog );
331 ldapport = atoi( optarg );
334 switch( atoi(optarg) ) {
336 if( version == LDAP_VERSION3 ) {
337 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
341 version = LDAP_VERSION2;
344 if( version == LDAP_VERSION2 ) {
345 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
349 version = LDAP_VERSION3;
352 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
355 return( EXIT_FAILURE );
358 #ifdef HAVE_CYRUS_SASL
359 if( version == LDAP_VERSION2 ) {
360 fprintf( stderr, "%s: -Q incompatible with version %d\n",
364 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
365 fprintf( stderr, "%s: incompatible previous "
366 "authentication choice\n",
370 authmethod = LDAP_AUTH_SASL;
371 version = LDAP_VERSION3;
372 sasl_flags = LDAP_SASL_QUIET;
375 fprintf( stderr, "%s: not compiled with SASL support\n",
377 return( EXIT_FAILURE );
380 #ifdef HAVE_CYRUS_SASL
381 if( sasl_realm != NULL ) {
382 fprintf( stderr, "%s: -R previously specified\n", prog );
385 if( version == LDAP_VERSION2 ) {
386 fprintf( stderr, "%s: -R incompatible with version %d\n",
390 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
391 fprintf( stderr, "%s: incompatible previous "
392 "authentication choice\n",
396 authmethod = LDAP_AUTH_SASL;
397 version = LDAP_VERSION3;
398 sasl_realm = strdup( optarg );
400 fprintf( stderr, "%s: not compiled with SASL support\n",
402 return( EXIT_FAILURE );
406 #ifdef HAVE_CYRUS_SASL
407 if( sasl_authc_id != NULL ) {
408 fprintf( stderr, "%s: -U previously specified\n", prog );
411 if( version == LDAP_VERSION2 ) {
412 fprintf( stderr, "%s: -U incompatible with version %d\n",
416 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
417 fprintf( stderr, "%s: incompatible previous "
418 "authentication choice\n",
422 authmethod = LDAP_AUTH_SASL;
423 version = LDAP_VERSION3;
424 sasl_authc_id = strdup( optarg );
426 fprintf( stderr, "%s: not compiled with SASL support\n",
428 return( EXIT_FAILURE );
431 case 'v': /* verbose mode */
434 case 'w': /* password */
435 passwd.bv_val = strdup( optarg );
439 for( p = optarg; *p != '\0'; p++ ) {
443 passwd.bv_len = strlen( passwd.bv_val );
452 #ifdef HAVE_CYRUS_SASL
453 if( sasl_mech != NULL ) {
454 fprintf( stderr, "%s: -Y previously specified\n", prog );
457 if( version == LDAP_VERSION2 ) {
458 fprintf( stderr, "%s: -Y incompatible with version %d\n",
462 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
463 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
466 authmethod = LDAP_AUTH_SASL;
467 version = LDAP_VERSION3;
468 sasl_mech = strdup( optarg );
470 fprintf( stderr, "%s: not compiled with SASL support\n",
472 return( EXIT_FAILURE );
476 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
477 fprintf( stderr, "%s: incompatible with previous "
478 "authentication choice\n", prog );
481 authmethod = LDAP_AUTH_SIMPLE;
484 #ifdef HAVE_CYRUS_SASL
485 if( sasl_authz_id != NULL ) {
486 fprintf( stderr, "%s: -X previously specified\n", prog );
489 if( version == LDAP_VERSION2 ) {
490 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
494 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
495 fprintf( stderr, "%s: -X incompatible with "
496 "authentication choice\n", prog );
499 authmethod = LDAP_AUTH_SASL;
500 version = LDAP_VERSION3;
501 sasl_authz_id = strdup( optarg );
503 fprintf( stderr, "%s: not compiled with SASL support\n",
505 return( EXIT_FAILURE );
510 if( version == LDAP_VERSION2 ) {
511 fprintf( stderr, "%s: -Z incompatible with version %d\n",
515 version = LDAP_VERSION3;
518 fprintf( stderr, "%s: not compiled with TLS support\n",
520 return( EXIT_FAILURE );
526 fprintf( stderr, "%s: unrecognized option -%c\n",
532 if (authmethod == -1) {
533 #ifdef HAVE_CYRUS_SASL
534 authmethod = LDAP_AUTH_SASL;
536 authmethod = LDAP_AUTH_SIMPLE;
540 if( argc - optind > 1 ) {
542 } else if ( argc - optind == 1 ) {
543 user = strdup( argv[optind] );
548 if ( pw_file || want_bindpw ) {
550 rc = lutil_get_filed_password( pw_file, &passwd );
551 if( rc ) return EXIT_FAILURE;
553 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
554 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
559 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
560 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
562 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
563 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
568 (void) SIGNAL( SIGPIPE, SIG_IGN );
571 /* connect to server */
572 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
574 fprintf( stderr, "ldap_init( %s, %d )\n",
575 ldaphost != NULL ? ldaphost : "<DEFAULT>",
579 ld = ldap_init( ldaphost, ldapport );
581 perror("ldapwhoami: ldap_init");
587 fprintf( stderr, "ldap_initialize( %s )\n",
588 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
591 rc = ldap_initialize( &ld, ldapuri );
592 if( rc != LDAP_SUCCESS ) {
593 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
594 rc, ldap_err2string(rc) );
600 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
601 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
603 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
604 referrals ? "on" : "off" );
609 version = LDAP_VERSION3;
610 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
612 if(rc != LDAP_OPT_SUCCESS ) {
613 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
617 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
618 ldap_perror( ld, "ldap_start_tls" );
620 return( EXIT_FAILURE );
624 if ( authmethod == LDAP_AUTH_SASL ) {
625 #ifdef HAVE_CYRUS_SASL
628 if( sasl_secprops != NULL ) {
629 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
630 (void *) sasl_secprops );
632 if( rc != LDAP_OPT_SUCCESS ) {
634 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
636 return( EXIT_FAILURE );
640 defaults = lutil_sasl_defaults( ld,
647 rc = ldap_sasl_interactive_bind_s( ld, binddn,
648 sasl_mech, NULL, NULL,
649 sasl_flags, lutil_sasl_interact, defaults );
651 if( rc != LDAP_SUCCESS ) {
652 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
653 return( EXIT_FAILURE );
656 fprintf( stderr, "%s: not compiled with SASL support\n",
658 return( EXIT_FAILURE );
662 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
664 ldap_perror( ld, "ldap_bind" );
665 return( EXIT_FAILURE );
674 if ( authzid || manageDSAit || noop ) {
675 int err, crit=0, i=0;
677 LDAPControl *ctrls[4];
680 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
681 c[i].ldctl_value.bv_val = authzid;
682 c[i].ldctl_value.bv_len = strlen( authzid );
683 c[i].ldctl_iscritical = 1;
685 if( c[i].ldctl_iscritical ) crit++;
691 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
692 c[i].ldctl_value.bv_val = NULL;
693 c[i].ldctl_value.bv_len = 0;
694 c[i].ldctl_iscritical = manageDSAit > 1;
696 if( c[i].ldctl_iscritical ) crit++;
702 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
703 c[i].ldctl_value.bv_val = NULL;
704 c[i].ldctl_value.bv_len = 0;
705 c[i].ldctl_iscritical = noop > 1;
707 if( c[i].ldctl_iscritical ) crit++;
712 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
714 if( err != LDAP_OPT_SUCCESS ) {
715 fprintf( stderr, "Could not set %scontrols\n",
716 crit ? "critical " : "" );
724 rc = ldap_whoami_s( ld, &retdata, NULL, NULL );
727 rc = ldap_extended_operation( ld,
728 LDAP_EXOP_X_WHO_AM_I, NULL,
731 if( rc != LDAP_SUCCESS ) {
732 ldap_perror( ld, "ldap_extended_operation" );
737 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
739 ldap_perror( ld, "ldappasswd: ldap_result" );
743 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
745 if( rc != LDAP_SUCCESS ) {
746 ldap_perror( ld, "ldap_parse_result" );
750 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
753 if( rc != LDAP_SUCCESS ) {
754 ldap_perror( ld, "ldap_parse_result" );
758 if( retdata != NULL ) {
759 if( retdata->bv_len == 0 ) {
760 printf("anonymous\n" );
762 printf("%s\n", retdata->bv_val );
766 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
767 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
769 if( text && *text ) {
770 printf( "Additional info: %s\n", text );
773 if( matcheddn && *matcheddn ) {
774 printf( "Matched DN: %s\n", matcheddn );
779 for( i=0; refs[i]; i++ ) {
780 printf("Referral: %s\n", refs[i] );
786 ber_memfree( matcheddn );
787 ber_memvfree( (void **) refs );
788 ber_memfree( retoid );
789 ber_bvfree( retdata );
792 /* disconnect from server */
795 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;