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