1 /* ldapwhoami.c -- a tool for asking the directory "Who Am I?" */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 1998-2001 Net Boolean Incorporated.
8 * Portions Copyright 2001-2003 IBM Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that this notice is preserved and that due credit is given
24 * to the University of Michigan at Ann Arbor. The name of the
25 * University may not be used to endorse or promote products derived
26 * from this software without specific prior written permission. This
27 * software is provided ``as is'' without express or implied warranty.
30 * This work was originally developed by Kurt D. Zeilenga for inclusion
31 * in OpenLDAP Software based, in part, on other client tools.
38 #include <ac/stdlib.h>
41 #include <ac/socket.h>
42 #include <ac/string.h>
44 #include <ac/unistd.h>
48 #include "lutil_ldap.h"
49 #include "ldap_defaults.h"
57 fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
58 fprintf( stderr, _("usage: %s [options]\n"), prog);
64 const char options[] = ""
65 "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
68 handle_private_option( int i )
72 char *control, *cvalue;
74 case 'E': /* whoami extension */
75 if( protocol == LDAP_VERSION2 ) {
76 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
81 /* should be extended to support comma separated list of
82 * [!]key[=value] parameters, e.g. -E !foo,bar=567
87 if( optarg[0] == '!' ) {
92 control = strdup( optarg );
93 if ( (cvalue = strchr( control, '=' )) != NULL ) {
97 fprintf( stderr, _("Invalid whoami extension name: %s\n"), control );
109 main( int argc, char *argv[] )
116 char *matcheddn = NULL, *text = NULL, **refs = NULL;
118 struct berval *retdata = NULL;
121 LDAPControl **ctrls = NULL;
124 prog = lutil_progname( "ldapwhoami", argc, argv );
127 protocol = LDAP_VERSION3;
129 tool_args( argc, argv );
131 if( argc - optind > 1 ) {
133 } else if ( argc - optind == 1 ) {
134 user = strdup( argv[optind] );
139 if ( pw_file || want_bindpw ) {
141 rc = lutil_get_filed_password( pw_file, &passwd );
142 if( rc ) return EXIT_FAILURE;
144 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
145 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
149 ld = tool_conn_setup( 0, 0 );
158 if ( assertion || authzid || manageDSAit || noop ) {
159 tool_server_controls( ld, NULL, 0 );
162 rc = ldap_whoami( ld, NULL, NULL, &id );
164 if( rc != LDAP_SUCCESS ) {
165 ldap_perror( ld, "ldap_whoami" );
173 if ( tool_check_abandon( ld, id ) ) {
174 return LDAP_CANCELLED;
180 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
182 ldap_perror( ld, "ldapwhoami: ldap_result" );
191 rc = ldap_parse_result( ld, res,
192 &code, &matcheddn, &text, &refs, &ctrls, 0 );
194 if( rc != LDAP_SUCCESS ) {
195 ldap_perror( ld, "ldap_parse_result" );
200 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
202 if( rc != LDAP_SUCCESS ) {
203 ldap_perror( ld, "ldap_parse_result" );
208 if( retdata != NULL ) {
209 if( retdata->bv_len == 0 ) {
210 printf(_("anonymous\n") );
212 printf("%s\n", retdata->bv_val );
217 if ( verbose || ( code != LDAP_SUCCESS ) ||
218 matcheddn || text || refs || ctrls )
220 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
222 if( text && *text ) {
223 printf( _("Additional info: %s\n"), text );
226 if( matcheddn && *matcheddn ) {
227 printf( _("Matched DN: %s\n"), matcheddn );
232 for( i=0; refs[i]; i++ ) {
233 printf(_("Referral: %s\n"), refs[i] );
238 tool_print_ctrls( ld, ctrls );
239 ldap_controls_free( ctrls );
244 ber_memfree( matcheddn );
245 ber_memvfree( (void **) refs );
246 ber_memfree( retoid );
247 ber_bvfree( retdata );
249 /* disconnect from server */
253 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;