1 /* ftest.c -- OpenLDAP Filter API Test */ 2 /* $OpenLDAP: pkg/ldap/libraries/libldap/ftest.c,v 1.15.2.3 2008/02/11 23:26:41 kurt Exp $ */ 3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 4 * 5 * Copyright 1998-2008 The OpenLDAP Foundation. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted only as authorized by the OpenLDAP 10 * Public License. 11 * 12 * A copy of this license is available in the file LICENSE in the 13 * top-level directory of the distribution or, alternatively, at 14 * <http://www.OpenLDAP.org/license.html>. 15 */ 16 17 #include "portable.h" 18 19 #include <ac/stdlib.h> 20 #include <ac/string.h> 21 #include <ac/unistd.h> 22 23 #include <stdio.h> 24 25 #include <ldap.h> 26 27 #include "ldap_pvt.h" 28 #include "lber_pvt.h" 29 30 #include "ldif.h" 31 #include "lutil.h" 32 #include "lutil_ldap.h" 33 #include "ldap_defaults.h" 34 35 static int filter2ber( char *filter ); 36 37 int usage() 38 { 39 fprintf( stderr, "usage:\n" 40 " ftest [-d n] filter\n" 41 " filter - RFC 4515 string representation of an " 42 "LDAP search filter\n" ); 43 return EXIT_FAILURE; 44 } 45 46 int 47 main( int argc, char *argv[] ) 48 { 49 int c; 50 int debug=0; 51 52 while( (c = getopt( argc, argv, "d:" )) != EOF ) { 53 switch ( c ) { 54 case 'd': 55 debug = atoi( optarg ); 56 break; 57 default: 58 fprintf( stderr, "ftest: unrecognized option -%c\n", 59 optopt ); 60 return usage(); 61 } 62 } 63 64 if ( debug ) { 65 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) 66 != LBER_OPT_SUCCESS ) 67 { 68 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", 69 debug ); 70 } 71 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) 72 != LDAP_OPT_SUCCESS ) 73 { 74 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", 75 debug ); 76 } 77 } 78 79 if ( argc - optind != 1 ) { 80 return usage(); 81 } 82 83 return filter2ber( strdup( argv[optind] ) ); 84 } 85 86 static int filter2ber( char *filter ) 87 { 88 int rc; 89 struct berval bv = BER_BVNULL; 90 BerElement *ber; 91 92 printf( "Filter: %s\n", filter ); 93 94 ber = ber_alloc_t( LBER_USE_DER ); 95 if( ber == NULL ) { 96 perror( "ber_alloc_t" ); 97 return EXIT_FAILURE; 98 } 99 100 rc = ldap_pvt_put_filter( ber, filter ); 101 if( rc < 0 ) { 102 fprintf( stderr, "Filter error!\n"); 103 return EXIT_FAILURE; 104 } 105 106 rc = ber_flatten2( ber, &bv, 0 ); 107 if( rc < 0 ) { 108 perror( "ber_flatten2" ); 109 return EXIT_FAILURE; 110 } 111 112 printf( "BER encoding (len=%ld):\n", (long) bv.bv_len ); 113 ber_bprint( bv.bv_val, bv.bv_len ); 114 115 ber_free( ber, 1 ); 116 117 return EXIT_SUCCESS; 118 } 119 120