#!/usr/bin/perl

use MIME::Base64;

if ($ARGV[0] eq "" || $ARGV[1] eq "") {
 print "Usage: ./openLDAP2Fedora.pl original-ldif-file ldif-file-to-create\n";
 exit;
}
open(INFILE,$ARGV[0]) || die ("Could not find/open $ARGV[0]");
open(OUTFILE,">$ARGV[1]") || die ("Could not create $ARGV[1]");
while (!eof(INFILE)) {
 $in = <INFILE>;
 chomp($in);
 if (substr($in,0,1) eq "#") { print OUTFILE "\n"; }

 if (index($in,"userPassword::") > -1) {
  ($prefix,$encoded) = split("::",$in,2);
  $encoded =~ s/ //g;
  print $encoded." -> ".decode_base64($encoded)."\n";
  $newpass = decode_base64($encoded);
  $newpass =~ s/{md5}//g;  
  $newpass =~ s/{MD5}//g;
  print OUTFILE "userPassword: {MD5}$newpass\n";
 }
 else {
  print OUTFILE $in."\n";
 }
}
