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 file Read passwd from file\n"
53 " -Y mech SASL mechanism\n"
54 " -Z Start TLS request (-ZZ to require successful response)\n"
61 main( int argc, char *argv[] )
65 char *ldaphost = NULL;
71 struct berval passwd = { 0, NULL };
82 #ifdef HAVE_CYRUS_SASL
83 unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
84 char *sasl_realm = NULL;
85 char *sasl_authc_id = NULL;
86 char *sasl_authz_id = NULL;
87 char *sasl_mech = NULL;
88 char *sasl_secprops = NULL;
94 int id, code = LDAP_OTHER;
96 char *matcheddn = NULL, *text = NULL, **refs = NULL;
98 struct berval *retdata = NULL;
100 prog = lutil_progname( "ldapwhoami", argc, argv );
102 while( (i = getopt( argc, argv,
103 "Cd:D:h:H:InO:p:QR:U:vw:WxX:y:Y:Z" )) != EOF )
106 /* Common Options (including options we don't use) */
111 debug |= atoi( optarg );
113 case 'D': /* bind DN */
114 if( binddn != NULL ) {
115 fprintf( stderr, "%s: -D previously specified\n", prog );
118 binddn = strdup( optarg );
120 case 'h': /* ldap host */
121 if( ldapuri != NULL ) {
122 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
125 if( ldaphost != NULL ) {
126 fprintf( stderr, "%s: -h previously specified\n", prog );
129 ldaphost = strdup( optarg );
131 case 'H': /* ldap URI */
132 if( ldaphost != NULL ) {
133 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
137 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
140 if( ldapuri != NULL ) {
141 fprintf( stderr, "%s: -H previously specified\n", prog );
144 ldapuri = strdup( optarg );
147 #ifdef HAVE_CYRUS_SASL
148 if( version == LDAP_VERSION2 ) {
149 fprintf( stderr, "%s: -I incompatible with version %d\n",
153 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
154 fprintf( stderr, "%s: incompatible previous "
155 "authentication choice\n",
159 authmethod = LDAP_AUTH_SASL;
160 version = LDAP_VERSION3;
161 sasl_flags = LDAP_SASL_INTERACTIVE;
164 fprintf( stderr, "%s: was not compiled with SASL support\n",
166 return( EXIT_FAILURE );
168 case 'k': /* kerberos bind */
169 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
170 if( version > LDAP_VERSION2 ) {
171 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
176 if( authmethod != -1 ) {
177 fprintf( stderr, "%s: -k incompatible with previous "
178 "authentication choice\n", prog );
182 authmethod = LDAP_AUTH_KRBV4;
184 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
188 case 'K': /* kerberos bind, part one only */
189 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
190 if( version > LDAP_VERSION2 ) {
191 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
195 if( authmethod != -1 ) {
196 fprintf( stderr, "%s: incompatible with previous "
197 "authentication choice\n", prog );
201 authmethod = LDAP_AUTH_KRBV41;
203 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
204 return( EXIT_FAILURE );
207 case 'n': /* print deletes, don't actually do them */
211 #ifdef HAVE_CYRUS_SASL
212 if( sasl_secprops != NULL ) {
213 fprintf( stderr, "%s: -O previously specified\n", prog );
216 if( version == LDAP_VERSION2 ) {
217 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
221 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
222 fprintf( stderr, "%s: incompatible previous "
223 "authentication choice\n", prog );
226 authmethod = LDAP_AUTH_SASL;
227 version = LDAP_VERSION3;
228 sasl_secprops = strdup( optarg );
230 fprintf( stderr, "%s: not compiled with SASL support\n",
232 return( EXIT_FAILURE );
237 fprintf( stderr, "%s: -p previously specified\n", prog );
240 ldapport = atoi( optarg );
243 switch( atoi(optarg) ) {
245 if( version == LDAP_VERSION3 ) {
246 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
250 version = LDAP_VERSION2;
253 if( version == LDAP_VERSION2 ) {
254 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
258 version = LDAP_VERSION3;
261 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
264 return( EXIT_FAILURE );
267 #ifdef HAVE_CYRUS_SASL
268 if( version == LDAP_VERSION2 ) {
269 fprintf( stderr, "%s: -Q incompatible with version %d\n",
273 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
274 fprintf( stderr, "%s: incompatible previous "
275 "authentication choice\n",
279 authmethod = LDAP_AUTH_SASL;
280 version = LDAP_VERSION3;
281 sasl_flags = LDAP_SASL_QUIET;
284 fprintf( stderr, "%s: not compiled with SASL support\n",
286 return( EXIT_FAILURE );
289 #ifdef HAVE_CYRUS_SASL
290 if( sasl_realm != NULL ) {
291 fprintf( stderr, "%s: -R previously specified\n", prog );
294 if( version == LDAP_VERSION2 ) {
295 fprintf( stderr, "%s: -R incompatible with version %d\n",
299 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
300 fprintf( stderr, "%s: incompatible previous "
301 "authentication choice\n",
305 authmethod = LDAP_AUTH_SASL;
306 version = LDAP_VERSION3;
307 sasl_realm = strdup( optarg );
309 fprintf( stderr, "%s: not compiled with SASL support\n",
311 return( EXIT_FAILURE );
315 #ifdef HAVE_CYRUS_SASL
316 if( sasl_authc_id != NULL ) {
317 fprintf( stderr, "%s: -U previously specified\n", prog );
320 if( version == LDAP_VERSION2 ) {
321 fprintf( stderr, "%s: -U incompatible with version %d\n",
325 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
326 fprintf( stderr, "%s: incompatible previous "
327 "authentication choice\n",
331 authmethod = LDAP_AUTH_SASL;
332 version = LDAP_VERSION3;
333 sasl_authc_id = strdup( optarg );
335 fprintf( stderr, "%s: not compiled with SASL support\n",
337 return( EXIT_FAILURE );
340 case 'v': /* verbose mode */
343 case 'w': /* password */
344 passwd.bv_val = strdup( optarg );
348 for( p = optarg; *p != '\0'; p++ ) {
352 passwd.bv_len = strlen( passwd.bv_val );
361 #ifdef HAVE_CYRUS_SASL
362 if( sasl_mech != NULL ) {
363 fprintf( stderr, "%s: -Y previously specified\n", prog );
366 if( version == LDAP_VERSION2 ) {
367 fprintf( stderr, "%s: -Y incompatible with version %d\n",
371 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
372 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
375 authmethod = LDAP_AUTH_SASL;
376 version = LDAP_VERSION3;
377 sasl_mech = strdup( optarg );
379 fprintf( stderr, "%s: not compiled with SASL support\n",
381 return( EXIT_FAILURE );
385 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
386 fprintf( stderr, "%s: incompatible with previous "
387 "authentication choice\n", prog );
390 authmethod = LDAP_AUTH_SIMPLE;
393 #ifdef HAVE_CYRUS_SASL
394 if( sasl_authz_id != NULL ) {
395 fprintf( stderr, "%s: -X previously specified\n", prog );
398 if( version == LDAP_VERSION2 ) {
399 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
403 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
404 fprintf( stderr, "%s: -X incompatible with "
405 "authentication choice\n", prog );
408 authmethod = LDAP_AUTH_SASL;
409 version = LDAP_VERSION3;
410 sasl_authz_id = strdup( optarg );
412 fprintf( stderr, "%s: not compiled with SASL support\n",
414 return( EXIT_FAILURE );
419 if( version == LDAP_VERSION2 ) {
420 fprintf( stderr, "%s: -Z incompatible with version %d\n",
424 version = LDAP_VERSION3;
427 fprintf( stderr, "%s: not compiled with TLS support\n",
429 return( EXIT_FAILURE );
435 fprintf( stderr, "%s: unrecognized option -%c\n",
441 if (authmethod == -1) {
442 #ifdef HAVE_CYRUS_SASL
443 authmethod = LDAP_AUTH_SASL;
445 authmethod = LDAP_AUTH_SIMPLE;
449 if( argc - optind > 1 ) {
451 } else if ( argc - optind == 1 ) {
452 user = strdup( argv[optind] );
457 if ( pw_file || want_bindpw ) {
459 rc = lutil_get_filed_password( pw_file, &passwd );
460 if( rc ) return EXIT_FAILURE;
462 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
463 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
468 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
469 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
471 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
472 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
477 (void) SIGNAL( SIGPIPE, SIG_IGN );
480 /* connect to server */
481 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
483 fprintf( stderr, "ldap_init( %s, %d )\n",
484 ldaphost != NULL ? ldaphost : "<DEFAULT>",
488 ld = ldap_init( ldaphost, ldapport );
490 perror("ldapwhoami: ldap_init");
496 fprintf( stderr, "ldap_initialize( %s )\n",
497 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
500 rc = ldap_initialize( &ld, ldapuri );
501 if( rc != LDAP_SUCCESS ) {
502 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
503 rc, ldap_err2string(rc) );
509 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
510 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
512 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
513 referrals ? "on" : "off" );
518 version = LDAP_VERSION3;
519 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
521 if(rc != LDAP_OPT_SUCCESS ) {
522 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
526 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
527 ldap_perror( ld, "ldap_start_tls" );
529 return( EXIT_FAILURE );
533 if ( authmethod == LDAP_AUTH_SASL ) {
534 #ifdef HAVE_CYRUS_SASL
537 if( sasl_secprops != NULL ) {
538 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
539 (void *) sasl_secprops );
541 if( rc != LDAP_OPT_SUCCESS ) {
543 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
545 return( EXIT_FAILURE );
549 defaults = lutil_sasl_defaults( ld,
556 rc = ldap_sasl_interactive_bind_s( ld, binddn,
557 sasl_mech, NULL, NULL,
558 sasl_flags, lutil_sasl_interact, defaults );
560 if( rc != LDAP_SUCCESS ) {
561 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
562 return( EXIT_FAILURE );
565 fprintf( stderr, "%s: not compiled with SASL support\n",
567 return( EXIT_FAILURE );
571 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
573 ldap_perror( ld, "ldap_bind" );
574 return( EXIT_FAILURE );
583 rc = ldap_extended_operation( ld,
584 LDAP_EXOP_X_WHO_AM_I, NULL,
587 if( rc != LDAP_SUCCESS ) {
588 ldap_perror( ld, "ldap_extended_operation" );
593 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
595 ldap_perror( ld, "ldappasswd: ldap_result" );
599 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
601 if( rc != LDAP_SUCCESS ) {
602 ldap_perror( ld, "ldap_parse_result" );
606 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
608 if( rc != LDAP_SUCCESS ) {
609 ldap_perror( ld, "ldap_parse_result" );
613 if( retdata != NULL ) {
614 if( retdata->bv_len == 0 ) {
615 printf("anonymous\n" );
617 printf("%s\n", retdata->bv_val );
621 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
622 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
624 if( text && *text ) {
625 printf( "Additional info: %s\n", text );
628 if( matcheddn && *matcheddn ) {
629 printf( "Matched DN: %s\n", matcheddn );
634 for( i=0; refs[i]; i++ ) {
635 printf("Referral: %s\n", refs[i] );
641 ber_memfree( matcheddn );
642 ber_memvfree( (void **) refs );
643 ber_memfree( retoid );
644 ber_bvfree( retdata );
647 /* disconnect from server */
650 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;