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