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 );
195 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
197 fprintf( stderr, "manageDSAit control previously specified\n");
200 if( cvalue != NULL ) {
201 fprintf( stderr, "manageDSAit: no control value expected\n" );
206 manageDSAit = 1 + crit;
208 } else if ( strcasecmp( control, "noop" ) == 0 ) {
210 fprintf( stderr, "noop control previously specified\n");
213 if( cvalue != NULL ) {
214 fprintf( stderr, "noop: no control value expected\n" );
222 fprintf( stderr, "Invalid general control name: %s\n", control );
227 case 'h': /* ldap host */
228 if( ldapuri != NULL ) {
229 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
232 if( ldaphost != NULL ) {
233 fprintf( stderr, "%s: -h previously specified\n", prog );
236 ldaphost = strdup( optarg );
238 case 'H': /* ldap URI */
239 if( ldaphost != NULL ) {
240 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
244 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
247 if( ldapuri != NULL ) {
248 fprintf( stderr, "%s: -H previously specified\n", prog );
251 ldapuri = strdup( optarg );
254 #ifdef HAVE_CYRUS_SASL
255 if( version == LDAP_VERSION2 ) {
256 fprintf( stderr, "%s: -I incompatible with version %d\n",
260 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
261 fprintf( stderr, "%s: incompatible previous "
262 "authentication choice\n",
266 authmethod = LDAP_AUTH_SASL;
267 version = LDAP_VERSION3;
268 sasl_flags = LDAP_SASL_INTERACTIVE;
271 fprintf( stderr, "%s: was not compiled with SASL support\n",
273 return( EXIT_FAILURE );
275 case 'k': /* kerberos bind */
276 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
277 if( version > LDAP_VERSION2 ) {
278 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
283 if( authmethod != -1 ) {
284 fprintf( stderr, "%s: -k incompatible with previous "
285 "authentication choice\n", prog );
289 authmethod = LDAP_AUTH_KRBV4;
291 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
295 case 'K': /* kerberos bind, part one only */
296 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
297 if( version > LDAP_VERSION2 ) {
298 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
302 if( authmethod != -1 ) {
303 fprintf( stderr, "%s: incompatible with previous "
304 "authentication choice\n", prog );
308 authmethod = LDAP_AUTH_KRBV41;
310 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
311 return( EXIT_FAILURE );
314 case 'n': /* print deletes, don't actually do them */
318 #ifdef HAVE_CYRUS_SASL
319 if( sasl_secprops != NULL ) {
320 fprintf( stderr, "%s: -O previously specified\n", prog );
323 if( version == LDAP_VERSION2 ) {
324 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
328 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
329 fprintf( stderr, "%s: incompatible previous "
330 "authentication choice\n", prog );
333 authmethod = LDAP_AUTH_SASL;
334 version = LDAP_VERSION3;
335 sasl_secprops = strdup( optarg );
337 fprintf( stderr, "%s: not compiled with SASL support\n",
339 return( EXIT_FAILURE );
344 fprintf( stderr, "%s: -p previously specified\n", prog );
347 ldapport = atoi( optarg );
350 switch( atoi(optarg) ) {
352 if( version == LDAP_VERSION3 ) {
353 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
357 version = LDAP_VERSION2;
360 if( version == LDAP_VERSION2 ) {
361 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
365 version = LDAP_VERSION3;
368 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
371 return( EXIT_FAILURE );
375 #ifdef HAVE_CYRUS_SASL
376 if( version == LDAP_VERSION2 ) {
377 fprintf( stderr, "%s: -Q 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_flags = LDAP_SASL_QUIET;
392 fprintf( stderr, "%s: not compiled with SASL support\n",
394 return( EXIT_FAILURE );
397 #ifdef HAVE_CYRUS_SASL
398 if( sasl_realm != NULL ) {
399 fprintf( stderr, "%s: -R previously specified\n", prog );
402 if( version == LDAP_VERSION2 ) {
403 fprintf( stderr, "%s: -R 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_realm = strdup( optarg );
417 fprintf( stderr, "%s: not compiled with SASL support\n",
419 return( EXIT_FAILURE );
423 #ifdef HAVE_CYRUS_SASL
424 if( sasl_authc_id != NULL ) {
425 fprintf( stderr, "%s: -U previously specified\n", prog );
428 if( version == LDAP_VERSION2 ) {
429 fprintf( stderr, "%s: -U incompatible with version %d\n",
433 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
434 fprintf( stderr, "%s: incompatible previous "
435 "authentication choice\n",
439 authmethod = LDAP_AUTH_SASL;
440 version = LDAP_VERSION3;
441 sasl_authc_id = strdup( optarg );
443 fprintf( stderr, "%s: not compiled with SASL support\n",
445 return( EXIT_FAILURE );
448 case 'v': /* verbose mode */
451 case 'w': /* password */
452 passwd.bv_val = strdup( optarg );
456 for( p = optarg; *p != '\0'; p++ ) {
460 passwd.bv_len = strlen( passwd.bv_val );
469 #ifdef HAVE_CYRUS_SASL
470 if( sasl_mech != NULL ) {
471 fprintf( stderr, "%s: -Y previously specified\n", prog );
474 if( version == LDAP_VERSION2 ) {
475 fprintf( stderr, "%s: -Y incompatible with version %d\n",
479 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
480 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
483 authmethod = LDAP_AUTH_SASL;
484 version = LDAP_VERSION3;
485 sasl_mech = strdup( optarg );
487 fprintf( stderr, "%s: not compiled with SASL support\n",
489 return( EXIT_FAILURE );
493 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
494 fprintf( stderr, "%s: incompatible with previous "
495 "authentication choice\n", prog );
498 authmethod = LDAP_AUTH_SIMPLE;
501 #ifdef HAVE_CYRUS_SASL
502 if( sasl_authz_id != NULL ) {
503 fprintf( stderr, "%s: -X previously specified\n", prog );
506 if( version == LDAP_VERSION2 ) {
507 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
511 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
512 fprintf( stderr, "%s: -X incompatible with "
513 "authentication choice\n", prog );
516 authmethod = LDAP_AUTH_SASL;
517 version = LDAP_VERSION3;
518 sasl_authz_id = strdup( optarg );
520 fprintf( stderr, "%s: not compiled with SASL support\n",
522 return( EXIT_FAILURE );
527 if( version == LDAP_VERSION2 ) {
528 fprintf( stderr, "%s: -Z incompatible with version %d\n",
532 version = LDAP_VERSION3;
535 fprintf( stderr, "%s: not compiled with TLS support\n",
537 return( EXIT_FAILURE );
543 fprintf( stderr, "%s: unrecognized option -%c\n",
549 if (authmethod == -1) {
550 #ifdef HAVE_CYRUS_SASL
551 authmethod = LDAP_AUTH_SASL;
553 authmethod = LDAP_AUTH_SIMPLE;
557 if( argc - optind > 1 ) {
559 } else if ( argc - optind == 1 ) {
560 user = strdup( argv[optind] );
565 if ( pw_file || want_bindpw ) {
567 rc = lutil_get_filed_password( pw_file, &passwd );
568 if( rc ) return EXIT_FAILURE;
570 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
571 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
576 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
577 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
579 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
580 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
585 (void) SIGNAL( SIGPIPE, SIG_IGN );
588 /* connect to server */
589 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
591 fprintf( stderr, "ldap_init( %s, %d )\n",
592 ldaphost != NULL ? ldaphost : "<DEFAULT>",
596 ld = ldap_init( ldaphost, ldapport );
598 perror("ldapwhoami: ldap_init");
604 fprintf( stderr, "ldap_initialize( %s )\n",
605 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
608 rc = ldap_initialize( &ld, ldapuri );
609 if( rc != LDAP_SUCCESS ) {
610 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
611 rc, ldap_err2string(rc) );
617 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
618 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
620 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
621 referrals ? "on" : "off" );
626 version = LDAP_VERSION3;
627 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
629 if(rc != LDAP_OPT_SUCCESS ) {
630 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
634 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
635 ldap_perror( ld, "ldap_start_tls" );
637 return( EXIT_FAILURE );
641 if ( authmethod == LDAP_AUTH_SASL ) {
642 #ifdef HAVE_CYRUS_SASL
645 if( sasl_secprops != NULL ) {
646 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
647 (void *) sasl_secprops );
649 if( rc != LDAP_OPT_SUCCESS ) {
651 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
653 return( EXIT_FAILURE );
657 defaults = lutil_sasl_defaults( ld,
664 rc = ldap_sasl_interactive_bind_s( ld, binddn,
665 sasl_mech, NULL, NULL,
666 sasl_flags, lutil_sasl_interact, defaults );
668 if( rc != LDAP_SUCCESS ) {
669 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
670 return( EXIT_FAILURE );
673 fprintf( stderr, "%s: not compiled with SASL support\n",
675 return( EXIT_FAILURE );
679 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
681 ldap_perror( ld, "ldap_bind" );
682 return( EXIT_FAILURE );
691 if ( authzid || manageDSAit || noop ) {
692 int err, crit=0, i=0;
694 LDAPControl *ctrls[4];
697 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
698 c[i].ldctl_value.bv_val = authzid;
699 c[i].ldctl_value.bv_len = strlen( authzid );
700 c[i].ldctl_iscritical = 1;
702 if( c[i].ldctl_iscritical ) crit++;
708 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
709 c[i].ldctl_value.bv_val = NULL;
710 c[i].ldctl_value.bv_len = 0;
711 c[i].ldctl_iscritical = manageDSAit > 1;
713 if( c[i].ldctl_iscritical ) crit++;
719 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
720 c[i].ldctl_value.bv_val = NULL;
721 c[i].ldctl_value.bv_len = 0;
722 c[i].ldctl_iscritical = noop > 1;
724 if( c[i].ldctl_iscritical ) crit++;
729 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
731 if( err != LDAP_OPT_SUCCESS ) {
732 fprintf( stderr, "Could not set %scontrols\n",
733 crit ? "critical " : "" );
740 rc = ldap_whoami_s( ld, &retdata, NULL, NULL );
742 if( retdata != NULL ) {
743 if( retdata->bv_len == 0 ) {
744 printf("anonymous\n" );
746 printf("%s\n", retdata->bv_val );
750 if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
751 printf( "Result: %s (%d)\n", ldap_err2string( rc ), rc );
753 if( text && *text ) {
754 printf( "Additional info: %s\n", text );
757 if( matcheddn && *matcheddn ) {
758 printf( "Matched DN: %s\n", matcheddn );
763 for( i=0; refs[i]; i++ ) {
764 printf("Referral: %s\n", refs[i] );
770 ber_memfree( matcheddn );
771 ber_memvfree( (void **) refs );
772 ber_memfree( retoid );
773 ber_bvfree( retdata );
776 /* disconnect from server */
779 return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;