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 char *matcheddn = NULL, *text = NULL, **refs = NULL;
105 struct berval *retdata = NULL;
107 prog = lutil_progname( "ldapwhoami", argc, argv );
109 while( (i = getopt( argc, argv,
110 "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:y:Y:Z" )) != EOF )
113 case 'E': /* whoami controls */
114 if( version == LDAP_VERSION2 ) {
115 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
120 /* should be extended to support comma separated list of
121 * [!]key[=value] parameters, e.g. -E !foo,bar=567
126 if( optarg[0] == '!' ) {
131 control = strdup( optarg );
132 if ( (cvalue = strchr( control, '=' )) != NULL ) {
135 fprintf( stderr, "Invalid whoami control name: %s\n", control );
139 /* Common Options (including options we don't use) */
144 debug |= atoi( optarg );
146 case 'D': /* bind DN */
147 if( binddn != NULL ) {
148 fprintf( stderr, "%s: -D previously specified\n", prog );
151 binddn = strdup( optarg );
153 case 'e': /* general controls */
154 if( version == LDAP_VERSION2 ) {
155 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
160 /* should be extended to support comma separated list of
161 * [!]key[=value] parameters, e.g. -e !foo,bar=567
166 if( optarg[0] == '!' ) {
171 control = strdup( optarg );
172 if ( (cvalue = strchr( control, '=' )) != NULL ) {
176 if ( strcasecmp( control, "authzid" ) == 0 ) {
177 if( authzid != NULL ) {
178 fprintf( stderr, "authzid control previously specified\n");
181 if( cvalue == NULL ) {
182 fprintf( stderr, "authzid: control value expected\n" );
187 fprintf( stderr, "authzid: must be marked critical\n" );
192 assert( authzid == NULL );
196 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
198 fprintf( stderr, "manageDSAit control previously specified\n");
201 if( cvalue != NULL ) {
202 fprintf( stderr, "manageDSAit: no control value expected\n" );
207 manageDSAit = 1 + crit;
210 } else if ( strcasecmp( control, "noop" ) == 0 ) {
212 fprintf( stderr, "noop control previously specified\n");
215 if( cvalue != NULL ) {
216 fprintf( stderr, "noop: no control value expected\n" );
225 fprintf( stderr, "Invalid general control name: %s\n", control );
229 case 'h': /* ldap host */
230 if( ldapuri != NULL ) {
231 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
234 if( ldaphost != NULL ) {
235 fprintf( stderr, "%s: -h previously specified\n", prog );
238 ldaphost = strdup( optarg );
240 case 'H': /* ldap URI */
241 if( ldaphost != NULL ) {
242 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
246 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
249 if( ldapuri != NULL ) {
250 fprintf( stderr, "%s: -H previously specified\n", prog );
253 ldapuri = strdup( optarg );
256 #ifdef HAVE_CYRUS_SASL
257 if( version == LDAP_VERSION2 ) {
258 fprintf( stderr, "%s: -I incompatible with version %d\n",
262 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
263 fprintf( stderr, "%s: incompatible previous "
264 "authentication choice\n",
268 authmethod = LDAP_AUTH_SASL;
269 version = LDAP_VERSION3;
270 sasl_flags = LDAP_SASL_INTERACTIVE;
273 fprintf( stderr, "%s: was not compiled with SASL support\n",
275 return( EXIT_FAILURE );
277 case 'k': /* kerberos bind */
278 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
279 if( version > LDAP_VERSION2 ) {
280 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
285 if( authmethod != -1 ) {
286 fprintf( stderr, "%s: -k incompatible with previous "
287 "authentication choice\n", prog );
291 authmethod = LDAP_AUTH_KRBV4;
293 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
297 case 'K': /* kerberos bind, part one only */
298 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
299 if( version > LDAP_VERSION2 ) {
300 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
304 if( authmethod != -1 ) {
305 fprintf( stderr, "%s: incompatible with previous "
306 "authentication choice\n", prog );
310 authmethod = LDAP_AUTH_KRBV41;
312 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
313 return( EXIT_FAILURE );
316 case 'n': /* print deletes, don't actually do them */
320 #ifdef HAVE_CYRUS_SASL
321 if( sasl_secprops != NULL ) {
322 fprintf( stderr, "%s: -O previously specified\n", prog );
325 if( version == LDAP_VERSION2 ) {
326 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
330 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
331 fprintf( stderr, "%s: incompatible previous "
332 "authentication choice\n", prog );
335 authmethod = LDAP_AUTH_SASL;
336 version = LDAP_VERSION3;
337 sasl_secprops = strdup( optarg );
339 fprintf( stderr, "%s: not compiled with SASL support\n",
341 return( EXIT_FAILURE );
346 fprintf( stderr, "%s: -p previously specified\n", prog );
349 ldapport = atoi( optarg );
352 switch( atoi(optarg) ) {
354 if( version == LDAP_VERSION3 ) {
355 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
359 version = LDAP_VERSION2;
362 if( version == LDAP_VERSION2 ) {
363 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
367 version = LDAP_VERSION3;
370 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
373 return( EXIT_FAILURE );
376 #ifdef HAVE_CYRUS_SASL
377 if( version == LDAP_VERSION2 ) {
378 fprintf( stderr, "%s: -Q incompatible with version %d\n",
382 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
383 fprintf( stderr, "%s: incompatible previous "
384 "authentication choice\n",
388 authmethod = LDAP_AUTH_SASL;
389 version = LDAP_VERSION3;
390 sasl_flags = LDAP_SASL_QUIET;
393 fprintf( stderr, "%s: not compiled with SASL support\n",
395 return( EXIT_FAILURE );
398 #ifdef HAVE_CYRUS_SASL
399 if( sasl_realm != NULL ) {
400 fprintf( stderr, "%s: -R previously specified\n", prog );
403 if( version == LDAP_VERSION2 ) {
404 fprintf( stderr, "%s: -R incompatible with version %d\n",
408 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
409 fprintf( stderr, "%s: incompatible previous "
410 "authentication choice\n",
414 authmethod = LDAP_AUTH_SASL;
415 version = LDAP_VERSION3;
416 sasl_realm = strdup( optarg );
418 fprintf( stderr, "%s: not compiled with SASL support\n",
420 return( EXIT_FAILURE );
424 #ifdef HAVE_CYRUS_SASL
425 if( sasl_authc_id != NULL ) {
426 fprintf( stderr, "%s: -U previously specified\n", prog );
429 if( version == LDAP_VERSION2 ) {
430 fprintf( stderr, "%s: -U incompatible with version %d\n",
434 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
435 fprintf( stderr, "%s: incompatible previous "
436 "authentication choice\n",
440 authmethod = LDAP_AUTH_SASL;
441 version = LDAP_VERSION3;
442 sasl_authc_id = strdup( optarg );
444 fprintf( stderr, "%s: not compiled with SASL support\n",
446 return( EXIT_FAILURE );
449 case 'v': /* verbose mode */
452 case 'w': /* password */
453 passwd.bv_val = strdup( optarg );
457 for( p = optarg; *p != '\0'; p++ ) {
461 passwd.bv_len = strlen( passwd.bv_val );
470 #ifdef HAVE_CYRUS_SASL
471 if( sasl_mech != NULL ) {
472 fprintf( stderr, "%s: -Y previously specified\n", prog );
475 if( version == LDAP_VERSION2 ) {
476 fprintf( stderr, "%s: -Y incompatible with version %d\n",
480 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
481 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
484 authmethod = LDAP_AUTH_SASL;
485 version = LDAP_VERSION3;
486 sasl_mech = strdup( optarg );
488 fprintf( stderr, "%s: not compiled with SASL support\n",
490 return( EXIT_FAILURE );
494 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
495 fprintf( stderr, "%s: incompatible with previous "
496 "authentication choice\n", prog );
499 authmethod = LDAP_AUTH_SIMPLE;
502 #ifdef HAVE_CYRUS_SASL
503 if( sasl_authz_id != NULL ) {
504 fprintf( stderr, "%s: -X previously specified\n", prog );
507 if( version == LDAP_VERSION2 ) {
508 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
512 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
513 fprintf( stderr, "%s: -X incompatible with "
514 "authentication choice\n", prog );
517 authmethod = LDAP_AUTH_SASL;
518 version = LDAP_VERSION3;
519 sasl_authz_id = strdup( optarg );
521 fprintf( stderr, "%s: not compiled with SASL support\n",
523 return( EXIT_FAILURE );
528 if( version == LDAP_VERSION2 ) {
529 fprintf( stderr, "%s: -Z incompatible with version %d\n",
533 version = LDAP_VERSION3;
536 fprintf( stderr, "%s: not compiled with TLS support\n",
538 return( EXIT_FAILURE );
544 fprintf( stderr, "%s: unrecognized option -%c\n",
550 if (authmethod == -1) {
551 #ifdef HAVE_CYRUS_SASL
552 authmethod = LDAP_AUTH_SASL;
554 authmethod = LDAP_AUTH_SIMPLE;
558 if( argc - optind > 1 ) {
560 } else if ( argc - optind == 1 ) {
561 user = strdup( argv[optind] );
566 if ( pw_file || want_bindpw ) {
568 rc = lutil_get_filed_password( pw_file, &passwd );
569 if( rc ) return EXIT_FAILURE;
571 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
572 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
577 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
578 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
580 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
581 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
586 (void) SIGNAL( SIGPIPE, SIG_IGN );
589 /* connect to server */
590 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
592 fprintf( stderr, "ldap_init( %s, %d )\n",
593 ldaphost != NULL ? ldaphost : "<DEFAULT>",
597 ld = ldap_init( ldaphost, ldapport );
599 perror("ldapwhoami: ldap_init");
605 fprintf( stderr, "ldap_initialize( %s )\n",
606 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
609 rc = ldap_initialize( &ld, ldapuri );
610 if( rc != LDAP_SUCCESS ) {
611 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
612 rc, ldap_err2string(rc) );
618 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
619 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
621 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
622 referrals ? "on" : "off" );
627 version = LDAP_VERSION3;
628 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
630 if(rc != LDAP_OPT_SUCCESS ) {
631 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
635 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
636 ldap_perror( ld, "ldap_start_tls" );
638 return( EXIT_FAILURE );
642 if ( authmethod == LDAP_AUTH_SASL ) {
643 #ifdef HAVE_CYRUS_SASL
646 if( sasl_secprops != NULL ) {
647 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
648 (void *) sasl_secprops );
650 if( rc != LDAP_OPT_SUCCESS ) {
652 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
654 return( EXIT_FAILURE );
658 defaults = lutil_sasl_defaults( ld,
665 rc = ldap_sasl_interactive_bind_s( ld, binddn,
666 sasl_mech, NULL, NULL,
667 sasl_flags, lutil_sasl_interact, defaults );
669 if( rc != LDAP_SUCCESS ) {
670 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
671 return( EXIT_FAILURE );
674 fprintf( stderr, "%s: not compiled with SASL support\n",
676 return( EXIT_FAILURE );
680 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
682 ldap_perror( ld, "ldap_bind" );
683 return( EXIT_FAILURE );
692 if ( authzid || manageDSAit || noop ) {
693 int err, crit=0, i=0;
695 LDAPControl *ctrls[4];
698 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
699 c[i].ldctl_value.bv_val = authzid;
700 c[i].ldctl_value.bv_len = strlen( authzid );
701 c[i].ldctl_iscritical = 1;
703 if( c[i].ldctl_iscritical ) crit++;
709 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
710 c[i].ldctl_value.bv_val = NULL;
711 c[i].ldctl_value.bv_len = 0;
712 c[i].ldctl_iscritical = manageDSAit > 1;
714 if( c[i].ldctl_iscritical ) crit++;
720 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
721 c[i].ldctl_value.bv_val = NULL;
722 c[i].ldctl_value.bv_len = 0;
723 c[i].ldctl_iscritical = noop > 1;
725 if( c[i].ldctl_iscritical ) crit++;
730 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
732 if( err != LDAP_OPT_SUCCESS ) {
733 fprintf( stderr, "Could not set %scontrols\n",
734 crit ? "critical " : "" );
741 rc = ldap_whoami_s( ld, &retdata, NULL, NULL );
743 if( retdata != NULL ) {
744 if( retdata->bv_len == 0 ) {
745 printf("anonymous\n" );
747 printf("%s\n", retdata->bv_val );
751 if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
752 printf( "Result: %s (%d)\n", ldap_err2string( rc ), rc );
754 if( text && *text ) {
755 printf( "Additional info: %s\n", text );
758 if( matcheddn && *matcheddn ) {
759 printf( "Matched DN: %s\n", matcheddn );
764 for( i=0; refs[i]; i++ ) {
765 printf("Referral: %s\n", refs[i] );
771 ber_memfree( matcheddn );
772 ber_memvfree( (void **) refs );
773 ber_memfree( retoid );
774 ber_bvfree( retdata );
777 /* disconnect from server */
780 return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;