| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
# Very basic script to create TLS certificates for qpsmtpd |
| 7 |
use File::Temp qw/ tempfile tempdir /; |
| 8 |
use Getopt::Long; |
| 9 |
|
| 10 |
my %opts = (); |
| 11 |
chomp (my $hostname = `hostname --fqdn`); |
| 12 |
if ($?) { |
| 13 |
chomp($hostname = `hostname`); |
| 14 |
} |
| 15 |
print "Using hostname: $hostname\n"; |
| 16 |
my %defaults = ( |
| 17 |
C => 'XY', |
| 18 |
ST => 'unknown', |
| 19 |
L => 'unknown', |
| 20 |
O => 'QSMTPD', |
| 21 |
OU => 'Server', |
| 22 |
CN => $hostname, |
| 23 |
); |
| 24 |
|
| 25 |
GetOptions(\%opts, |
| 26 |
'C|Country:s', |
| 27 |
'ST|State:s', |
| 28 |
'L|Locality|City:s', |
| 29 |
'O|Organization:s', |
| 30 |
'OU|OrganizationalUnit|U:s', |
| 31 |
'CN|CommonName|N:s', |
| 32 |
'emailAddress|email|E:s', |
| 33 |
'help|H', |
| 34 |
); |
| 35 |
|
| 36 |
usage() if $opts{help}; |
| 37 |
|
| 38 |
# initialize defaults |
| 39 |
foreach my $key ( keys %defaults ) { |
| 40 |
$opts{$key} = $defaults{$key} unless $opts{$key} |
| 41 |
} |
| 42 |
$opts{emailAddress} = 'postmaster@'.$opts{CN}; |
| 43 |
|
| 44 |
mkdir('ssl') unless -d 'ssl'; |
| 45 |
|
| 46 |
my $CA_key = 'ssl/qpsmtpd-ca.key'; |
| 47 |
my $CA_crt = 'ssl/qpsmtpd-ca.crt'; |
| 48 |
my $CA_serial = 'ssl/.cert.serial'; |
| 49 |
|
| 50 |
my $template; |
| 51 |
my ($CA, $CAfilename) = tempfile( $template, DIR => "ssl", UNLINK => 1); |
| 52 |
|
| 53 |
print ${CA} return_cfg('CA'); |
| 54 |
close ${CA}; |
| 55 |
|
| 56 |
system('openssl', 'genrsa', '-out', $CA_key, 2048) == 0 |
| 57 |
or die "Cannot create CA key: $?"; |
| 58 |
|
| 59 |
system('openssl', 'req', '-config', $CAfilename, '-new', '-x509', |
| 60 |
'-days', (365*6), '-key', $CA_key, |
| 61 |
'-out', $CA_crt) == 0 |
| 62 |
or die "Cannot create CA cert: $?"; |
| 63 |
|
| 64 |
my $SERVER_key = 'ssl/qpsmtpd-server.key'; |
| 65 |
my $SERVER_csr = 'ssl/qpsmtpd-server.csr'; |
| 66 |
my $SERVER_crt = 'ssl/qpsmtpd-server.crt'; |
| 67 |
|
| 68 |
my ($SERVER, $SERVERfilename) = tempfile( $template, DIR => "ssl", UNLINK => 1); |
| 69 |
print ${SERVER} return_cfg($opts{OU}); |
| 70 |
close ${SERVER}; |
| 71 |
|
| 72 |
system('openssl', 'genrsa', '-out', $SERVER_key, 1024) == 0 |
| 73 |
or die "Cannot create server key: $?"; |
| 74 |
|
| 75 |
system('openssl', 'req', '-config', $SERVERfilename, '-new', |
| 76 |
'-key', $SERVER_key, '-out', $SERVER_csr) == 0 |
| 77 |
or die "Cannot create server cert: $?"; |
| 78 |
|
| 79 |
my ($SIGN, $SIGNfilename) = tempfile( $template, DIR => "ssl", UNLINK => 1); |
| 80 |
print ${SIGN} <<"EOT"; |
| 81 |
extensions = x509v3 |
| 82 |
[ x509v3 ] |
| 83 |
subjectAltName = email:copy |
| 84 |
nsComment = tls certificate |
| 85 |
nsCertType = server |
| 86 |
EOT |
| 87 |
close ${SIGN}; |
| 88 |
|
| 89 |
open my $SERIAL, '>', $CA_serial; |
| 90 |
print ${SERIAL} "01\n"; |
| 91 |
close ${SERIAL}; |
| 92 |
|
| 93 |
system('openssl', 'x509', '-extfile', $SIGNfilename, '-days', (365*2), |
| 94 |
'-CAserial', $CA_serial, '-CA', $CA_crt, |
| 95 |
'-CAkey', $CA_key, '-in', $SERVER_csr, |
| 96 |
'-req', '-out', $SERVER_crt) == 0 |
| 97 |
or die "Cannot sign cert: $?"; |
| 98 |
|
| 99 |
exit(0); |
| 100 |
|
| 101 |
sub return_cfg { |
| 102 |
my $OU = shift; |
| 103 |
my $RANDOM = int(rand(1000)).'RAN'.int(rand(1000)).'DOM'; |
| 104 |
my $cfg = <<"EOT"; |
| 105 |
[ req ] |
| 106 |
default_bits = 1024 |
| 107 |
default_keyfile = keyfile.pem |
| 108 |
distinguished_name = req_distinguished_name |
| 109 |
attributes = req_attributes |
| 110 |
prompt = no |
| 111 |
output_password = mypass |
| 112 |
|
| 113 |
[ req_distinguished_name ] |
| 114 |
C = $opts{C} |
| 115 |
ST = $opts{ST} |
| 116 |
L = $opts{L} |
| 117 |
O = $opts{O} |
| 118 |
OU = $OU |
| 119 |
CN = $opts{CN} |
| 120 |
emailAddress = $opts{emailAddress} |
| 121 |
|
| 122 |
[ req_attributes ] |
| 123 |
challengePassword = $RANDOM challenge password |
| 124 |
EOT |
| 125 |
return $cfg; |
| 126 |
} |
| 127 |
|
| 128 |
sub usage { |
| 129 |
print STDERR <<"EOT"; |
| 130 |
|
| 131 |
$0 will generate a TLS certificate "the quick way", |
| 132 |
i.e. without interaction. You can change some defaults however. |
| 133 |
|
| 134 |
These options are recognized: Default: |
| 135 |
|
| 136 |
--C Country (two letters, e.g. DE) $defaults{C} |
| 137 |
--ST State (spelled out) $defaults{ST} |
| 138 |
--L City $defaults{L} |
| 139 |
--O Organization $defaults{O} |
| 140 |
--OU Organizational Unit $defaults{OU} |
| 141 |
--CN Common name $defaults{CN} |
| 142 |
--email Email address of postmaster postmaster\@CN |
| 143 |
--help Show usage |
| 144 |
|
| 145 |
EOT |
| 146 |
exit(1); |
| 147 |
} |