xref: /netbsd-src/external/bsd/openldap/dist/clients/tools/ldapwhoami.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: ldapwhoami.c,v 1.3 2021/08/14 16:14:49 christos 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-2021 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 <sys/cdefs.h>
37 __RCSID("$NetBSD: ldapwhoami.c,v 1.3 2021/08/14 16:14:49 christos Exp $");
38 
39 #include "portable.h"
40 
41 #include <stdio.h>
42 
43 #include <ac/stdlib.h>
44 
45 #include <ac/ctype.h>
46 #include <ac/socket.h>
47 #include <ac/string.h>
48 #include <ac/time.h>
49 #include <ac/unistd.h>
50 
51 #include <ldap.h>
52 #include "lutil.h"
53 #include "lutil_ldap.h"
54 #include "ldap_defaults.h"
55 
56 #include "common.h"
57 
58 
59 void
usage(void)60 usage( void )
61 {
62 	fprintf( stderr, _("Issue LDAP Who am I? operation to request user's authzid\n\n"));
63 	fprintf( stderr, _("usage: %s [options]\n"), prog);
64 	tool_common_usage();
65 	exit( EXIT_FAILURE );
66 }
67 
68 
69 const char options[] = ""
70 	"d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
71 
72 int
handle_private_option(int i)73 handle_private_option( int i )
74 {
75 	switch ( i ) {
76 #if 0
77 		char	*control, *cvalue;
78 		int		crit;
79 	case 'E': /* whoami extension */
80 		if( protocol == LDAP_VERSION2 ) {
81 			fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
82 				prog, protocol );
83 			exit( EXIT_FAILURE );
84 		}
85 
86 		/* should be extended to support comma separated list of
87 		 *	[!]key[=value] parameters, e.g.  -E !foo,bar=567
88 		 */
89 
90 		crit = 0;
91 		cvalue = NULL;
92 		if( optarg[0] == '!' ) {
93 			crit = 1;
94 			optarg++;
95 		}
96 
97 		control = optarg;
98 		if ( (cvalue = strchr( control, '=' )) != NULL ) {
99 			*cvalue++ = '\0';
100 		}
101 
102 		fprintf( stderr, _("Invalid whoami extension name: %s\n"), control );
103 		usage();
104 #endif
105 
106 	default:
107 		return 0;
108 	}
109 	return 1;
110 }
111 
112 
113 int
main(int argc,char * argv[])114 main( int argc, char *argv[] )
115 {
116 	int		rc;
117 	LDAP		*ld = NULL;
118 	char		*matcheddn = NULL, *text = NULL, **refs = NULL;
119 	struct berval	*authzid = NULL;
120 	int		id, code = 0;
121 	LDAPMessage	*res = NULL;
122 	LDAPControl	**ctrls = NULL;
123 
124 	tool_init( TOOL_WHOAMI );
125 	prog = lutil_progname( "ldapwhoami", argc, argv );
126 
127 	/* LDAPv3 only */
128 	protocol = LDAP_VERSION3;
129 
130 	tool_args( argc, argv );
131 
132 	if( argc - optind > 0 ) {
133 		usage();
134 	}
135 
136 	ld = tool_conn_setup( 0, 0 );
137 
138 	tool_bind( ld );
139 
140 	if ( dont ) {
141 		rc = LDAP_SUCCESS;
142 		goto skip;
143 	}
144 
145 	tool_server_controls( ld, NULL, 0 );
146 
147 	rc = ldap_whoami( ld, NULL, NULL, &id );
148 
149 	if( rc != LDAP_SUCCESS ) {
150 		tool_perror( "ldap_whoami", rc, NULL, NULL, NULL, NULL );
151 		rc = EXIT_FAILURE;
152 		goto skip;
153 	}
154 
155 	for ( ; ; ) {
156 		struct timeval	tv;
157 
158 		if ( tool_check_abandon( ld, id ) ) {
159 			tool_exit( ld, LDAP_CANCELLED );
160 		}
161 
162 		tv.tv_sec = 0;
163 		tv.tv_usec = 100000;
164 
165 		rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
166 		if ( rc < 0 ) {
167 			tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
168 			tool_exit( ld, rc );
169 		}
170 
171 		if ( rc != 0 ) {
172 			break;
173 		}
174 	}
175 
176 	rc = ldap_parse_result( ld, res,
177 		&code, &matcheddn, &text, &refs, &ctrls, 0 );
178 
179 	if ( rc == LDAP_SUCCESS ) {
180 		rc = code;
181 	}
182 
183 	if ( rc != LDAP_SUCCESS ) {
184 		tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
185 		rc = EXIT_FAILURE;
186 		goto skip;
187 	}
188 
189 	rc = ldap_parse_whoami( ld, res, &authzid );
190 
191 	if( rc != LDAP_SUCCESS ) {
192 		tool_perror( "ldap_parse_whoami", rc, NULL, NULL, NULL, NULL );
193 		rc = EXIT_FAILURE;
194 		goto skip;
195 	}
196 
197 	if( authzid != NULL ) {
198 		if( authzid->bv_len == 0 ) {
199 			printf(_("anonymous\n") );
200 		} else {
201 			printf("%s\n", authzid->bv_val );
202 		}
203 	}
204 
205 skip:
206 	ldap_msgfree(res);
207 	if ( verbose || code != LDAP_SUCCESS ||
208 		( matcheddn && *matcheddn ) || ( text && *text ) || refs || ctrls )
209 	{
210 		printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
211 
212 		if( text && *text ) {
213 			printf( _("Additional info: %s\n"), text );
214 		}
215 
216 		if( matcheddn && *matcheddn ) {
217 			printf( _("Matched DN: %s\n"), matcheddn );
218 		}
219 
220 		if( refs ) {
221 			int i;
222 			for( i=0; refs[i]; i++ ) {
223 				printf(_("Referral: %s\n"), refs[i] );
224 			}
225 		}
226 
227 		if (ctrls) {
228 			tool_print_ctrls( ld, ctrls );
229 			ldap_controls_free( ctrls );
230 		}
231 	}
232 
233 	ber_memfree( text );
234 	ber_memfree( matcheddn );
235 	ber_memvfree( (void **) refs );
236 	ber_bvfree( authzid );
237 
238 	/* disconnect from server */
239 	tool_exit( ld, code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE );
240 }
241