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 search\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;
96 struct berval *bv = NULL;
100 char *matcheddn = NULL, *text = NULL, **refs = NULL;
102 struct berval *retdata = NULL;
104 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
106 while( (i = getopt( argc, argv, "Aa:Ss:"
107 "Cd:D:h:H:InO:p:QR:U:vw:WxX:Y:Z" )) != EOF )
110 /* Password Options */
111 case 'A': /* prompt for old password */
115 case 'a': /* old password (secret) */
116 oldpw = strdup (optarg);
121 for( p = optarg; *p != '\0'; p++ ) {
127 case 'S': /* prompt for user password */
131 case 's': /* new password (secret) */
132 newpw = strdup (optarg);
136 for( p = optarg; *p != '\0'; p++ ) {
142 /* Common Options (including options we don't use) */
147 debug |= atoi( optarg );
149 case 'D': /* bind DN */
150 if( binddn != NULL ) {
151 fprintf( stderr, "%s: -D previously specified\n", prog );
154 binddn = strdup( optarg );
156 case 'h': /* ldap host */
157 if( ldapuri != NULL ) {
158 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
161 if( ldaphost != NULL ) {
162 fprintf( stderr, "%s: -h previously specified\n", prog );
165 ldaphost = strdup( optarg );
167 case 'H': /* ldap URI */
168 if( ldaphost != NULL ) {
169 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
173 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
176 if( ldapuri != NULL ) {
177 fprintf( stderr, "%s: -H previously specified\n", prog );
180 ldapuri = strdup( optarg );
183 #ifdef HAVE_CYRUS_SASL
184 if( version == LDAP_VERSION2 ) {
185 fprintf( stderr, "%s: -I incompatible with version %d\n",
189 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
190 fprintf( stderr, "%s: incompatible previous "
191 "authentication choice\n",
195 authmethod = LDAP_AUTH_SASL;
196 version = LDAP_VERSION3;
197 sasl_flags = LDAP_SASL_INTERACTIVE;
200 fprintf( stderr, "%s: was not compiled with SASL support\n",
202 return( EXIT_FAILURE );
204 case 'k': /* kerberos bind */
205 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
206 if( version > LDAP_VERSION2 ) {
207 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
212 if( authmethod != -1 ) {
213 fprintf( stderr, "%s: -k incompatible with previous "
214 "authentication choice\n", prog );
218 authmethod = LDAP_AUTH_KRBV4;
220 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
224 case 'K': /* kerberos bind, part one only */
225 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
226 if( version > LDAP_VERSION2 ) {
227 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
231 if( authmethod != -1 ) {
232 fprintf( stderr, "%s: incompatible with previous "
233 "authentication choice\n", prog );
237 authmethod = LDAP_AUTH_KRBV41;
239 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
240 return( EXIT_FAILURE );
244 /* enable Manage DSA IT */
245 if( version == LDAP_VERSION2 ) {
246 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
251 version = LDAP_VERSION3;
253 case 'n': /* print deletes, don't actually do them */
257 #ifdef HAVE_CYRUS_SASL
258 if( sasl_secprops != NULL ) {
259 fprintf( stderr, "%s: -O previously specified\n", prog );
262 if( version == LDAP_VERSION2 ) {
263 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
267 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
268 fprintf( stderr, "%s: incompatible previous "
269 "authentication choice\n", prog );
272 authmethod = LDAP_AUTH_SASL;
273 version = LDAP_VERSION3;
274 sasl_secprops = strdup( optarg );
276 fprintf( stderr, "%s: not compiled with SASL support\n",
278 return( EXIT_FAILURE );
283 fprintf( stderr, "%s: -p previously specified\n", prog );
286 ldapport = atoi( optarg );
289 switch( atoi(optarg) ) {
291 if( version == LDAP_VERSION3 ) {
292 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
296 version = LDAP_VERSION2;
299 if( version == LDAP_VERSION2 ) {
300 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
304 version = LDAP_VERSION3;
307 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
310 return( EXIT_FAILURE );
313 #ifdef HAVE_CYRUS_SASL
314 if( version == LDAP_VERSION2 ) {
315 fprintf( stderr, "%s: -Q incompatible with version %d\n",
319 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
320 fprintf( stderr, "%s: incompatible previous "
321 "authentication choice\n",
325 authmethod = LDAP_AUTH_SASL;
326 version = LDAP_VERSION3;
327 sasl_flags = LDAP_SASL_QUIET;
330 fprintf( stderr, "%s: not compiled with SASL support\n",
332 return( EXIT_FAILURE );
335 #ifdef HAVE_CYRUS_SASL
336 if( sasl_realm != NULL ) {
337 fprintf( stderr, "%s: -R previously specified\n", prog );
340 if( version == LDAP_VERSION2 ) {
341 fprintf( stderr, "%s: -R incompatible with version %d\n",
345 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
346 fprintf( stderr, "%s: incompatible previous "
347 "authentication choice\n",
351 authmethod = LDAP_AUTH_SASL;
352 version = LDAP_VERSION3;
353 sasl_realm = strdup( optarg );
355 fprintf( stderr, "%s: not compiled with SASL support\n",
357 return( EXIT_FAILURE );
361 #ifdef HAVE_CYRUS_SASL
362 if( sasl_authc_id != NULL ) {
363 fprintf( stderr, "%s: -U previously specified\n", prog );
366 if( version == LDAP_VERSION2 ) {
367 fprintf( stderr, "%s: -U incompatible with version %d\n",
371 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
372 fprintf( stderr, "%s: incompatible previous "
373 "authentication choice\n",
377 authmethod = LDAP_AUTH_SASL;
378 version = LDAP_VERSION3;
379 sasl_authc_id = strdup( optarg );
381 fprintf( stderr, "%s: not compiled with SASL support\n",
383 return( EXIT_FAILURE );
386 case 'v': /* verbose mode */
389 case 'w': /* password */
390 passwd.bv_val = strdup( optarg );
394 for( p = optarg; *p != '\0'; p++ ) {
398 passwd.bv_len = strlen( passwd.bv_val );
404 #ifdef HAVE_CYRUS_SASL
405 if( sasl_mech != NULL ) {
406 fprintf( stderr, "%s: -Y previously specified\n", prog );
409 if( version == LDAP_VERSION2 ) {
410 fprintf( stderr, "%s: -Y incompatible with version %d\n",
414 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
415 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
418 authmethod = LDAP_AUTH_SASL;
419 version = LDAP_VERSION3;
420 sasl_mech = strdup( optarg );
422 fprintf( stderr, "%s: not compiled with SASL support\n",
424 return( EXIT_FAILURE );
428 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
429 fprintf( stderr, "%s: incompatible with previous "
430 "authentication choice\n", prog );
433 authmethod = LDAP_AUTH_SIMPLE;
436 #ifdef HAVE_CYRUS_SASL
437 if( sasl_authz_id != NULL ) {
438 fprintf( stderr, "%s: -X previously specified\n", prog );
441 if( version == LDAP_VERSION2 ) {
442 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
446 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
447 fprintf( stderr, "%s: -X incompatible with "
448 "authentication choice\n", prog );
451 authmethod = LDAP_AUTH_SASL;
452 version = LDAP_VERSION3;
453 sasl_authz_id = strdup( optarg );
455 fprintf( stderr, "%s: not compiled with SASL support\n",
457 return( EXIT_FAILURE );
462 if( version == LDAP_VERSION2 ) {
463 fprintf( stderr, "%s: -Z incompatible with version %d\n",
467 version = LDAP_VERSION3;
470 fprintf( stderr, "%s: not compiled with TLS support\n",
472 return( EXIT_FAILURE );
478 fprintf( stderr, "%s: unrecognized option -%c\n",
484 if (authmethod == -1) {
485 #ifdef HAVE_CYRUS_SASL
486 authmethod = LDAP_AUTH_SASL;
488 authmethod = LDAP_AUTH_SIMPLE;
492 if( argc - optind > 1 ) {
494 } else if ( argc - optind == 1 ) {
495 user = strdup( argv[optind] );
500 if( want_oldpw && oldpw == NULL ) {
501 /* prompt for old password */
503 oldpw = strdup(getpassphrase("Old password: "));
504 ckoldpw = getpassphrase("Re-enter old password: ");
506 if( oldpw== NULL || ckoldpw == NULL ||
507 strcmp( oldpw, ckoldpw ))
509 fprintf( stderr, "passwords do not match\n" );
514 if( want_newpw && newpw == NULL ) {
515 /* prompt for new password */
517 newpw = strdup(getpassphrase("New password: "));
518 cknewpw = getpassphrase("Re-enter new password: ");
520 if( newpw== NULL || cknewpw == NULL ||
521 strcmp( newpw, cknewpw ))
523 fprintf( stderr, "passwords do not match\n" );
528 if (want_bindpw && passwd.bv_val == NULL ) {
529 /* handle bind password */
530 passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
531 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
535 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
536 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
538 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
539 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
544 (void) SIGNAL( SIGPIPE, SIG_IGN );
547 /* connect to server */
548 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
550 fprintf( stderr, "ldap_init( %s, %d )\n",
551 ldaphost != NULL ? ldaphost : "<DEFAULT>",
555 ld = ldap_init( ldaphost, ldapport );
557 perror("ldapsearch: ldap_init");
563 fprintf( stderr, "ldap_initialize( %s )\n",
564 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
567 rc = ldap_initialize( &ld, ldapuri );
568 if( rc != LDAP_SUCCESS ) {
569 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
570 rc, ldap_err2string(rc) );
576 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
577 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
579 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
580 referrals ? "on" : "off" );
585 version = LDAP_VERSION3;
586 rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
588 if(rc != LDAP_OPT_SUCCESS ) {
589 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
593 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
594 ldap_perror( ld, "ldap_start_tls" );
596 return( EXIT_FAILURE );
600 if ( authmethod == LDAP_AUTH_SASL ) {
601 #ifdef HAVE_CYRUS_SASL
604 if( sasl_secprops != NULL ) {
605 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
606 (void *) sasl_secprops );
608 if( rc != LDAP_OPT_SUCCESS ) {
610 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
612 return( EXIT_FAILURE );
616 defaults = lutil_sasl_defaults( ld,
623 rc = ldap_sasl_interactive_bind_s( ld, binddn,
624 sasl_mech, NULL, NULL,
625 sasl_flags, lutil_sasl_interact, defaults );
627 if( rc != LDAP_SUCCESS ) {
628 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
629 return( EXIT_FAILURE );
632 fprintf( stderr, "%s: not compiled with SASL support\n",
634 return( EXIT_FAILURE );
638 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
640 ldap_perror( ld, "ldap_bind" );
641 return( EXIT_FAILURE );
650 rc = ldap_extended_operation( ld,
651 LDAP_EXOP_X_WHO_AM_I, NULL,
654 if( rc != LDAP_SUCCESS ) {
655 ldap_perror( ld, "ldap_extended_operation" );
660 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
662 ldap_perror( ld, "ldappasswd: ldap_result" );
666 rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 );
668 if( rc != LDAP_SUCCESS ) {
669 ldap_perror( ld, "ldap_parse_result" );
673 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
675 if( rc != LDAP_SUCCESS ) {
676 ldap_perror( ld, "ldap_parse_result" );
680 if( retdata != NULL ) {
681 if( retdata->bv_len == 0 ) {
682 printf("anonymous\n" );
684 printf("%s\n", retdata->bv_val );
688 if( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs ) {
689 printf( "Result: %s (%d)\n", ldap_err2string( code ), code );
691 if( text && *text ) {
692 printf( "Additional info: %s\n", text );
695 if( matcheddn && *matcheddn ) {
696 printf( "Matched DN: %s\n", matcheddn );
701 for( i=0; refs[i]; i++ ) {
702 printf("Referral: %s\n", refs[i] );
708 ber_memfree( matcheddn );
709 ber_memvfree( (void **) refs );
710 ber_memfree( retoid );
711 ber_bvfree( retdata );
714 /* disconnect from server */