]> git.sur5r.net Git - openldap/blob - clients/tools/ldapwhoami.c
c4e613b4c25b52f0112310c3ddca86b41fb36b55
[openldap] / clients / tools / ldapwhoami.c
1 /* ldapwhoami.c -- a tool for asking the directory "Who Am I?" */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 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.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
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>.
18  */
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20  * All rights reserved.
21  *
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.
28  */
29 /* ACKNOWLEDGEMENTS:
30  * This work was originally developed by Kurt D. Zeilenga for inclusion
31  * in OpenLDAP Software based, in part, on other client tools.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/stdlib.h>
39
40 #include <ac/ctype.h>
41 #include <ac/socket.h>
42 #include <ac/string.h>
43 #include <ac/time.h>
44 #include <ac/unistd.h>
45
46 #include <ldap.h>
47 #include "lutil.h"
48 #include "lutil_ldap.h"
49 #include "ldap_defaults.h"
50
51 #include "common.h"
52
53
54 void
55 usage( void )
56 {
57         fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
58         fprintf( stderr, _("usage: %s [options]\n"), prog);
59         tool_common_usage();
60         exit( EXIT_FAILURE );
61 }
62
63
64 const char options[] = ""
65         "Cd:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
66
67 int
68 handle_private_option( int i )
69 {
70         switch ( i ) {
71 #if 0
72                 char    *control, *cvalue;
73                 int             crit;
74         case 'E': /* whoami controls */
75                 if( protocol == LDAP_VERSION2 ) {
76                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
77                                 prog, protocol );
78                         exit( EXIT_FAILURE );
79                 }
80
81                 /* should be extended to support comma separated list of
82                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
83                  */
84
85                 crit = 0;
86                 cvalue = NULL;
87                 if( optarg[0] == '!' ) {
88                         crit = 1;
89                         optarg++;
90                 }
91
92                 control = strdup( optarg );
93                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
94                         *cvalue++ = '\0';
95                 }
96                 fprintf( stderr, _("Invalid whoami control name: %s\n"), control );
97                 usage();
98 #endif
99
100         default:
101                 return 0;
102         }
103         return 1;
104 }
105
106
107 int
108 main( int argc, char *argv[] )
109 {
110         int rc;
111         char    *user = NULL;
112
113         LDAP           *ld = NULL;
114
115         char *matcheddn = NULL, *text = NULL, **refs = NULL;
116         char    *retoid = NULL;
117         struct berval *retdata = NULL;
118
119     tool_init();
120         prog = lutil_progname( "ldapwhoami", argc, argv );
121
122         /* LDAPv3 only */
123         protocol = LDAP_VERSION3;
124
125         tool_args( argc, argv );
126
127         if( argc - optind > 1 ) {
128                 usage();
129         } else if ( argc - optind == 1 ) {
130                 user = strdup( argv[optind] );
131         } else {
132                 user = NULL;
133         }
134
135         if ( pw_file || want_bindpw ) {
136                 if ( pw_file ) {
137                         rc = lutil_get_filed_password( pw_file, &passwd );
138                         if( rc ) return EXIT_FAILURE;
139                 } else {
140                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
141                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
142                 }
143         }
144
145         ld = tool_conn_setup( 0, 0 );
146
147         tool_bind( ld );
148
149         if ( not ) {
150                 rc = LDAP_SUCCESS;
151                 goto skip;
152         }
153
154         if ( assertion || authzid || manageDSAit || noop ) {
155                 tool_server_controls( ld, NULL, 0 );
156         }
157
158         rc = ldap_whoami_s( ld, &retdata, NULL, NULL ); 
159
160         if( retdata != NULL ) {
161                 if( retdata->bv_len == 0 ) {
162                         printf(_("anonymous\n") );
163                 } else {
164                         printf("%s\n", retdata->bv_val );
165                 }
166         }
167
168         if( verbose || ( rc != LDAP_SUCCESS ) || matcheddn || text || refs ) {
169                 printf( _("Result: %s (%d)\n"), ldap_err2string( rc ), rc );
170
171                 if( text && *text ) {
172                         printf( _("Additional info: %s\n"), text );
173                 }
174
175                 if( matcheddn && *matcheddn ) {
176                         printf( _("Matched DN: %s\n"), matcheddn );
177                 }
178
179                 if( refs ) {
180                         int i;
181                         for( i=0; refs[i]; i++ ) {
182                                 printf(_("Referral: %s\n"), refs[i] );
183                         }
184                 }
185         }
186
187         ber_memfree( text );
188         ber_memfree( matcheddn );
189         ber_memvfree( (void **) refs );
190         ber_memfree( retoid );
191         ber_bvfree( retdata );
192
193 skip:
194         /* disconnect from server */
195         ldap_unbind (ld);
196
197         return rc == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
198 }