summaryrefslogtreecommitdiffstats
path: root/inc/simplesamlphp/bin/ldapattrschemaparser.pl
blob: 11af4b5f27eef062fd72196895d6f178c5ac940c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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 "?>";