[merged]
authorHeiko Schlittermann <hs@schlittermann.de>
Thu, 01 Oct 2015 16:48:04 +0200
changeset 4 01d73aa9d30d
parent 3 e26e2d5702bb (current diff)
parent 2 1816ff04e91c (diff)
child 5 08185907e5cc
[merged]
schema2ldif
--- a/schema2ldif	Thu Oct 01 16:47:01 2015 +0200
+++ b/schema2ldif	Thu Oct 01 16:48:04 2015 +0200
@@ -1,4 +1,5 @@
 #! /usr/bin/perl
+# source: https://ssl.schlittermann.de/hg/schema2ldif
 # according
 # http://www.zytrax.com/books/ldap/ch6/slapd-config.html#use-schemas
 # © 2013 Heiko Schlittermann <hs@schlittermann.de>
@@ -9,6 +10,7 @@
 use if $ENV{DEBUG} => 'Smart::Comments';
 use English qw(-no_match_vars);
 use File::Temp;
+use File::Basename;
 use File::Spec::Functions;
 use Getopt::Long;
 use Pod::Usage;
@@ -29,7 +31,7 @@
 
     open(my $f, '<', $ARGV[0]) or die "$PROGRAM_NAME: $ARGV[0]: $!\n";
     while (<$f>) {
-	if (/^#schema2ldif:include\s+(\S+)/) {
+	if (/^#\s*schema2ldif:include\s+(\S+)/) {
 	    push @opt_additional_schemas, $1;
 	}
     }
@@ -55,25 +57,35 @@
 ### @schemas
 
 (my $name = $ARGV[0]) =~ s/\.schema$//xms;
+my $schema_name = basename $name;
 my $cf = File::Temp->new();
 my $cd = File::Temp->newdir();
 
 # create a short temp config
 $cf->print("include $_\n") foreach @schemas, $ARGV[0];
 close $cf or die "Can't close $cf: $!\n";
-# ... convert it
+# run slaptest on the temp config, this should create
+# the cn=config style structure.
 system slaptest => ( -f => $cf ),
 		   ( -F => $cd );
 die "slaptest failed, exit\n" if $?;
 
+# find the generated file
+$_ = do { 
+	my $dir = "$cd/cn=config/cn=schema";
+	$_ = [glob("$dir/*$schema_name.ldif")];
+	die "$0: no ldif found in $dir\n" if not @$_;
+	die "$0: more than one ldif found in $dir: @$_\n" if @$_ > 1;
+	$_->[0];
+};
+
 # slurp the generated file
-# FIXME: what, if we had multiple preconditions
-open(my $ldif, '<', $_ = "$cd/cn=config/cn=schema/cn={1}$name.ldif")
-    or die "Can't open $_: $!\n";
+open(my $ldif, '<', $_) or die "Can't open $_: $!\n";
 $_ = do { local $RS = undef; <$ldif> };
 close($ldif) or die "Can't close ldif generated ldif file: $!\n";
 
-# some modifications
+# some modifications according to the above mentioned
+# zytrax url
 s/\n\s+//xgms;
 s/^(?!(?:olc|dn|objectClass|cn)).*?\n//xmsg;
 s/(?<=^dn:\scn=){\d+}(\w+)/$1,cn=schema,cn=config/xms;
@@ -112,13 +124,13 @@
 =item B<--additional-schema> I<schema>
 
 Additional schema to be included (preconditions for your self 
-defind schema). The F<.schema> extension may be omitted. First it's
+defined schema). The F<.schema> extension may be omitted. First it's
 searched in the local directory, then in the directories given in
 B<--schema-dir> option(s). (default: none)
 
 NOTE: A pseudo comment 
 
-    #include core.schema
+    #schema2ldif:include core.schema
 
 may be included in the schema to convert. It works the same way.