]> git.sur5r.net Git - openldap/blob - clients/tools/ldapvc.c
c801cd17594b96460291429c2d2477add6b6d1d5
[openldap] / clients / tools / ldapvc.c
1 /* ldapvc.c -- a tool for verifying credentials */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2010 The OpenLDAP Foundation.
6  * Portions Copyright 2010 Kurt D. Zeilenga.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * provided that this notice is preserved and that due credit is given
22  * to the University of Michigan at Ann Arbor.  The name of the
23  * University may not be used to endorse or promote products derived
24  * from this software without specific prior written permission.  This
25  * software is provided ``as is'' without express or implied warranty.
26  */
27 /* ACKNOWLEDGEMENTS:
28  * This work was originally developed by Kurt D. Zeilenga for inclusion
29  * in OpenLDAP Software based, in part, on other client tools.
30  */
31
32 #include "portable.h"
33
34 #include <stdio.h>
35
36 #include <ac/stdlib.h>
37
38 #include <ac/ctype.h>
39 #include <ac/socket.h>
40 #include <ac/string.h>
41 #include <ac/time.h>
42 #include <ac/unistd.h>
43
44 #include <ldap.h>
45 #include "lutil.h"
46 #include "lutil_ldap.h"
47 #include "ldap_defaults.h"
48
49 #include "common.h"
50
51 static char * mech = NULL;
52 static char * dn = NULL;
53 static struct berval cred = {0, NULL};
54
55 void
56 usage( void )
57 {
58         fprintf( stderr, _("Issue LDAP Verify Credentials operation to verify a user's credentials\n\n"));
59         fprintf( stderr, _("usage: %s [options] (-S mech|[DN [cred]])\n"), prog);
60         fprintf( stderr, _("where:\n"));
61         fprintf( stderr, _("    DN\tDistinguished Name\n"));
62         fprintf( stderr, _("    cred\tCredentials (prompt if not present)\n"));
63         fprintf( stderr, _("options:\n"));
64         fprintf( stderr, _("    -S mech\tSASL mechanism (default "" e.g. Simple)\n"));
65         tool_common_usage();
66         exit( EXIT_FAILURE );
67 }
68
69
70 const char options[] = "S"
71         "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
72
73 int
74 handle_private_option( int i )
75 {
76         switch ( i ) {
77 #if 0
78                 char    *control, *cvalue;
79                 int             crit;
80         case 'E': /* vc extension */
81                 if( protocol == LDAP_VERSION2 ) {
82                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
83                                 prog, protocol );
84                         exit( EXIT_FAILURE );
85                 }
86
87                 /* should be extended to support comma separated list of
88                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
89                  */
90
91                 crit = 0;
92                 cvalue = NULL;
93                 if( optarg[0] == '!' ) {
94                         crit = 1;
95                         optarg++;
96                 }
97
98                 control = strdup( optarg );
99                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
100                         *cvalue++ = '\0';
101                 }
102
103                 fprintf( stderr, _("Invalid Verify Credentials extension name: %s\n"), control );
104                 usage();
105 #endif
106
107         case 'S':  /* SASL mechanism */
108                 mech = optarg;
109                 break;
110
111         default:
112                 return 0;
113         }
114         return 1;
115 }
116
117
118 int
119 main( int argc, char *argv[] )
120 {
121         int             rc;
122         LDAP            *ld = NULL;
123         char            *matcheddn = NULL, *text = NULL, **refs = NULL;
124         int rcode;
125         char * diag = NULL;
126         struct berval   *scookie = NULL;
127         struct berval   *scred = NULL;
128         struct berval   *authzid = NULL;
129         int             id, code = 0;
130         LDAPMessage     *res;
131         LDAPControl     **ctrls = NULL;
132
133         tool_init( TOOL_VC );
134         prog = lutil_progname( "ldapvc", argc, argv );
135
136         /* LDAPv3 only */
137         protocol = LDAP_VERSION3;
138
139         tool_args( argc, argv );
140
141         if (mech) {
142                 if (argc - optind > 0) {
143                         usage();
144                 }
145
146                 fprintf(stderr, "SASL credential verification not yet implemented!\n");
147                 rc = EXIT_FAILURE;
148                 goto skip;
149
150         } else {
151                 if (argc - optind > 0) {
152                         dn = argv[optind++];
153                 }
154                 if (argc - optind > 0) {
155                         cred.bv_val = argv[optind++];
156                         cred.bv_len = strlen(cred.bv_val);
157                 }
158
159                 if (argc - optind > 0) {
160                     usage();
161             }
162
163             if (!cred.bv_val) {
164                     cred.bv_val = strdup(getpassphrase(_("User's password: ")));
165             }
166                 cred.bv_len = strlen(cred.bv_val);
167         }
168
169         ld = tool_conn_setup( 0, 0 );
170
171         tool_bind( ld );
172
173         if ( dont ) {
174                 rc = LDAP_SUCCESS;
175                 goto skip;
176         }
177
178         tool_server_controls( ld, NULL, 0 );
179
180         rc = ldap_verify_credentials( ld,
181                 NULL,
182                 dn, mech, cred.bv_val ? &cred: NULL, NULL,
183                 NULL, NULL, &id ); 
184
185         if( rc != LDAP_SUCCESS ) {
186                 tool_perror( "ldap_verify_credentials", rc, NULL, NULL, NULL, NULL );
187                 rc = EXIT_FAILURE;
188                 goto skip;
189         }
190
191         for ( ; ; ) {
192                 struct timeval  tv;
193
194                 if ( tool_check_abandon( ld, id ) ) {
195                         return LDAP_CANCELLED;
196                 }
197
198                 tv.tv_sec = 0;
199                 tv.tv_usec = 100000;
200
201                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
202                 if ( rc < 0 ) {
203                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
204                         return rc;
205                 }
206
207                 if ( rc != 0 ) {
208                         break;
209                 }
210         }
211
212         rc = ldap_parse_result( ld, res,
213                 &code, &matcheddn, &text, &refs, &ctrls, 0 );
214
215         if ( rc == LDAP_SUCCESS ) {
216                 rc = code;
217         }
218
219         if ( rc != LDAP_SUCCESS ) {
220                 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
221                 rc = EXIT_FAILURE;
222                 goto skip;
223         }
224
225         rc = ldap_parse_verify_credentials( ld, res, &rcode, &diag, &scookie, &scred, &authzid, NULL );
226         ldap_msgfree(res);
227
228         if( rc != LDAP_SUCCESS ) {
229                 tool_perror( "ldap_parse_verify_credentials", rc, NULL, NULL, NULL, NULL );
230                 rc = EXIT_FAILURE;
231                 goto skip;
232         }
233
234         if (!rcode) {
235                 printf(_("Failed: %s (%d)\n"), ldap_err2string(rcode), rcode);
236     } else {
237             if( authzid != NULL ) {
238                 if( authzid->bv_len == 0 ) {
239                         printf(_("anonymous\n") );
240                 } else {
241                         printf("%s\n", authzid->bv_val );
242                 }
243             }
244         }
245
246         if (diag && *diag) {
247             printf(_("Diagnostic: %s\n"), diag);
248         }
249
250     /* print vc controls here (once added) */
251
252 skip:
253         if ( verbose || ( code != LDAP_SUCCESS ) ||
254                 matcheddn || text || refs || ctrls )
255         {
256                 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
257
258                 if( text && *text ) {
259                         printf( _("Additional info: %s\n"), text );
260                 }
261
262                 if( matcheddn && *matcheddn ) {
263                         printf( _("Matched DN: %s\n"), matcheddn );
264                 }
265
266                 if( refs ) {
267                         int i;
268                         for( i=0; refs[i]; i++ ) {
269                                 printf(_("Referral: %s\n"), refs[i] );
270                         }
271                 }
272
273                 if (ctrls) {
274                         tool_print_ctrls( ld, ctrls );
275                         ldap_controls_free( ctrls );
276                 }
277         }
278
279         ber_memfree( text );
280         ber_memfree( matcheddn );
281         ber_memvfree( (void **) refs );
282         ber_bvfree( scookie );
283         ber_bvfree( scred );
284         ber_bvfree( authzid );
285
286         /* disconnect from server */
287         tool_unbind( ld );
288         tool_destroy();
289
290         return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;
291 }