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