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