1 /* ldapvc.c -- a tool for verifying credentials */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2010 The OpenLDAP Foundation.
6 * Portions Copyright 2010 Kurt D. Zeilenga.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
18 * All rights reserved.
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.
28 * This work was originally developed by Kurt D. Zeilenga for inclusion
29 * in OpenLDAP Software based, in part, on other client tools.
36 #include <ac/stdlib.h>
39 #include <ac/socket.h>
40 #include <ac/string.h>
42 #include <ac/unistd.h>
46 #include "lutil_ldap.h"
47 #include "ldap_defaults.h"
51 static char * mech = NULL;
52 static char * dn = NULL;
53 static struct berval cred = {0, NULL};
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"));
70 const char options[] = "S"
71 "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
74 handle_private_option( int i )
78 char *control, *cvalue;
80 case 'E': /* vc extension */
81 if( protocol == LDAP_VERSION2 ) {
82 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
87 /* should be extended to support comma separated list of
88 * [!]key[=value] parameters, e.g. -E !foo,bar=567
93 if( optarg[0] == '!' ) {
98 control = strdup( optarg );
99 if ( (cvalue = strchr( control, '=' )) != NULL ) {
103 fprintf( stderr, _("Invalid Verify Credentials extension name: %s\n"), control );
107 case 'S': /* SASL mechanism */
119 main( int argc, char *argv[] )
123 char *matcheddn = NULL, *text = NULL, **refs = NULL;
126 struct berval *scookie = NULL;
127 struct berval *scred = NULL;
130 LDAPControl **ctrls = NULL;
132 tool_init( TOOL_VC );
133 prog = lutil_progname( "ldapvc", argc, argv );
136 protocol = LDAP_VERSION3;
138 tool_args( argc, argv );
141 if (argc - optind > 0) {
145 fprintf(stderr, "SASL credential verification not yet implemented!\n");
150 if (argc - optind > 0) {
153 if (argc - optind > 0) {
154 cred.bv_val = argv[optind++];
155 cred.bv_len = strlen(cred.bv_val);
158 if (argc - optind > 0) {
163 cred.bv_val = strdup(getpassphrase(_("User's password: ")));
165 cred.bv_len = strlen(cred.bv_val);
168 ld = tool_conn_setup( 0, 0 );
177 tool_server_controls( ld, NULL, 0 );
179 rc = ldap_verify_credentials( ld,
181 dn, mech, cred.bv_val ? &cred: NULL, NULL,
184 if( rc != LDAP_SUCCESS ) {
185 tool_perror( "ldap_verify_credentials", rc, NULL, NULL, NULL, NULL );
193 if ( tool_check_abandon( ld, id ) ) {
194 return LDAP_CANCELLED;
200 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
202 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
211 rc = ldap_parse_result( ld, res,
212 &code, &matcheddn, &text, &refs, &ctrls, 0 );
214 if ( rc == LDAP_SUCCESS ) {
218 if ( rc != LDAP_SUCCESS ) {
219 tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
224 rc = ldap_parse_verify_credentials( ld, res, &rcode, &diag, &scookie, &scred, NULL );
227 if( rc != LDAP_SUCCESS ) {
228 tool_perror( "ldap_parse_verify_credentials", rc, NULL, NULL, NULL, NULL );
234 printf(_("Failed: %s (%d)\n"), ldap_err2string(rcode), rcode);
238 printf(_("Diagnostic: %s\n"), diag);
241 /* print vc controls here (once added) */
244 if ( verbose || ( code != LDAP_SUCCESS ) ||
245 matcheddn || text || refs || ctrls )
247 printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
249 if( text && *text ) {
250 printf( _("Additional info: %s\n"), text );
253 if( matcheddn && *matcheddn ) {
254 printf( _("Matched DN: %s\n"), matcheddn );
259 for( i=0; refs[i]; i++ ) {
260 printf(_("Referral: %s\n"), refs[i] );
265 tool_print_ctrls( ld, ctrls );
266 ldap_controls_free( ctrls );
271 ber_memfree( matcheddn );
272 ber_memvfree( (void **) refs );
273 ber_bvfree( scookie );
277 /* disconnect from server */
281 return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE;