ldapcompare.c 4.22 KB
Newer Older
1
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3
4
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
/* $OpenLDAP$ */
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include "portable.h"

#include <stdio.h>

#include <ac/stdlib.h>

#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/errno.h>
#include <sys/stat.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif

#include <ldap.h>

#include "lutil.h"
#include "lutil_ldap.h"
#include "ldap_defaults.h"

35
36
37
38
39
40
41
42
#include "common.h"


static int quiet = 0;


void
usage( void )
43
{
44
45
46
47
48
49
50
51
	fprintf( stderr, _("usage: %s [options] DN <attr:value|attr::b64value>\n"), prog);
	fprintf( stderr, _("where:\n"));
	fprintf( stderr, _("  DN\tDistinguished Name\n"));
	fprintf( stderr, _("  attr\tassertion attribute\n"));
	fprintf( stderr, _("  value\tassertion value\n"));
	fprintf( stderr, _("  b64value\tbase64 encoding of assertion value\n"));

	fprintf( stderr, _("Compare options:\n"));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
52
53
	fprintf( stderr, _("  -z         Quiet mode,"
		" don't print anything, use return values\n"));
54
	tool_common_usage();
55
56
57
58
59
60
61
62
63
64
65
66
	exit( EXIT_FAILURE );
}

static int docompare LDAP_P((
	LDAP *ld,
	char *dn,
	char *attr,
	struct berval *bvalue,
	int quiet,
	LDAPControl **sctrls,
	LDAPControl **cctrls));

67

68
69
const char options[] = "z"
	"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
77
		char	*control, *cvalue;
		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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
		}

		/* should be extended to support comma separated list of
		 *	[!]key[=value] parameters, e.g.  -E !foo,bar=567
		 */

		crit = 0;
		cvalue = NULL;
		if( optarg[0] == '!' ) {
			crit = 1;
			optarg++;
		}

		control = strdup( optarg );
		if ( (cvalue = strchr( control, '=' )) != NULL ) {
			*cvalue++ = '\0';
		}
100
		fprintf( stderr, _("Invalid compare control name: %s\n"), control );
101
		usage();
102
#endif
103

104
105
106
	case 'z':
		quiet = 1;
		break;
107

108
109
110
111
112
	default:
		return 0;
	}
	return 1;
}
113
114


115
116
117
118
119
120
121
122
int
main( int argc, char **argv )
{
	char	*compdn = NULL, *attrs = NULL;
	char	*sep;
	int		rc;
	LDAP	*ld = NULL;
	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
134
135
136
137
138
139
140
	}

	compdn = argv[optind++];
	attrs = argv[optind++];

	/* user passed in only 2 args, the last one better be in
	 * the form attr:value or attr::b64value
	 */
	sep = strchr(attrs, ':');
	if (!sep) {
141
		usage();
142
143
144
145
146
147
148
149
150
151
152
153
154
	}

	*sep++='\0';
	if ( *sep != ':' ) {
		bvalue.bv_val = strdup( sep );
		bvalue.bv_len = strlen( bvalue.bv_val );

	} else {
		/* it's base64 encoded. */
		bvalue.bv_val = malloc( strlen( &sep[1] ));
		bvalue.bv_len = lutil_b64_pton( &sep[1],
			bvalue.bv_val, strlen( &sep[1] ));

155
		if (bvalue.bv_len == (ber_len_t)-1) {
156
			fprintf(stderr, _("base64 decode error\n"));
157
158
159
160
			exit(-1);
		}
	}

161
	ld = tool_conn_setup( 0, 0 );
162

163
164
165
166
167
	if ( pw_file || want_bindpw ) {
		if ( pw_file ) {
			rc = lutil_get_filed_password( pw_file, &passwd );
			if( rc ) return EXIT_FAILURE;
		} else {
168
			passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
169
170
			passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
		}
171
172
	}

173
	tool_bind( ld );
174

175
176
	if ( authzid || manageDSAit || noop )
		tool_server_controls( ld, NULL, 0 );
177
178

	if ( verbose ) {
179
		fprintf( stderr, _("DN:%s, attr:%s, value:%s\n"),
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
			compdn, attrs, sep );
	}

	rc = docompare( ld, compdn, attrs, &bvalue, quiet, NULL, NULL );

	free( bvalue.bv_val );

	ldap_unbind( ld );

	return rc;
}


static int docompare(
	LDAP *ld,
	char *dn,
	char *attr,
	struct berval *bvalue,
	int quiet,
	LDAPControl **sctrls,
	LDAPControl **cctrls )
{
	int			rc;

	if ( not ) {
		return LDAP_SUCCESS;
	}

	rc = ldap_compare_ext_s( ld, dn, attr, bvalue,
		sctrls, cctrls );

	if ( rc == -1 ) {
		ldap_perror( ld, "ldap_result" );
		return( rc );
	}

	/* if we were told to be quiet, use the return value. */
	if ( !quiet ) {
		if ( rc == LDAP_COMPARE_TRUE ) {
			rc = 0;
220
			printf(_("TRUE\n"));
221
222
		} else if ( rc == LDAP_COMPARE_FALSE ) {
			rc = 0;
223
			printf(_("FALSE\n"));
224
225
226
227
228
229
230
231
		} else {
			ldap_perror( ld, "ldap_compare" );
		}
	}

	return( rc );
}