From e60338fab5fb8b563833bf59cf5cef637e9e12e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Fri, 20 Mar 2009 10:28:53 +0000
Subject: [PATCH] Added script for parsing LDAP schemas and create
 attributemaps (by Olav Morken)

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1428 44740490-163a-0410-bde0-09ae8108e29a
---
 bin/ldapattrschemaparser.pl | 102 ++++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
 create mode 100755 bin/ldapattrschemaparser.pl

diff --git a/bin/ldapattrschemaparser.pl b/bin/ldapattrschemaparser.pl
new file mode 100755
index 000000000..11af4b5f2
--- /dev/null
+++ b/bin/ldapattrschemaparser.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+my @valid_formats = (
+    'simple',
+    'oid2name',
+    'oid2urn',
+    'name2oid',
+    'name2urn',
+    'urn2oid',
+    'urn2name',
+    );
+
+my $format = shift;
+unless (defined($format)) {
+    print(STDERR "Usage: simpleparser.pl FORMAT <FILES>\n");
+    print(STDERR "Valid formats: ", join(' ', @valid_formats), "\n");
+    exit(1);
+}
+
+unless (grep { $_ eq $format } @valid_formats) {
+    print(STDERR "Invalid format: $format\n");
+    print(STDERR "Valid formats: ", join(' ', @valid_formats), "\n");
+    exit(1);
+}
+    
+
+# Load file
+my $text = join('', <>);
+
+# Strip comments
+$text =~ s/#.*$//gm;
+
+my %oids;
+my %names;
+
+while ($text =~ m"attributetype\s*\(\s*([\d.]+).*?NAME\s+(?:'(.*?)'|(\(.*?\)))"sg) {
+    my $oid = $1;
+    my @attributes;
+    if (defined($2)) {
+	# Single attribute
+	@attributes = ($2);
+    } else {
+	# Multiple attributes
+	my $input = $3;
+	while ($input =~ m"'(.*?)'"gs) {
+	    push(@attributes, $1);
+	}
+    }
+
+    foreach my $attrname (@attributes) {
+	$names{$attrname} = $oid;
+    }
+    $oids{$oid} = [ @attributes ];
+}
+
+
+if ($format eq 'simple') {
+    foreach my $oid (sort keys %oids) {
+	my @names = @{$oids{$oid}};
+	print "$oid ", join(' ', @names), "\n";
+    }
+    exit(0);
+}
+
+print "<?php\n";
+print "\$attributemap = array(\n";
+
+if ($format eq 'oid2name') {
+    foreach my $oid (sort keys %oids) {
+	my $name = $oids{$oid}->[0];
+	print "\t'urn:oid:$oid' => '$name',\n";
+    }
+} elsif ($format eq 'oid2urn') {
+    foreach my $oid (sort keys %oids) {
+	my $name = $oids{$oid}->[0];
+	print "\t'urn:oid:$oid' => 'urn:mace:dir:attribute-def:$name',\n";
+    }
+} elsif ($format eq 'name2oid') {
+    foreach my $name (sort keys %names) {
+	my $oid = $names{$name};
+	print "\t'$name' => 'urn:oid:$oid',\n";
+    }
+} elsif ($format eq 'name2urn') {
+    foreach my $name (sort keys %names) {
+	print "\t'$name' => 'urn:mace:dir:attribute-def:$name',\n";
+    }
+} elsif ($format eq 'urn2oid') {
+    foreach my $name (sort keys %names) {
+	my $oid = $names{$name};
+	print "\t'urn:mace:dir:attribute-def:$name' => 'urn:oid:$oid',\n";
+    }
+} elsif ($format eq 'urn2name') {
+    foreach my $name (sort keys %names) {
+	print "\t'urn:mace:dir:attribute-def:$name' => '$name',\n";
+    }
+}
+
+print ");\n";
+print "?>";
+
-- 
GitLab