xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/slapdn.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: slapdn.c,v 1.3 2021/08/14 16:14:58 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 2004-2021 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 <sys/cdefs.h>
24 __RCSID("$NetBSD: slapdn.c,v 1.3 2021/08/14 16:14:58 christos Exp $");
25 
26 #include "portable.h"
27 
28 #include <stdio.h>
29 
30 #include <ac/stdlib.h>
31 
32 #include <ac/ctype.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
36 
37 #include <lber.h>
38 #include <ldif.h>
39 #include <lutil.h>
40 
41 #include "slapcommon.h"
42 
43 int
slapdn(int argc,char ** argv)44 slapdn( int argc, char **argv )
45 {
46 	int			rc = 0;
47 	const char		*progname = "slapdn";
48 
49 	slap_tool_init( progname, SLAPDN, argc, argv );
50 
51 	argv = &argv[ optind ];
52 	argc -= optind;
53 
54 	for ( ; argc--; argv++ ) {
55 		struct berval	dn,
56 				pdn = BER_BVNULL,
57 				ndn = BER_BVNULL;
58 
59 		ber_str2bv( argv[ 0 ], 0, 0, &dn );
60 
61 		switch ( dn_mode ) {
62 		case SLAP_TOOL_LDAPDN_PRETTY:
63 			rc = dnPretty( NULL, &dn, &pdn, NULL );
64 			break;
65 
66 		case SLAP_TOOL_LDAPDN_NORMAL:
67 			rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
68 			break;
69 
70 		default:
71 			rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
72 			break;
73 		}
74 
75 		if ( rc != LDAP_SUCCESS ) {
76 			fprintf( stderr, "DN: <%s> check failed %d (%s)\n",
77 					dn.bv_val, rc,
78 					ldap_err2string( rc ) );
79 			if ( !continuemode ) {
80 				rc = -1;
81 				break;
82 			}
83 
84 		} else {
85 			switch ( dn_mode ) {
86 			case SLAP_TOOL_LDAPDN_PRETTY:
87 				printf( "%s\n", pdn.bv_val );
88 				break;
89 
90 			case SLAP_TOOL_LDAPDN_NORMAL:
91 				printf( "%s\n", ndn.bv_val );
92 				break;
93 
94 			default:
95 				printf( "DN: <%s> check succeeded\n"
96 						"normalized: <%s>\n"
97 						"pretty:     <%s>\n",
98 						dn.bv_val,
99 						ndn.bv_val, pdn.bv_val );
100 				break;
101 			}
102 
103 			ch_free( ndn.bv_val );
104 			ch_free( pdn.bv_val );
105 		}
106 	}
107 
108 	if ( slap_tool_destroy())
109 		rc = EXIT_FAILURE;
110 
111 	return rc;
112 }
113