2001-12-28 11:39:05 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* OpenLDAP Filter API Test */
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ldap.h>
|
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
|
|
|
#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-03 02:16:09 +08:00
|
|
|
"\tftest [-d n] filter\n" );
|
2001-12-28 11:39:05 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char *argv[] )
|
|
|
|
{
|
2001-12-28 12:33:57 +08:00
|
|
|
int i;
|
|
|
|
int debug=0;
|
2001-12-28 11:39:05 +08:00
|
|
|
char *filter=NULL;
|
|
|
|
|
2001-12-28 12:33:57 +08:00
|
|
|
while( (i = getopt( argc, argv, "Aa:Ss:" "d:" )) != EOF ) {
|
2001-12-28 11:39:05 +08:00
|
|
|
switch ( i ) {
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc - optind > 1 ) {
|
|
|
|
return usage();
|
2001-12-28 12:33:57 +08:00
|
|
|
|
|
|
|
} else if ( argc - optind != 1 ) {
|
|
|
|
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;
|
|
|
|
struct berval *bv = NULL;
|
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;
|
|
|
|
}
|
|
|
|
|
2001-12-28 12:33:57 +08:00
|
|
|
rc = ldap_int_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
|
|
|
|
|
|
|
rc = ber_flatten( ber, &bv );
|
|
|
|
if( rc < 0 ) {
|
|
|
|
perror( "ber_flatten" );
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-01-03 02:16:09 +08:00
|
|
|
printf( "BER encoding (len=%ld):\n", (long) bv->bv_len );
|
2001-12-28 12:33:57 +08:00
|
|
|
ber_bprint( bv->bv_val, bv->bv_len );
|
|
|
|
|
|
|
|
ber_free( ber, 0 );
|
2001-12-28 11:39:05 +08:00
|
|
|
ber_bvfree( bv );
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2001-12-28 12:33:57 +08:00
|
|
|
}
|
|
|
|
|