3 # InspIRCd -- Internet Relay Chat Daemon
5 # Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 # Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
7 # Copyright (C) 2013 Peter Powell <petpow@saberuk.com>
9 # This file is part of InspIRCd. InspIRCd is free software: you can
10 # redistribute it and/or modify it under the terms of the GNU General Public
11 # License as published by the Free Software Foundation, version 2.
13 # This program is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 use warnings FATAL => qw(all);
32 # IMPORTANT: This script has to be able to run by itself so that it can be used
33 # by binary distributions where the make/utilities.pm module will not
37 my ($question, $default) = @_;
39 print "[$default] => ";
40 chomp(my $answer = <STDIN>);
42 return $answer ? $answer : $default;
45 if ($#ARGV != 0 || $ARGV[0] !~ /^(?:auto|gnutls|openssl)$/i) {
46 print "Syntax: genssl <auto|gnutls|openssl>\n";
50 # On OS X the GnuTLS certtool is prefixed to avoid collision with the system certtool.
51 my $certtool = $^O eq 'darwin' ? 'gnutls-certtool' : 'certtool';
53 # Check whether the user has the required tools installed.
54 my $has_gnutls = !system "$certtool --version >/dev/null 2>&1";
55 my $has_openssl = !system 'openssl version >/dev/null 2>&1';
57 # The framework the user has specified.
58 my $tool = lc $ARGV[0];
60 # If the user has not explicitly specified a framework then detect one.
61 if ($tool eq 'auto') {
64 } elsif ($has_openssl) {
67 print STDERR "SSL generation failed: could not find $certtool or openssl in the PATH!\n";
70 } elsif ($tool eq 'gnutls' && !$has_gnutls) {
71 print STDERR "SSL generation failed: could not find '$certtool' in the PATH!\n";
73 } elsif ($tool eq 'openssl' && !$has_openssl) {
74 print STDERR "SSL generation failed: could not find 'openssl' in the PATH!\n";
78 # Harvest information needed to generate the certificate.
79 my $common_name = prompt('What is the hostname of your server?', 'irc.example.com');
80 my $email = prompt('What email address can you be contacted at?', 'example@example.com');
81 my $unit = prompt('What is the name of your unit?', 'Server Admins');
82 my $organization = prompt('What is the name of your organization?', 'Example IRC Network');
83 my $city = prompt('What city are you located in?', 'Example City');
84 my $state = prompt('What state are you located in?', 'Example State');
85 my $country = prompt('What is the ISO 3166-1 code for the country you are located in?', 'XZ');
86 my $days = prompt('How many days do you want your certificate to be valid for?', '365');
88 # Contains the exit code of openssl/gnutls-certtool.
91 if ($tool eq 'gnutls') {
92 my $tmp = new File::Temp();
93 print $tmp <<__GNUTLS_END__;
97 organization = "$organization"
101 expiration_days = $days
113 $status ||= system "$certtool --generate-privkey --outfile key.pem";
114 $status ||= system "$certtool --generate-self-signed --load-privkey key.pem --outfile cert.pem --template $tmp";
115 $status ||= system "$certtool --generate-dh-params --bits 2048 --outfile dhparams.pem";
116 } elsif ($tool eq 'openssl') {
117 my $tmp = new File::Temp();
118 print $tmp <<__OPENSSL_END__;
128 $status ||= system "cat $tmp | openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days $days 2>/dev/null";
129 $status ||= system 'openssl dhparam -out dhparams.pem 2048';
133 print STDERR "SSL generation failed: $tool exited with a non-zero status!\n";