]> git.sur5r.net Git - openldap/blob - libraries/libldap/ftest.c
append trailing slash to AD canonical if DN is exactly a domain (ITS#3000)
[openldap] / libraries / libldap / ftest.c
1 /* ftest.c -- OpenLDAP Filter API Test */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 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
29 #include "ldif.h"
30 #include "lutil.h"
31 #include "lutil_ldap.h"
32 #include "ldap_defaults.h"
33
34 static int filter2ber( char *filter );
35
36 int usage()
37 {
38         fprintf( stderr, "usage:\n"
39                 "  ftest [-d n] filter\n"
40                 "    filter - RFC 2254 string representation of an "
41                         "LDAP search filter\n" );
42         return EXIT_FAILURE;
43 }
44
45 int
46 main( int argc, char *argv[] )
47 {
48         int c;
49         int debug=0;
50
51     while( (c = getopt( argc, argv, "d:" )) != EOF ) {
52                 switch ( c ) {
53                 case 'd':
54                         debug = atoi( optarg );
55                         break;
56                 default:
57                         fprintf( stderr, "ftest: unrecognized option -%c\n",
58                                 optopt );
59                         return usage();
60                 }
61         }
62
63         if ( debug ) {
64                 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
65                         != LBER_OPT_SUCCESS )
66                 {
67                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n",
68                                 debug );
69                 }
70                 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
71                         != LDAP_OPT_SUCCESS )
72                 {
73                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n",
74                                 debug );
75                 }
76         }
77
78         if ( argc - optind != 1 ) {
79                 return usage();
80         }
81
82         return filter2ber( strdup( argv[optind] ) );
83 }
84
85 static int filter2ber( char *filter )
86 {
87         int rc;
88         struct berval bv = {0, NULL};
89         BerElement *ber;
90
91         printf( "Filter: %s\n", filter );
92
93         ber = ber_alloc_t( LBER_USE_DER );
94         if( ber == NULL ) {
95                 perror( "ber_alloc_t" );
96                 return EXIT_FAILURE;
97         }
98
99         rc = ldap_pvt_put_filter( ber, filter );
100         if( rc < 0 ) {
101                 fprintf( stderr, "Filter error!\n");
102                 return EXIT_FAILURE;
103         }
104
105         rc = ber_flatten2( ber, &bv, 0 );
106         if( rc < 0 ) {
107                 perror( "ber_flatten2" );
108                 return EXIT_FAILURE;
109         }
110
111         printf( "BER encoding (len=%ld):\n", (long) bv.bv_len );
112         ber_bprint( bv.bv_val, bv.bv_len );
113
114         ber_free( ber, 1 );
115
116         return EXIT_SUCCESS;
117 }
118