1 /* dntest.c -- OpenLDAP DN API Test Program */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2012 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This program was initially developed by Pierangelo Masarati <ando@OpenLDAP.org>
18 * for inclusion in OpenLDAP Software.
22 * This program is designed to test the ldap_str2dn/ldap_dn2str
29 #include <ac/stdlib.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
39 #include "lutil_ldap.h"
40 #include "ldap_defaults.h"
43 main( int argc, char *argv[] )
45 int rc, i, debug = 0, f2 = 0;
46 unsigned flags[ 2 ] = { 0U, 0 };
47 char *strin, *str = NULL, buf[ 1024 ];
48 LDAPDN dn, dn2 = NULL;
51 int opt = getopt( argc, argv, "d:" );
59 debug = atoi( optarg );
69 fprintf( stderr, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
70 fprintf( stderr, "\tflags-in: V3,V2,DCE,<flags>\n" );
71 fprintf( stderr, "\tflags-out: V3,V2,UFN,DCE,AD,<flags>\n\n" );
72 fprintf( stderr, "\t<flags>: PRETTY,PEDANTIC,NOSPACES,NOONESPACE\n\n" );
76 if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
77 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
79 if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
80 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
83 if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
84 size_t len = fgets( buf, sizeof( buf ), stdin ) ? strlen( buf ) : 0;
86 if ( len == 0 || buf[ --len ] == '\n' ) {
95 for ( i = 0; i < argc - 2; i++ ) {
97 for ( s = argv[ 2 + i ]; s; s = e ) {
104 if ( !strcasecmp( s, "V3" ) ) {
105 flags[ i ] |= LDAP_DN_FORMAT_LDAPV3;
106 } else if ( !strcasecmp( s, "V2" ) ) {
107 flags[ i ] |= LDAP_DN_FORMAT_LDAPV2;
108 } else if ( !strcasecmp( s, "DCE" ) ) {
109 flags[ i ] |= LDAP_DN_FORMAT_DCE;
110 } else if ( !strcasecmp( s, "UFN" ) ) {
111 flags[ i ] |= LDAP_DN_FORMAT_UFN;
112 } else if ( !strcasecmp( s, "AD" ) ) {
113 flags[ i ] |= LDAP_DN_FORMAT_AD_CANONICAL;
114 } else if ( !strcasecmp( s, "PRETTY" ) ) {
115 flags[ i ] |= LDAP_DN_PRETTY;
116 } else if ( !strcasecmp( s, "PEDANTIC" ) ) {
117 flags[ i ] |= LDAP_DN_PEDANTIC;
118 } else if ( !strcasecmp( s, "NOSPACES" ) ) {
119 flags[ i ] |= LDAP_DN_P_NOLEADTRAILSPACES;
120 } else if ( !strcasecmp( s, "NOONESPACE" ) ) {
121 flags[ i ] |= LDAP_DN_P_NOSPACEAFTERRDN;
127 if ( flags[ 1 ] == 0 )
128 flags[ 1 ] = LDAP_DN_FORMAT_LDAPV3;
132 rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
134 if ( rc == LDAP_SUCCESS ) {
137 for ( i = 0; dn[ i ]; i++ ) {
138 LDAPRDN rdn = dn[ i ];
141 if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
142 fprintf( stdout, "\tldap_rdn2str() failed\n" );
146 fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
147 ldap_memfree( rstr );
150 fprintf( stdout, "\tempty DN\n" );
155 if ( rc == LDAP_SUCCESS &&
156 ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
158 char **values, *tmp, *tmp2, *str2 = NULL;
161 fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
162 "\t= \"%s\"\n", strin, str );
164 switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
165 case LDAP_DN_FORMAT_UFN:
166 case LDAP_DN_FORMAT_AD_CANONICAL:
169 case LDAP_DN_FORMAT_LDAPV3:
170 case LDAP_DN_FORMAT_LDAPV2:
171 n = ldap_dn2domain( strin, &tmp );
173 fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
175 fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
176 "\t= \"%s\"\n", strin, tmp ? tmp : "" );
180 tmp = ldap_dn2ufn( strin );
181 fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
182 "\t= \"%s\"\n", strin, tmp ? tmp : "" );
185 tmp = ldap_dn2dcedn( strin );
186 fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
187 "\t= \"%s\"\n", strin, tmp ? tmp : "" );
188 tmp2 = ldap_dcedn2dn( tmp );
189 fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
191 tmp ? tmp : "", tmp2 ? tmp2 : "" );
193 ldap_memfree( tmp2 );
195 tmp = ldap_dn2ad_canonical( strin );
196 fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
197 "\t= \"%s\"\n", strin, tmp ? tmp : "" );
200 fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
201 values = ldap_explode_dn( str, 0 );
202 for ( n = 0; values && values[ n ]; n++ ) {
206 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
208 fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
210 vv = ldap_explode_rdn( values[ n ], 0 );
211 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
212 fprintf( stdout, "\t\t'%s'\n",
217 fprintf( stdout, "\tldap_explode_rdn(\"%s\")"
218 " (no types)\n", values[ n ] );
219 vv = ldap_explode_rdn( values[ n ], 1 );
220 for ( nn = 0; vv && vv[ nn ]; nn++ ) {
221 fprintf( stdout, "\t\t\t\"%s\"\n",
227 LDAP_VFREE( values );
229 fprintf( stdout, "\nldap_explode_dn(\"%s\")"
230 " (no types):\n", str );
231 values = ldap_explode_dn( str, 1 );
232 for ( n = 0; values && values[ n ]; n++ ) {
233 fprintf( stdout, "\t\"%s\"\n", values[ n ] );
235 LDAP_VFREE( values );
241 rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
243 if ( rc == LDAP_SUCCESS &&
244 ldap_dn2str( dn2, &str2, flags[ f2 ] )
248 fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n",
250 strcmp( str, str2 ) == 0 ? "yes" : "no" );
252 if( dn != NULL && dn2 == NULL ) {
253 fprintf( stdout, "dn mismatch\n" );
254 } else if (( dn != NULL ) && (dn2 != NULL))
255 for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ )
257 LDAPRDN r = dn[ iRDN ];
258 LDAPRDN r2 = dn2[ iRDN ];
261 for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) {
262 LDAPAVA *a = r[ iAVA ];
263 LDAPAVA *a2 = r2[ iAVA ];
265 if ( a->la_attr.bv_len != a2->la_attr.bv_len ) {
266 fprintf( stdout, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n",
268 a->la_attr.bv_len, a2->la_attr.bv_len );
269 } else if ( memcmp( a->la_attr.bv_val, a2->la_attr.bv_val, a->la_attr.bv_len ) ) {
270 fprintf( stdout, "ava(%d), rdn(%d) attr mismatch\n",
271 iAVA + 1, iRDN + 1 );
272 } else if ( a->la_flags != a2->la_flags ) {
273 fprintf( stdout, "ava(%d), rdn(%d) flag mismatch (%x->%x)\n",
274 iAVA + 1, iRDN + 1, a->la_flags, a2->la_flags );
275 } else if ( a->la_value.bv_len != a2->la_value.bv_len ) {
276 fprintf( stdout, "ava(%d), rdn(%d) value len mismatch (%ld->%ld)\n",
278 a->la_value.bv_len, a2->la_value.bv_len );
279 } else if ( memcmp( a->la_value.bv_val, a2->la_value.bv_val, a->la_value.bv_len ) ) {
280 fprintf( stdout, "ava(%d), rdn(%d) value mismatch\n",
281 iAVA + 1, iRDN + 1 );
287 ldap_memfree( str2 );
293 /* note: dn is not freed */