ftest.c 2.36 KB
Newer Older
1
/* ftest.c -- OpenLDAP Filter API Test */
2
/* $OpenLDAP$ */
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 1998-2021 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
15
16
17
18
19
20
21
 */

#include "portable.h"

#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/unistd.h>
22
#include <ac/socket.h>
23
24
25
26
27

#include <stdio.h>

#include <ldap.h>

28
#include "ldap_pvt.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
#include "lber_pvt.h"
30
31
32
33
34
35

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

36
static int filter2ber( char *filter );
37
38
39
40

int usage()
{
	fprintf( stderr, "usage:\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
41
		"  ftest [-d n] filter\n"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
42
		"    filter - RFC 4515 string representation of an "
Kurt Zeilenga's avatar
Kurt Zeilenga committed
43
			"LDAP search filter\n" );
44
45
46
47
48
49
	return EXIT_FAILURE;
}

int
main( int argc, char *argv[] )
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
50
	int c;
51
	int debug=0;
52

Kurt Zeilenga's avatar
Kurt Zeilenga committed
53
54
    while( (c = getopt( argc, argv, "d:" )) != EOF ) {
		switch ( c ) {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		case 'd':
			debug = atoi( optarg );
			break;
		default:
			fprintf( stderr, "ftest: unrecognized option -%c\n",
				optopt );
			return usage();
		}
	}

	if ( debug ) {
		if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
			!= LBER_OPT_SUCCESS )
		{
			fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
				debug );
		}
		if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
			!= LDAP_OPT_SUCCESS )
		{
			fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
				debug );
		}
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
	if ( argc - optind != 1 ) {
81
		return usage();
82
83
	}

84
	return filter2ber( strdup( argv[optind] ) );
85
86
}

87
static int filter2ber( char *filter )
88
89
{
	int rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
90
	struct berval bv = BER_BVNULL;
91
	BerElement *ber;
92

93
94
95
	printf( "Filter: %s\n", filter );

	ber = ber_alloc_t( LBER_USE_DER );
96
97
98
99
100
	if( ber == NULL ) {
		perror( "ber_alloc_t" );
		return EXIT_FAILURE;
	}

101
	rc = ldap_pvt_put_filter( ber, filter );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
102
103
	if( rc < 0 ) {
		fprintf( stderr, "Filter error!\n");
104
105
		return EXIT_FAILURE;
	}
106

107
	rc = ber_flatten2( ber, &bv, 0 );
108
	if( rc < 0 ) {
109
		perror( "ber_flatten2" );
110
111
112
		return EXIT_FAILURE;
	}

113
114
	printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
	ber_bprint( bv.bv_val, bv.bv_len );
115

116
	ber_free( ber, 1 );
117
118

	return EXIT_SUCCESS;
119
120
}