]> git.sur5r.net Git - openldap/blob - clients/tools/ldapwhoami.c
Add LDBM comment
[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,
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[] = ""
40         "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
41
42 int
43 handle_private_option( int i )
44 {
45         switch ( i ) {
46 #if 0
47                 char    *control, *cvalue;
48                 int             crit;
49         case 'E': /* whoami controls */
50                 if( protocol == LDAP_VERSION2 ) {
51                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
52                                 prog, protocol );
53                         exit( EXIT_FAILURE );
54                 }
55
56                 /* should be extended to support comma separated list of
57                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
58                  */
59
60                 crit = 0;
61                 cvalue = NULL;
62                 if( optarg[0] == '!' ) {
63                         crit = 1;
64                         optarg++;
65                 }
66
67                 control = strdup( optarg );
68                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
69                         *cvalue++ = '\0';
70                 }
71                 fprintf( stderr, "Invalid whoami control name: %s\n", control );
72                 usage();
73 #endif
74
75         default:
76                 return 0;
77         }
78         return 1;
79 }
80
81
82 int
83 main( int argc, char *argv[] )
84 {
85         int rc;
86         char    *user = NULL;
87
88         LDAP           *ld = NULL;
89
90         char *matcheddn = NULL, *text = NULL, **refs = NULL;
91         char    *retoid = NULL;
92         struct berval *retdata = NULL;
93
94         prog = lutil_progname( "ldapwhoami", argc, argv );
95
96         /* LDAPv3 only */
97         protocol = LDAP_VERSION3;
98
99         tool_args( argc, argv );
100
101         if( argc - optind > 1 ) {
102                 usage();
103         } else if ( argc - optind == 1 ) {
104                 user = strdup( argv[optind] );
105         } else {
106                 user = NULL;
107         }
108
109         if ( pw_file || want_bindpw ) {
110                 if ( pw_file ) {
111                         rc = lutil_get_filed_password( pw_file, &passwd );
112                         if( rc ) return EXIT_FAILURE;
113                 } else {
114                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
115                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
116                 }
117         }
118
119         ld = tool_conn_setup( 0, 0 );
120
121         tool_bind( ld );
122
123         if ( not ) {
124                 rc = LDAP_SUCCESS;
125                 goto skip;
126         }
127
128         if ( authzid || manageDSAit || noop )
129                 tool_server_controls( ld, NULL, 0 );
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 }