Back to index

openldap  2.4.31
dntest.c
Go to the documentation of this file.
00001 /* dntest.c -- OpenLDAP DN API Test Program */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENT:
00017  * This program was initially developed by Pierangelo Masarati <ando@OpenLDAP.org>
00018  * for inclusion in OpenLDAP Software.
00019  */
00020 
00021 /*
00022  * This program is designed to test the ldap_str2dn/ldap_dn2str
00023  * functions
00024  */
00025 #include "portable.h"
00026 
00027 #include <stdio.h>
00028 
00029 #include <ac/stdlib.h>
00030 #include <ac/string.h>
00031 #include <ac/unistd.h>
00032 
00033 #include <ldap.h>
00034 
00035 #include "ldap-int.h"
00036 
00037 #include "ldif.h"
00038 #include "lutil.h"
00039 #include "lutil_ldap.h"
00040 #include "ldap_defaults.h"
00041 
00042 int
00043 main( int argc, char *argv[] )
00044 {
00045        int           rc, i, debug = 0, f2 = 0;
00046        unsigned      flags[ 2 ] = { 0U, 0 };
00047        char          *strin, *str = NULL, buf[ 1024 ];
00048        LDAPDN        dn, dn2 = NULL;
00049 
00050        while ( 1 ) {
00051               int opt = getopt( argc, argv, "d:" );
00052 
00053               if ( opt == EOF ) {
00054                      break;
00055               }
00056 
00057               switch ( opt ) {
00058               case 'd':
00059                      debug = atoi( optarg );
00060                      break;
00061               }
00062        }
00063 
00064        optind--;
00065        argc -= optind;
00066        argv += optind;
00067 
00068        if ( argc < 2 ) {
00069               fprintf( stderr, "usage: dntest <dn> [flags-in[,...]] [flags-out[,...]]\n\n" );
00070               fprintf( stderr, "\tflags-in:   V3,V2,DCE,<flags>\n" );
00071               fprintf( stderr, "\tflags-out:  V3,V2,UFN,DCE,AD,<flags>\n\n" );
00072               fprintf( stderr, "\t<flags>: PRETTY,PEDANTIC,NOSPACES,NOONESPACE\n\n" );
00073               return( 0 );
00074        }
00075 
00076        if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
00077               fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
00078        }
00079        if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
00080               fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
00081        }
00082 
00083        if ( strcmp( argv[ 1 ], "-" ) == 0 ) {
00084               size_t len = fgets( buf, sizeof( buf ), stdin ) ? strlen( buf ) : 0;
00085 
00086               if ( len == 0 || buf[ --len ] == '\n' ) {
00087                      buf[ len ] = '\0';
00088               }
00089               strin = buf;
00090        } else {
00091               strin = argv[ 1 ];
00092        }
00093 
00094        if ( argc >= 3 ) {
00095               for ( i = 0; i < argc - 2; i++ ) {
00096                      char *s, *e;
00097                      for ( s = argv[ 2 + i ]; s; s = e ) {
00098                             e = strchr( s, ',' );
00099                             if ( e != NULL ) {
00100                                    e[ 0 ] = '\0';
00101                                    e++;
00102                             }
00103        
00104                             if ( !strcasecmp( s, "V3" ) ) {
00105                                    flags[ i ] |= LDAP_DN_FORMAT_LDAPV3;
00106                             } else if ( !strcasecmp( s, "V2" ) ) {
00107                                    flags[ i ] |= LDAP_DN_FORMAT_LDAPV2;
00108                             } else if ( !strcasecmp( s, "DCE" ) ) {
00109                                    flags[ i ] |= LDAP_DN_FORMAT_DCE;
00110                             } else if ( !strcasecmp( s, "UFN" ) ) {
00111                                    flags[ i ] |= LDAP_DN_FORMAT_UFN;
00112                             } else if ( !strcasecmp( s, "AD" ) ) {
00113                                    flags[ i ] |= LDAP_DN_FORMAT_AD_CANONICAL;
00114                             } else if ( !strcasecmp( s, "PRETTY" ) ) {
00115                                    flags[ i ] |= LDAP_DN_PRETTY;
00116                             } else if ( !strcasecmp( s, "PEDANTIC" ) ) {
00117                                    flags[ i ] |= LDAP_DN_PEDANTIC;
00118                             } else if ( !strcasecmp( s, "NOSPACES" ) ) {
00119                                    flags[ i ] |= LDAP_DN_P_NOLEADTRAILSPACES;
00120                             } else if ( !strcasecmp( s, "NOONESPACE" ) ) {
00121                                    flags[ i ] |= LDAP_DN_P_NOSPACEAFTERRDN;
00122                             }
00123                      }
00124               }
00125        }
00126 
00127        if ( flags[ 1 ] == 0 )
00128               flags[ 1 ] = LDAP_DN_FORMAT_LDAPV3;
00129 
00130        f2 = 1;
00131 
00132        rc = ldap_str2dn( strin, &dn, flags[ 0 ] );
00133 
00134        if ( rc == LDAP_SUCCESS ) {
00135               int i;
00136               if ( dn ) {
00137                      for ( i = 0; dn[ i ]; i++ ) {
00138                             LDAPRDN              rdn = dn[ i ];
00139                             char          *rstr = NULL;
00140 
00141                             if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) {
00142                                    fprintf( stdout, "\tldap_rdn2str() failed\n" );
00143                                    continue;
00144                             }
00145 
00146                             fprintf( stdout, "\tldap_rdn2str() = \"%s\"\n", rstr );
00147                             ldap_memfree( rstr );
00148                      }
00149               } else {
00150                      fprintf( stdout, "\tempty DN\n" );
00151               }
00152        }
00153 
00154        str = NULL;
00155        if ( rc == LDAP_SUCCESS &&
00156               ldap_dn2str( dn, &str, flags[ f2 ] ) == LDAP_SUCCESS )
00157        {
00158               char   **values, *tmp, *tmp2, *str2 = NULL;
00159               int    n;
00160               
00161               fprintf( stdout, "\nldap_dn2str(ldap_str2dn(\"%s\"))\n"
00162                             "\t= \"%s\"\n", strin, str );
00163                      
00164               switch ( flags[ f2 ] & LDAP_DN_FORMAT_MASK ) {
00165               case LDAP_DN_FORMAT_UFN:
00166               case LDAP_DN_FORMAT_AD_CANONICAL:
00167                      return( 0 );
00168 
00169               case LDAP_DN_FORMAT_LDAPV3:
00170               case LDAP_DN_FORMAT_LDAPV2:
00171                      n = ldap_dn2domain( strin, &tmp );
00172                      if ( n ) {
00173                             fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin );
00174                      } else {
00175                             fprintf( stdout, "\nldap_dn2domain(\"%s\")\n"
00176                                    "\t= \"%s\"\n", strin, tmp ? tmp : "" );
00177                      }
00178                      ldap_memfree( tmp );
00179 
00180                      tmp = ldap_dn2ufn( strin );
00181                      fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n"
00182                                    "\t= \"%s\"\n", strin, tmp ? tmp : "" );
00183                      ldap_memfree( tmp );
00184 
00185                      tmp = ldap_dn2dcedn( strin );
00186                      fprintf( stdout, "\nldap_dn2dcedn(\"%s\")\n"
00187                                    "\t= \"%s\"\n", strin, tmp ? tmp : "" );
00188                      tmp2 = ldap_dcedn2dn( tmp );
00189                      fprintf( stdout, "\nldap_dcedn2dn(\"%s\")\n"
00190                                    "\t= \"%s\"\n",
00191                                    tmp ? tmp : "", tmp2 ? tmp2 : "" );
00192                      ldap_memfree( tmp );
00193                      ldap_memfree( tmp2 );
00194 
00195                      tmp = ldap_dn2ad_canonical( strin );
00196                      fprintf( stdout, "\nldap_dn2ad_canonical(\"%s\")\n"
00197                                    "\t= \"%s\"\n", strin, tmp ? tmp : "" );
00198                      ldap_memfree( tmp );
00199 
00200                      fprintf( stdout, "\nldap_explode_dn(\"%s\"):\n", str );
00201                      values = ldap_explode_dn( str, 0 );
00202                      for ( n = 0; values && values[ n ]; n++ ) {
00203                             char   **vv;
00204                             int    nn;
00205                             
00206                             fprintf( stdout, "\t\"%s\"\n", values[ n ] );
00207 
00208                             fprintf( stdout, "\tldap_explode_rdn(\"%s\")\n",
00209                                           values[ n ] );
00210                             vv = ldap_explode_rdn( values[ n ], 0 );
00211                             for ( nn = 0; vv && vv[ nn ]; nn++ ) {
00212                                    fprintf( stdout, "\t\t'%s'\n", 
00213                                                  vv[ nn ] );
00214                             }
00215                             LDAP_VFREE( vv );
00216 
00217                             fprintf( stdout, "\tldap_explode_rdn(\"%s\")"
00218                                           " (no types)\n", values[ n ] );
00219                             vv = ldap_explode_rdn( values[ n ], 1 );
00220                             for ( nn = 0; vv && vv[ nn ]; nn++ ) {
00221                                    fprintf( stdout, "\t\t\t\"%s\"\n", 
00222                                                  vv[ nn ] );
00223                             }
00224                             LDAP_VFREE( vv );
00225                             
00226                      }
00227                      LDAP_VFREE( values );
00228 
00229                      fprintf( stdout, "\nldap_explode_dn(\"%s\")"
00230                                    " (no types):\n", str );
00231                      values = ldap_explode_dn( str, 1 );
00232                      for ( n = 0; values && values[ n ]; n++ ) {
00233                             fprintf( stdout, "\t\"%s\"\n", values[ n ] );
00234                      }
00235                      LDAP_VFREE( values );
00236 
00237                      break;
00238               }
00239 
00240               dn2 = NULL;   
00241               rc = ldap_str2dn( str, &dn2, flags[ f2 ] );
00242               str2 = NULL;
00243               if ( rc == LDAP_SUCCESS && 
00244                             ldap_dn2str( dn2, &str2, flags[ f2 ] )
00245                             == LDAP_SUCCESS ) {
00246                      int    iRDN;
00247                      
00248                      fprintf( stdout, "\n\"%s\"\n\t == \"%s\" ? %s\n", 
00249                             str, str2, 
00250                             strcmp( str, str2 ) == 0 ? "yes" : "no" );
00251 
00252                      if( dn != NULL && dn2 == NULL ) {
00253                             fprintf( stdout, "dn mismatch\n" );
00254                      } else if (( dn != NULL ) && (dn2 != NULL))
00255                             for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ )
00256                      {
00257                             LDAPRDN       r = dn[ iRDN ];
00258                             LDAPRDN       r2 = dn2[ iRDN ];
00259                             int           iAVA;
00260                             
00261                             for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) {
00262                                    LDAPAVA              *a = r[ iAVA ];
00263                                    LDAPAVA              *a2 = r2[ iAVA ];
00264 
00265                                    if ( a->la_attr.bv_len != a2->la_attr.bv_len ) {
00266                                           fprintf( stdout, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n", 
00267                                                         iAVA + 1, iRDN + 1,
00268                                                         a->la_attr.bv_len, a2->la_attr.bv_len );
00269                                    } else if ( memcmp( a->la_attr.bv_val, a2->la_attr.bv_val, a->la_attr.bv_len ) ) {
00270                                           fprintf( stdout, "ava(%d), rdn(%d) attr mismatch\n", 
00271                                                         iAVA + 1, iRDN + 1 );
00272                                    } else if ( a->la_flags != a2->la_flags ) {
00273                                           fprintf( stdout, "ava(%d), rdn(%d) flag mismatch (%x->%x)\n", 
00274                                                         iAVA + 1, iRDN + 1, a->la_flags, a2->la_flags );
00275                                    } else if ( a->la_value.bv_len != a2->la_value.bv_len ) {
00276                                           fprintf( stdout, "ava(%d), rdn(%d) value len mismatch (%ld->%ld)\n", 
00277                                                         iAVA + 1, iRDN + 1, 
00278                                                         a->la_value.bv_len, a2->la_value.bv_len );
00279                                    } else if ( memcmp( a->la_value.bv_val, a2->la_value.bv_val, a->la_value.bv_len ) ) {
00280                                           fprintf( stdout, "ava(%d), rdn(%d) value mismatch\n", 
00281                                                         iAVA + 1, iRDN + 1 );
00282                                    }
00283                             }
00284                      }
00285                      
00286                      ldap_dnfree( dn2 );
00287                      ldap_memfree( str2 );
00288               }
00289               ldap_memfree( str );
00290        }
00291        ldap_dnfree( dn );
00292 
00293        /* note: dn is not freed */
00294 
00295        return( 0 );
00296 }