]> git.sur5r.net Git - openldap/blob - clients/tools/ldapcompare.c
7023e9b63d1d6f9eefca3c16896dc34a1efcb6b4
[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 <sys/stat.h>
50
51 #ifdef HAVE_FCNTL_H
52 #include <fcntl.h>
53 #endif
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
56 #endif
57 #ifdef HAVE_IO_H
58 #include <io.h>
59 #endif
60
61 #include <ldap.h>
62
63 #include "lutil.h"
64 #include "lutil_ldap.h"
65 #include "ldap_defaults.h"
66
67 #include "common.h"
68
69
70 static int quiet = 0;
71
72
73 void
74 usage( void )
75 {
76         fprintf( stderr, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
77         fprintf( stderr, _("where:\n"));
78         fprintf( stderr, _("  DN\tDistinguished Name\n"));
79         fprintf( stderr, _("  attr\tassertion attribute\n"));
80         fprintf( stderr, _("  value\tassertion value\n"));
81         fprintf( stderr, _("  b64value\tbase64 encoding of assertion value\n"));
82
83         fprintf( stderr, _("Compare options:\n"));
84         fprintf( stderr, _("  -z         Quiet mode,"
85                 " don't print anything, use return values\n"));
86         tool_common_usage();
87         exit( EXIT_FAILURE );
88 }
89
90 static int docompare LDAP_P((
91         LDAP *ld,
92         char *dn,
93         char *attr,
94         struct berval *bvalue,
95         int quiet,
96         LDAPControl **sctrls,
97         LDAPControl **cctrls));
98
99
100 const char options[] = "z"
101         "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
102
103 int
104 handle_private_option( int i )
105 {
106         switch ( i ) {
107 #if 0
108                 char    *control, *cvalue;
109                 int             crit;
110         case 'E': /* compare extensions */
111                 if( protocol == LDAP_VERSION2 ) {
112                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
113                                 prog, protocol );
114                         exit( EXIT_FAILURE );
115                 }
116
117                 /* should be extended to support comma separated list of
118                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
119                  */
120
121                 crit = 0;
122                 cvalue = NULL;
123                 if( optarg[0] == '!' ) {
124                         crit = 1;
125                         optarg++;
126                 }
127
128                 control = strdup( optarg );
129                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
130                         *cvalue++ = '\0';
131                 }
132                 fprintf( stderr, _("Invalid compare extension name: %s\n"), control );
133                 usage();
134 #endif
135
136         case 'z':
137                 quiet = 1;
138                 break;
139
140         default:
141                 return 0;
142         }
143         return 1;
144 }
145
146
147 int
148 main( int argc, char **argv )
149 {
150         char    *compdn = NULL, *attrs = NULL;
151         char    *sep;
152         int             rc;
153         LDAP    *ld = NULL;
154         struct berval bvalue = { 0, NULL };
155
156         tool_init();
157         prog = lutil_progname( "ldapcompare", argc, argv );
158
159         tool_args( argc, argv );
160
161         if ( argc - optind != 2 ) {
162                 usage();
163         }
164
165         compdn = argv[optind++];
166         attrs = argv[optind++];
167
168         /* user passed in only 2 args, the last one better be in
169          * the form attr:value or attr::b64value
170          */
171         sep = strchr(attrs, ':');
172         if (!sep) {
173                 usage();
174         }
175
176         *sep++='\0';
177         if ( *sep != ':' ) {
178                 bvalue.bv_val = strdup( sep );
179                 bvalue.bv_len = strlen( bvalue.bv_val );
180
181         } else {
182                 /* it's base64 encoded. */
183                 bvalue.bv_val = malloc( strlen( &sep[1] ));
184                 bvalue.bv_len = lutil_b64_pton( &sep[1],
185                         (unsigned char *) bvalue.bv_val, strlen( &sep[1] ));
186
187                 if (bvalue.bv_len == (ber_len_t)-1) {
188                         fprintf(stderr, _("base64 decode error\n"));
189                         exit(-1);
190                 }
191         }
192
193         ld = tool_conn_setup( 0, 0 );
194
195         if ( pw_file || want_bindpw ) {
196                 if ( pw_file ) {
197                         rc = lutil_get_filed_password( pw_file, &passwd );
198                         if( rc ) return EXIT_FAILURE;
199                 } else {
200                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
201                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
202                 }
203         }
204
205         tool_bind( ld );
206
207         if ( assertion || authzid || manageDSAit || noop ) {
208                 tool_server_controls( ld, NULL, 0 );
209         }
210
211         if ( verbose ) {
212                 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
213                         compdn, attrs, sep );
214         }
215
216         rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
217
218         free( bvalue.bv_val );
219
220         ldap_unbind_ext( ld, NULL, NULL );
221
222         return rc;
223 }
224
225
226 static int docompare(
227         LDAP *ld,
228         char *dn,
229         char *attr,
230         struct berval *bvalue,
231         int quiet,
232         LDAPControl **sctrls,
233         LDAPControl **cctrls )
234 {
235         int                     rc;
236
237         if ( not ) {
238                 return LDAP_SUCCESS;
239         }
240
241         rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
242                 sctrls, cctrls );
243
244         /* if we were told to be quiet, use the return value. */
245         if ( !quiet ) {
246                 if ( rc == LDAP_COMPARE_TRUE ) {
247                         printf(_("TRUE\n"));
248                 } else if ( rc == LDAP_COMPARE_FALSE ) {
249                         printf(_("FALSE\n"));
250                 } else {
251                         printf(_("UNDEFINED\n"));
252                         ldap_perror( ld, "ldap_compare" );
253                 }
254         }
255
256         return( rc );
257 }
258