From 764aa5d938ceefe2bb6fd8e4731e43dff562be84 Mon Sep 17 00:00:00 2001
From: Pierangelo Masarati <ando@openldap.org>
Date: Wed, 17 Mar 2004 23:25:09 +0000
Subject: [PATCH] move DN/config test in slapdn/slaptest

---
 servers/slapd/Makefile.in  |  8 ++--
 servers/slapd/main.c       |  4 +-
 servers/slapd/slapcommon.c | 93 +++++++++++++++++++++++++++++---------
 servers/slapd/slapcommon.h |  5 +-
 servers/slapd/slapdn.c     | 80 ++++++++++++++++++++++++++++++++
 servers/slapd/slaptest.c   | 53 ++++++++++++++++++++++
 6 files changed, 217 insertions(+), 26 deletions(-)
 create mode 100644 servers/slapd/slapdn.c
 create mode 100644 servers/slapd/slaptest.c

diff --git a/servers/slapd/Makefile.in b/servers/slapd/Makefile.in
index c044775b3a..644d064c7a 100644
--- a/servers/slapd/Makefile.in
+++ b/servers/slapd/Makefile.in
@@ -13,7 +13,7 @@
 ## top-level directory of the distribution or, alternatively, at
 ## <http://www.OpenLDAP.org/license.html>.
 
-SLAPTOOLS=slapadd slapcat slapindex slappasswd
+SLAPTOOLS=slapadd slapcat slapdn slapindex slappasswd slaptest
 PROGRAMS=slapd $(SLAPTOOLS)
 XPROGRAMS=sslapd libbackends.a .backend liboverlays.a
 XSRCS=version.c
@@ -35,7 +35,8 @@ SRCS	= main.c globals.c config.c daemon.c \
 		oidm.c starttls.c index.c sets.c referral.c root_dse.c \
 		sasl.c module.c mra.c mods.c sl_malloc.c limits.c \
 		backglue.c operational.c matchedValues.c cancel.c syncrepl.c \
-		slapadd.c slapcat.c slapcommon.c slapindex.c slappasswd.c \
+		slapadd.c slapcat.c slapcommon.c slapdn.c slapindex.c \
+		slappasswd.c slaptest.c \
 		backover.c ctxcsn.c ldapsync.c sessionlog.c $(@PLAT@_SRCS)
 
 OBJS	= main.o globals.o config.o daemon.o \
@@ -50,7 +51,8 @@ OBJS	= main.o globals.o config.o daemon.o \
 		oidm.o starttls.o index.o sets.o referral.o root_dse.o \
 		sasl.o module.o mra.o mods.o sl_malloc.o limits.o \
 		backglue.o operational.o matchedValues.o cancel.o syncrepl.o \
-		slapadd.o slapcat.o slapcommon.o slapindex.o slappasswd.o \
+		slapadd.o slapcat.o slapcommon.o slapdn.o slapindex.o \
+		slappasswd.o slaptest.c \
 		backover.o ctxcsn.o ldapsync.o sessionlog.o $(@PLAT@_OBJS)
 
 LDAP_INCDIR= ../../include -I$(srcdir)/slapi
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 6d4096b9bc..4ed2c6cc6d 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -65,7 +65,7 @@ static struct sockaddr_in	bind_addr;
 #endif
 
 typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
-extern MainFunc slapadd, slapcat, slapindex, slappasswd;
+extern MainFunc slapadd, slapcat, slapdn, slapindex, slappasswd, slaptest;
 
 static struct {
 	char *name;
@@ -73,8 +73,10 @@ static struct {
 } tools[] = {
 	{"slapadd", slapadd},
 	{"slapcat", slapcat},
+	{"slapdn", slapdn},
 	{"slapindex", slapindex},
 	{"slappasswd", slappasswd},
+	{"slaptest", slaptest},
 	{NULL, NULL}
 };
 
diff --git a/servers/slapd/slapcommon.c b/servers/slapd/slapcommon.c
index 9db0214ef0..996536b4f8 100644
--- a/servers/slapd/slapcommon.c
+++ b/servers/slapd/slapcommon.c
@@ -49,24 +49,29 @@ usage( int tool, const char *progname )
 {
 	char *options = NULL;
 	fprintf( stderr,
-		"usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
-			"\t[-n databasenumber | -b suffix]", progname );
+		"usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n",
+		progname );
 
 	switch( tool ) {
 	case SLAPADD:
-		options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
+		options = "\t[-n databasenumber | -b suffix]\n"
+			"\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
 		break;
 
 	case SLAPCAT:
-		options = "\t[-l ldiffile] [-m] [-k]\n";
+		options = "\t[-n databasenumber | -b suffix] [-l ldiffile] [-m] [-k]\n";
+		break;
+
+	case SLAPDN:
+		options = "\tDN [...]\n";
 		break;
 
 	case SLAPINDEX:
-		options = "\n";
+		options = "\t[-n databasenumber | -b suffix]\n";
 		break;
 	}
 
-	if( options != NULL ) {
+	if ( options != NULL ) {
 		fputs( options, stderr );
 	}
 	exit( EXIT_FAILURE );
@@ -110,16 +115,21 @@ slap_tool_init(
 		options = "b:cd:f:i:l:n:prtuvWw";
 		break;
 
-	case SLAPINDEX:
-		options = "b:cd:f:n:v";
-		mode |= SLAP_TOOL_READMAIN;
-		break;
-
 	case SLAPCAT:
 		options = "b:cd:f:kl:mn:s:v";
 		mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
 		break;
 
+	case SLAPDN:
+	case SLAPTEST:
+		options = "d:f:v";
+		break;
+
+	case SLAPINDEX:
+		options = "b:cd:f:n:v";
+		mode |= SLAP_TOOL_READMAIN;
+		break;
+
 	default:
 		fprintf( stderr, "%s: unknown tool mode (%d)\n",
 		         progname, tool );
@@ -223,16 +233,38 @@ slap_tool_init(
 		}
 	}
 
-	if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
-		usage( tool, progname );
-	}
+	switch ( tool ) {
+	case SLAPADD:
+	case SLAPCAT:
+	case SLAPINDEX:
+		if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
+			usage( tool, progname );
+		}
+
+		if ( replica_promotion && replica_demotion ) {
+			usage( tool, progname );
+
+		} else if ( !replica_promotion && !replica_demotion ) {
+			if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
+				usage( tool, progname );
+			}
+		}
+		break;
 
-	if ( replica_promotion && replica_demotion ) {
-		usage( tool, progname );
-	} else if ( !replica_promotion && !replica_demotion ) {
-		if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
+	case SLAPDN:
+		if ( argc == optind ) {
 			usage( tool, progname );
 		}
+		break;
+
+	case SLAPTEST:
+		if ( argc != optind ) {
+			usage( tool, progname );
+		}
+		break;
+
+	default:
+		break;
 	}
 
 	if ( ldiffile == NULL ) {
@@ -279,9 +311,19 @@ slap_tool_init(
 
 	ldap_syslog = 0;
 
-	if ( !nbackends ) {
-		fprintf( stderr, "No databases found in config file\n" );
-		exit( EXIT_FAILURE );
+	switch ( tool ) {
+	case SLAPADD:
+	case SLAPCAT:
+	case SLAPINDEX:
+		if ( !nbackends ) {
+			fprintf( stderr, "No databases found "
+					"in config file\n" );
+			exit( EXIT_FAILURE );
+		}
+		break;
+
+	default:
+		break;
 	}
 
 	rc = glue_sub_init();
@@ -298,6 +340,15 @@ slap_tool_init(
 		exit( EXIT_FAILURE );
 	}
 
+	switch ( tool ) {
+	case SLAPDN:
+	case SLAPTEST:
+		return;
+
+	default:
+		break;
+	}
+
 	if( subtree ) {
 		struct berval val;
 		val.bv_val = subtree;
diff --git a/servers/slapd/slapcommon.h b/servers/slapd/slapcommon.h
index fdeb68a1a4..12a1b57ffd 100644
--- a/servers/slapd/slapcommon.h
+++ b/servers/slapd/slapcommon.h
@@ -23,8 +23,11 @@
 enum slaptool {
 	SLAPADD=1,	/* LDIF -> database tool */
 	SLAPCAT,	/* database -> LDIF tool */
+	SLAPDN,		/* DN check w/ syntax tool */
 	SLAPINDEX,	/* database index tool */
-	SLAPPASSWD	/* password generation tool */
+	SLAPPASSWD,	/* password generation tool */
+	SLAPTEST,	/* slapd.conf test tool */
+	SLAPLAST
 };
 
 #define SLAP_TOOL_CTXCSN_KEEP	0
diff --git a/servers/slapd/slapdn.c b/servers/slapd/slapdn.c
new file mode 100644
index 0000000000..3db6e0be1f
--- /dev/null
+++ b/servers/slapd/slapdn.c
@@ -0,0 +1,80 @@
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2004 The OpenLDAP Foundation.
+ * Portions Copyright 2004 Pierangelo Masarati.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by Pierangelo Masarati for inclusion
+ * in OpenLDAP Software.
+ */
+
+#include "portable.h"
+
+#include <stdio.h>
+
+#include <ac/stdlib.h>
+
+#include <ac/ctype.h>
+#include <ac/string.h>
+#include <ac/socket.h>
+#include <ac/unistd.h>
+
+#include <lber.h>
+#include <ldif.h>
+#include <lutil.h>
+
+#include "slapcommon.h"
+
+int
+slapdn( int argc, char **argv )
+{
+	int			rc = EXIT_SUCCESS;
+	const char		*progname = "slapdn";
+
+#ifdef NEW_LOGGING
+	lutil_log_initialize( argc, argv );
+#endif
+	slap_tool_init( progname, SLAPDN, argc, argv );
+
+	argv = &argv[ optind ];
+	argc -= optind;
+
+	for ( ; argc--; argv++ ) {
+		struct berval	dn, pdn, ndn;
+
+		dn.bv_val = argv[ 0 ];
+		dn.bv_len = strlen( argv[ 0 ] );
+
+		rc = dnPrettyNormal( NULL, &dn,
+					&pdn, &ndn, NULL );
+		if ( rc != LDAP_SUCCESS ) {
+			fprintf( stderr, "DN: <%s> check failed %d (%s)\n",
+					dn.bv_val, rc,
+					ldap_err2string( rc ) );
+			rc = 1;
+			
+		} else {
+			fprintf( stderr, "DN: <%s> check succeeded\n"
+					"normalized: <%s>\n"
+					"pretty:     <%s>\n",
+					dn.bv_val,
+					ndn.bv_val, pdn.bv_val );
+			ch_free( ndn.bv_val );
+			ch_free( pdn.bv_val );
+			rc = 0;
+		}
+	}
+	
+	slap_tool_destroy();
+
+	return rc;
+}
diff --git a/servers/slapd/slaptest.c b/servers/slapd/slaptest.c
new file mode 100644
index 0000000000..7a20dfdab9
--- /dev/null
+++ b/servers/slapd/slaptest.c
@@ -0,0 +1,53 @@
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2004 The OpenLDAP Foundation.
+ * Portions Copyright 2004 Pierangelo Masarati.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by Pierangelo Masarati for inclusion
+ * in OpenLDAP Software.
+ */
+
+#include "portable.h"
+
+#include <stdio.h>
+
+#include <ac/stdlib.h>
+
+#include <ac/ctype.h>
+#include <ac/string.h>
+#include <ac/socket.h>
+#include <ac/unistd.h>
+
+#include <lber.h>
+#include <ldif.h>
+#include <lutil.h>
+
+#include "slapcommon.h"
+
+int
+slaptest( int argc, char **argv )
+{
+	int			rc = EXIT_SUCCESS;
+	const char		*progname = "slaptest";
+
+#ifdef NEW_LOGGING
+	lutil_log_initialize( argc, argv );
+#endif
+	slap_tool_init( progname, SLAPTEST, argc, argv );
+
+	fprintf( stderr, "config file testing succeeded\n");
+
+	slap_tool_destroy();
+
+	return rc;
+}
-- 
GitLab