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