Handle hostnames with upper-case letters
[webmin.git] / certmgr / gencert.cgi
1 #!/usr/local/bin/perl
2 # gencert.cgi
3 # Generates self-signed certificates
4
5 require './certmgr-lib.pl';
6 &ReadParse();
7 $access{'gencert'} || &error($text{'ecannot'});
8 &header($text{'gencert_title'}, "");
9
10 if ($in{'keysize'}==512){$checked[0]=" checked";}
11 elsif ($in{'keysize'}==2048){$checked[2]=" checked";}
12 else {$checked[1]=" checked";}  # Default keysize 1024
13 $in{'c'}=~tr/[a-z]/[A-Z]/;
14 if ($in{'submitted'} eq "generate") {
15         if (!$in{'cn'}) { $error.=$text{'gencert_e_nocn'}."<br>\n"; }
16         if (!$in{'days'}) { $error.=$text{'gencert_e_nodays'}."<br>\n"; }
17         if ($in{'password'} ne $in{'confirm_password'}) {
18                 $error.=$text{'gencert_e_badpw'}."<br>\n";
19                 $in{'password'}="";
20                 $in{'confirm_password'}="";
21         }
22         if (!($in{'certfile'} && $in{'keyfile'})){
23                 $error.=$text{'gencert_e_nofilename'}."<br>\n";
24         }
25         if (!$error) {
26                 &process();
27                 exit;
28         }
29 } else {
30         if (!$in{'certfile'}) { $in{'certfile'}=$config{'ssl_cert_dir'}."/".
31                 $config{'cert_filename'}; }
32         if (!$in{'keyfile'}) { $in{'keyfile'}=$config{'ssl_key_dir'}."/".
33                 $config{'key_filename'}; }
34         if (!$in{'keycertfile'}) { $in{'keycertfile'}=
35                 $config{'ssl_key_dir'}."/".$config{'key_cert_filename'};}
36
37 if (!$in{'cn'}) { $in{'cn'}=&get_system_hostname(); }
38 if (!$in{'days'}) { $in{'days'}=$config{'default_days'}; }
39
40 if ($error) {
41         print "<hr> <b>$text{'gencert_error'}</b>\n<ul>\n";
42         print "$error</ul>\n$text{'gencert_pleasefix'}\n";
43 }
44
45 print &ui_hr();
46 &print_cert_form("gencert");
47 print &ui_hr();
48 &footer("", $text{'index_return'});
49
50 sub process{
51         $conffilename=&tempname();
52         $outfile=&tempname();
53         if (((-e $in{'certfile'})||(-e $in{'keyfile'})||(-e $in{'keycertfile'}))&&($in{'overwrite'} ne "yes")) {
54                 &overwriteprompt();
55                 print &ui_hr();
56                 &footer("", $text{'index_return'});
57                 exit;
58         }
59         open(CONF,">$conffilename");
60         print CONF <<EOF;
61 [ req ]
62  distinguished_name = req_dn
63  prompt = no
64 [ req_dn ]
65  CN = $in{'cn'}
66 EOF
67         if ($in{'o'}) {print CONF " O = $in{'o'}\n";}
68         if ($in{'ou'}) {print CONF " OU = $in{'ou'}\n";}
69         if ($in{'l'}) {print CONF " L = $in{'l'}\n";}
70         if ($in{'st'}) {print CONF " ST = $in{'st'}\n";}
71         if ($in{'c'}) {print CONF " C = $in{'c'}\n";}
72         if ($in{'emailAddress'}) {print CONF " emailAddress = $in{'emailAddress'}\n";}
73         close(CONF);
74         if ($in{'password'}){ $des="-passout pass:".quotemeta($in{'password'}); }
75         else { $des="-nodes"; }
76         if (!(open(OPENSSL,"|$config{'openssl_cmd'} req $des -newkey rsa:$in{'keysize'} -keyout $in{'keyfile'} -new \\
77                                 -out $in{'certfile'} -config $conffilename -x509 -days $in{'days'} \\
78                                 -outform pem >$outfile 2>&1"))) {
79                 $error="$text{'e_genfailed'}: $!";
80         } else {
81                 close(OPENSSL);
82                 open(ERROR,"<$outfile");
83                 while(<ERROR>){$out.=$_;}
84                 close(ERROR);
85                 if (!((-e $in{'certfile'})&&(-e $in{'keyfile'}))) { 
86                         $error=$out;
87                 } else{
88                         $error=0;
89                         chmod(0400,$in{'keyfile'});
90                         if ($in{'keycertfile'}) {
91                                 open(OUTFILE,">$in{'keycertfile'}");
92                                 open(INFILE,"$in{'keyfile'}");
93                                 while(<INFILE>) { print OUTFILE; }
94                                 close(INFILE);
95                                 open(INFILE,"$in{'certfile'}");
96                                 while(<INFILE>) { print OUTFILE; }
97                                 close(INFILE);
98                                 close(OUTFILE);
99                                 chmod(0400,$in{'keycertfile'});
100                         }
101                 }
102         }
103         unlink($outfile);
104         unlink($conffilename);
105         print &ui_hr();
106         if ($error){ print "<b>$text{'gencert_e_genfailed'}</b>\n<pre>$error</pre>\n<hr>\n";}
107         else {
108                 print "<b>$text{'gencert_genworked'}</b>\n<pre>$out</pre>\n";
109                 $url="\"view.cgi?certfile=".&my_urlize($in{'certfile'}).'"';
110                 print "<b>$text{'gencert_saved_cert'} <a href=$url>$in{'certfile'}</a></b><br>\n";
111                 $url="\"view.cgi?keyfile=".&my_urlize($in{'keyfile'}).'"';
112                 print "<b>$text{'gencert_saved_key'} <a href=$url>$in{'keyfile'}</a></b><br>\n";
113                 $url="\"view.cgi?keycertfile=".&my_urlize($in{'keycertfile'}).'"';
114                 if (-e $in{'keycertfile'}) {
115                         print "<b>$text{'gencert_saved_keycert'} <a href=$url>$in{'keyfile'}</a></b><br>\n";
116                 }
117                 print &ui_hr();
118         }
119         print &ui_hr();
120         &footer("", $text{'index_return'});
121 }
122
123 sub overwriteprompt{
124         my($buffer1,$buffer2,$buffer,$key,$temp_pem,$url);
125         
126         print "<table>\n<tr valign=top>";
127         if (-e $in{'certfile'}) {
128                 open(OPENSSL,"$config{'openssl_cmd'} x509 -in $in{'certfile'} -text -fingerprint -noout|");
129                 while(<OPENSSL>){ $buffer1.=$_; }
130                 close(OPENSSL);
131                 $url="\"view.cgi?certfile=".&my_urlize($in{'certfile'}).'"';
132                 print "<td><table border><tr $tb><td align=center><b><a href=$url>$in{'certfile'}</a></b></td> </tr>\n<tr $cb> <td>\n";
133                 if (!$buffer1) { print $text{'e_file'};}
134                 else { &print_cert_info(0,$buffer1); }
135                 print "</td></tr></table></td>\n";
136         }
137         if (-e $in{'keyfile'}) {
138                 open(OPENSSL,"$config{'openssl_cmd'} rsa -in $in{'keyfile'} -text -noout|");
139                 while(<OPENSSL>){ $buffer.=$_; }
140                 close(OPENSSL);
141                 $url="\"view.cgi?keyfile=".&my_urlize($in{'keyfile'}).'"';
142                 print "<td><table border><tr $tb> <td align=center><b><a href=$url>$in{'keyfile'}</a></b></td> </tr>\n<tr $cb> <td>\n";
143                 if (!$buffer) { print $text{'e_file'};}
144                 else { &print_key_info(0,$buffer); }
145                 print "</td></tr></table></td>\n";
146         }
147         if (-e $in{'keycertfile'}) {
148                 undef($buffer);
149                 open(OPENSSL,"$config{'openssl_cmd'} x509 -in $in{'keycertfile'} -text -fingerprint -noout|");
150                 while(<OPENSSL>){ $buffer2.=$_; }
151                 close(OPENSSL);
152                 open(OPENSSL,"$config{'openssl_cmd'} rsa -in $in{'keycertfile'} -text -noout|");
153                 while(<OPENSSL>){ $buffer.=$_; }
154                 close(OPENSSL);
155                 if ($buffer1 ne $buffer2) {
156                         $url="\"view.cgi?keycertfile=".&my_urlize($in{'keycertfile'}).'"';
157                         print "<td><table border><tr $tb> <td align=center colspan=2><b><a href=$url>$in{'keycertfile'}</a></b></td> </tr>\n";
158                         print "<tr $cb><td><b>$text{'certificate'}</b></td><td><b>$text{'key'}</b></td></tr>\n<tr $cb valign=top> <td>\n";
159                         if (!$buffer2) { print $text{'e_file'};}
160                         else {&print_cert_info(0,$buffer2); }
161                         print "</td><td>\n";
162                         if (!$buffer) { print $text{'e_file'};}
163                         else {&print_key_info(0,$buffer); }
164                         print "</td></tr></table></td>\n";
165                 }
166         }
167         print "</tr></table>\n";
168         print "$text{'gencert_moreinfo'}";
169         print "<hr>\n$text{'gencert_overwrite'}\n<p>\n";
170         
171         print "<form action=gencert.cgi method=post>\n";
172         foreach $key (keys %in) {
173                 print "<input name=\"$key\" type=hidden value=\"$in{$key}\">\n";
174         }
175         print "<input name=overwrite value=\"yes\" type=hidden>\n";
176         print "<input type=submit value=\"$text{'continue'}\"></form>\n";
177 }