2001-10-19 03:00:07 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* OpenLDAP API Test
|
|
|
|
* Written by: Pierangelo Masarati <ando@OpenLDAP.org>
|
|
|
|
*
|
|
|
|
* This program is designed to test the ldap_str2dn/ldap_dn2str
|
|
|
|
* functions
|
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2001-10-19 03:00:07 +08:00
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/string.h>
|
2001-10-27 02:10:05 +08:00
|
|
|
#include <ac/unistd.h>
|
2001-10-19 03:00:07 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ldap.h>
|
2001-10-27 02:10:05 +08:00
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
2001-10-19 03:00:07 +08:00
|
|
|
#include "ldif.h"
|
|
|
|
#include "lutil.h"
|
|
|
|
#include "lutil_ldap.h"
|
|
|
|
#include "ldap_defaults.h"
|
|
|
|
|
|
|
|
int
|
2001-10-27 02:10:05 +08:00
|
|
|
main( int argc, char *argv[] )
|
2001-10-19 03:00:07 +08:00
|
|
|
{
|
2001-10-27 02:10:05 +08:00
|
|
|
int rc, i, debug = 0, f2 = 0;
|
2001-10-19 03:00:07 +08:00
|
|
|
unsigned flags[ 2 ] = { 0U, 0U };
|
2001-10-27 02:10:05 +08:00
|
|
|
char *strin, *str, *str2, buf[ 1024 ];
|
2001-10-29 16:05:23 +08:00
|
|
|
LDAPDN *dn, *dn2 = NULL;
|
2001-10-19 03:00:07 +08:00
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
while ( 1 ) {
|
|
|
|
int opt = getopt( argc, argv, "d:" );
|
|
|
|
|
|
|
|
if ( opt == EOF ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( opt ) {
|
|
|
|
case 'd':
|
|
|
|
debug = atoi( optarg );
|
|
|
|
break;
|
|
|
|
}
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
optind--;
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if ( argc < 2 ) {
|
|
|
|
fprintf( stderr, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
|
2001-10-29 16:05:23 +08:00
|
|
|
fprintf( stderr, "\tflags-in: V3,V2,DCE,<pedantic>\n" );
|
|
|
|
fprintf( stderr, "\tflags-out: V3,V2,UFN,DCE,AD,<pedantic>\n\n" );
|
|
|
|
fprintf( stderr, "\t<pedantic>: PEDANTIC,NOSPACES,NOONESPACE\n\n" );
|
2001-10-27 02:10:05 +08:00
|
|
|
return( 0 );
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
2001-10-27 02:10:05 +08:00
|
|
|
|
|
|
|
if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
|
|
|
|
fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
|
|
|
|
}
|
|
|
|
if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
|
2001-10-19 03:00:07 +08:00
|
|
|
size_t len;
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
fgets( buf, sizeof( buf ), stdin );
|
|
|
|
len = strlen( buf ) - 1;
|
|
|
|
if ( len >= 0 && buf[ len ] == '\n' ) {
|
|
|
|
buf[ len ] = '\0';
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
2001-10-27 02:10:05 +08:00
|
|
|
strin = buf;
|
2001-10-19 03:00:07 +08:00
|
|
|
} else {
|
2001-10-27 02:10:05 +08:00
|
|
|
strin = argv[ 1 ];
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
if ( argc >= 3 ) {
|
|
|
|
for ( i = 0; i < argc - 2; i++ ) {
|
2001-10-19 03:00:07 +08:00
|
|
|
char *s, *e;
|
2001-10-27 02:10:05 +08:00
|
|
|
for ( s = argv[ 2 + i ]; s; s = e ) {
|
|
|
|
e = strchr( s, ',' );
|
|
|
|
if ( e != NULL ) {
|
|
|
|
e[ 0 ] = '\0';
|
2001-10-19 03:00:07 +08:00
|
|
|
e++;
|
|
|
|
}
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
if ( !strcasecmp( s, "V3" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_FORMAT_LDAPV3;
|
|
|
|
} else if ( !strcasecmp( s, "V2" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_FORMAT_LDAPV2;
|
|
|
|
} else if ( !strcasecmp( s, "DCE" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_FORMAT_DCE;
|
|
|
|
} else if ( !strcasecmp( s, "UFN" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_FORMAT_UFN;
|
|
|
|
} else if ( !strcasecmp( s, "AD" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_FORMAT_AD_CANONICAL;
|
2001-12-05 04:22:26 +08:00
|
|
|
} else if ( !strcasecmp( s, "PRETTY" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_PRETTY;
|
2001-10-27 02:10:05 +08:00
|
|
|
} else if ( !strcasecmp( s, "PEDANTIC" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_PEDANTIC;
|
2001-10-29 16:05:23 +08:00
|
|
|
} else if ( !strcasecmp( s, "NOSPACES" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_P_NOLEADTRAILSPACES;
|
|
|
|
} else if ( !strcasecmp( s, "NOONESPACE" ) ) {
|
|
|
|
flags[ i ] |= LDAP_DN_P_NOSPACEAFTERRDN;
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-27 02:10:05 +08:00
|
|
|
|
|
|
|
f2 = argc > 3 ? 1 : 0;
|
|
|
|
|
|
|
|
rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
|
2001-10-19 03:00:07 +08:00
|
|
|
|
2001-10-30 18:04:24 +08:00
|
|
|
if ( rc == LDAP_SUCCESS ) {
|
|
|
|
int i;
|
|
|
|
for ( i = 0; dn[ i ]; i++ ) {
|
|
|
|
LDAPRDN *rdn = dn[ i ][ 0 ];
|
|
|
|
char *rstr;
|
|
|
|
|
|
|
|
if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
|
|
|
|
fprintf( stdout, "\tldap_rdn2str() failed\n" );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
|
|
|
|
ldap_memfree( rstr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-19 03:00:07 +08:00
|
|
|
if ( rc == LDAP_SUCCESS &&
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS ) {
|
|
|
|
char **values, *tmp, *tmp2;
|
|
|
|
int n;
|
|
|
|
|
2001-10-29 16:05:23 +08:00
|
|
|
fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
|
|
|
|
"\t= \"%s\"\n", strin, str );
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
|
|
|
|
case LDAP_DN_FORMAT_UFN:
|
|
|
|
case LDAP_DN_FORMAT_AD_CANONICAL:
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
case LDAP_DN_FORMAT_LDAPV3:
|
|
|
|
case LDAP_DN_FORMAT_LDAPV2:
|
|
|
|
tmp = ldap_dn2ufn( strin );
|
2001-10-29 16:05:23 +08:00
|
|
|
fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
|
|
|
|
"\t= \"%s\"\n", strin, tmp );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( tmp );
|
|
|
|
tmp = ldap_dn2dcedn( strin );
|
|
|
|
fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
|
2001-10-29 16:05:23 +08:00
|
|
|
"\t= \"%s\"\n", strin, tmp );
|
2001-10-27 02:10:05 +08:00
|
|
|
tmp2 = ldap_dcedn2dn( tmp );
|
|
|
|
fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
|
2001-10-29 16:05:23 +08:00
|
|
|
"\t= \"%s\"\n", tmp, tmp2 );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( tmp );
|
|
|
|
ldap_memfree( tmp2 );
|
|
|
|
tmp = ldap_dn2ad_canonical( strin );
|
|
|
|
fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
|
2001-10-29 16:05:23 +08:00
|
|
|
"\t= \"%s\"\n", strin, tmp );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( tmp );
|
|
|
|
|
|
|
|
fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
|
|
|
|
values = ldap_explode_dn( str, 0 );
|
|
|
|
for ( n = 0; values && values[ n ]; n++ ) {
|
|
|
|
char **vv;
|
|
|
|
int nn;
|
|
|
|
|
|
|
|
fprintf( stdout, "\t\"%s\"\n", values[ n ] );
|
|
|
|
|
|
|
|
fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
|
|
|
|
values[ n ] );
|
|
|
|
vv = ldap_explode_rdn( values[ n ], 0 );
|
|
|
|
for ( nn = 0; vv && vv[ nn ]; nn++ ) {
|
|
|
|
fprintf( stdout, "\t\t'%s'\n",
|
|
|
|
vv[ nn ] );
|
|
|
|
}
|
|
|
|
LDAP_VFREE( vv );
|
|
|
|
|
|
|
|
fprintf( stdout, "\tldap_explode_rdn(\"%s\")"
|
|
|
|
" (no types)\n", values[ n ] );
|
|
|
|
vv = ldap_explode_rdn( values[ n ], 1 );
|
|
|
|
for ( nn = 0; vv && vv[ nn ]; nn++ ) {
|
|
|
|
fprintf( stdout, "\t\t\t\"%s\"\n",
|
|
|
|
vv[ nn ] );
|
|
|
|
}
|
|
|
|
LDAP_VFREE( vv );
|
|
|
|
|
|
|
|
}
|
|
|
|
LDAP_VFREE( values );
|
|
|
|
|
|
|
|
fprintf( stdout, "\nldap_explode_dn(\"%s\")"
|
|
|
|
" (no types):\n", str );
|
|
|
|
values = ldap_explode_dn( str, 1 );
|
|
|
|
for ( n = 0; values && values[ n ]; n++ ) {
|
|
|
|
fprintf( stdout, "\t\"%s\"\n", values[ n ] );
|
|
|
|
}
|
|
|
|
LDAP_VFREE( values );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-10-29 16:05:23 +08:00
|
|
|
rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
|
2001-10-27 02:10:05 +08:00
|
|
|
if ( rc == LDAP_SUCCESS &&
|
2001-10-29 16:05:23 +08:00
|
|
|
ldap_dn2str( dn2, &str2, flags[ f2 ] )
|
2001-10-27 02:10:05 +08:00
|
|
|
== LDAP_SUCCESS ) {
|
2001-10-29 16:05:23 +08:00
|
|
|
int iRDN;
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n",
|
|
|
|
str, str2,
|
|
|
|
strcmp( str, str2 ) == 0 ? "yes" : "no" );
|
2001-10-29 16:05:23 +08:00
|
|
|
|
|
|
|
for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ ) {
|
|
|
|
LDAPRDN *r = dn[ iRDN ][ 0 ];
|
|
|
|
LDAPRDN *r2 = dn2[ iRDN ][ 0 ];
|
|
|
|
int iAVA;
|
|
|
|
|
|
|
|
for ( iAVA = 0; r[ iAVA ] && r[ iAVA ]; iAVA++ ) {
|
|
|
|
LDAPAVA *a = r[ iAVA ][ 0 ];
|
|
|
|
LDAPAVA *a2 = r2[ iAVA ][ 0 ];
|
|
|
|
|
2001-11-12 16:10:12 +08:00
|
|
|
if ( a->la_attr->bv_len != a2->la_attr->bv_len
|
|
|
|
|| memcmp( a->la_attr->bv_val, a2->la_attr->bv_val, a->la_attr->bv_len )
|
2001-10-29 16:05:23 +08:00
|
|
|
|| a->la_flags != a2->la_flags
|
|
|
|
|| a->la_value->bv_len != a2->la_value->bv_len
|
|
|
|
|| memcmp( a->la_value->bv_val, a2->la_value->bv_val, a->la_value->bv_len ) ) {
|
|
|
|
fprintf( stdout, "mismatch\n" );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ldapava_free_dn( dn2 );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( str2 );
|
|
|
|
}
|
|
|
|
ldap_memfree( str );
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
2001-10-29 16:05:23 +08:00
|
|
|
ldapava_free_dn( dn );
|
2001-10-19 03:00:07 +08:00
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
/* note: dn is not freed */
|
|
|
|
|
|
|
|
return( 0 );
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
|
|
|
|