Tweaks for key generation:
authorMax Semenik <maxsem@users.mediawiki.org>
Tue, 29 Mar 2011 19:00:23 +0000 (19:00 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Tue, 29 Mar 2011 19:00:23 +0000 (19:00 +0000)
* Don't show that scary message for every key, just once with list of them all
* If we're on Windows, we will probably be unable to open /dev/urandom, right?
* Tweaked the meaning of warning message, previously it sounded a bit like "we couldn't generate the key at all"

includes/installer/Installer.i18n.php
includes/installer/Installer.php
includes/installer/WebInstallerPage.php

index 290be8e..6864b73 100644 (file)
@@ -490,10 +490,8 @@ Skipping creation.",
        'config-install-interwiki-exists' => "'''Warning''': The interwiki table seems to already have entries.
 Skipping default list.",
        'config-install-stats'            => 'Initializing statistics',
-       'config-install-secretkey'        => 'Generating secret key',
-       'config-insecure-secret'          => "'''Warning:''' Unable to create a secure <code>$1</code>.
-Consider changing it manually.",
-       'config-install-upgradekey'        => 'Generating default upgrade key',
+       'config-install-keys'             => 'Generating secret keys',
+       'config-insecure-keys'            => "'''Warning:''' {{PLURAL:$2|A secure key|Secure keys}} $1 generated during installation are not completely safe. Consider changing {{PLURAL:$2|it|them}} manually.",
        'config-install-sysop'            => 'Creating administrator user account',
        'config-install-subscribe-fail'   => 'Unable to subscribe to mediawiki-announce',
        'config-install-mainpage'         => 'Creating main page with default content',
index 5f982d7..b25a601 100644 (file)
@@ -1228,8 +1228,7 @@ abstract class Installer {
                        array( 'name' => 'tables',     'callback' => array( $installer, 'createTables' ) ),
                        array( 'name' => 'interwiki',  'callback' => array( $installer, 'populateInterwikiTable' ) ),
                        array( 'name' => 'stats',      'callback' => array( $this, 'populateSiteStats' ) ),
-                       array( 'name' => 'secretkey',  'callback' => array( $this, 'generateSecretKey' ) ),
-                       array( 'name' => 'upgradekey', 'callback' => array( $this, 'generateUpgradeKey' ) ),
+                       array( 'name' => 'keys',       'callback' => array( $this, 'generateKeys' ) ),
                        array( 'name' => 'sysop',      'callback' => array( $this, 'createSysop' ) ),
                        array( 'name' => 'mainpage',   'callback' => array( $this, 'createMainpage' ) ),
                );
@@ -1309,58 +1308,54 @@ abstract class Installer {
         *
         * @return Status
         */
-       public function generateSecretKey() {
-               return $this->generateSecret( 'wgSecretKey' );
+       public function generateKeys() {
+               $keys = array( 'wgSecretKey' => 64 );
+               if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
+                       $keys['wgUpgradeKey'] = 16;
+               }
+               return $this->doGenerateKeys( $keys );
        }
 
        /**
-        * Generate a secret value for a variable using either
-        * /dev/urandom or mt_rand() Produce a warning in the later case.
+        * Generate a secret value for variables using either
+        * /dev/urandom or mt_rand(). Produce a warning in the later case.
         *
+        * @param $keys Array
         * @return Status
         */
-       protected function generateSecret( $secretName, $length = 64 ) {
-               if ( wfIsWindows() ) {
-                       $file = null;
-               } else {
-                       wfSuppressWarnings();
-                       $file = fopen( "/dev/urandom", "r" );
-                       wfRestoreWarnings();
-               }
-
+       protected function doGenerateKeys( $keys ) {
                $status = Status::newGood();
 
-               if ( $file ) {
-                       $secretKey = bin2hex( fread( $file, $length / 2 ) );
-                       fclose( $file );
-               } else {
-                       $secretKey = '';
+               wfSuppressWarnings();
+               $file = fopen( "/dev/urandom", "r" );
+               wfRestoreWarnings();
+
+               foreach ( $keys as $name => $length ) {
+                       if ( $file ) {
+                                       $secretKey = bin2hex( fread( $file, $length / 2 ) );
+                       } else {
+                               $secretKey = '';
 
-                       for ( $i = 0; $i < $length / 8; $i++ ) {
-                               $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
+                               for ( $i = 0; $i < $length / 8; $i++ ) {
+                                       $secretKey .= dechex( mt_rand( 0, 0x7fffffff ) );
+                               }
                        }
 
-                       $status->warning( 'config-insecure-secret', '$' . $secretName );
+                       $this->setVar( $name, $secretKey );
                }
 
-               $this->setVar( $secretName, $secretKey );
+               if ( $file ) {
+                       fclose( $file );
+               } else {
+                       $names = array_keys ( $keys );
+                       $names = preg_replace( '/^(.*)$/', '\$$1', $names );
+                       global $wgLang;
+                       $status->warning( 'config-insecure-keys', $wgLang->listToText( $names ), count( $names ) );
+               }
 
                return $status;
        }
 
-       /**
-        * Generate a default $wgUpgradeKey. Will warn if we had to use
-        * mt_rand() instead of /dev/urandom
-        *
-        * @return Status
-        */
-       public function generateUpgradeKey() {
-               if ( strval( $this->getVar( 'wgUpgradeKey' ) ) === '' ) {
-                       return $this->generateSecret( 'wgUpgradeKey', 16 );
-               }
-               return Status::newGood();
-       }
-
        /**
         * Create the first user account, grant it sysop and bureaucrat rights
         *
index 8fa9c0b..38b12d5 100644 (file)
@@ -478,8 +478,7 @@ class WebInstaller_Upgrade extends WebInstallerPage {
                                // If they're going to possibly regenerate LocalSettings, we
                                // need to create the upgrade/secret keys. Bug 26481
                                if( !$this->getVar( '_ExistingDBSettings' ) ) {
-                                       $this->parent->generateSecretKey();
-                                       $this->parent->generateUpgradeKey();
+                                       $this->parent->generateKeys();
                                }
                                $this->setVar( '_UpgradeDone', true );
                                $this->showDoneMessage();