3 * Copyright 1998-2003 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[] = ""
40 "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
43 handle_private_option( int i )
47 char *control, *cvalue;
49 case 'E': /* whoami controls */
50 if( protocol == LDAP_VERSION2 ) {
51 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
56 /* should be extended to support comma separated list of
57 * [!]key[=value] parameters, e.g. -E !foo,bar=567
62 if( optarg[0] == '!' ) {
67 control = strdup( optarg );
68 if ( (cvalue = strchr( control, '=' )) != NULL ) {
71 fprintf( stderr, "Invalid whoami control name: %s\n", control );
83 main( int argc, char *argv[] )
90 char *matcheddn = NULL, *text = NULL, **refs = NULL;
92 struct berval *retdata = NULL;
94 prog = lutil_progname( "ldapwhoami", argc, argv );
97 protocol = LDAP_VERSION3;
99 tool_args( argc, argv );
101 if( argc - optind > 1 ) {
103 } else if ( argc - optind == 1 ) {
104 user = strdup( argv[optind] );
109 if ( pw_file || want_bindpw ) {
111 rc = lutil_get_filed_password( pw_file, &passwd );
112 if( rc ) return EXIT_FAILURE;
114 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
115 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
119 ld = tool_conn_setup( 0, 0 );
128 if ( authzid || manageDSAit || noop )
129 tool_server_controls( ld, NULL, 0 );
131 rc = ldap_whoami_s( ld, &retdata, NULL, NULL );
133 if( retdata != NULL ) {
134 if( retdata->bv_len == 0 ) {
135 printf("anonymous\n" );
137 printf("%s\n", retdata->bv_val );
141 if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
142 printf( "Result: %s (%d)\n", ldap_err2string( rc ), rc );
144 if( text && *text ) {
145 printf( "Additional info: %s\n", text );
148 if( matcheddn && *matcheddn ) {
149 printf( "Matched DN: %s\n", matcheddn );
154 for( i=0; refs[i]; i++ ) {
155 printf("Referral: %s\n", refs[i] );
161 ber_memfree( matcheddn );
162 ber_memvfree( (void **) refs );
163 ber_memfree( retoid );
164 ber_bvfree( retdata );
167 /* disconnect from server */
170 return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;