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, "manageDSAit" ) == 0 ) {
178 fprintf( stderr, "manageDSAit control previously specified");
181 if( cvalue != NULL ) {
182 fprintf( stderr, "manageDSAit: no control value expected" );
187 manageDSAit = 1 + crit;
190 } else if ( strcasecmp( control, "noop" ) == 0 ) {
192 fprintf( stderr, "noop control previously specified");
195 if( cvalue != NULL ) {
196 fprintf( stderr, "noop: no control value expected" );
205 fprintf( stderr, "Invalid general control name: %s\n", control );
209 case 'h': /* ldap host */
210 if( ldapuri != NULL ) {
211 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
214 if( ldaphost != NULL ) {
215 fprintf( stderr, "%s: -h previously specified\n", prog );
218 ldaphost = strdup( optarg );
220 case 'H': /* ldap URI */
221 if( ldaphost != NULL ) {
222 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
226 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
229 if( ldapuri != NULL ) {
230 fprintf( stderr, "%s: -H previously specified\n", prog );
233 ldapuri = strdup( optarg );
236 #ifdef HAVE_CYRUS_SASL
237 if( version == LDAP_VERSION2 ) {
238 fprintf( stderr, "%s: -I incompatible with version %d\n",
242 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
243 fprintf( stderr, "%s: incompatible previous "
244 "authentication choice\n",
248 authmethod = LDAP_AUTH_SASL;
249 version = LDAP_VERSION3;
250 sasl_flags = LDAP_SASL_INTERACTIVE;
253 fprintf( stderr, "%s: was not compiled with SASL support\n",
255 return( EXIT_FAILURE );
257 case 'k': /* kerberos bind */
258 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
259 if( version > LDAP_VERSION2 ) {
260 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
265 if( authmethod != -1 ) {
266 fprintf( stderr, "%s: -k incompatible with previous "
267 "authentication choice\n", prog );
271 authmethod = LDAP_AUTH_KRBV4;
273 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
277 case 'K': /* kerberos bind, part one only */
278 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
279 if( version > LDAP_VERSION2 ) {
280 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
284 if( authmethod != -1 ) {
285 fprintf( stderr, "%s: incompatible with previous "
286 "authentication choice\n", prog );
290 authmethod = LDAP_AUTH_KRBV41;
292 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
293 return( EXIT_FAILURE );
296 case 'n': /* print deletes, don't actually do them */
300 #ifdef HAVE_CYRUS_SASL
301 if( sasl_secprops != NULL ) {
302 fprintf( stderr, "%s: -O previously specified\n", prog );
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
310 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311 fprintf( stderr, "%s: incompatible previous "
312 "authentication choice\n", prog );
315 authmethod = LDAP_AUTH_SASL;
316 version = LDAP_VERSION3;
317 sasl_secprops = strdup( optarg );
319 fprintf( stderr, "%s: not compiled with SASL support\n",
321 return( EXIT_FAILURE );
326 fprintf( stderr, "%s: -p previously specified\n", prog );
329 ldapport = atoi( optarg );
332 switch( atoi(optarg) ) {
334 if( version == LDAP_VERSION3 ) {
335 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
339 version = LDAP_VERSION2;
342 if( version == LDAP_VERSION2 ) {
343 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
347 version = LDAP_VERSION3;
350 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
353 return( EXIT_FAILURE );
356 #ifdef HAVE_CYRUS_SASL
357 if( version == LDAP_VERSION2 ) {
358 fprintf( stderr, "%s: -Q incompatible with version %d\n",
362 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
363 fprintf( stderr, "%s: incompatible previous "
364 "authentication choice\n",
368 authmethod = LDAP_AUTH_SASL;
369 version = LDAP_VERSION3;
370 sasl_flags = LDAP_SASL_QUIET;
373 fprintf( stderr, "%s: not compiled with SASL support\n",
375 return( EXIT_FAILURE );
378 #ifdef HAVE_CYRUS_SASL
379 if( sasl_realm != NULL ) {
380 fprintf( stderr, "%s: -R previously specified\n", prog );
383 if( version == LDAP_VERSION2 ) {
384 fprintf( stderr, "%s: -R incompatible with version %d\n",
388 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
389 fprintf( stderr, "%s: incompatible previous "
390 "authentication choice\n",
394 authmethod = LDAP_AUTH_SASL;
395 version = LDAP_VERSION3;
396 sasl_realm = strdup( optarg );
398 fprintf( stderr, "%s: not compiled with SASL support\n",
400 return( EXIT_FAILURE );
404 #ifdef HAVE_CYRUS_SASL
405 if( sasl_authc_id != NULL ) {
406 fprintf( stderr, "%s: -U previously specified\n", prog );
409 if( version == LDAP_VERSION2 ) {
410 fprintf( stderr, "%s: -U incompatible with version %d\n",
414 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
415 fprintf( stderr, "%s: incompatible previous "
416 "authentication choice\n",
420 authmethod = LDAP_AUTH_SASL;
421 version = LDAP_VERSION3;
422 sasl_authc_id = strdup( optarg );
424 fprintf( stderr, "%s: not compiled with SASL support\n",
426 return( EXIT_FAILURE );
429 case 'v': /* verbose mode */
432 case 'w': /* password */
433 passwd.bv_val = strdup( optarg );
437 for( p = optarg; *p != '\0'; p++ ) {
441 passwd.bv_len = strlen( passwd.bv_val );
450 #ifdef HAVE_CYRUS_SASL
451 if( sasl_mech != NULL ) {
452 fprintf( stderr, "%s: -Y previously specified\n", prog );
455 if( version == LDAP_VERSION2 ) {
456 fprintf( stderr, "%s: -Y incompatible with version %d\n",
460 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
461 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
464 authmethod = LDAP_AUTH_SASL;
465 version = LDAP_VERSION3;
466 sasl_mech = strdup( optarg );
468 fprintf( stderr, "%s: not compiled with SASL support\n",
470 return( EXIT_FAILURE );
474 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
475 fprintf( stderr, "%s: incompatible with previous "
476 "authentication choice\n", prog );
479 authmethod = LDAP_AUTH_SIMPLE;
482 #ifdef HAVE_CYRUS_SASL
483 if( sasl_authz_id != NULL ) {
484 fprintf( stderr, "%s: -X previously specified\n", prog );
487 if( version == LDAP_VERSION2 ) {
488 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
492 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
493 fprintf( stderr, "%s: -X incompatible with "
494 "authentication choice\n", prog );
497 authmethod = LDAP_AUTH_SASL;
498 version = LDAP_VERSION3;
499 sasl_authz_id = strdup( optarg );
501 fprintf( stderr, "%s: not compiled with SASL support\n",
503 return( EXIT_FAILURE );
508 if( version == LDAP_VERSION2 ) {
509 fprintf( stderr, "%s: -Z incompatible with version %d\n",
513 version = LDAP_VERSION3;
516 fprintf( stderr, "%s: not compiled with TLS support\n",
518 return( EXIT_FAILURE );
524 fprintf( stderr, "%s: unrecognized option -%c\n",
530 if (authmethod == -1) {
531 #ifdef HAVE_CYRUS_SASL
532 authmethod = LDAP_AUTH_SASL;
534 authmethod = LDAP_AUTH_SIMPLE;
538 if( argc - optind > 1 ) {
540 } else if ( argc - optind == 1 ) {
541 user = strdup( argv[optind] );
546 if ( pw_file || want_bindpw ) {
548 rc = lutil_get_filed_password( pw_file, &passwd );
549 if( rc ) return EXIT_FAILURE;
551 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
552 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
557 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
558 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
560 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
561 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
566 (void) SIGNAL( SIGPIPE, SIG_IGN );
569 /* connect to server */
570 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
572 fprintf( stderr, "ldap_init( %s, %d )\n",
573 ldaphost != NULL ? ldaphost : "<DEFAULT>",
577 ld = ldap_init( ldaphost, ldapport );
579 perror("ldapwhoami: ldap_init");
585 fprintf( stderr, "ldap_initialize( %s )\n",
586 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
589 rc = ldap_initialize( &ld, ldapuri );
590 if( rc != LDAP_SUCCESS ) {
591 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
592 rc, ldap_err2string(rc) );
598 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
599 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
601 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
602 referrals ? "on" : "off" );
607 version = LDAP_VERSION3;
608 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
610 if(rc != LDAP_OPT_SUCCESS ) {
611 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
615 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
616 ldap_perror( ld, "ldap_start_tls" );
618 return( EXIT_FAILURE );
622 if ( authmethod == LDAP_AUTH_SASL ) {
623 #ifdef HAVE_CYRUS_SASL
626 if( sasl_secprops != NULL ) {
627 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
628 (void *) sasl_secprops );
630 if( rc != LDAP_OPT_SUCCESS ) {
632 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
634 return( EXIT_FAILURE );
638 defaults = lutil_sasl_defaults( ld,
645 rc = ldap_sasl_interactive_bind_s( ld, binddn,
646 sasl_mech, NULL, NULL,
647 sasl_flags, lutil_sasl_interact, defaults );
649 if( rc != LDAP_SUCCESS ) {
650 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
651 return( EXIT_FAILURE );
654 fprintf( stderr, "%s: not compiled with SASL support\n",
656 return( EXIT_FAILURE );
660 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
662 ldap_perror( ld, "ldap_bind" );
663 return( EXIT_FAILURE );
672 if ( authzid || manageDSAit || noop ) {
673 int err, crit=0, i=0;
675 LDAPControl *ctrls[4];
678 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
679 c[i].ldctl_value.bv_val = authzid;
680 c[i].ldctl_value.bv_len = strlen( authzid );
681 c[i].ldctl_iscritical = 1;
683 if( c[i].ldctl_iscritical ) crit++;
689 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
690 c[i].ldctl_value.bv_val = NULL;
691 c[i].ldctl_value.bv_len = 0;
692 c[i].ldctl_iscritical = manageDSAit > 1;
694 if( c[i].ldctl_iscritical ) crit++;
700 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
701 c[i].ldctl_value.bv_val = NULL;
702 c[i].ldctl_value.bv_len = 0;
703 c[i].ldctl_iscritical = noop > 1;
705 if( c[i].ldctl_iscritical ) crit++;
710 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
712 if( err != LDAP_OPT_SUCCESS ) {
713 fprintf( stderr, "Could not set %scontrols\n",
714 crit ? "critical " : "" );
721 rc = ldap_whoami_s( ld, &retdata, NULL, NULL );
723 if( retdata != NULL ) {
724 if( retdata->bv_len == 0 ) {
725 printf("anonymous\n" );
727 printf("%s\n", retdata->bv_val );
731 if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
732 printf( "Result: %s (%d)\n", ldap_err2string( rc ), rc );
734 if( text && *text ) {
735 printf( "Additional info: %s\n", text );
738 if( matcheddn && *matcheddn ) {
739 printf( "Matched DN: %s\n", matcheddn );
744 for( i=0; refs[i]; i++ ) {
745 printf("Referral: %s\n", refs[i] );
751 ber_memfree( matcheddn );
752 ber_memvfree( (void **) refs );
753 ber_memfree( retoid );
754 ber_bvfree( retdata );
757 /* disconnect from server */
760 return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;