Handle hostnames with upper-case letters
[webmin.git] / certmgr / import.cgi
1 #!/usr/local/bin/perl
2 # import.cgi
3 # Import Signed Certificates and Keys
4
5 require './certmgr-lib.pl';
6 $access{'import'} || &error($text{'ecannot'});
7 if ($ENV{'REQUEST_METHOD'} eq 'POST') {
8         &ReadParseMime();
9         }
10 &header($text{'import_title'}, "");
11
12 if ($in{'submitted'} eq "import") {
13         if ($in{'import'} eq $text{'import_upload_cert'}){
14                 $type="cert";
15         } elsif ($in{'import'} eq $text{'import_upload_key'}){
16                 $type="key";
17         }
18         $filename="$in{$type.'_directory'}/$in{$type.'_file_filename'}";
19         $filename=~s#//#/#g;
20         if (!$in{$type.'_directory'}) { 
21                 $error.="<li> $text{'import_e_nodir'}<br>\n";
22         }
23         if (!$in{$type.'_file_filename'}) {
24                 $error.="<li> $text{'import_e_nofilename'}<br>\n";
25         } 
26         if (!$in{$type.'_file_upload'}) {
27                 $error.="<li> $text{'import_e_nofile'}<br>\n";
28         }
29         if (!$error) {
30                 if ((-e $filename)&&(!$in{'overwrite'})) {
31                         &overwriteprompt($type);
32                 }
33                 &receive($type);
34                 exit;
35         }
36 }
37
38 if ($error) {
39         print "<hr> <b>$text{'import_error'}</b>\n<ul>\n";
40         print "$error</ul>\n$text{'import_pleasefix'}\n";
41 }
42 if (!$in{'cert_directory'}) { $in{'cert_directory'}=$config{'ssl_cert_dir'}; }
43 if (!$in{'key_directory'}) { $in{'key_directory'}=$config{'ssl_key_dir'}; }
44 if (!$in{'cert_file_filename'}) { $in{'cert_file_filename'}=$config{'cert_filename'}; }
45 if (!$in{'key_file_filename'}) { $in{'key_file_filename'}=$config{'key_filename'}; }
46         
47 print <<EOF;
48 <hr>
49 <form action="import.cgi" enctype=multipart/form-data method=post>
50 <input type=hidden name="submitted" value="import">
51 <table border>
52 <tr $tb> <td><center><b>$text{'import_header'}</b></center></td> </tr>
53 <tr $cb> <td>
54  <table width=100%>
55  <tr> <td width=35%><b>$text{'import_cert_file'}</b></td>
56  <td width=65%><input name="cert_file_upload" type="file" size="48" value="$in{'cert_file_upload_filename'}"></td></tr>
57  <tr> <td><b>$text{'import_cert_destination'}</b></td>
58  <td><select name=cert_directory>
59 EOF
60 print "  <option value='' ";
61 if (!$in{'cert_directory'}) {print "selected";}
62 print ">$text{'import_choose'}";
63 foreach $f ( &getdirs($config{'ssl_dir'})) {
64         if ($config{'ssl_dir'}."/".$f eq $in{'cert_directory'}) {print "  <option selected>$config{'ssl_dir'}/$f\n";}
65         else {print "  <option>$config{'ssl_dir'}/$f\n";}
66         }
67 print <<EOF;
68  </select></td> </tr>
69  <tr><td><b>$text{'import_cert_filename'}</b></td><td><input name="cert_file_filename" size="48" value="$in{'cert_file_filename'}"></td></tr>
70  <tr> <td colspan=2 align=right>
71  <input type=reset value="$text{'import_reset'}">
72  <input type=submit name=import value="$text{'import_upload_cert'}"></td> </tr>
73  </table>
74 </td></tr>
75 <tr $cb><td>
76  <table width=100%>
77  <tr> <td width=35%><b>$text{'import_key_file'}</b></td>
78  <td width=65%><input name="key_file_upload" type="file" size="48" value="$in{'key_file_upload_filename'}"></td></tr>
79  <tr> <td><b>$text{'import_key_destination'}</b></td>
80  <td><select name=key_directory>
81 EOF
82 print "  <option value='' ";
83 if (!$in{'key_directory'}) {print "selected";}
84 print ">$text{'import_choose'}";
85 foreach $f ( &getdirs($config{'ssl_dir'})) {
86         if ($config{'ssl_dir'}."/".$f eq $in{'key_directory'}) {print "  <option selected>$config{'ssl_dir'}/$f\n";}
87         else {print "  <option>$config{'ssl_dir'}/$f\n";}
88         }
89 print <<EOF;
90  </select></td> </tr>
91  <tr> <td><b>$text{'import_key_filename'}</b></td><td><input name="key_file_filename" size="48" value="$in{'key_file_filename'}"></td></tr>
92  <tr> <td colspan=2 align=right>
93  <input type=reset value="$text{'import_reset'}">
94  <input type=submit name="import" value="$text{'import_upload_key'}"></td> </tr>
95  </table>
96 </td></tr></table></form>
97 <hr>
98 EOF
99 &footer("", $text{'import_return'});
100
101 sub getdirs {
102         my(@dirs,@subdirs,$thisdir);
103         $thisdir=$_[0];
104         opendir(DIR, $thisdir);
105         @dirs= sort grep { !/^[.]{1,2}$/ && -d "$thisdir/$_" } readdir(DIR);
106         closedir(DIR);
107         foreach $dir (@dirs) {
108                 push(@subdirs, $dir);
109                 push(@subdirs, grep { $_=$dir.'/'.$_ } &getdirs($thisdir."/".$dir));
110         }
111         return(@subdirs);
112 }
113
114 sub receive {
115         my $type=$_[0];
116         open(FILE,">$filename");
117         print FILE $in{$type.'_file_upload'};
118         close(FILE);
119         if ($type eq "cert") { chmod(0644,$filename); }
120         elsif ($type eq "key") { chmod(0400,$filename); }
121         print &ui_hr();
122         print "<h4>File $filename uploaded successfully</h4>\n";
123         print &ui_hr();
124         &footer("", $text{'import_return'});
125 }
126
127 sub overwriteprompt{
128         my $type=$_[0];
129         my($buffer1,$buffer2,$buffer,$key,$temp_pem,$url);
130         
131         print "<table>\n<tr valign=top>";
132         if ($type eq "cert") {
133                 open(OPENSSL,"$config{'openssl_cmd'} x509 -in $filename -text -fingerprint -noout|");
134                 while(<OPENSSL>){ $buffer1.=$_; }
135                 close(OPENSSL);
136                 $url="\"view.cgi?certfile=".&my_urlize($filename).'"';
137                 print "<td><table border><tr $tb><td align=center><b><a href=$url>$filename</a></b></td> </tr>\n<tr $cb> <td>\n";
138                 if (!$buffer1) { print $text{'e_file'};}
139                 else { &print_cert_info(0,$buffer1); }
140                 print "</td></tr></table></td>\n";
141         }
142         if ($type eq "key") {
143                 open(OPENSSL,"$config{'openssl_cmd'} rsa -in $filename -text -noout|");
144                 while(<OPENSSL>){ $buffer.=$_; }
145                 close(OPENSSL);
146                 $url="\"view.cgi?keyfile=".&my_urlize($filename).'"';
147                 print "<td><table border><tr $tb> <td align=center><b><a href=$url>$filename</a></b></td> </tr>\n<tr $cb> <td>\n";
148                 if (!$buffer) { print $text{'e_file'};}
149                 else { &print_key_info(0,$buffer); }
150                 print "</td></tr></table></td>\n";
151         }
152         print "</tr></table>\n";
153         print "$text{'gencert_moreinfo'}";
154         print "<hr>\n$text{'gencert_overwrite'}\n<p>\n";
155         
156         print "<form action=import.cgi enctype=multipart/form-data method=post>\n";
157         foreach $key (keys %in) {
158                 print "<input name=\"$key\" type=hidden value=\"$in{$key}\">\n";
159         }
160         print "<input name=overwrite value=\"yes\" type=hidden>\n";
161         print "<input type=submit value=\"$text{'continue'}\"></form>\n";
162 }