#!/usr/bin/perl use strict; use YAML; use Data::Dumper; use Getopt::Std; my %options=(); getopts("hf:ro",\%options); my $schema; sub usage{ print "$0 -h\n"; print "$0 [ -r|-o ] -f \n"; print " -o : openldap schema file [default]\n"; print " -r : redhat-ds ldif file\n"; #print " -t : table of contents\n"; } sub print_ldif{ my $string = shift; if(length($string) <= 78){ print "$string\n"; }else{ my $prtstr=substr($string,0,78); print $prtstr."\n"; $string=substr($string,78); while(length($string) > 0){ $prtstr=substr($string,0,77); print " ".$prtstr."\n"; $string=substr($string,77); } } } if((defined $options{'h'})||(!defined $options{'f'})){ usage(); exit -1; } my $data=YAML::LoadFile("$options{'f'}"); foreach my $attr (@{ $data->{'attributetype'} }){ my $attrstr; if((defined $attr->{'oid'})&&(defined $attr->{'name'})&&(defined $attr->{'syntax'})){ #print Data::Dumper->Dump([$attr]); push(@{ $attrstr }, "( $attr->{'oid'} NAME \'$attr->{'name'}\'"); push(@{ $attrstr }, "DESC \'$attr->{'desc'}\'") if $attr->{'desc'}; push(@{ $attrstr }, "SYNTAX $attr->{'syntax'}"); } foreach my $key (keys(%{ $attr })){ next if(($key eq "oid")||($key eq "name")||($key eq "desc")||($key eq "syntax")); my $bigkey=$key; $bigkey=~tr/a-z/A-Z/; if(defined($attr->{$key})){ push(@{ $attrstr }, "$bigkey \'$attr->{$key}\'"); }else{ push(@{ $attrstr }, "$bigkey"); } } push(@{ $attrstr }, ")"); if(defined $options{'r'}){ print_ldif("attributeTypes: ".join(" ",@{ $attrstr })); }else{ print "attributetype ".join("\n ",@{ $attrstr })."\n"; } } foreach my $objc (@{ $data->{'objectclass'} }){ my $objstr; if((defined $objc->{'oid'})||(defined $objc->{'name'})){ push(@{ $objstr }, "( $objc->{'oid'} NAME \'$objc->{'name'}\'"); } foreach my $key (keys(%{ $objc })){ next if(($key eq "oid")||($key eq "name")||($key eq "must")||($key eq "may")); my $bigkey=$key; $bigkey=~tr/a-z/A-Z/; if(defined($objc->{$key})){ push(@{ $objstr }, "$bigkey \'$objc->{$key}\'"); }else{ push(@{ $objstr }, "$bigkey"); } } if(defined($objc->{'must'})){ my $muststr=""; $muststr = "MUST ( "; my $mustlist; foreach my $attr (@{ $objc->{'must'} }){ push (@{ $mustlist },$attr); } $muststr .= join(" \$ ",@{ $mustlist }); $muststr .= " ) "; push(@{ $objstr }, $muststr); } if(defined($objc->{'may'})){ my $maystr=""; $maystr = "MAY ( "; my $maylist; foreach my $attr (@{ $objc->{'may'} }){ push (@{ $maylist },$attr); } $maystr .= join(" \$ ",@{ $maylist }); $maystr .= " ) "; push(@{ $objstr }, $maystr); } push(@{ $objstr }, ")"); if(defined $options{'r'}){ print_ldif("objectClasses: ".join(" ",@{ $objstr })); }else{ print "objectClass ".join("\n ",@{ $objstr })."\n"; } #print Data::Dumper->Dump([$objc]); }