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"
39 #ifdef LDAP_CONTROL_NOOP
42 " -f file read operations from `file'\n"
43 " -h host LDAP server(s)\n"
44 " -H URI LDAP Uniform Resource Indentifier(s)\n"
45 " -I use SASL Interactive mode\n"
46 " -n show what would be done but don't actually do it\n"
47 " -O props SASL security properties\n"
48 " -p port port on LDAP server\n"
49 " -Q use SASL Quiet mode\n"
50 " -R realm SASL realm\n"
51 " -U authcid SASL authentication identity\n"
52 " -v run in verbose mode (diagnostics to standard output)\n"
53 " -w passwd bind passwd (for simple authentication)\n"
54 " -W prompt for bind passwd\n"
55 " -x Simple authentication\n"
56 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
57 " -y file Read passwd from file\n"
58 " -Y mech SASL mechanism\n"
59 " -Z Start TLS request (-ZZ to require successful response)\n"
66 main( int argc, char *argv[] )
70 char *ldaphost = NULL;
76 struct berval passwd = { 0, NULL };
87 #ifdef HAVE_CYRUS_SASL
88 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
89 char *sasl_realm = NULL;
90 char *sasl_authc_id = NULL;
91 char *sasl_authz_id = NULL;
92 char *sasl_mech = NULL;
93 char *sasl_secprops = NULL;
99 #ifdef LDAP_CONTROL_NOOP
102 char *control, *cvalue;
105 int id, code = LDAP_OTHER;
107 char *matcheddn = NULL, *text = NULL, **refs = NULL;
109 struct berval *retdata = NULL;
111 prog = lutil_progname( "ldapwhoami", argc, argv );
113 while( (i = getopt( argc, argv,
114 "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:y:Y:Z" )) != EOF )
117 case 'E': /* whoami controls */
118 if( version == LDAP_VERSION2 ) {
119 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
124 /* should be extended to support comma separated list of
125 * [!]key[=value] parameters, e.g. -E !foo,bar=567
130 if( optarg[0] == '!' ) {
135 control = strdup( optarg );
136 if ( (cvalue = strchr( control, '=' )) != NULL ) {
139 fprintf( stderr, "Invalid whoami control name: %s\n", control );
143 /* Common Options (including options we don't use) */
148 debug |= atoi( optarg );
150 case 'D': /* bind DN */
151 if( binddn != NULL ) {
152 fprintf( stderr, "%s: -D previously specified\n", prog );
155 binddn = strdup( optarg );
157 case 'e': /* general controls */
158 if( version == LDAP_VERSION2 ) {
159 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
164 /* should be extended to support comma separated list of
165 * [!]key[=value] parameters, e.g. -e !foo,bar=567
170 if( optarg[0] == '!' ) {
175 control = strdup( optarg );
176 if ( (cvalue = strchr( control, '=' )) != NULL ) {
180 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
181 if( cvalue != NULL ) {
182 fprintf( stderr, "manageDSAit: no control value expected" );
187 manageDSAit = 1 + crit;
191 #ifdef LDAP_CONTROL_NOOP
192 } else if ( strcasecmp( control, "noop" ) == 0 ) {
193 if( cvalue != NULL ) {
194 fprintf( stderr, "noop: no control value expected" );
204 fprintf( stderr, "Invalid general control name: %s\n", control );
208 case 'h': /* ldap host */
209 if( ldapuri != NULL ) {
210 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
213 if( ldaphost != NULL ) {
214 fprintf( stderr, "%s: -h previously specified\n", prog );
217 ldaphost = strdup( optarg );
219 case 'H': /* ldap URI */
220 if( ldaphost != NULL ) {
221 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
225 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
228 if( ldapuri != NULL ) {
229 fprintf( stderr, "%s: -H previously specified\n", prog );
232 ldapuri = strdup( optarg );
235 #ifdef HAVE_CYRUS_SASL
236 if( version == LDAP_VERSION2 ) {
237 fprintf( stderr, "%s: -I incompatible with version %d\n",
241 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
242 fprintf( stderr, "%s: incompatible previous "
243 "authentication choice\n",
247 authmethod = LDAP_AUTH_SASL;
248 version = LDAP_VERSION3;
249 sasl_flags = LDAP_SASL_INTERACTIVE;
252 fprintf( stderr, "%s: was not compiled with SASL support\n",
254 return( EXIT_FAILURE );
256 case 'k': /* kerberos bind */
257 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
258 if( version > LDAP_VERSION2 ) {
259 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
264 if( authmethod != -1 ) {
265 fprintf( stderr, "%s: -k incompatible with previous "
266 "authentication choice\n", prog );
270 authmethod = LDAP_AUTH_KRBV4;
272 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
276 case 'K': /* kerberos bind, part one only */
277 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
278 if( version > LDAP_VERSION2 ) {
279 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
283 if( authmethod != -1 ) {
284 fprintf( stderr, "%s: incompatible with previous "
285 "authentication choice\n", prog );
289 authmethod = LDAP_AUTH_KRBV41;
291 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
292 return( EXIT_FAILURE );
295 case 'n': /* print deletes, don't actually do them */
299 #ifdef HAVE_CYRUS_SASL
300 if( sasl_secprops != NULL ) {
301 fprintf( stderr, "%s: -O previously specified\n", prog );
304 if( version == LDAP_VERSION2 ) {
305 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
309 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
310 fprintf( stderr, "%s: incompatible previous "
311 "authentication choice\n", prog );
314 authmethod = LDAP_AUTH_SASL;
315 version = LDAP_VERSION3;
316 sasl_secprops = strdup( optarg );
318 fprintf( stderr, "%s: not compiled with SASL support\n",
320 return( EXIT_FAILURE );
325 fprintf( stderr, "%s: -p previously specified\n", prog );
328 ldapport = atoi( optarg );
331 switch( atoi(optarg) ) {
333 if( version == LDAP_VERSION3 ) {
334 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
338 version = LDAP_VERSION2;
341 if( version == LDAP_VERSION2 ) {
342 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
346 version = LDAP_VERSION3;
349 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
352 return( EXIT_FAILURE );
355 #ifdef HAVE_CYRUS_SASL
356 if( version == LDAP_VERSION2 ) {
357 fprintf( stderr, "%s: -Q incompatible with version %d\n",
361 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
362 fprintf( stderr, "%s: incompatible previous "
363 "authentication choice\n",
367 authmethod = LDAP_AUTH_SASL;
368 version = LDAP_VERSION3;
369 sasl_flags = LDAP_SASL_QUIET;
372 fprintf( stderr, "%s: not compiled with SASL support\n",
374 return( EXIT_FAILURE );
377 #ifdef HAVE_CYRUS_SASL
378 if( sasl_realm != NULL ) {
379 fprintf( stderr, "%s: -R previously specified\n", prog );
382 if( version == LDAP_VERSION2 ) {
383 fprintf( stderr, "%s: -R incompatible with version %d\n",
387 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
388 fprintf( stderr, "%s: incompatible previous "
389 "authentication choice\n",
393 authmethod = LDAP_AUTH_SASL;
394 version = LDAP_VERSION3;
395 sasl_realm = strdup( optarg );
397 fprintf( stderr, "%s: not compiled with SASL support\n",
399 return( EXIT_FAILURE );
403 #ifdef HAVE_CYRUS_SASL
404 if( sasl_authc_id != NULL ) {
405 fprintf( stderr, "%s: -U previously specified\n", prog );
408 if( version == LDAP_VERSION2 ) {
409 fprintf( stderr, "%s: -U incompatible with version %d\n",
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
414 fprintf( stderr, "%s: incompatible previous "
415 "authentication choice\n",
419 authmethod = LDAP_AUTH_SASL;
420 version = LDAP_VERSION3;
421 sasl_authc_id = strdup( optarg );
423 fprintf( stderr, "%s: not compiled with SASL support\n",
425 return( EXIT_FAILURE );
428 case 'v': /* verbose mode */
431 case 'w': /* password */
432 passwd.bv_val = strdup( optarg );
436 for( p = optarg; *p != '\0'; p++ ) {
440 passwd.bv_len = strlen( passwd.bv_val );
449 #ifdef HAVE_CYRUS_SASL
450 if( sasl_mech != NULL ) {
451 fprintf( stderr, "%s: -Y previously specified\n", prog );
454 if( version == LDAP_VERSION2 ) {
455 fprintf( stderr, "%s: -Y incompatible with version %d\n",
459 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
460 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
463 authmethod = LDAP_AUTH_SASL;
464 version = LDAP_VERSION3;
465 sasl_mech = strdup( optarg );
467 fprintf( stderr, "%s: not compiled with SASL support\n",
469 return( EXIT_FAILURE );
473 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
474 fprintf( stderr, "%s: incompatible with previous "
475 "authentication choice\n", prog );
478 authmethod = LDAP_AUTH_SIMPLE;
481 #ifdef HAVE_CYRUS_SASL
482 if( sasl_authz_id != NULL ) {
483 fprintf( stderr, "%s: -X previously specified\n", prog );
486 if( version == LDAP_VERSION2 ) {
487 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
491 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
492 fprintf( stderr, "%s: -X incompatible with "
493 "authentication choice\n", prog );
496 authmethod = LDAP_AUTH_SASL;
497 version = LDAP_VERSION3;
498 sasl_authz_id = strdup( optarg );
500 fprintf( stderr, "%s: not compiled with SASL support\n",
502 return( EXIT_FAILURE );
507 if( version == LDAP_VERSION2 ) {
508 fprintf( stderr, "%s: -Z incompatible with version %d\n",
512 version = LDAP_VERSION3;
515 fprintf( stderr, "%s: not compiled with TLS support\n",
517 return( EXIT_FAILURE );
523 fprintf( stderr, "%s: unrecognized option -%c\n",
529 if (authmethod == -1) {
530 #ifdef HAVE_CYRUS_SASL
531 authmethod = LDAP_AUTH_SASL;
533 authmethod = LDAP_AUTH_SIMPLE;
537 if( argc - optind > 1 ) {
539 } else if ( argc - optind == 1 ) {
540 user = strdup( argv[optind] );
545 if ( pw_file || want_bindpw ) {
547 rc = lutil_get_filed_password( pw_file, &passwd );
548 if( rc ) return EXIT_FAILURE;
550 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
551 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
556 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
557 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
559 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
560 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
565 (void) SIGNAL( SIGPIPE, SIG_IGN );
568 /* connect to server */
569 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
571 fprintf( stderr, "ldap_init( %s, %d )\n",
572 ldaphost != NULL ? ldaphost : "<DEFAULT>",
576 ld = ldap_init( ldaphost, ldapport );
578 perror("ldapwhoami: ldap_init");
584 fprintf( stderr, "ldap_initialize( %s )\n",
585 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
588 rc = ldap_initialize( &ld, ldapuri );
589 if( rc != LDAP_SUCCESS ) {
590 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
591 rc, ldap_err2string(rc) );
597 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
598 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
600 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
601 referrals ? "on" : "off" );
606 version = LDAP_VERSION3;
607 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
609 if(rc != LDAP_OPT_SUCCESS ) {
610 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
614 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
615 ldap_perror( ld, "ldap_start_tls" );
617 return( EXIT_FAILURE );
621 if ( authmethod == LDAP_AUTH_SASL ) {
622 #ifdef HAVE_CYRUS_SASL
625 if( sasl_secprops != NULL ) {
626 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
627 (void *) sasl_secprops );
629 if( rc != LDAP_OPT_SUCCESS ) {
631 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
633 return( EXIT_FAILURE );
637 defaults = lutil_sasl_defaults( ld,
644 rc = ldap_sasl_interactive_bind_s( ld, binddn,
645 sasl_mech, NULL, NULL,
646 sasl_flags, lutil_sasl_interact, defaults );
648 if( rc != LDAP_SUCCESS ) {
649 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
650 return( EXIT_FAILURE );
653 fprintf( stderr, "%s: not compiled with SASL support\n",
655 return( EXIT_FAILURE );
659 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
661 ldap_perror( ld, "ldap_bind" );
662 return( EXIT_FAILURE );
672 #ifdef LDAP_CONTROL_NOOP
678 LDAPControl *ctrls[3];
683 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
684 c1.ldctl_value.bv_val = NULL;
685 c1.ldctl_value.bv_len = 0;
686 c1.ldctl_iscritical = manageDSAit > 1;
689 #ifdef LDAP_CONTROL_NOOP
694 c2.ldctl_oid = LDAP_CONTROL_NOOP;
695 c2.ldctl_value.bv_val = NULL;
696 c2.ldctl_value.bv_len = 0;
697 c2.ldctl_iscritical = noop > 1;
701 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
703 if( err != LDAP_OPT_SUCCESS ) {
704 fprintf( stderr, "Could not set %scontrols\n",
705 (c1.ldctl_iscritical || c2.ldctl_iscritical)
706 ? "critical " : "" );
707 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
713 rc = ldap_extended_operation( ld,
714 LDAP_EXOP_X_WHO_AM_I, NULL,
717 if( rc != LDAP_SUCCESS ) {
718 ldap_perror( ld, "ldap_extended_operation" );
723 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
725 ldap_perror( ld, "ldappasswd: ldap_result" );
729 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
731 if( rc != LDAP_SUCCESS ) {
732 ldap_perror( ld, "ldap_parse_result" );
736 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
738 if( rc != LDAP_SUCCESS ) {
739 ldap_perror( ld, "ldap_parse_result" );
743 if( retdata != NULL ) {
744 if( retdata->bv_len == 0 ) {
745 printf("anonymous\n" );
747 printf("%s\n", retdata->bv_val );
751 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
752 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
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 code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;