]> git.sur5r.net Git - openldap/blob - clients/tools/ldapwhoami.c
Happy New Year
[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-2018 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         "d:D:e:h:H:InNO:o: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 extension */
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
97                 fprintf( stderr, _("Invalid whoami extension name: %s\n"), control );
98                 usage();
99 #endif
100
101         default:
102                 return 0;
103         }
104         return 1;
105 }
106
107
108 int
109 main( int argc, char *argv[] )
110 {
111         int             rc;
112         LDAP            *ld = NULL;
113         char            *matcheddn = NULL, *text = NULL, **refs = NULL;
114         struct berval   *authzid = NULL;
115         int             id, code = 0;
116         LDAPMessage     *res = NULL;
117         LDAPControl     **ctrls = NULL;
118
119         tool_init( TOOL_WHOAMI );
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 > 0 ) {
128                 usage();
129         }
130
131         ld = tool_conn_setup( 0, 0 );
132
133         tool_bind( ld );
134
135         if ( dont ) {
136                 rc = LDAP_SUCCESS;
137                 goto skip;
138         }
139
140         tool_server_controls( ld, NULL, 0 );
141
142         rc = ldap_whoami( ld, NULL, NULL, &id ); 
143
144         if( rc != LDAP_SUCCESS ) {
145                 tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL );
146                 rc = EXIT_FAILURE;
147                 goto skip;
148         }
149
150         for ( ; ; ) {
151                 struct timeval  tv;
152
153                 if ( tool_check_abandon( ld, id ) ) {
154                         tool_exit( ld, LDAP_CANCELLED );
155                 }
156
157                 tv.tv_sec = 0;
158                 tv.tv_usec = 100000;
159
160                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
161                 if ( rc < 0 ) {
162                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
163                         tool_exit( ld, rc );
164                 }
165
166                 if ( rc != 0 ) {
167                         break;
168                 }
169         }
170
171         rc = ldap_parse_result( ld, res,
172                 &code, &matcheddn, &text, &refs, &ctrls, 0 );
173
174         if ( rc == LDAP_SUCCESS ) {
175                 rc = code;
176         }
177
178         if ( rc != LDAP_SUCCESS ) {
179                 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
180                 rc = EXIT_FAILURE;
181                 goto skip;
182         }
183
184         rc = ldap_parse_whoami( ld, res, &authzid );
185
186         if( rc != LDAP_SUCCESS ) {
187                 tool_perror( "ldap_parse_whoami", rc, NULL, NULL, NULL, NULL );
188                 rc = EXIT_FAILURE;
189                 goto skip;
190         }
191
192         if( authzid != NULL ) {
193                 if( authzid->bv_len == 0 ) {
194                         printf(_("anonymous\n") );
195                 } else {
196                         printf("%s\n", authzid->bv_val );
197                 }
198         }
199
200 skip:
201         ldap_msgfree(res);
202         if ( verbose || code != LDAP_SUCCESS ||
203                 ( matcheddn && *matcheddn ) || ( text && *text ) || refs || ctrls )
204         {
205                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
206
207                 if( text && *text ) {
208                         printf( _("Additional info: %s\n"), text );
209                 }
210
211                 if( matcheddn && *matcheddn ) {
212                         printf( _("Matched DN: %s\n"), matcheddn );
213                 }
214
215                 if( refs ) {
216                         int i;
217                         for( i=0; refs[i]; i++ ) {
218                                 printf(_("Referral: %s\n"), refs[i] );
219                         }
220                 }
221
222                 if (ctrls) {
223                         tool_print_ctrls( ld, ctrls );
224                         ldap_controls_free( ctrls );
225                 }
226         }
227
228         ber_memfree( text );
229         ber_memfree( matcheddn );
230         ber_memvfree( (void **) refs );
231         ber_bvfree( authzid );
232
233         /* disconnect from server */
234         tool_exit( ld, code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE );
235 }