xref: /netbsd-src/external/bsd/openldap/dist/clients/tools/ldapwhoami.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ldapwhoami.c,v 1.1.1.4 2014/05/28 09:58:22 tron Exp $	*/
2 
3 /* ldapwhoami.c -- a tool for asking the directory "Who Am I?" */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2014 The OpenLDAP Foundation.
8  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
9  * Portions Copyright 1998-2001 Net Boolean Incorporated.
10  * Portions Copyright 2001-2003 IBM Corporation.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted only as authorized by the OpenLDAP
15  * Public License.
16  *
17  * A copy of this license is available in the file LICENSE in the
18  * top-level directory of the distribution or, alternatively, at
19  * <http://www.OpenLDAP.org/license.html>.
20  */
21 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms are permitted
25  * provided that this notice is preserved and that due credit is given
26  * to the University of Michigan at Ann Arbor.  The name of the
27  * University may not be used to endorse or promote products derived
28  * from this software without specific prior written permission.  This
29  * software is provided ``as is'' without express or implied warranty.
30  */
31 /* ACKNOWLEDGEMENTS:
32  * This work was originally developed by Kurt D. Zeilenga for inclusion
33  * in OpenLDAP Software based, in part, on other client tools.
34  */
35 
36 #include "portable.h"
37 
38 #include <stdio.h>
39 
40 #include <ac/stdlib.h>
41 
42 #include <ac/ctype.h>
43 #include <ac/socket.h>
44 #include <ac/string.h>
45 #include <ac/time.h>
46 #include <ac/unistd.h>
47 
48 #include <ldap.h>
49 #include "lutil.h"
50 #include "lutil_ldap.h"
51 #include "ldap_defaults.h"
52 
53 #include "common.h"
54 
55 
56 void
57 usage( void )
58 {
59 	fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
60 	fprintf( stderr, _("usage: %s [options]\n"), prog);
61 	tool_common_usage();
62 	exit( EXIT_FAILURE );
63 }
64 
65 
66 const char options[] = ""
67 	"d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
68 
69 int
70 handle_private_option( int i )
71 {
72 	switch ( i ) {
73 #if 0
74 		char	*control, *cvalue;
75 		int		crit;
76 	case 'E': /* whoami extension */
77 		if( protocol == LDAP_VERSION2 ) {
78 			fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
79 				prog, protocol );
80 			exit( EXIT_FAILURE );
81 		}
82 
83 		/* should be extended to support comma separated list of
84 		 *	[!]key[=value] parameters, e.g.  -E !foo,bar=567
85 		 */
86 
87 		crit = 0;
88 		cvalue = NULL;
89 		if( optarg[0] == '!' ) {
90 			crit = 1;
91 			optarg++;
92 		}
93 
94 		control = strdup( optarg );
95 		if ( (cvalue = strchr( control, '=' )) != NULL ) {
96 			*cvalue++ = '\0';
97 		}
98 
99 		fprintf( stderr, _("Invalid whoami extension name: %s\n"), control );
100 		usage();
101 #endif
102 
103 	default:
104 		return 0;
105 	}
106 	return 1;
107 }
108 
109 
110 int
111 main( int argc, char *argv[] )
112 {
113 	int		rc;
114 	LDAP		*ld = NULL;
115 	char		*matcheddn = NULL, *text = NULL, **refs = NULL;
116 	struct berval	*authzid = NULL;
117 	int		id, code = 0;
118 	LDAPMessage	*res = NULL;
119 	LDAPControl	**ctrls = NULL;
120 
121 	tool_init( TOOL_WHOAMI );
122 	prog = lutil_progname( "ldapwhoami", argc, argv );
123 
124 	/* LDAPv3 only */
125 	protocol = LDAP_VERSION3;
126 
127 	tool_args( argc, argv );
128 
129 	if( argc - optind > 0 ) {
130 		usage();
131 	}
132 
133 	ld = tool_conn_setup( 0, 0 );
134 
135 	tool_bind( ld );
136 
137 	if ( dont ) {
138 		rc = LDAP_SUCCESS;
139 		goto skip;
140 	}
141 
142 	tool_server_controls( ld, NULL, 0 );
143 
144 	rc = ldap_whoami( ld, NULL, NULL, &id );
145 
146 	if( rc != LDAP_SUCCESS ) {
147 		tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL );
148 		rc = EXIT_FAILURE;
149 		goto skip;
150 	}
151 
152 	for ( ; ; ) {
153 		struct timeval	tv;
154 
155 		if ( tool_check_abandon( ld, id ) ) {
156 			tool_exit( ld, LDAP_CANCELLED );
157 		}
158 
159 		tv.tv_sec = 0;
160 		tv.tv_usec = 100000;
161 
162 		rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
163 		if ( rc < 0 ) {
164 			tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
165 			tool_exit( ld, rc );
166 		}
167 
168 		if ( rc != 0 ) {
169 			break;
170 		}
171 	}
172 
173 	rc = ldap_parse_result( ld, res,
174 		&code, &matcheddn, &text, &refs, &ctrls, 0 );
175 
176 	if ( rc == LDAP_SUCCESS ) {
177 		rc = code;
178 	}
179 
180 	if ( rc != LDAP_SUCCESS ) {
181 		tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
182 		rc = EXIT_FAILURE;
183 		goto skip;
184 	}
185 
186 	rc = ldap_parse_whoami( ld, res, &authzid );
187 
188 	if( rc != LDAP_SUCCESS ) {
189 		tool_perror( "ldap_parse_whoami", rc, NULL, NULL, NULL, NULL );
190 		rc = EXIT_FAILURE;
191 		goto skip;
192 	}
193 
194 	if( authzid != NULL ) {
195 		if( authzid->bv_len == 0 ) {
196 			printf(_("anonymous\n") );
197 		} else {
198 			printf("%s\n", authzid->bv_val );
199 		}
200 	}
201 
202 skip:
203 	ldap_msgfree(res);
204 	if ( verbose || code != LDAP_SUCCESS ||
205 		( matcheddn && *matcheddn ) || ( text && *text ) || refs || ctrls )
206 	{
207 		printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
208 
209 		if( text && *text ) {
210 			printf( _("Additional info: %s\n"), text );
211 		}
212 
213 		if( matcheddn && *matcheddn ) {
214 			printf( _("Matched DN: %s\n"), matcheddn );
215 		}
216 
217 		if( refs ) {
218 			int i;
219 			for( i=0; refs[i]; i++ ) {
220 				printf(_("Referral: %s\n"), refs[i] );
221 			}
222 		}
223 
224 		if (ctrls) {
225 			tool_print_ctrls( ld, ctrls );
226 			ldap_controls_free( ctrls );
227 		}
228 	}
229 
230 	ber_memfree( text );
231 	ber_memfree( matcheddn );
232 	ber_memvfree( (void **) refs );
233 	ber_bvfree( authzid );
234 
235 	/* disconnect from server */
236 	tool_exit( ld, code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE );
237 }
238