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