diff --git a/setup.php-presetup b/setup.php-presetup
index 630d63d..09b0ec7 100644
--- a/setup.php-presetup
+++ b/setup.php-presetup
@@ -3,27 +3,27 @@ include('./config.php');
include('./include/my_functions.php');
include('./include/common.php');
-function flush_exec($command, $line_length=200) {
- $handle = popen("$command 2>&1",'r');
+function flush_exec($command, $line_length = 200)
+{
+ $handle = popen("$command 2>&1", 'r');
$line = '';
- while (! feof($handle)) {
- $chr = fread($handle, 1);
- $line .= $chr;
- if ($chr == "\n") {
- print str_replace("\n", "
\n", $line);
- $line = '';
- flush();
- }
- elseif (strlen($line) > $line_length) {
- print $line."
\n";
- $line = '';
- flush();
- }
+ while (! feof($handle)) {
+ $chr = fread($handle, 1);
+ $line .= $chr;
+ if ($chr == "\n") {
+ print str_replace("\n", "
\n", $line);
+ $line = '';
+ flush();
+ } elseif (strlen($line) > $line_length) {
+ print $line."
\n";
+ $line = '';
+ flush();
}
+ }
print $line."
\n";
- flush();
- return;
+ flush();
+ return;
}
$version = PHPKI_VERSION;
@@ -69,7 +69,9 @@ $ca_prefix = gpvar('ca_prefix');
$header_title = gpvar('header_title');
$store_dir = gpvar('store_dir');
-if ($base_url && substr($base_url,-1) != '/') $base_url .= '/';
+if ($base_url && substr($base_url, -1) != '/') {
+ $base_url .= '/';
+}
$hidden_fields = '
@@ -101,62 +103,99 @@ $hidden_fields = '
';
switch ($stage) {
-case 'validate':
- $er = '';
+ case 'validate':
+ $er = '';
- if (! $country) $er .= 'Missing Country
';
- if (! $province) $er .= 'Missing State/Province
';
- if (! $locality) $er .= 'Missing Locality
';
- if (! $organization) $er .= 'Missing Organization
';
- if (! $unit) $er .= 'Missing Unit/Department
';
- if (! $contact) $er .= 'Missing Contact E-mail Address
';
- if (! $common_name) $er .= 'Missing Common Name
';
- if (! $passwd) $er .= 'Missing Certificate Password
';
- if (! $passwdv) $er .= 'Missing Certificate Password Verification "Again"
';
- if (! $header_title) $er .= 'Missing Header Title
';
- if (! $passwd_file) $er .= 'Missing User Password File Location';
- if (! $store_dir) $er .= 'Missing Storage Directory
';
+ if (! $country) {
+ $er .= 'Missing Country
';
+ }
+ if (! $province) {
+ $er .= 'Missing State/Province
';
+ }
+ if (! $locality) {
+ $er .= 'Missing Locality
';
+ }
+ if (! $organization) {
+ $er .= 'Missing Organization
';
+ }
+ if (! $unit) {
+ $er .= 'Missing Unit/Department
';
+ }
+ if (! $contact) {
+ $er .= 'Missing Contact E-mail Address
';
+ }
+ if (! $common_name) {
+ $er .= 'Missing Common Name
';
+ }
+ if (! $passwd) {
+ $er .= 'Missing Certificate Password
';
+ }
+ if (! $passwdv) {
+ $er .= 'Missing Certificate Password Verification "Again"
';
+ }
+ if (! $header_title) {
+ $er .= 'Missing Header Title
';
+ }
+ if (! $passwd_file) {
+ $er .= 'Missing User Password File Location';
+ }
+ if (! $store_dir) {
+ $er .= 'Missing Storage Directory
';
+ }
- $countrycode = strtoupper($country);
+ $countrycode = strtoupper($country);
- if (! preg_match("/\b[A-Z][A-Z]\b/", $countrycode, $match) ) {
- $er .= 'Country Code must be ISO 3166 two letters
';
- }
-
- if ( $passwd && strlen($passwd) < 8 )
- $er .= 'Certificate password is too short.
';
+ if (! preg_match("/\b[A-Z][A-Z]\b/", $countrycode, $match)) {
+ $er .= 'Country Code must be ISO 3166 two letters
';
+ }
+
+ if ($passwd && strlen($passwd) < 8) {
+ $er .= 'Certificate password is too short.
';
+ }
- if ( $passwd and $passwd != $passwdv )
- $er .= 'Password and password verification do not match.
';
+ if ($passwd and $passwd != $passwdv) {
+ $er .= 'Password and password verification do not match.
';
+ }
- if ( $contact && ! is_email($contact) )
+ if ($contact && ! is_email($contact)) {
$er .= 'E-mail address ('. htvar($contact) . ') may be invalid.
';
+ }
- if (strpos($store_dir,$_SERVER['DOCUMENT_ROOT']) === 0)
- $er .= 'Store directory must exist somewhere outside of DOCUMENT_ROOT ('.$_SERVER['DOCUMENT_ROOT'].').
';
+ if (strpos($store_dir, $_SERVER['DOCUMENT_ROOT']) === 0) {
+ $er .= 'Store directory must exist somewhere outside of DOCUMENT_ROOT ('.$_SERVER['DOCUMENT_ROOT'].').
';
+ }
- if (strpos($store_dir,dirname($_SERVER['SCRIPT_FILENAME'])) === 0)
- $er .= 'Store directory cannot exist within the PHPki installation directory ('.dirname($_SERVER['SCRIPT_FILENAME']).').
';
+ if (strpos($store_dir, dirname($_SERVER['SCRIPT_FILENAME'])) === 0) {
+ $er .= 'Store directory cannot exist within the PHPki installation directory ('.dirname($_SERVER['SCRIPT_FILENAME']).').
';
+ }
- if (! $er) {
- if (! file_exists($store_dir) ) {
- if ( ! mkdir("$store_dir",$store_perms)) $er .= "Could not create the store directory \"$store_dir\"
";
- }
+ if (! $er) {
+ if (! file_exists($store_dir)) {
+ if (! mkdir("$store_dir", $store_perms)) {
+ $er .= "Could not create the store directory \"$store_dir\"
";
+ }
+ }
- if (file_exists($store_dir)) {
- if (! chmod($store_dir, $store_perms)) $er .= "Could not change permissions on the store directory \"$store_dir\"
";
- if (! is_readable($store_dir)) $er .= "The store directory \"$store_dir\" is not readable by the web server user \"$uname\"
";
- if (! is_writeable($store_dir)) $er .= "The store directory \"$store_dir\: is not writeable by the web server user \"$uname\"
";
- }
- else {
- $er .= "Store directory \"$store_dir\" does not exist. You will have to manually create it as desribed in the setup form.
";
- }
- }
+ if (file_exists($store_dir)) {
+ if (! chmod($store_dir, $store_perms)) {
+ $er .= "Could not change permissions on the store directory \"$store_dir\"
";
+ }
+ if (! is_readable($store_dir)) {
+ $er .= "The store directory \"$store_dir\" is not readable by the web server user \"$uname\"
";
+ }
+ if (! is_writeable($store_dir)) {
+ $er .= "The store directory \"$store_dir\: is not writeable by the web server user \"$uname\"
";
+ }
+ } else {
+ $er .= "Store directory \"$store_dir\" does not exist. You will have to manually create it as desribed in the setup form.
";
+ }
+ }
- if ( $er )
+ if ($er) {
$er = '
' . $er . '
Creating 1024 bit Diffie-Hellman parameters used by OpenVPN.
';
- #print "Saving to $config[private_dir]/dhparam1024.pem.
";
- #$cmd = "openssl dhparam -rand '$config[random]' -out '$config[private_dir]/dhparam1024.pem' 1024";
- #print $cmd.'
';
- #flush();
- #flush_exec($cmd,100);
-
- #print "Please ignore warnings about \"unable to write 'random state\'
";
-
- // This method works but still errors in logs
- // exec(DH . "-rand '$config[random]' -out '$config[private_dir]/dhparam1024.pem' 1024");
- // exec(DH . " -out '$config[private_dir]/dhparam1024.pem' 1024");
-
- print '
Creating 2048 bit Diffie-Hellman parameters used by OpenVPN.
';
- print "Saving to $store_dir/dhparam2048.pem.
";
- $cmd = "openssl dhparam -rand '$config[random]' -out '$config[private_dir]/dhparam2048.pem' 2048";
- print $cmd.'
';
- flush();
- flush_exec($cmd,200);
-
- print "Please ignore warnings about \"unable to write 'random state'
";
-
- #
- # Create a TLS auth key for OpenVPN if openvpn is installed
- #
- $command = 'which openvpn';
- $command = escapeshellcmd($command);
-
- if (system ($command) == '/usr/sbin/openvpn') {
-
- print '
Creating a TLS authentication key used by OpenVPN.
';
- print "Saving to $store_dir/takey.pem.
";
- $cmd = "openvpn --genkey --secret '$config[private_dir]/takey.pem'";
- print $cmd.'
';
+ #
+ # Create a new self-signed CA certificate in PEM format.
+ #
+ print 'Creating root certificate...
';
flush();
- flush_exec($cmd);
- } else {
- echo "openvpn is required to generate a takey.pem
";
- echo "You can create one later like this:
";
- echo "openvpn --genkey --secret". $config['private_dir'] . "/takey.pem
";
- }
+
+ $configOpenssl_cnf = $config['openssl_cnf'];
+ $configPrivate_dir = $config['private_dir'];
+ $configCacert_pem = $config['cacert_pem'];
+ $configCa_pwd = $config['ca_pwd'];
+ $configCakey = $config['cakey'];
+ $configRandom = $config['random'];
+ $configCacrl_der = $config['cacrl_der'];
+ $configCacrl_pem = $config['cacrl_pem'];
+
+ // .rnd created here
+ exec(REQ . " -x509 -config $tmp_cnf -extensions root_ext -newkey rsa:$keysize -keyout $configCakey -out $configCacert_pem -passout pass:'$configCa_pwd' -days $days 2>&1");
+
+ # **** DISABLED *****
+ # It appears that both IE and Netscape accept PEM formatted root certificates
+ #
+ # Create a copy of the CA certificate in DER format.
+ #
+ #exec(X509 . " -in ca/$config[cacert_pem] -inform PEM -out ca/$config[cacert_der] -outform DER 2>&1");
+
+ #
+ # Generate the initial CRL.
+ #
+ print 'Generating certificate revocation list...
';
+ flush();
+ exec(CA . " -gencrl -config $configOpenssl_cnf -out $configCacrl_pem -passin pass:'$configCa_pwd'");
+
+ # Make a copy of the CRL in DER format.
+ #
+ exec(CRL . " -in $configCacrl_pem -out $configCarcrl_der -inform PEM -outform DER");
+
+ #
+ # Clean up.
+ #
+ if (! unlink("$store_dir/tmp/openssl.cnf")) {
+ print "Can't unlink $store_dir/tmp/openssl.cnf";
+ }
+
+ #
+ # Create dhparam files for OpenVPN and others.
+ #
+ #print '
Creating 1024 bit Diffie-Hellman parameters used by OpenVPN.
';
+ #print "Saving to $config[private_dir]/dhparam1024.pem.
";
+ #$cmd = "openssl dhparam -rand '$config[random]' -out '$config[private_dir]/dhparam1024.pem' 1024";
+ #print $cmd.'
';
+ #flush();
+ #flush_exec($cmd,100);
+
+ #print "Please ignore warnings about \"unable to write 'random state\'
";
+
+ // This method works but still errors in logs
+ // exec(DH . "-rand '$config[random]' -out '$config[private_dir]/dhparam1024.pem' 1024");
+ // exec(DH . " -out '$config[private_dir]/dhparam1024.pem' 1024");
+
+ print '
Creating 2048 bit Diffie-Hellman parameters used by OpenVPN.
';
+ print "Saving to $store_dir/dhparam2048.pem.
";
+ $cmd = "openssl dhparam -rand '$configRandom' -out '$configPrivate_dir/dhparam2048.pem' 2048";
+ print $cmd.'
';
+ flush();
+ flush_exec($cmd, 200);
+
+ print "Please ignore warnings about \"unable to write 'random state'
";
+
+ #
+ # Create a TLS auth key for OpenVPN if openvpn is installed
+ #
+ $command = 'which openvpn';
+ $command = escapeshellcmd($command);
+
+ if (system($command) == '/usr/sbin/openvpn') {
+ print '
Creating a TLS authentication key used by OpenVPN.
';
+ print "Saving to $store_dir/takey.pem.
";
+ $cmd = "openvpn --genkey --secret '$configPrivate_dir/takey.pem'";
+ print $cmd.'
';
+ flush();
+ flush_exec($cmd);
+ } else {
+ echo "openvpn is required to generate a takey.pem
";
+ echo "You can create one later like this:
";
+ echo "openvpn --genkey --secret". $configPrivate_dir . "/takey.pem
";
+ }
-
- #
- # Step aside and let the users in (create index.php files).
- #
- if (! unlink('index.php')) print "Can't unlink index.php";
- if (! unlink('setup.php')) print "Can't unlink setup.php";;
- if (! unlink('ca/index.php')) print "Can't unlink ca/index.php";
- if (! symlink('main.php','index.php')) print "Can't symlink main.php";
- if (! symlink('main.php','ca/index.php')) print "Can't symlink ca/main.php";
+
+ #
+ # Step aside and let the users in (create index.php files).
+ #
+ if (! unlink('index.php')) {
+ print "Can't unlink index.php";
+ }
+ if (! unlink('setup.php')) {
+ print "Can't unlink setup.php";
+ };
+ if (! unlink('ca/index.php')) {
+ print "Can't unlink ca/index.php";
+ }
+ if (! symlink('main.php', 'index.php')) {
+ print "Can't symlink main.php";
+ }
+ if (! symlink('main.php', 'ca/index.php')) {
+ print "Can't symlink ca/main.php";
+ }
- ?>
-
-
+