Back to index

openldap  2.4.31
ftest.c
Go to the documentation of this file.
00001 /* ftest.c -- OpenLDAP Filter API Test */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <ac/stdlib.h>
00020 #include <ac/string.h>
00021 #include <ac/unistd.h>
00022 
00023 #include <stdio.h>
00024 
00025 #include <ldap.h>
00026 
00027 #include "ldap_pvt.h"
00028 #include "lber_pvt.h"
00029 
00030 #include "ldif.h"
00031 #include "lutil.h"
00032 #include "lutil_ldap.h"
00033 #include "ldap_defaults.h"
00034 
00035 static int filter2ber( char *filter );
00036 
00037 int usage()
00038 {
00039        fprintf( stderr, "usage:\n"
00040               "  ftest [-d n] filter\n"
00041               "    filter - RFC 4515 string representation of an "
00042                      "LDAP search filter\n" );
00043        return EXIT_FAILURE;
00044 }
00045 
00046 int
00047 main( int argc, char *argv[] )
00048 {
00049        int c;
00050        int debug=0;
00051 
00052     while( (c = getopt( argc, argv, "d:" )) != EOF ) {
00053               switch ( c ) {
00054               case 'd':
00055                      debug = atoi( optarg );
00056                      break;
00057               default:
00058                      fprintf( stderr, "ftest: unrecognized option -%c\n",
00059                             optopt );
00060                      return usage();
00061               }
00062        }
00063 
00064        if ( debug ) {
00065               if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
00066                      != LBER_OPT_SUCCESS )
00067               {
00068                      fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
00069                             debug );
00070               }
00071               if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
00072                      != LDAP_OPT_SUCCESS )
00073               {
00074                      fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
00075                             debug );
00076               }
00077        }
00078 
00079        if ( argc - optind != 1 ) {
00080               return usage();
00081        }
00082 
00083        return filter2ber( strdup( argv[optind] ) );
00084 }
00085 
00086 static int filter2ber( char *filter )
00087 {
00088        int rc;
00089        struct berval bv = BER_BVNULL;
00090        BerElement *ber;
00091 
00092        printf( "Filter: %s\n", filter );
00093 
00094        ber = ber_alloc_t( LBER_USE_DER );
00095        if( ber == NULL ) {
00096               perror( "ber_alloc_t" );
00097               return EXIT_FAILURE;
00098        }
00099 
00100        rc = ldap_pvt_put_filter( ber, filter );
00101        if( rc < 0 ) {
00102               fprintf( stderr, "Filter error!\n");
00103               return EXIT_FAILURE;
00104        }
00105 
00106        rc = ber_flatten2( ber, &bv, 0 );
00107        if( rc < 0 ) {
00108               perror( "ber_flatten2" );
00109               return EXIT_FAILURE;
00110        }
00111 
00112        printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
00113        ber_bprint( bv.bv_val, bv.bv_len );
00114 
00115        ber_free( ber, 1 );
00116 
00117        return EXIT_SUCCESS;
00118 }
00119