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/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
21 #include "lutil_ldap.h"
22 #include "ldap_defaults.h"
31 "Issue LDAP Who am I? operation to request user's authzid\n\n"
32 "usage: %s [options]\n"
39 const char options[] = "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:y:Y:Z";
42 handle_private_option( int i )
46 char *control, *cvalue;
48 case 'E': /* whoami controls */
49 if( version == LDAP_VERSION2 ) {
50 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
55 /* should be extended to support comma separated list of
56 * [!]key[=value] parameters, e.g. -E !foo,bar=567
61 if( optarg[0] == '!' ) {
66 control = strdup( optarg );
67 if ( (cvalue = strchr( control, '=' )) != NULL ) {
70 fprintf( stderr, "Invalid whoami control name: %s\n", control );
82 main( int argc, char *argv[] )
89 char *matcheddn = NULL, *text = NULL, **refs = NULL;
91 struct berval *retdata = NULL;
93 prog = lutil_progname( "ldapwhoami", argc, argv );
96 version = LDAP_VERSION3;
98 tool_args( argc, argv );
100 if( argc - optind > 1 ) {
102 } else if ( argc - optind == 1 ) {
103 user = strdup( argv[optind] );
108 if ( pw_file || want_bindpw ) {
110 rc = lutil_get_filed_password( pw_file, &passwd );
111 if( rc ) return EXIT_FAILURE;
113 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
114 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
118 ld = tool_conn_setup( 0, 0 );
127 if ( authzid || manageDSAit || noop )
128 tool_server_controls( ld, NULL, 0 );
130 rc = ldap_whoami_s( ld, &retdata, NULL, NULL );
132 if( retdata != NULL ) {
133 if( retdata->bv_len == 0 ) {
134 printf("anonymous\n" );
136 printf("%s\n", retdata->bv_val );
140 if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
141 printf( "Result: %s (%d)\n", ldap_err2string( rc ), rc );
143 if( text && *text ) {
144 printf( "Additional info: %s\n", text );
147 if( matcheddn && *matcheddn ) {
148 printf( "Matched DN: %s\n", matcheddn );
153 for( i=0; refs[i]; i++ ) {
154 printf("Referral: %s\n", refs[i] );
160 ber_memfree( matcheddn );
161 ber_memvfree( (void **) refs );
162 ber_memfree( retoid );
163 ber_bvfree( retdata );
166 /* disconnect from server */
169 return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;