Back to index

openldap  2.4.31
slapdn.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2004-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2004 Pierangelo Masarati.
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 file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Pierangelo Masarati for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/stdlib.h>
00026 
00027 #include <ac/ctype.h>
00028 #include <ac/string.h>
00029 #include <ac/socket.h>
00030 #include <ac/unistd.h>
00031 
00032 #include <lber.h>
00033 #include <ldif.h>
00034 #include <lutil.h>
00035 
00036 #include "slapcommon.h"
00037 
00038 int
00039 slapdn( int argc, char **argv )
00040 {
00041        int                  rc = 0;
00042        const char           *progname = "slapdn";
00043 
00044        slap_tool_init( progname, SLAPDN, argc, argv );
00045 
00046        argv = &argv[ optind ];
00047        argc -= optind;
00048 
00049        for ( ; argc--; argv++ ) {
00050               struct berval dn,
00051                             pdn = BER_BVNULL,
00052                             ndn = BER_BVNULL;
00053 
00054               ber_str2bv( argv[ 0 ], 0, 0, &dn );
00055 
00056               switch ( dn_mode ) {
00057               case SLAP_TOOL_LDAPDN_PRETTY:
00058                      rc = dnPretty( NULL, &dn, &pdn, NULL );
00059                      break;
00060 
00061               case SLAP_TOOL_LDAPDN_NORMAL:
00062                      rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
00063                      break;
00064 
00065               default:
00066                      rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
00067                      break;
00068               }
00069 
00070               if ( rc != LDAP_SUCCESS ) {
00071                      fprintf( stderr, "DN: <%s> check failed %d (%s)\n",
00072                                    dn.bv_val, rc,
00073                                    ldap_err2string( rc ) );
00074                      if ( !continuemode ) {
00075                             rc = -1;
00076                             break;
00077                      }
00078                      
00079               } else {
00080                      switch ( dn_mode ) {
00081                      case SLAP_TOOL_LDAPDN_PRETTY:
00082                             printf( "%s\n", pdn.bv_val );
00083                             break;
00084 
00085                      case SLAP_TOOL_LDAPDN_NORMAL:
00086                             printf( "%s\n", ndn.bv_val );
00087                             break;
00088 
00089                      default:
00090                             printf( "DN: <%s> check succeeded\n"
00091                                           "normalized: <%s>\n"
00092                                           "pretty:     <%s>\n",
00093                                           dn.bv_val,
00094                                           ndn.bv_val, pdn.bv_val );
00095                             break;
00096                      }
00097 
00098                      ch_free( ndn.bv_val );
00099                      ch_free( pdn.bv_val );
00100               }
00101        }
00102        
00103        if ( slap_tool_destroy())
00104               rc = EXIT_FAILURE;
00105 
00106        return rc;
00107 }