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"
30 fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
31 fprintf( stderr, _("usage: %s [options]\n"), prog);
37 const char options[] = ""
38 "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
41 handle_private_option( int i )
45 char *control, *cvalue;
47 case 'E': /* whoami controls */
48 if( protocol == LDAP_VERSION2 ) {
49 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
54 /* should be extended to support comma separated list of
55 * [!]key[=value] parameters, e.g. -E !foo,bar=567
60 if( optarg[0] == '!' ) {
65 control = strdup( optarg );
66 if ( (cvalue = strchr( control, '=' )) != NULL ) {
69 fprintf( stderr, _("Invalid whoami control name: %s\n"), control );
81 main( int argc, char *argv[] )
88 char *matcheddn = NULL, *text = NULL, **refs = NULL;
90 struct berval *retdata = NULL;
93 prog = lutil_progname( "ldapwhoami", argc, argv );
96 protocol = 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;