- Abraxas - https://www.effinger.org -

LDIF-Dateien zur Konfiguration von OpenLDAP bequem erzeugen

In einem vorherigen Artikel habe ich beschrieben, wie man ein eigenes LDAP-Schemas erstellen kann [1]. Im letzten Schritt muss dabei die von slaptest erzeugte LDIF-Datei von Hand angepasst werden. Diesen Vorgang habe ich mittlerweile durch ein sed Skript autmatisiert. Geholfen hat mir dabei die Vorlage von Vishal Goenka [2]. Neben dem Einfügen von Kommentaren wurde das Skript noch ergänzt, damit alle von OpenLDAP eingefügten Nummerierungen und unnötigen Zeilen am Schluss entfernt werden. Als sed-Neuling haben mit dabei folgende Webseiten weitergeholfen:

Nach Aufrufen des Skriptes von der Shell mit

unldif.sed original_cn_config_file.ldif > modified_cn_config_file.ldif

muss man lediglich noch wie gehabt die erste Zeile mit dn anpassen, die sich von Fall zu Fall unterscheiden kann. Zum Schluss nun das Skript:

#!/bin/sed -nf
# Disclaimer and Terms: You may use these scripts for commercial or
# non-commercial use at your own risk, as long as you retain the
# copyright statements in the source code. These scripts are provided
# "AS IS" with no warranty whatsoever and are FREE for as long as you
# want to use them. You can edit and adapt them to your requirements
# without seeking permission from me. I only ask that you retain the
# credits where they are due.
#
# Adapted by Markus Effinger <blog[at-nospam]effinger.org>
#
# Original Author: Vishal Goenka <vgoenka@hotmail.com>
#
# Unfold LDIF (LDAP Data Interchange Format) lines for scheme files
# Version 1.0
#
# Usage: unldif.sed <ldif file>
#              or
#        cat <ldif file> | unldif.sed
# and if /usr/bin/sed is not available,
#        sed -nf unldif.sed <ldif file>
#
# Most LDIF generators will fold a long field on multiple lines by
# inserting a line separator (either a linefeed or carriage
# return/linefeed pair) followed by a space. Processing such ldif
# files through another script becomes much easier if such lines were
# unfolded. That is exactly what this script does. It unfolds ldif
# entries that are folded (broken) across lines and writes them on a
# single line.
#
# This Line
# Replace any "{2}" numbering that was added by slaptest
s/\(^[^:]*: \){[^}]*}\(.*\)$/\1\2/;
{
	# Append first line to hold space and
	# put the next line into the pattern space
	1{
		h;
		n;
	};
	# Delete anything after structuralObjectClass
	/^structuralObjectClass/,$ {
		# These commands get run for each line after
		# structuralObjectClass - Therefore we have to make
		# sure, that the hold buffer gets only printed once
		# Get content from hold buffer to pattern buffer
		g;
		# Remove a newline followed by a space
		s/\n //;
		# Print the pattern buffer if not empty
		/^$/!p;
		# Make pattern buffer empty and save in hold buffer
		s/^.*$//;
		h;
		# Continue with next line
		d;
	};
	# If line starts not with space
	/^ /!{
		# Append a newline to the hold space and add the current line
		H;
		# Replace the content of the pattern space with the hold space
		g;
		# Print the first line of the pattern space
		# Delete everything after the first line in the pattern space
		# and print the rest (first line)
		s/\n.*//p;
		# Fill the pattern space again with the hold space
		g;
		# Delete the first line which has just been printed
		s/.*\n//;
		# Put the result in the hold space
		h;
	};
	/^ /{
		# Append a newline to the hold space and add the current line
		H;
		# Replace the content of the pattern space with the hold space
		g;
		# Remove a newline followed by a space
		s/\n //;
		# Write the result to hold space
		h;
	};
	${
		# Replace the content of the pattern space with the hold space
		g;
		# Remove a newline followed by a space
		s/\n //;
		# Print the result
		p;
	};
};