2003-11-26 15:16:36 +08:00
|
|
|
/* ftest.c -- OpenLDAP Filter API Test */
|
2001-12-28 11:39:05 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2021-01-12 03:25:53 +08:00
|
|
|
* Copyright 1998-2021 The OpenLDAP Foundation.
|
2003-11-26 15:16:36 +08:00
|
|
|
* 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>.
|
2001-12-28 11:39:05 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ldap.h>
|
|
|
|
|
2002-03-30 08:42:40 +08:00
|
|
|
#include "ldap_pvt.h"
|
2004-04-07 09:11:32 +08:00
|
|
|
#include "lber_pvt.h"
|
2001-12-28 11:39:05 +08:00
|
|
|
|
|
|
|
#include "ldif.h"
|
|
|
|
#include "lutil.h"
|
|
|
|
#include "lutil_ldap.h"
|
|
|
|
#include "ldap_defaults.h"
|
|
|
|
|
2001-12-28 12:33:57 +08:00
|
|
|
static int filter2ber( char *filter );
|
2001-12-28 11:39:05 +08:00
|
|
|
|
|
|
|
int usage()
|
|
|
|
{
|
|
|
|
fprintf( stderr, "usage:\n"
|
2002-01-04 04:05:29 +08:00
|
|
|
" ftest [-d n] filter\n"
|
2006-10-28 03:37:04 +08:00
|
|
|
" filter - RFC 4515 string representation of an "
|
2002-01-04 04:05:29 +08:00
|
|
|
"LDAP search filter\n" );
|
2001-12-28 11:39:05 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char *argv[] )
|
|
|
|
{
|
2002-01-04 04:05:29 +08:00
|
|
|
int c;
|
2001-12-28 12:33:57 +08:00
|
|
|
int debug=0;
|
2001-12-28 11:39:05 +08:00
|
|
|
|
2002-01-04 04:05:29 +08:00
|
|
|
while( (c = getopt( argc, argv, "d:" )) != EOF ) {
|
|
|
|
switch ( c ) {
|
2001-12-28 11:39:05 +08:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-04 04:05:29 +08:00
|
|
|
if ( argc - optind != 1 ) {
|
2001-12-28 12:33:57 +08:00
|
|
|
return usage();
|
2001-12-28 11:39:05 +08:00
|
|
|
}
|
|
|
|
|
2001-12-28 12:33:57 +08:00
|
|
|
return filter2ber( strdup( argv[optind] ) );
|
2001-12-28 11:39:05 +08:00
|
|
|
}
|
|
|
|
|
2001-12-28 12:33:57 +08:00
|
|
|
static int filter2ber( char *filter )
|
2001-12-28 11:39:05 +08:00
|
|
|
{
|
|
|
|
int rc;
|
2004-04-07 09:11:32 +08:00
|
|
|
struct berval bv = BER_BVNULL;
|
2001-12-28 12:33:57 +08:00
|
|
|
BerElement *ber;
|
2001-12-28 11:39:05 +08:00
|
|
|
|
2001-12-28 12:33:57 +08:00
|
|
|
printf( "Filter: %s\n", filter );
|
|
|
|
|
|
|
|
ber = ber_alloc_t( LBER_USE_DER );
|
2001-12-28 11:39:05 +08:00
|
|
|
if( ber == NULL ) {
|
|
|
|
perror( "ber_alloc_t" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-03-30 08:42:40 +08:00
|
|
|
rc = ldap_pvt_put_filter( ber, filter );
|
2002-01-03 02:16:09 +08:00
|
|
|
if( rc < 0 ) {
|
|
|
|
fprintf( stderr, "Filter error!\n");
|
2001-12-28 12:33:57 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2001-12-28 11:39:05 +08:00
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
rc = ber_flatten2( ber, &bv, 0 );
|
2001-12-28 11:39:05 +08:00
|
|
|
if( rc < 0 ) {
|
2002-12-19 03:16:31 +08:00
|
|
|
perror( "ber_flatten2" );
|
2001-12-28 11:39:05 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
|
|
|
|
ber_bprint( bv.bv_val, bv.bv_len );
|
2001-12-28 12:33:57 +08:00
|
|
|
|
2002-12-19 03:16:31 +08:00
|
|
|
ber_free( ber, 1 );
|
2001-12-28 11:39:05 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2001-12-28 12:33:57 +08:00
|
|
|
}
|
|
|
|
|