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 " -f file read operations from `file'\n"
38 " -h host LDAP server(s)\n"
39 " -H URI LDAP Uniform Resource Indentifier(s)\n"
40 " -I use SASL Interactive mode\n"
41 " -n show what would be done but don't actually do it\n"
42 " -O props SASL security properties\n"
43 " -p port port on LDAP server\n"
44 " -Q use SASL Quiet mode\n"
45 " -R realm SASL realm\n"
46 " -U authcid SASL authentication identity\n"
47 " -v run in verbose mode (diagnostics to standard output)\n"
48 " -w passwd bind passwd (for simple authentication)\n"
49 " -W prompt for bind passwd\n"
50 " -x Simple authentication\n"
51 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
52 " -Y mech SASL mechanism\n"
53 " -Z Start TLS request (-ZZ to require successful response)\n"
60 main( int argc, char *argv[] )
64 char *ldaphost = NULL;
70 struct berval passwd = { 0, NULL };
85 #ifdef HAVE_CYRUS_SASL
86 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
87 char *sasl_realm = NULL;
88 char *sasl_authc_id = NULL;
89 char *sasl_authz_id = NULL;
90 char *sasl_mech = NULL;
91 char *sasl_secprops = NULL;
97 int id, code = LDAP_OTHER;
99 char *matcheddn = NULL, *text = NULL, **refs = NULL;
101 struct berval *retdata = NULL;
103 prog = lutil_progname( "ldapwhoami", argc, argv );
105 while( (i = getopt( argc, argv, "Aa:Ss:"
106 "Cd:D:h:H:InO:p:QR:U:vw:WxX:Y:Z" )) != EOF )
109 /* Password Options */
110 case 'A': /* prompt for old password */
114 case 'a': /* old password (secret) */
115 oldpw = strdup (optarg);
120 for( p = optarg; *p != '\0'; p++ ) {
126 case 'S': /* prompt for user password */
130 case 's': /* new password (secret) */
131 newpw = strdup (optarg);
135 for( p = optarg; *p != '\0'; p++ ) {
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 'h': /* ldap host */
156 if( ldapuri != NULL ) {
157 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
160 if( ldaphost != NULL ) {
161 fprintf( stderr, "%s: -h previously specified\n", prog );
164 ldaphost = strdup( optarg );
166 case 'H': /* ldap URI */
167 if( ldaphost != NULL ) {
168 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
172 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
175 if( ldapuri != NULL ) {
176 fprintf( stderr, "%s: -H previously specified\n", prog );
179 ldapuri = strdup( optarg );
182 #ifdef HAVE_CYRUS_SASL
183 if( version == LDAP_VERSION2 ) {
184 fprintf( stderr, "%s: -I incompatible with version %d\n",
188 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
189 fprintf( stderr, "%s: incompatible previous "
190 "authentication choice\n",
194 authmethod = LDAP_AUTH_SASL;
195 version = LDAP_VERSION3;
196 sasl_flags = LDAP_SASL_INTERACTIVE;
199 fprintf( stderr, "%s: was not compiled with SASL support\n",
201 return( EXIT_FAILURE );
203 case 'k': /* kerberos bind */
204 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
205 if( version > LDAP_VERSION2 ) {
206 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
211 if( authmethod != -1 ) {
212 fprintf( stderr, "%s: -k incompatible with previous "
213 "authentication choice\n", prog );
217 authmethod = LDAP_AUTH_KRBV4;
219 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
223 case 'K': /* kerberos bind, part one only */
224 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
225 if( version > LDAP_VERSION2 ) {
226 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
230 if( authmethod != -1 ) {
231 fprintf( stderr, "%s: incompatible with previous "
232 "authentication choice\n", prog );
236 authmethod = LDAP_AUTH_KRBV41;
238 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
239 return( EXIT_FAILURE );
243 /* enable Manage DSA IT */
244 if( version == LDAP_VERSION2 ) {
245 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
250 version = LDAP_VERSION3;
252 case 'n': /* print deletes, don't actually do them */
256 #ifdef HAVE_CYRUS_SASL
257 if( sasl_secprops != NULL ) {
258 fprintf( stderr, "%s: -O previously specified\n", prog );
261 if( version == LDAP_VERSION2 ) {
262 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
266 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
267 fprintf( stderr, "%s: incompatible previous "
268 "authentication choice\n", prog );
271 authmethod = LDAP_AUTH_SASL;
272 version = LDAP_VERSION3;
273 sasl_secprops = strdup( optarg );
275 fprintf( stderr, "%s: not compiled with SASL support\n",
277 return( EXIT_FAILURE );
282 fprintf( stderr, "%s: -p previously specified\n", prog );
285 ldapport = atoi( optarg );
288 switch( atoi(optarg) ) {
290 if( version == LDAP_VERSION3 ) {
291 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
295 version = LDAP_VERSION2;
298 if( version == LDAP_VERSION2 ) {
299 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
303 version = LDAP_VERSION3;
306 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
309 return( EXIT_FAILURE );
312 #ifdef HAVE_CYRUS_SASL
313 if( version == LDAP_VERSION2 ) {
314 fprintf( stderr, "%s: -Q incompatible with version %d\n",
318 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
319 fprintf( stderr, "%s: incompatible previous "
320 "authentication choice\n",
324 authmethod = LDAP_AUTH_SASL;
325 version = LDAP_VERSION3;
326 sasl_flags = LDAP_SASL_QUIET;
329 fprintf( stderr, "%s: not compiled with SASL support\n",
331 return( EXIT_FAILURE );
334 #ifdef HAVE_CYRUS_SASL
335 if( sasl_realm != NULL ) {
336 fprintf( stderr, "%s: -R previously specified\n", prog );
339 if( version == LDAP_VERSION2 ) {
340 fprintf( stderr, "%s: -R incompatible with version %d\n",
344 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
345 fprintf( stderr, "%s: incompatible previous "
346 "authentication choice\n",
350 authmethod = LDAP_AUTH_SASL;
351 version = LDAP_VERSION3;
352 sasl_realm = strdup( optarg );
354 fprintf( stderr, "%s: not compiled with SASL support\n",
356 return( EXIT_FAILURE );
360 #ifdef HAVE_CYRUS_SASL
361 if( sasl_authc_id != NULL ) {
362 fprintf( stderr, "%s: -U previously specified\n", prog );
365 if( version == LDAP_VERSION2 ) {
366 fprintf( stderr, "%s: -U incompatible with version %d\n",
370 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
371 fprintf( stderr, "%s: incompatible previous "
372 "authentication choice\n",
376 authmethod = LDAP_AUTH_SASL;
377 version = LDAP_VERSION3;
378 sasl_authc_id = strdup( optarg );
380 fprintf( stderr, "%s: not compiled with SASL support\n",
382 return( EXIT_FAILURE );
385 case 'v': /* verbose mode */
388 case 'w': /* password */
389 passwd.bv_val = strdup( optarg );
393 for( p = optarg; *p != '\0'; p++ ) {
397 passwd.bv_len = strlen( passwd.bv_val );
403 #ifdef HAVE_CYRUS_SASL
404 if( sasl_mech != NULL ) {
405 fprintf( stderr, "%s: -Y previously specified\n", prog );
408 if( version == LDAP_VERSION2 ) {
409 fprintf( stderr, "%s: -Y incompatible with version %d\n",
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
414 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
417 authmethod = LDAP_AUTH_SASL;
418 version = LDAP_VERSION3;
419 sasl_mech = strdup( optarg );
421 fprintf( stderr, "%s: not compiled with SASL support\n",
423 return( EXIT_FAILURE );
427 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
428 fprintf( stderr, "%s: incompatible with previous "
429 "authentication choice\n", prog );
432 authmethod = LDAP_AUTH_SIMPLE;
435 #ifdef HAVE_CYRUS_SASL
436 if( sasl_authz_id != NULL ) {
437 fprintf( stderr, "%s: -X previously specified\n", prog );
440 if( version == LDAP_VERSION2 ) {
441 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
445 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
446 fprintf( stderr, "%s: -X incompatible with "
447 "authentication choice\n", prog );
450 authmethod = LDAP_AUTH_SASL;
451 version = LDAP_VERSION3;
452 sasl_authz_id = strdup( optarg );
454 fprintf( stderr, "%s: not compiled with SASL support\n",
456 return( EXIT_FAILURE );
461 if( version == LDAP_VERSION2 ) {
462 fprintf( stderr, "%s: -Z incompatible with version %d\n",
466 version = LDAP_VERSION3;
469 fprintf( stderr, "%s: not compiled with TLS support\n",
471 return( EXIT_FAILURE );
477 fprintf( stderr, "%s: unrecognized option -%c\n",
483 if (authmethod == -1) {
484 #ifdef HAVE_CYRUS_SASL
485 authmethod = LDAP_AUTH_SASL;
487 authmethod = LDAP_AUTH_SIMPLE;
491 if( argc - optind > 1 ) {
493 } else if ( argc - optind == 1 ) {
494 user = strdup( argv[optind] );
499 if( want_oldpw && oldpw == NULL ) {
500 /* prompt for old password */
502 oldpw = strdup(getpassphrase("Old password: "));
503 ckoldpw = getpassphrase("Re-enter old password: ");
505 if( oldpw== NULL || ckoldpw == NULL ||
506 strcmp( oldpw, ckoldpw ))
508 fprintf( stderr, "passwords do not match\n" );
513 if( want_newpw && newpw == NULL ) {
514 /* prompt for new password */
516 newpw = strdup(getpassphrase("New password: "));
517 cknewpw = getpassphrase("Re-enter new password: ");
519 if( newpw== NULL || cknewpw == NULL ||
520 strcmp( newpw, cknewpw ))
522 fprintf( stderr, "passwords do not match\n" );
527 if (want_bindpw && passwd.bv_val == NULL ) {
528 /* handle bind password */
529 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
530 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
534 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
535 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
537 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
538 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
543 (void) SIGNAL( SIGPIPE, SIG_IGN );
546 /* connect to server */
547 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
549 fprintf( stderr, "ldap_init( %s, %d )\n",
550 ldaphost != NULL ? ldaphost : "<DEFAULT>",
554 ld = ldap_init( ldaphost, ldapport );
556 perror("ldapwhoami: ldap_init");
562 fprintf( stderr, "ldap_initialize( %s )\n",
563 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
566 rc = ldap_initialize( &ld, ldapuri );
567 if( rc != LDAP_SUCCESS ) {
568 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
569 rc, ldap_err2string(rc) );
575 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
576 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
578 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
579 referrals ? "on" : "off" );
584 version = LDAP_VERSION3;
585 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
587 if(rc != LDAP_OPT_SUCCESS ) {
588 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
592 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
593 ldap_perror( ld, "ldap_start_tls" );
595 return( EXIT_FAILURE );
599 if ( authmethod == LDAP_AUTH_SASL ) {
600 #ifdef HAVE_CYRUS_SASL
603 if( sasl_secprops != NULL ) {
604 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
605 (void *) sasl_secprops );
607 if( rc != LDAP_OPT_SUCCESS ) {
609 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
611 return( EXIT_FAILURE );
615 defaults = lutil_sasl_defaults( ld,
622 rc = ldap_sasl_interactive_bind_s( ld, binddn,
623 sasl_mech, NULL, NULL,
624 sasl_flags, lutil_sasl_interact, defaults );
626 if( rc != LDAP_SUCCESS ) {
627 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
628 return( EXIT_FAILURE );
631 fprintf( stderr, "%s: not compiled with SASL support\n",
633 return( EXIT_FAILURE );
637 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
639 ldap_perror( ld, "ldap_bind" );
640 return( EXIT_FAILURE );
649 rc = ldap_extended_operation( ld,
650 LDAP_EXOP_X_WHO_AM_I, NULL,
653 if( rc != LDAP_SUCCESS ) {
654 ldap_perror( ld, "ldap_extended_operation" );
659 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
661 ldap_perror( ld, "ldappasswd: ldap_result" );
665 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
667 if( rc != LDAP_SUCCESS ) {
668 ldap_perror( ld, "ldap_parse_result" );
672 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
674 if( rc != LDAP_SUCCESS ) {
675 ldap_perror( ld, "ldap_parse_result" );
679 if( retdata != NULL ) {
680 if( retdata->bv_len == 0 ) {
681 printf("anonymous\n" );
683 printf("%s\n", retdata->bv_val );
687 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
688 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
690 if( text && *text ) {
691 printf( "Additional info: %s\n", text );
694 if( matcheddn && *matcheddn ) {
695 printf( "Matched DN: %s\n", matcheddn );
700 for( i=0; refs[i]; i++ ) {
701 printf("Referral: %s\n", refs[i] );
707 ber_memfree( matcheddn );
708 ber_memvfree( (void **) refs );
709 ber_memfree( retoid );
710 ber_bvfree( retdata );
713 /* disconnect from server */
716 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;