]> git.sur5r.net Git - openldap/blob - clients/tools/ldapwhoami.c
Plug memory leak
[openldap] / clients / tools / ldapwhoami.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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,
31 "Issue LDAP Who am I? operation to request user's authzid\n\n"
32 "usage: %s [options]\n"
33                 , prog);
34         tool_common_usage();
35         exit( EXIT_FAILURE );
36 }
37
38
39 const char options[] = "Cd:D:e:h:H:InO:p:QR:U:vw:WxX:y:Y:Z";
40
41 int
42 handle_private_option( int i )
43 {
44         switch ( i ) {
45 #if 0
46                 char    *control, *cvalue;
47                 int             crit;
48         case 'E': /* whoami controls */
49                 if( version == LDAP_VERSION2 ) {
50                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
51                                 prog, version );
52                         exit( EXIT_FAILURE );
53                 }
54
55                 /* should be extended to support comma separated list of
56                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
57                  */
58
59                 crit = 0;
60                 cvalue = NULL;
61                 if( optarg[0] == '!' ) {
62                         crit = 1;
63                         optarg++;
64                 }
65
66                 control = strdup( optarg );
67                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
68                         *cvalue++ = '\0';
69                 }
70                 fprintf( stderr, "Invalid whoami control name: %s\n", control );
71                 usage();
72 #endif
73
74         default:
75                 return 0;
76         }
77         return 1;
78 }
79
80
81 int
82 main( int argc, char *argv[] )
83 {
84         int rc;
85         char    *user = NULL;
86
87         LDAP           *ld = NULL;
88
89         char *matcheddn = NULL, *text = NULL, **refs = NULL;
90         char    *retoid = NULL;
91         struct berval *retdata = NULL;
92
93         prog = lutil_progname( "ldapwhoami", argc, argv );
94
95         /* LDAPv3 only */
96         version = 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 ( authzid || manageDSAit || noop )
128                 tool_server_controls( ld, NULL, 0 );
129
130         rc = ldap_whoami_s( ld, &retdata, NULL, NULL ); 
131
132         if( retdata != NULL ) {
133                 if( retdata->bv_len == 0 ) {
134                         printf("anonymous\n" );
135                 } else {
136                         printf("%s\n", retdata->bv_val );
137                 }
138         }
139
140         if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
141                 printf( "Result: %s (%d)\n", ldap_err2string( rc ), rc );
142
143                 if( text && *text ) {
144                         printf( "Additional info: %s\n", text );
145                 }
146
147                 if( matcheddn && *matcheddn ) {
148                         printf( "Matched DN: %s\n", matcheddn );
149                 }
150
151                 if( refs ) {
152                         int i;
153                         for( i=0; refs[i]; i++ ) {
154                                 printf("Referral: %s\n", refs[i] );
155                         }
156                 }
157         }
158
159         ber_memfree( text );
160         ber_memfree( matcheddn );
161         ber_memvfree( (void **) refs );
162         ber_memfree( retoid );
163         ber_bvfree( retdata );
164
165 skip:
166         /* disconnect from server */
167         ldap_unbind (ld);
168
169         return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
170 }