]> git.sur5r.net Git - openldap/blob - clients/tools/ldapcompare.c
improve previous commit
[openldap] / clients / tools / ldapcompare.c
1 /* ldapcompare.c -- LDAP compare tool */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 1998-2001 Net Boolean Incorporated.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms are permitted
22  * provided that this notice is preserved and that due credit is given
23  * to the University of Michigan at Ann Arbor.  The name of the
24  * University may not be used to endorse or promote products derived
25  * from this software without specific prior written permission.  This
26  * software is provided ``as is'' without express or implied warranty.
27  */
28 /* Portions Copyright 2002, F5 Networks, Inc, All rights reserved.
29  * This software is not subject to any license of F5 Networks.
30  * This is free software; you can redistribute and use it
31  * under the same terms as OpenLDAP itself.
32  */
33 /* ACKNOWLEDGEMENTS:
34  * This work was originally developed by Jeff Costlow (F5 Networks)
35  * based, in part, on existing LDAP tools and adapted for inclusion
36  * into OpenLDAP Software by Kurt D. Zeilenga.
37  */
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/string.h>
47 #include <ac/unistd.h>
48 #include <ac/errno.h>
49 #include <ac/time.h>
50 #include <sys/stat.h>
51
52 #ifdef HAVE_FCNTL_H
53 #include <fcntl.h>
54 #endif
55 #ifdef HAVE_SYS_TYPES_H
56 #include <sys/types.h>
57 #endif
58 #ifdef HAVE_IO_H
59 #include <io.h>
60 #endif
61
62 #include <ldap.h>
63
64 #include "lutil.h"
65 #include "lutil_ldap.h"
66 #include "ldap_defaults.h"
67
68 #include "common.h"
69
70
71 static int quiet = 0;
72
73
74 void
75 usage( void )
76 {
77         fprintf( stderr, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
78         fprintf( stderr, _("where:\n"));
79         fprintf( stderr, _("  DN\tDistinguished Name\n"));
80         fprintf( stderr, _("  attr\tassertion attribute\n"));
81         fprintf( stderr, _("  value\tassertion value\n"));
82         fprintf( stderr, _("  b64value\tbase64 encoding of assertion value\n"));
83
84         fprintf( stderr, _("Compare options:\n"));
85         fprintf( stderr, _("  -z         Quiet mode,"
86                 " don't print anything, use return values\n"));
87         tool_common_usage();
88         exit( EXIT_FAILURE );
89 }
90
91 static int docompare LDAP_P((
92         LDAP *ld,
93         char *dn,
94         char *attr,
95         struct berval *bvalue,
96         int quiet,
97         LDAPControl **sctrls,
98         LDAPControl **cctrls));
99
100
101 const char options[] = "z"
102         "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
103
104 int
105 handle_private_option( int i )
106 {
107         switch ( i ) {
108 #if 0
109                 char    *control, *cvalue;
110                 int             crit;
111         case 'E': /* compare extensions */
112                 if( protocol == LDAP_VERSION2 ) {
113                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
114                                 prog, protocol );
115                         exit( EXIT_FAILURE );
116                 }
117
118                 /* should be extended to support comma separated list of
119                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
120                  */
121
122                 crit = 0;
123                 cvalue = NULL;
124                 if( optarg[0] == '!' ) {
125                         crit = 1;
126                         optarg++;
127                 }
128
129                 control = strdup( optarg );
130                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
131                         *cvalue++ = '\0';
132                 }
133                 fprintf( stderr, _("Invalid compare extension name: %s\n"), control );
134                 usage();
135 #endif
136
137         case 'z':
138                 quiet = 1;
139                 break;
140
141         default:
142                 return 0;
143         }
144         return 1;
145 }
146
147
148 int
149 main( int argc, char **argv )
150 {
151         char    *compdn = NULL, *attrs = NULL;
152         char    *sep;
153         int             rc;
154         LDAP    *ld = NULL;
155         struct berval bvalue = { 0, NULL };
156
157         tool_init();
158         prog = lutil_progname( "ldapcompare", argc, argv );
159
160         tool_args( argc, argv );
161
162         if ( argc - optind != 2 ) {
163                 usage();
164         }
165
166         compdn = argv[optind++];
167         attrs = argv[optind++];
168
169         /* user passed in only 2 args, the last one better be in
170          * the form attr:value or attr::b64value
171          */
172         sep = strchr(attrs, ':');
173         if (!sep) {
174                 usage();
175         }
176
177         *sep++='\0';
178         if ( *sep != ':' ) {
179                 bvalue.bv_val = strdup( sep );
180                 bvalue.bv_len = strlen( bvalue.bv_val );
181
182         } else {
183                 /* it's base64 encoded. */
184                 bvalue.bv_val = malloc( strlen( &sep[1] ));
185                 bvalue.bv_len = lutil_b64_pton( &sep[1],
186                         (unsigned char *) bvalue.bv_val, strlen( &sep[1] ));
187
188                 if (bvalue.bv_len == (ber_len_t)-1) {
189                         fprintf(stderr, _("base64 decode error\n"));
190                         exit(-1);
191                 }
192         }
193
194         ld = tool_conn_setup( 0, 0 );
195
196         if ( pw_file || want_bindpw ) {
197                 if ( pw_file ) {
198                         rc = lutil_get_filed_password( pw_file, &passwd );
199                         if( rc ) return EXIT_FAILURE;
200                 } else {
201                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
202                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
203                 }
204         }
205
206         tool_bind( ld );
207
208         if ( assertion || authzid || manageDSAit || noop ) {
209                 tool_server_controls( ld, NULL, 0 );
210         }
211
212         if ( verbose ) {
213                 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
214                         compdn, attrs, sep );
215         }
216
217         rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
218
219         free( bvalue.bv_val );
220
221         tool_unbind( ld );
222         tool_destroy();
223         return rc;
224 }
225
226
227 static int docompare(
228         LDAP *ld,
229         char *dn,
230         char *attr,
231         struct berval *bvalue,
232         int quiet,
233         LDAPControl **sctrls,
234         LDAPControl **cctrls )
235 {
236         int             rc, msgid, code;
237         LDAPMessage     *res;
238         char            *matcheddn;
239         char            *text;
240         char            **refs;
241
242         if ( not ) {
243                 return LDAP_SUCCESS;
244         }
245
246         rc = ldap_compare_ext( ld, dn, attr, bvalue,
247                 sctrls, cctrls, &msgid );
248         if ( rc == -1 ) {
249                 return( rc );
250         }
251
252         for ( ; ; ) {
253                 struct timeval  tv;
254
255                 tv.tv_sec = 0;
256                 tv.tv_usec = 100000;
257
258                 if ( tool_check_abandon( ld, msgid ) ) {
259                         return LDAP_CANCELLED;
260                 }
261
262                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
263                 if ( rc < 0 ) {
264                         ldap_perror( ld, "ldapcompare: ldap_result" );
265                         return rc;
266                 }
267
268                 if ( rc != 0 ) {
269                         break;
270                 }
271         }
272
273         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
274
275         if( rc != LDAP_SUCCESS ) {
276                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
277                         prog, ldap_err2string( rc ), rc );
278                 return rc;
279         }
280
281         if ( !quiet && ( verbose || ( code != LDAP_SUCCESS && code != LDAP_COMPARE_TRUE && code != LDAP_COMPARE_FALSE )||
282                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) )
283         {
284                 printf( _("Compare Result: %s (%d)\n"),
285                         ldap_err2string( code ), code );
286
287                 if( text && *text ) {
288                         printf( _("Additional info: %s\n"), text );
289                 }
290
291                 if( matcheddn && *matcheddn ) {
292                         printf( _("Matched DN: %s\n"), matcheddn );
293                 }
294
295                 if( refs ) {
296                         int i;
297                         for( i=0; refs[i]; i++ ) {
298                                 printf(_("Referral: %s\n"), refs[i] );
299                         }
300                 }
301         }
302
303         ber_memfree( text );
304         ber_memfree( matcheddn );
305         ber_memvfree( (void **) refs );
306
307         /* if we were told to be quiet, use the return value. */
308         if ( !quiet ) {
309                 if ( code == LDAP_COMPARE_TRUE ) {
310                         printf(_("TRUE\n"));
311                 } else if ( code == LDAP_COMPARE_FALSE ) {
312                         printf(_("FALSE\n"));
313                 } else {
314                         printf(_("UNDEFINED\n"));
315                 }
316         }
317
318         return( code );
319 }
320