#!/usr/local/bin/perl # import.cgi # Import Signed Certificates and Keys require './certmgr-lib.pl'; $access{'import'} || &error($text{'ecannot'}); if ($ENV{'REQUEST_METHOD'} eq 'POST') { &ReadParseMime(); } &header($text{'import_title'}, ""); if ($in{'submitted'} eq "import") { if ($in{'import'} eq $text{'import_upload_cert'}){ $type="cert"; } elsif ($in{'import'} eq $text{'import_upload_key'}){ $type="key"; } $filename="$in{$type.'_directory'}/$in{$type.'_file_filename'}"; $filename=~s#//#/#g; if (!$in{$type.'_directory'}) { $error.="
  • $text{'import_e_nodir'}
    \n"; } if (!$in{$type.'_file_filename'}) { $error.="
  • $text{'import_e_nofilename'}
    \n"; } if (!$in{$type.'_file_upload'}) { $error.="
  • $text{'import_e_nofile'}
    \n"; } if (!$error) { if ((-e $filename)&&(!$in{'overwrite'})) { &overwriteprompt($type); } &receive($type); exit; } } if ($error) { print "
    $text{'import_error'}\n\n$text{'import_pleasefix'}\n"; } if (!$in{'cert_directory'}) { $in{'cert_directory'}=$config{'ssl_cert_dir'}; } if (!$in{'key_directory'}) { $in{'key_directory'}=$config{'ssl_key_dir'}; } if (!$in{'cert_file_filename'}) { $in{'cert_file_filename'}=$config{'cert_filename'}; } if (!$in{'key_file_filename'}) { $in{'key_file_filename'}=$config{'key_filename'}; } print <
    $text{'import_header'}
    $text{'import_cert_file'}
    $text{'import_cert_destination'}
    $text{'import_cert_filename'}
    $text{'import_key_file'}
    $text{'import_key_destination'}
    $text{'import_key_filename'}

    EOF &footer("", $text{'import_return'}); sub getdirs { my(@dirs,@subdirs,$thisdir); $thisdir=$_[0]; opendir(DIR, $thisdir); @dirs= sort grep { !/^[.]{1,2}$/ && -d "$thisdir/$_" } readdir(DIR); closedir(DIR); foreach $dir (@dirs) { push(@subdirs, $dir); push(@subdirs, grep { $_=$dir.'/'.$_ } &getdirs($thisdir."/".$dir)); } return(@subdirs); } sub receive { my $type=$_[0]; open(FILE,">$filename"); print FILE $in{$type.'_file_upload'}; close(FILE); if ($type eq "cert") { chmod(0644,$filename); } elsif ($type eq "key") { chmod(0400,$filename); } print &ui_hr(); print "

    File $filename uploaded successfully

    \n"; print &ui_hr(); &footer("", $text{'import_return'}); } sub overwriteprompt{ my $type=$_[0]; my($buffer1,$buffer2,$buffer,$key,$temp_pem,$url); print "\n"; if ($type eq "cert") { open(OPENSSL,"$config{'openssl_cmd'} x509 -in $filename -text -fingerprint -noout|"); while(){ $buffer1.=$_; } close(OPENSSL); $url="\"view.cgi?certfile=".&my_urlize($filename).'"'; print "\n"; } if ($type eq "key") { open(OPENSSL,"$config{'openssl_cmd'} rsa -in $filename -text -noout|"); while(){ $buffer.=$_; } close(OPENSSL); $url="\"view.cgi?keyfile=".&my_urlize($filename).'"'; print "\n"; } print "
    \n
    $filename
    \n"; if (!$buffer1) { print $text{'e_file'};} else { &print_cert_info(0,$buffer1); } print "
    \n
    $filename
    \n"; if (!$buffer) { print $text{'e_file'};} else { &print_key_info(0,$buffer); } print "
    \n"; print "$text{'gencert_moreinfo'}"; print "
    \n$text{'gencert_overwrite'}\n

    \n"; print "

    \n"; foreach $key (keys %in) { print "\n"; } print "\n"; print "
    \n"; }