]> git.sur5r.net Git - openldap/blob - clients/tools/ldapwhoami.c
f0ea1c40d8bc782de6dc668bb0543a4f77fb2400
[openldap] / clients / tools / ldapwhoami.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17 #include <ac/unistd.h>
18
19 #include <ldap.h>
20 #include "lutil.h"
21 #include "lutil_ldap.h"
22 #include "ldap_defaults.h"
23
24 #include "common.h"
25
26
27 void
28 usage( void )
29 {
30         fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
31         fprintf( stderr, _("usage: %s [options]\n"), prog);
32         tool_common_usage();
33         exit( EXIT_FAILURE );
34 }
35
36
37 const char options[] = ""
38         "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
39
40 int
41 handle_private_option( int i )
42 {
43         switch ( i ) {
44 #if 0
45                 char    *control, *cvalue;
46                 int             crit;
47         case 'E': /* whoami controls */
48                 if( protocol == LDAP_VERSION2 ) {
49                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
50                                 prog, protocol );
51                         exit( EXIT_FAILURE );
52                 }
53
54                 /* should be extended to support comma separated list of
55                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
56                  */
57
58                 crit = 0;
59                 cvalue = NULL;
60                 if( optarg[0] == '!' ) {
61                         crit = 1;
62                         optarg++;
63                 }
64
65                 control = strdup( optarg );
66                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
67                         *cvalue++ = '\0';
68                 }
69                 fprintf( stderr, _("Invalid whoami control name: %s\n"), control );
70                 usage();
71 #endif
72
73         default:
74                 return 0;
75         }
76         return 1;
77 }
78
79
80 int
81 main( int argc, char *argv[] )
82 {
83         int rc;
84         char    *user = NULL;
85
86         LDAP           *ld = NULL;
87
88         char *matcheddn = NULL, *text = NULL, **refs = NULL;
89         char    *retoid = NULL;
90         struct berval *retdata = NULL;
91
92     tool_init();
93         prog = lutil_progname( "ldapwhoami", argc, argv );
94
95         /* LDAPv3 only */
96         protocol = LDAP_VERSION3;
97
98         tool_args( argc, argv );
99
100         if( argc - optind > 1 ) {
101                 usage();
102         } else if ( argc - optind == 1 ) {
103                 user = strdup( argv[optind] );
104         } else {
105                 user = NULL;
106         }
107
108         if ( pw_file || want_bindpw ) {
109                 if ( pw_file ) {
110                         rc = lutil_get_filed_password( pw_file, &passwd );
111                         if( rc ) return EXIT_FAILURE;
112                 } else {
113                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
114                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
115                 }
116         }
117
118         ld = tool_conn_setup( 0, 0 );
119
120         tool_bind( ld );
121
122         if ( not ) {
123                 rc = LDAP_SUCCESS;
124                 goto skip;
125         }
126
127         if ( assertion || authzid || manageDSAit || noop ) {
128                 tool_server_controls( ld, NULL, 0 );
129         }
130
131         rc = ldap_whoami_s( ld, &retdata, NULL, NULL ); 
132
133         if( retdata != NULL ) {
134                 if( retdata->bv_len == 0 ) {
135                         printf(_("anonymous\n") );
136                 } else {
137                         printf("%s\n", retdata->bv_val );
138                 }
139         }
140
141         if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
142                 printf( _("Result: %s (%d)\n"), ldap_err2string( rc ), rc );
143
144                 if( text && *text ) {
145                         printf( _("Additional info: %s\n"), text );
146                 }
147
148                 if( matcheddn && *matcheddn ) {
149                         printf( _("Matched DN: %s\n"), matcheddn );
150                 }
151
152                 if( refs ) {
153                         int i;
154                         for( i=0; refs[i]; i++ ) {
155                                 printf(_("Referral: %s\n"), refs[i] );
156                         }
157                 }
158         }
159
160         ber_memfree( text );
161         ber_memfree( matcheddn );
162         ber_memvfree( (void **) refs );
163         ber_memfree( retoid );
164         ber_bvfree( retdata );
165
166 skip:
167         /* disconnect from server */
168         ldap_unbind (ld);
169
170         return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
171 }