gencrl.in 6 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#
use strict;
use English;
use Getopt::Std;

#
# Generate a CRL certificate.
# 
sub usage()
{
    print "Usage: gencrl [-f]\n";
    exit(1);
}
my $optlist = "df";
my $debug   = 0;
my $force   = 0;

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $PGENIDOMAIN   = "@PROTOGENI_DOMAIN@";
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
my $SSLDIR        = "$TB/lib/ssl";
my $CACONFIG      = "$SSLDIR/ca.cnf";
my $EMULAB_CERT   = "$TB/etc/emulab.pem";
my $EMULAB_KEY    = "$TB/etc/emulab.key";
my $OPENSSL       = "/usr/bin/openssl";
my $WORKDIR       = "$TB/ssl";
my $CRLPEM	  = "crl.pem";
my $CRLDAYS	  = 30;

# Locals
my $crlcreated	  = 0;	# Last update in seconds. 0 effectively forces regeneration.
my $regen         = 0;  # If anyting has changed or if crl is about to expire.

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Protos
sub fatal($);

#
# Turn off line buffering on output
#
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use POSIX qw(strftime);
use Date::Parse;
use Time::Local;

if ($UID != 0) {
    fatal("Must be root to run this script\n");
}

#
# Check args.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"f"})) {
    $force = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}

#
# CD to the workdir, and then serialize on the lock file since there is
# some shared goop that the ssl tools muck with (serial number, index, etc.).
# 
chdir("$WORKDIR") or
    fatal("Could not chdir to $WORKDIR: $!");

TBScriptLock("mkusercert") == 0 or
    fatal("Could not get the lock!");

#
# If the crl exists and not in force mode, see how old it is. If less
# than 1/2 the distance to expiration, then do not bother to regen if
# there no one has expired since the last time (revoked field in the
# certs table).
#
if (-e $CRLPEM) {
    my $lastupdate = `$OPENSSL crl -noout -lastupdate -in $CRLPEM`;
    chomp($lastupdate);
    if (! (defined($lastupdate) && ($lastupdate =~ /^lastupdate/i))) {
	fatal("Could not parse the lastupdate field from CRL file");
    }
    $lastupdate =~ s/^lastupdate=//i;

    $crlcreated = timegm(strptime($lastupdate));
    if (!defined($crlcreated)) {
	fatal("Could not convert lastupdate field from CRL file to gmtime");
    }
    $crlcreated = timegm(strptime($lastupdate));

    #
    # Get expiration time
    #
    my $expires = `$OPENSSL crl -noout -nextupdate -in $CRLPEM`;
    chomp($expires);
    if (! (defined($expires) && ($expires =~ /^nextupdate/i))) {
	fatal("Could not parse the nextupdate field from CRL file");
    }
    $expires =~ s/^nextupdate=//i;
    $expires = timegm(strptime($expires));
    if (!defined($expires)) {
	fatal("Could not convert nextupdate field from CRL file to gmtime");
    }

    my $diff = $expires - $crlcreated;
150 151
    if (time() >= $expires ||
	$diff <= 0 ||
152
	time() >= $crlcreated + $diff / 2) {
Leigh Stoller's avatar
Leigh Stoller committed
153 154 155 156 157 158
	$force = 1;
    }

    print "$crlcreated, $expires, $diff, $force\n"
	if ($debug);
}
159 160 161
else {
    $force = 1;
}
Leigh Stoller's avatar
Leigh Stoller committed
162 163 164 165 166 167 168 169

#
# Find all revoked certificates. We want to create an index.txt file. 
#
my $query_result =
    DBQueryWarn("select idx,DN,UNIX_TIMESTAMP(created), ".
		"   UNIX_TIMESTAMP(revoked) ".
		" from user_sslcerts ".
170
		"where revoked is not null and expires > NOW()");
Leigh Stoller's avatar
Leigh Stoller committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
if (!$query_result) {
    fatal("Could not get the crl list from the DB");
}

my @list = ();

while (my ($idx,$dn,$created,$revoked) = $query_result->fetchrow_array()) {
    chomp($dn);
    
    my $string = "R\t";
    $string .= POSIX::strftime("%y%m%d%H%M%SZ", gmtime($created)) . "\t";
    $string .= POSIX::strftime("%y%m%d%H%M%SZ", gmtime($revoked)) . "\t";
    $string .= sprintf("%08x\t", $idx);
    $string .= "unknown\t$dn\n";

    push(@list, $string);

    if ($debug) {
	print STDERR "$created, $revoked\n";
    }

    # if this was revoked since last CRL, we really do need to regen,
    $regen++
	if ($revoked >= $crlcreated);
}
if (! ($regen || $force)) {
    print STDERR "No reason to regenerate. Exiting ...\n";
    # exit value important; tells caller nothing changed.
    exit(1);
}

open(CRL, ">crl.txt")
    or fatal("Could not create new crl.txt file");
foreach my $string (@list) {
    print CRL $string;
}
close(CRL);

# This file needs to exist. Not sure why. Not documented.
if (!-e "crl.txt.attr") {
    system("echo 'unique_subject = no' > crl.txt.attr");
}

#
# Generate the CRL certificate.
#
system("$OPENSSL ca -gencrl -name CA_crl -crldays $CRLDAYS -config $CACONFIG ".
       " -out $CRLPEM -cert $EMULAB_CERT -keyfile $EMULAB_KEY") == 0
    or fatal("Could not sign certificate request");

TBScriptUnlock();
exit(0);

sub fatal($)
{
    my ($msg) = @_;

    SENDMAIL($TBOPS, "Could not generate CRL", $msg);
    TBScriptUnlock();
    print STDERR "*** $0:\n".
	         "    $msg\n";
    # exit value important.
    exit(-1);
}