2003-11-26 15:16:36 +08:00
|
|
|
/* dntest.c -- OpenLDAP DN API Test Program */
|
2001-10-19 03:00:07 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2021-01-12 03:25:53 +08:00
|
|
|
* Copyright 1998-2021 The OpenLDAP Foundation.
|
2003-11-26 15:16:36 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENT:
|
|
|
|
* This program was initially developed by Pierangelo Masarati <ando@OpenLDAP.org>
|
|
|
|
* for inclusion in OpenLDAP Software.
|
2001-10-19 03:00:07 +08:00
|
|
|
*/
|
2003-11-26 15:16:36 +08:00
|
|
|
|
2001-10-19 03:00:07 +08:00
|
|
|
/*
|
|
|
|
* This program is designed to test the ldap_str2dn/ldap_dn2str
|
|
|
|
* functions
|
|
|
|
*/
|
|
|
|
#include "portable.h"
|
|
|
|
|
2002-07-26 11:16:21 +08:00
|
|
|
#include <stdio.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 <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;
|
2002-02-09 05:53:04 +08:00
|
|
|
unsigned flags[ 2 ] = { 0U, 0 };
|
2002-01-07 23:10:50 +08:00
|
|
|
char *strin, *str = NULL, buf[ 1024 ];
|
2003-04-11 09:02:08 +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-12-06 18:01:25 +08:00
|
|
|
fprintf( stderr, "\tflags-in: V3,V2,DCE,<flags>\n" );
|
|
|
|
fprintf( stderr, "\tflags-out: V3,V2,UFN,DCE,AD,<flags>\n\n" );
|
|
|
|
fprintf( stderr, "\t<flags>: PRETTY,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 ) {
|
2010-11-23 20:12:54 +08:00
|
|
|
size_t len = fgets( buf, sizeof( buf ), stdin ) ? strlen( buf ) : 0;
|
|
|
|
|
|
|
|
if ( len == 0 || buf[ --len ] == '\n' ) {
|
2001-10-27 02:10:05 +08:00
|
|
|
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
|
|
|
|
2002-02-09 05:53:04 +08:00
|
|
|
if ( flags[ 1 ] == 0 )
|
|
|
|
flags[ 1 ] = LDAP_DN_FORMAT_LDAPV3;
|
|
|
|
|
2001-12-06 18:01:25 +08:00
|
|
|
f2 = 1;
|
2001-10-27 02:10:05 +08:00
|
|
|
|
|
|
|
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;
|
2002-01-15 03:42:59 +08:00
|
|
|
if ( dn ) {
|
2003-04-11 09:02:08 +08:00
|
|
|
for ( i = 0; dn[ i ]; i++ ) {
|
|
|
|
LDAPRDN rdn = dn[ i ];
|
2002-01-07 23:10:50 +08:00
|
|
|
char *rstr = NULL;
|
2001-10-30 18:04:24 +08:00
|
|
|
|
2001-12-24 15:22:25 +08:00
|
|
|
if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
|
|
|
|
fprintf( stdout, "\tldap_rdn2str() failed\n" );
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-30 18:04:24 +08:00
|
|
|
|
2001-12-24 15:22:25 +08:00
|
|
|
fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
|
|
|
|
ldap_memfree( rstr );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf( stdout, "\tempty DN\n" );
|
2001-10-30 18:04:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-07 23:10:50 +08:00
|
|
|
str = NULL;
|
2001-12-24 15:22:25 +08:00
|
|
|
if ( rc == LDAP_SUCCESS &&
|
|
|
|
ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
|
|
|
|
{
|
2002-01-07 23:10:50 +08:00
|
|
|
char **values, *tmp, *tmp2, *str2 = NULL;
|
2001-10-27 02:10:05 +08:00
|
|
|
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:
|
2002-08-18 03:42:59 +08:00
|
|
|
n = ldap_dn2domain( strin, &tmp );
|
2005-08-19 23:47:52 +08:00
|
|
|
if ( n ) {
|
2002-08-18 03:42:59 +08:00
|
|
|
fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
|
|
|
|
} else {
|
|
|
|
fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
|
2005-08-19 23:47:52 +08:00
|
|
|
"\t= \"%s\"\n", strin, tmp ? tmp : "" );
|
2002-08-18 03:42:59 +08:00
|
|
|
}
|
|
|
|
ldap_memfree( tmp );
|
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
tmp = ldap_dn2ufn( strin );
|
2001-10-29 16:05:23 +08:00
|
|
|
fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
|
2005-08-19 23:47:52 +08:00
|
|
|
"\t= \"%s\"\n", strin, tmp ? tmp : "" );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( tmp );
|
2002-01-07 23:10:50 +08:00
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
tmp = ldap_dn2dcedn( strin );
|
|
|
|
fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
|
2005-08-19 23:47:52 +08:00
|
|
|
"\t= \"%s\"\n", strin, tmp ? tmp : "" );
|
2001-10-27 02:10:05 +08:00
|
|
|
tmp2 = ldap_dcedn2dn( tmp );
|
|
|
|
fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
|
2005-08-19 23:47:52 +08:00
|
|
|
"\t= \"%s\"\n",
|
|
|
|
tmp ? tmp : "", tmp2 ? tmp2 : "" );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( tmp );
|
|
|
|
ldap_memfree( tmp2 );
|
2002-01-07 23:10:50 +08:00
|
|
|
|
2001-10-27 02:10:05 +08:00
|
|
|
tmp = ldap_dn2ad_canonical( strin );
|
|
|
|
fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
|
2005-08-19 23:47:52 +08:00
|
|
|
"\t= \"%s\"\n", strin, tmp ? 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;
|
|
|
|
}
|
2002-01-07 23:10:50 +08:00
|
|
|
|
|
|
|
dn2 = NULL;
|
2001-10-29 16:05:23 +08:00
|
|
|
rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
|
2002-01-07 23:10:50 +08:00
|
|
|
str2 = NULL;
|
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
|
|
|
|
2002-01-07 23:10:50 +08:00
|
|
|
if( dn != NULL && dn2 == NULL ) {
|
|
|
|
fprintf( stdout, "dn mismatch\n" );
|
2001-12-24 15:22:25 +08:00
|
|
|
} else if (( dn != NULL ) && (dn2 != NULL))
|
2003-04-11 09:02:08 +08:00
|
|
|
for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ )
|
2001-12-24 15:22:25 +08:00
|
|
|
{
|
2003-04-11 09:02:08 +08:00
|
|
|
LDAPRDN r = dn[ iRDN ];
|
|
|
|
LDAPRDN r2 = dn2[ iRDN ];
|
2001-10-29 16:05:23 +08:00
|
|
|
int iAVA;
|
|
|
|
|
2003-04-11 09:02:08 +08:00
|
|
|
for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) {
|
|
|
|
LDAPAVA *a = r[ iAVA ];
|
|
|
|
LDAPAVA *a2 = r2[ iAVA ];
|
2001-10-29 16:05:23 +08:00
|
|
|
|
2002-01-07 23:10:50 +08:00
|
|
|
if ( a->la_attr.bv_len != a2->la_attr.bv_len ) {
|
2002-01-15 03:48:09 +08:00
|
|
|
fprintf( stdout, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n",
|
2002-01-07 23:10:50 +08:00
|
|
|
iAVA + 1, iRDN + 1,
|
|
|
|
a->la_attr.bv_len, a2->la_attr.bv_len );
|
|
|
|
} else if ( memcmp( a->la_attr.bv_val, a2->la_attr.bv_val, a->la_attr.bv_len ) ) {
|
|
|
|
fprintf( stdout, "ava(%d), rdn(%d) attr mismatch\n",
|
|
|
|
iAVA + 1, iRDN + 1 );
|
|
|
|
} else if ( a->la_flags != a2->la_flags ) {
|
|
|
|
fprintf( stdout, "ava(%d), rdn(%d) flag mismatch (%x->%x)\n",
|
|
|
|
iAVA + 1, iRDN + 1, a->la_flags, a2->la_flags );
|
|
|
|
} else if ( a->la_value.bv_len != a2->la_value.bv_len ) {
|
2002-01-15 03:48:09 +08:00
|
|
|
fprintf( stdout, "ava(%d), rdn(%d) value len mismatch (%ld->%ld)\n",
|
2002-01-07 23:10:50 +08:00
|
|
|
iAVA + 1, iRDN + 1,
|
|
|
|
a->la_value.bv_len, a2->la_value.bv_len );
|
|
|
|
} else if ( memcmp( a->la_value.bv_val, a2->la_value.bv_val, a->la_value.bv_len ) ) {
|
|
|
|
fprintf( stdout, "ava(%d), rdn(%d) value mismatch\n",
|
|
|
|
iAVA + 1, iRDN + 1 );
|
2001-10-29 16:05:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-24 06:46:37 +08:00
|
|
|
ldap_dnfree( dn2 );
|
2001-10-27 02:10:05 +08:00
|
|
|
ldap_memfree( str2 );
|
|
|
|
}
|
|
|
|
ldap_memfree( str );
|
2001-10-19 03:00:07 +08:00
|
|
|
}
|
2001-12-24 06:46:37 +08:00
|
|
|
ldap_dnfree( 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
|
|
|
}
|