Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / utilities / ovs-pki-cgi.in
1 #! @PERL@
2
3 use CGI;
4 use Digest::SHA1;
5 use Fcntl;
6
7 $CGI::POST_MAX = 65536;    # Limit POSTs to 64 kB.
8
9 use strict;
10 use warnings;
11
12 my $pkidir = '@PKIDIR@';
13 my $q = new CGI;
14
15 die unless $q->request_method() eq 'POST';
16
17 my $type = $q->param('type');
18 die unless defined $type;
19 die unless $type eq 'switch' or $type eq 'controller';
20
21 my $req = $q->param('req');
22 die unless defined $req;
23 die unless $req =~ /^-----BEGIN CERTIFICATE REQUEST-----$/m;
24 die unless $req =~ /^-----END CERTIFICATE REQUEST-----$/m;
25
26 my $digest = Digest::SHA1::sha1_hex($req);
27 my $incoming = "$pkidir/${type}ca/incoming";
28 my $dst = "$incoming/$digest-req.pem";
29
30 sysopen(REQUEST, "$dst.tmp", O_RDWR | O_CREAT | O_EXCL, 0600)
31   or die "sysopen $dst.tmp: $!";
32 print REQUEST $req;
33 close(REQUEST) or die "close $dst.tmp: $!";
34
35 rename("$dst.tmp", $dst) or die "rename $dst.tmp to $dst: $!";
36
37 print $q->header('text/html', '204 No response');
38
39 # Local Variables:
40 # mode: perl
41 # End: