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