No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / clients / tools / ldapwhoami.c
blob4602c103f2742fb45c415c0320c402c8b70838a0
1 /* ldapwhoami.c -- a tool for asking the directory "Who Am I?" */
2 /* $OpenLDAP: pkg/ldap/clients/tools/ldapwhoami.c,v 1.42.2.3 2008/02/11 23:26:38 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 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.
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.
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>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
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.
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.
34 #include "portable.h"
36 #include <stdio.h>
38 #include <ac/stdlib.h>
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>
46 #include <ldap.h>
47 #include "lutil.h"
48 #include "lutil_ldap.h"
49 #include "ldap_defaults.h"
51 #include "common.h"
54 void
55 usage( void )
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 );
64 const char options[] = ""
65 "d:D:e:h:H:InO:o:p:QR:U:vVw:WxX:y:Y:Z";
67 int
68 handle_private_option( int i )
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 );
81 /* should be extended to support comma separated list of
82 * [!]key[=value] parameters, e.g. -E !foo,bar=567
85 crit = 0;
86 cvalue = NULL;
87 if( optarg[0] == '!' ) {
88 crit = 1;
89 optarg++;
92 control = strdup( optarg );
93 if ( (cvalue = strchr( control, '=' )) != NULL ) {
94 *cvalue++ = '\0';
97 fprintf( stderr, _("Invalid whoami extension name: %s\n"), control );
98 usage();
99 #endif
101 default:
102 return 0;
104 return 1;
109 main( int argc, char *argv[] )
111 int rc;
112 LDAP *ld = NULL;
113 char *matcheddn = NULL, *text = NULL, **refs = NULL;
114 char *retoid = NULL;
115 struct berval *retdata = NULL;
116 int id, code = 0;
117 LDAPMessage *res;
118 LDAPControl **ctrls = NULL;
120 tool_init( TOOL_WHOAMI );
121 prog = lutil_progname( "ldapwhoami", argc, argv );
123 /* LDAPv3 only */
124 protocol = LDAP_VERSION3;
126 tool_args( argc, argv );
128 if( argc - optind > 0 ) {
129 usage();
132 if ( pw_file || want_bindpw ) {
133 if ( pw_file ) {
134 rc = lutil_get_filed_password( pw_file, &passwd );
135 if( rc ) return EXIT_FAILURE;
136 } else {
137 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
138 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
142 ld = tool_conn_setup( 0, 0 );
144 tool_bind( ld );
146 if ( dont ) {
147 rc = LDAP_SUCCESS;
148 goto skip;
151 tool_server_controls( ld, NULL, 0 );
153 rc = ldap_whoami( ld, NULL, NULL, &id );
155 if( rc != LDAP_SUCCESS ) {
156 tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL );
157 rc = EXIT_FAILURE;
158 goto skip;
161 for ( ; ; ) {
162 struct timeval tv;
164 if ( tool_check_abandon( ld, id ) ) {
165 return LDAP_CANCELLED;
168 tv.tv_sec = 0;
169 tv.tv_usec = 100000;
171 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
172 if ( rc < 0 ) {
173 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
174 return rc;
177 if ( rc != 0 ) {
178 break;
182 rc = ldap_parse_result( ld, res,
183 &code, &matcheddn, &text, &refs, &ctrls, 0 );
185 if ( rc == LDAP_SUCCESS ) {
186 rc = code;
189 if ( rc != LDAP_SUCCESS ) {
190 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
191 rc = EXIT_FAILURE;
192 goto skip;
195 rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
197 if( rc != LDAP_SUCCESS ) {
198 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
199 rc = EXIT_FAILURE;
200 goto skip;
203 if( retdata != NULL ) {
204 if( retdata->bv_len == 0 ) {
205 printf(_("anonymous\n") );
206 } else {
207 printf("%s\n", retdata->bv_val );
211 skip:
212 if ( verbose || ( code != LDAP_SUCCESS ) ||
213 matcheddn || text || refs || ctrls )
215 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
217 if( text && *text ) {
218 printf( _("Additional info: %s\n"), text );
221 if( matcheddn && *matcheddn ) {
222 printf( _("Matched DN: %s\n"), matcheddn );
225 if( refs ) {
226 int i;
227 for( i=0; refs[i]; i++ ) {
228 printf(_("Referral: %s\n"), refs[i] );
232 if (ctrls) {
233 tool_print_ctrls( ld, ctrls );
234 ldap_controls_free( ctrls );
238 ber_memfree( text );
239 ber_memfree( matcheddn );
240 ber_memvfree( (void **) refs );
241 ber_memfree( retoid );
242 ber_bvfree( retdata );
244 /* disconnect from server */
245 tool_unbind( ld );
246 tool_destroy();
248 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;