]> git.sur5r.net Git - openldap/blob - clients/tools/ldapcompare.c
Allow arbitrary exop data to be base64 encoded
[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-2006 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 #ifdef LDAP_CONTROL_DONTUSECOPY
86         fprintf( stderr, _("  -E [!]<ext>[=<extparam>] compare extensions (! indicates criticality)\n"));
87         fprintf( stderr, _("             !dontUseCopy                (Don't Use Copy)\n"));
88 #endif
89         fprintf( stderr, _("  -z         Quiet mode,"
90                 " don't print anything, use return values\n"));
91         tool_common_usage();
92         exit( EXIT_FAILURE );
93 }
94
95 static int docompare LDAP_P((
96         LDAP *ld,
97         char *dn,
98         char *attr,
99         struct berval *bvalue,
100         int quiet,
101         LDAPControl **sctrls,
102         LDAPControl **cctrls));
103
104
105 const char options[] = "z"
106         "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
107
108 #ifdef LDAP_CONTROL_DONTUSECOPY
109 int dontUseCopy = 0;
110 #endif
111
112 int
113 handle_private_option( int i )
114 {
115         char    *control, *cvalue;
116         int             crit;
117
118         switch ( i ) {
119         case 'E': /* compare extensions */
120                 if( protocol == LDAP_VERSION2 ) {
121                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
122                                 prog, protocol );
123                         exit( EXIT_FAILURE );
124                 }
125
126                 /* should be extended to support comma separated list of
127                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
128                  */
129
130                 crit = 0;
131                 cvalue = NULL;
132                 if( optarg[0] == '!' ) {
133                         crit = 1;
134                         optarg++;
135                 }
136
137                 control = ber_strdup( optarg );
138                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
139                         *cvalue++ = '\0';
140                 }
141
142 #ifdef LDAP_CONTROL_DONTUSECOPY
143                 if ( strcasecmp( control, "dontUseCopy" ) == 0 ) {
144                         if( dontUseCopy ) {
145                                 fprintf( stderr,
146                                         _("dontUseCopy control previously specified\n"));
147                                 exit( EXIT_FAILURE );
148                         }
149                         if( cvalue != NULL ) {
150                                 fprintf( stderr,
151                                         _("dontUseCopy: no control value expected\n") );
152                                 usage();
153                         }
154                         if( !crit ) {
155                                 fprintf( stderr,
156                                         _("dontUseCopy: critical flag required\n") );
157                                 usage();
158                         }
159
160                         dontUseCopy = 1 + crit;
161                 } else
162 #endif
163                 {
164                         fprintf( stderr,
165                                 _("Invalid compare extension name: %s\n"), control );
166                         usage();
167                 }
168                 break;
169
170         case 'z':
171                 quiet = 1;
172                 break;
173
174         default:
175                 return 0;
176         }
177         return 1;
178 }
179
180
181 int
182 main( int argc, char **argv )
183 {
184         char            *compdn = NULL, *attrs = NULL;
185         char            *sep;
186         int             rc;
187         LDAP            *ld = NULL;
188         struct berval   bvalue = { 0, NULL };
189         int             i = 0; 
190         LDAPControl     c[1];
191
192
193         tool_init( TOOL_COMPARE );
194         prog = lutil_progname( "ldapcompare", argc, argv );
195
196         tool_args( argc, argv );
197
198         if ( argc - optind != 2 ) {
199                 usage();
200         }
201
202         compdn = argv[optind++];
203         attrs = argv[optind++];
204
205         /* user passed in only 2 args, the last one better be in
206          * the form attr:value or attr::b64value
207          */
208         sep = strchr(attrs, ':');
209         if (!sep) {
210                 usage();
211         }
212
213         *sep++='\0';
214         if ( *sep != ':' ) {
215                 bvalue.bv_val = strdup( sep );
216                 bvalue.bv_len = strlen( bvalue.bv_val );
217
218         } else {
219                 /* it's base64 encoded. */
220                 bvalue.bv_val = malloc( strlen( &sep[1] ));
221                 bvalue.bv_len = lutil_b64_pton( &sep[1],
222                         (unsigned char *) bvalue.bv_val, strlen( &sep[1] ));
223
224                 if (bvalue.bv_len == (ber_len_t)-1) {
225                         fprintf(stderr, _("base64 decode error\n"));
226                         exit(-1);
227                 }
228         }
229
230         ld = tool_conn_setup( 0, 0 );
231
232         if ( pw_file || want_bindpw ) {
233                 if ( pw_file ) {
234                         rc = lutil_get_filed_password( pw_file, &passwd );
235                         if( rc ) return EXIT_FAILURE;
236                 } else {
237                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
238                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
239                 }
240         }
241
242         tool_bind( ld );
243
244         if ( 0
245 #ifdef LDAP_CONTROL_DONTUSECOPY
246                 || dontUseCopy
247 #endif
248                 )
249         {
250 #ifdef LDAP_CONTROL_DONTUSECOPY
251                 if ( dontUseCopy ) {  
252                         c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
253                         c[i].ldctl_value.bv_val = NULL;
254                         c[i].ldctl_value.bv_len = 0;
255                         c[i].ldctl_iscritical = dontUseCopy > 1;
256                         i++;    
257                 }
258 #endif
259         }
260
261         tool_server_controls( ld, c, i );
262
263         if ( verbose ) {
264                 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
265                         compdn, attrs, sep );
266         }
267
268         rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
269
270         free( bvalue.bv_val );
271
272         tool_unbind( ld );
273         tool_destroy();
274         return rc;
275 }
276
277
278 static int docompare(
279         LDAP *ld,
280         char *dn,
281         char *attr,
282         struct berval *bvalue,
283         int quiet,
284         LDAPControl **sctrls,
285         LDAPControl **cctrls )
286 {
287         int             rc, msgid, code;
288         LDAPMessage     *res;
289         char            *matcheddn;
290         char            *text;
291         char            **refs;
292
293         if ( dont ) {
294                 return LDAP_SUCCESS;
295         }
296
297         rc = ldap_compare_ext( ld, dn, attr, bvalue,
298                 sctrls, cctrls, &msgid );
299         if ( rc == -1 ) {
300                 return( rc );
301         }
302
303         for ( ; ; ) {
304                 struct timeval  tv;
305
306                 tv.tv_sec = 0;
307                 tv.tv_usec = 100000;
308
309                 if ( tool_check_abandon( ld, msgid ) ) {
310                         return LDAP_CANCELLED;
311                 }
312
313                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
314                 if ( rc < 0 ) {
315                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
316                         return rc;
317                 }
318
319                 if ( rc != 0 ) {
320                         break;
321                 }
322         }
323
324         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
325
326         if( rc != LDAP_SUCCESS ) {
327                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
328                         prog, ldap_err2string( rc ), rc );
329                 return rc;
330         }
331
332         if ( !quiet && ( verbose || ( code != LDAP_SUCCESS && code != LDAP_COMPARE_TRUE && code != LDAP_COMPARE_FALSE )||
333                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) )
334         {
335                 printf( _("Compare Result: %s (%d)\n"),
336                         ldap_err2string( code ), code );
337
338                 if( text && *text ) {
339                         printf( _("Additional info: %s\n"), text );
340                 }
341
342                 if( matcheddn && *matcheddn ) {
343                         printf( _("Matched DN: %s\n"), matcheddn );
344                 }
345
346                 if( refs ) {
347                         int i;
348                         for( i=0; refs[i]; i++ ) {
349                                 printf(_("Referral: %s\n"), refs[i] );
350                         }
351                 }
352         }
353
354         ber_memfree( text );
355         ber_memfree( matcheddn );
356         ber_memvfree( (void **) refs );
357
358         /* if we were told to be quiet, use the return value. */
359         if ( !quiet ) {
360                 if ( code == LDAP_COMPARE_TRUE ) {
361                         printf(_("TRUE\n"));
362                 } else if ( code == LDAP_COMPARE_FALSE ) {
363                         printf(_("FALSE\n"));
364                 } else {
365                         printf(_("UNDEFINED\n"));
366                 }
367         }
368
369         return( code );
370 }
371