]> git.sur5r.net Git - openldap/blob - clients/tools/ldapcompare.c
Happy new year!
[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
190         tool_init();
191         prog = lutil_progname( "ldapcompare", argc, argv );
192
193         tool_args( argc, argv );
194
195         if ( argc - optind != 2 ) {
196                 usage();
197         }
198
199         compdn = argv[optind++];
200         attrs = argv[optind++];
201
202         /* user passed in only 2 args, the last one better be in
203          * the form attr:value or attr::b64value
204          */
205         sep = strchr(attrs, ':');
206         if (!sep) {
207                 usage();
208         }
209
210         *sep++='\0';
211         if ( *sep != ':' ) {
212                 bvalue.bv_val = strdup( sep );
213                 bvalue.bv_len = strlen( bvalue.bv_val );
214
215         } else {
216                 /* it's base64 encoded. */
217                 bvalue.bv_val = malloc( strlen( &sep[1] ));
218                 bvalue.bv_len = lutil_b64_pton( &sep[1],
219                         (unsigned char *) bvalue.bv_val, strlen( &sep[1] ));
220
221                 if (bvalue.bv_len == (ber_len_t)-1) {
222                         fprintf(stderr, _("base64 decode error\n"));
223                         exit(-1);
224                 }
225         }
226
227         ld = tool_conn_setup( 0, 0 );
228
229         if ( pw_file || want_bindpw ) {
230                 if ( pw_file ) {
231                         rc = lutil_get_filed_password( pw_file, &passwd );
232                         if( rc ) return EXIT_FAILURE;
233                 } else {
234                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
235                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
236                 }
237         }
238
239         tool_bind( ld );
240
241         if ( assertion || authzid || manageDSAit || noop 
242 #ifdef LDAP_CONTROL_DONTUSECOPY
243                 || dontUseCopy
244 #endif
245                 )
246         {
247                 int err;
248                 int i = 0; 
249                 LDAPControl c[1];
250
251 #ifdef LDAP_CONTROL_DONTUSECOPY
252                 if ( dontUseCopy ) {  
253                         c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
254                         c[i].ldctl_value.bv_val = NULL;
255                         c[i].ldctl_value.bv_len = 0;
256                         c[i].ldctl_iscritical = dontUseCopy > 1;
257                         i++;    
258                 }
259 #endif
260
261                 tool_server_controls( ld, c, i );
262         }
263
264         if ( verbose ) {
265                 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
266                         compdn, attrs, sep );
267         }
268
269         rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
270
271         free( bvalue.bv_val );
272
273         tool_unbind( ld );
274         tool_destroy();
275         return rc;
276 }
277
278
279 static int docompare(
280         LDAP *ld,
281         char *dn,
282         char *attr,
283         struct berval *bvalue,
284         int quiet,
285         LDAPControl **sctrls,
286         LDAPControl **cctrls )
287 {
288         int             rc, msgid, code;
289         LDAPMessage     *res;
290         char            *matcheddn;
291         char            *text;
292         char            **refs;
293
294         if ( not ) {
295                 return LDAP_SUCCESS;
296         }
297
298         rc = ldap_compare_ext( ld, dn, attr, bvalue,
299                 sctrls, cctrls, &msgid );
300         if ( rc == -1 ) {
301                 return( rc );
302         }
303
304         for ( ; ; ) {
305                 struct timeval  tv;
306
307                 tv.tv_sec = 0;
308                 tv.tv_usec = 100000;
309
310                 if ( tool_check_abandon( ld, msgid ) ) {
311                         return LDAP_CANCELLED;
312                 }
313
314                 rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
315                 if ( rc < 0 ) {
316                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
317                         return rc;
318                 }
319
320                 if ( rc != 0 ) {
321                         break;
322                 }
323         }
324
325         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
326
327         if( rc != LDAP_SUCCESS ) {
328                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
329                         prog, ldap_err2string( rc ), rc );
330                 return rc;
331         }
332
333         if ( !quiet && ( verbose || ( code != LDAP_SUCCESS && code != LDAP_COMPARE_TRUE && code != LDAP_COMPARE_FALSE )||
334                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) ) )
335         {
336                 printf( _("Compare Result: %s (%d)\n"),
337                         ldap_err2string( code ), code );
338
339                 if( text && *text ) {
340                         printf( _("Additional info: %s\n"), text );
341                 }
342
343                 if( matcheddn && *matcheddn ) {
344                         printf( _("Matched DN: %s\n"), matcheddn );
345                 }
346
347                 if( refs ) {
348                         int i;
349                         for( i=0; refs[i]; i++ ) {
350                                 printf(_("Referral: %s\n"), refs[i] );
351                         }
352                 }
353         }
354
355         ber_memfree( text );
356         ber_memfree( matcheddn );
357         ber_memvfree( (void **) refs );
358
359         /* if we were told to be quiet, use the return value. */
360         if ( !quiet ) {
361                 if ( code == LDAP_COMPARE_TRUE ) {
362                         printf(_("TRUE\n"));
363                 } else if ( code == LDAP_COMPARE_FALSE ) {
364                         printf(_("FALSE\n"));
365                 } else {
366                         printf(_("UNDEFINED\n"));
367                 }
368         }
369
370         return( code );
371 }
372