]> git.sur5r.net Git - openldap/blob - clients/tools/ldapcompare.c
Add rules to patch current package name & version number into portable.h
[openldap] / clients / tools / ldapcompare.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* $OpenLDAP$ */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16 #include <ac/errno.h>
17 #include <sys/stat.h>
18
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #ifdef HAVE_IO_H
26 #include <io.h>
27 #endif
28
29 #include <ldap.h>
30
31 #include "ldif.h"
32 #include "lutil.h"
33 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
35
36 #include "common.h"
37
38 #define _OLV_APP        "ldapcompare"
39 #define _OLV_STATIC
40 #include "ol_version.h"
41
42 static int quiet = 0;
43
44
45 void
46 usage( void )
47 {
48         fprintf( stderr,
49 "usage: %s [options] DN <attr:value|attr::b64value>\n"
50 "where:\n"
51 "  DN\tDistinguished Name\n"
52 "  attr\tassertion attribute\n"
53 "  value\tassertion value\n"
54 "  b64value\tbase64 encoding of assertion value\n"
55
56 "Compare options:\n"
57 "  -z         Quiet mode, don't print anything, use return values\n"
58                  , prog );
59         tool_common_usage();
60         exit( EXIT_FAILURE );
61 }
62
63 static int docompare LDAP_P((
64         LDAP *ld,
65         char *dn,
66         char *attr,
67         struct berval *bvalue,
68         int quiet,
69         LDAPControl **sctrls,
70         LDAPControl **cctrls));
71
72
73 const char options[] = "z"
74         "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
75
76 int
77 handle_private_option( int i )
78 {
79         switch ( i ) {
80 #if 0
81                 char    *control, *cvalue;
82                 int             crit;
83         case 'E': /* compare controls */
84                 if( protocol == LDAP_VERSION2 ) {
85                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
86                                 prog, protocol );
87                         exit( EXIT_FAILURE );
88                 }
89
90                 /* should be extended to support comma separated list of
91                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
92                  */
93
94                 crit = 0;
95                 cvalue = NULL;
96                 if( optarg[0] == '!' ) {
97                         crit = 1;
98                         optarg++;
99                 }
100
101                 control = strdup( optarg );
102                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
103                         *cvalue++ = '\0';
104                 }
105                 fprintf( stderr, "Invalid compare control name: %s\n", control );
106                 usage();
107 #endif
108
109         case 'z':
110                 quiet = 1;
111                 break;
112
113         default:
114                 return 0;
115         }
116         return 1;
117 }
118
119
120 int
121 main( int argc, char **argv )
122 {
123         char    *compdn = NULL, *attrs = NULL;
124         char    *sep;
125         int             rc;
126         LDAP    *ld = NULL;
127         struct berval bvalue = { 0, NULL };
128
129         prog = lutil_progname( "ldapcompare", argc, argv );
130
131         tool_args( argc, argv );
132
133         if ( argc - optind != 2 ) {
134                 usage();
135         }
136
137         compdn = argv[optind++];
138         attrs = argv[optind++];
139
140         /* user passed in only 2 args, the last one better be in
141          * the form attr:value or attr::b64value
142          */
143         sep = strchr(attrs, ':');
144         if (!sep) {
145                 usage();
146         }
147
148         *sep++='\0';
149         if ( *sep != ':' ) {
150                 bvalue.bv_val = strdup( sep );
151                 bvalue.bv_len = strlen( bvalue.bv_val );
152
153         } else {
154                 /* it's base64 encoded. */
155                 bvalue.bv_val = malloc( strlen( &sep[1] ));
156                 bvalue.bv_len = lutil_b64_pton( &sep[1],
157                         bvalue.bv_val, strlen( &sep[1] ));
158
159                 if (bvalue.bv_len == (ber_len_t)-1) {
160                         fprintf(stderr, "base64 decode error\n");
161                         exit(-1);
162                 }
163         }
164
165         if ( debug )
166                 ldif_debug = debug;
167
168         ld = tool_conn_setup( 0, 0 );
169
170         if ( pw_file || want_bindpw ) {
171                 if ( pw_file ) {
172                         rc = lutil_get_filed_password( pw_file, &passwd );
173                         if( rc ) return EXIT_FAILURE;
174                 } else {
175                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
176                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
177                 }
178         }
179
180         tool_bind( ld );
181
182         if ( authzid || manageDSAit || noop )
183                 tool_server_controls( ld, NULL, 0 );
184
185         if ( verbose ) {
186                 fprintf( stderr, "DN:%s, attr:%s, value:%s\n",
187                         compdn, attrs, sep );
188         }
189
190         rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
191
192         free( bvalue.bv_val );
193
194         ldap_unbind( ld );
195
196         return rc;
197 }
198
199
200 static int docompare(
201         LDAP *ld,
202         char *dn,
203         char *attr,
204         struct berval *bvalue,
205         int quiet,
206         LDAPControl **sctrls,
207         LDAPControl **cctrls )
208 {
209         int                     rc;
210
211         if ( not ) {
212                 return LDAP_SUCCESS;
213         }
214
215         rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
216                 sctrls, cctrls );
217
218         if ( rc == -1 ) {
219                 ldap_perror( ld, "ldap_result" );
220                 return( rc );
221         }
222
223         /* if we were told to be quiet, use the return value. */
224         if ( !quiet ) {
225                 if ( rc == LDAP_COMPARE_TRUE ) {
226                         rc = 0;
227                         printf("TRUE\n");
228                 } else if ( rc == LDAP_COMPARE_FALSE ) {
229                         rc = 0;
230                         printf("FALSE\n");
231                 } else {
232                         ldap_perror( ld, "ldap_compare" );
233                 }
234         }
235
236         return( rc );
237 }
238