]> git.sur5r.net Git - openldap/blob - clients/tools/ldapcompare.c
Merge in latest changes from HEAD
[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, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
45         fprintf( stderr, _("where:\n"));
46         fprintf( stderr, _("  DN\tDistinguished Name\n"));
47         fprintf( stderr, _("  attr\tassertion attribute\n"));
48         fprintf( stderr, _("  value\tassertion value\n"));
49         fprintf( stderr, _("  b64value\tbase64 encoding of assertion value\n"));
50
51         fprintf( stderr, _("Compare options:\n"));
52         fprintf( stderr, _("  -z         Quiet mode,"
53                 " don't print anything, use return values\n"));
54         tool_common_usage();
55         exit( EXIT_FAILURE );
56 }
57
58 static int docompare LDAP_P((
59         LDAP *ld,
60         char *dn,
61         char *attr,
62         struct berval *bvalue,
63         int quiet,
64         LDAPControl **sctrls,
65         LDAPControl **cctrls));
66
67
68 const char options[] = "z"
69         "Cd:D:e:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
70
71 int
72 handle_private_option( int i )
73 {
74         switch ( i ) {
75 #if 0
76                 char    *control, *cvalue;
77                 int             crit;
78         case 'E': /* compare controls */
79                 if( protocol == LDAP_VERSION2 ) {
80                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
81                                 prog, protocol );
82                         exit( EXIT_FAILURE );
83                 }
84
85                 /* should be extended to support comma separated list of
86                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
87                  */
88
89                 crit = 0;
90                 cvalue = NULL;
91                 if( optarg[0] == '!' ) {
92                         crit = 1;
93                         optarg++;
94                 }
95
96                 control = strdup( optarg );
97                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
98                         *cvalue++ = '\0';
99                 }
100                 fprintf( stderr, _("Invalid compare control name: %s\n"), control );
101                 usage();
102 #endif
103
104         case 'z':
105                 quiet = 1;
106                 break;
107
108         default:
109                 return 0;
110         }
111         return 1;
112 }
113
114
115 int
116 main( int argc, char **argv )
117 {
118         char    *compdn = NULL, *attrs = NULL;
119         char    *sep;
120         int             rc;
121         LDAP    *ld = NULL;
122         struct berval bvalue = { 0, NULL };
123
124         tool_init();
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                         (unsigned char *) 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 ( assertion || authzid || manageDSAit || noop ) {
176                 tool_server_controls( ld, NULL, 0 );
177         }
178
179         if ( verbose ) {
180                 fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
181                         compdn, attrs, sep );
182         }
183
184         rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );
185
186         free( bvalue.bv_val );
187
188         ldap_unbind( ld );
189
190         return rc;
191 }
192
193
194 static int docompare(
195         LDAP *ld,
196         char *dn,
197         char *attr,
198         struct berval *bvalue,
199         int quiet,
200         LDAPControl **sctrls,
201         LDAPControl **cctrls )
202 {
203         int                     rc;
204
205         if ( not ) {
206                 return LDAP_SUCCESS;
207         }
208
209         rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
210                 sctrls, cctrls );
211
212         if ( rc == -1 ) {
213                 ldap_perror( ld, "ldap_result" );
214                 return( rc );
215         }
216
217         /* if we were told to be quiet, use the return value. */
218         if ( !quiet ) {
219                 if ( rc == LDAP_COMPARE_TRUE ) {
220                         rc = 0;
221                         printf(_("TRUE\n"));
222                 } else if ( rc == LDAP_COMPARE_FALSE ) {
223                         rc = 0;
224                         printf(_("FALSE\n"));
225                 } else {
226                         ldap_perror( ld, "ldap_compare" );
227                 }
228         }
229
230         return( rc );
231 }
232