1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2004-2021 The OpenLDAP Foundation.
5  * Portions Copyright 2004 Pierangelo Masarati.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software.
19  */
20 
21 #include "portable.h"
22 
23 #include <stdio.h>
24 
25 #include <ac/stdlib.h>
26 
27 #include <ac/ctype.h>
28 #include <ac/string.h>
29 #include <ac/socket.h>
30 #include <ac/unistd.h>
31 
32 #include <lber.h>
33 #include <ldif.h>
34 #include <lutil.h>
35 
36 #include "slapcommon.h"
37 
38 int
slapdn(int argc,char ** argv)39 slapdn( int argc, char **argv )
40 {
41 	int			rc = 0;
42 	const char		*progname = "slapdn";
43 
44 	slap_tool_init( progname, SLAPDN, argc, argv );
45 
46 	argv = &argv[ optind ];
47 	argc -= optind;
48 
49 	for ( ; argc--; argv++ ) {
50 		struct berval	dn,
51 				pdn = BER_BVNULL,
52 				ndn = BER_BVNULL;
53 
54 		ber_str2bv( argv[ 0 ], 0, 0, &dn );
55 
56 		switch ( dn_mode ) {
57 		case SLAP_TOOL_LDAPDN_PRETTY:
58 			rc = dnPretty( NULL, &dn, &pdn, NULL );
59 			break;
60 
61 		case SLAP_TOOL_LDAPDN_NORMAL:
62 			rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
63 			break;
64 
65 		default:
66 			rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
67 			break;
68 		}
69 
70 		if ( rc != LDAP_SUCCESS ) {
71 			fprintf( stderr, "DN: <%s> check failed %d (%s)\n",
72 					dn.bv_val, rc,
73 					ldap_err2string( rc ) );
74 			if ( !continuemode ) {
75 				rc = -1;
76 				break;
77 			}
78 
79 		} else {
80 			switch ( dn_mode ) {
81 			case SLAP_TOOL_LDAPDN_PRETTY:
82 				printf( "%s\n", pdn.bv_val );
83 				break;
84 
85 			case SLAP_TOOL_LDAPDN_NORMAL:
86 				printf( "%s\n", ndn.bv_val );
87 				break;
88 
89 			default:
90 				printf( "DN: <%s> check succeeded\n"
91 						"normalized: <%s>\n"
92 						"pretty:     <%s>\n",
93 						dn.bv_val,
94 						ndn.bv_val, pdn.bv_val );
95 				break;
96 			}
97 
98 			ch_free( ndn.bv_val );
99 			ch_free( pdn.bv_val );
100 		}
101 	}
102 
103 	if ( slap_tool_destroy())
104 		rc = EXIT_FAILURE;
105 
106 	return rc;
107 }
108