[ Index ]

PHP Cross Reference of Joomla 3.3.0

title

Body

[close]

/bin/ -> keychain.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  /**
   4   * @package    Joomla.Platform
   5   *
   6   * @copyright  Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  define('_JEXEC', 1);
  11  define('JPATH_BASE', dirname(__FILE__));
  12  
  13  // Load the Joomla! Platform
  14  require_once realpath('../libraries/import.php');
  15  
  16  /**
  17   * Keychain Manager
  18   *
  19   * @package  Joomla.Platform
  20   * @since    12.3
  21   */
  22  class KeychainManager extends JApplicationCli
  23  {
  24      /**
  25       * @var    boolean  A flag if the keychain has been updated to trigger saving the keychain
  26       * @since  12.3
  27       */
  28      protected $updated = false;
  29  
  30      /**
  31       * @var    JKeychain  The keychain object being manipulated.
  32       * @since  12.3
  33       */
  34      protected $keychain = null;
  35  
  36      /**
  37       * Execute the application
  38       *
  39       * @return  void
  40       *
  41       * @since   12.3
  42       */
  43  	public function execute( )
  44      {
  45          if (!count($this->input->args))
  46          {
  47              // Check if they passed --help in otherwise display short usage summary
  48              if ($this->input->get('help', false) === false)
  49              {
  50                  $this->out("usage: {$this->input->executable} [options] [command] [<args>]");
  51                  exit(1);
  52              }
  53              else
  54              {
  55                  $this->displayHelp();
  56                  exit(0);
  57              }
  58          }
  59  
  60          // For all tasks but help and init we use the keychain
  61          if (!in_array($this->input->args[0], array('help', 'init')))
  62          {
  63              $this->loadKeychain();
  64          }
  65  
  66          switch ($this->input->args[0])
  67          {
  68              case 'init':
  69                  $this->initPassphraseFile();
  70                  break;
  71              case 'list':
  72                  $this->listEntries();
  73                  break;
  74              case 'create':
  75                  $this->create();
  76                  break;
  77              case 'change':
  78                  $this->change();
  79              case 'delete':
  80                  $this->delete();
  81                  break;
  82              case 'read':
  83                  $this->read();
  84                  break;
  85              case 'help':
  86                  $this->displayHelp();
  87                  break;
  88              default:
  89                  $this->out('Invalid command.');
  90                  break;
  91          }
  92  
  93          if ($this->updated)
  94          {
  95              $this->saveKeychain();
  96          }
  97          exit(0);
  98      }
  99  
 100      /**
 101       * Load the keychain from a file.
 102       *
 103       * @return  void
 104       *
 105       * @since   12.3
 106       */
 107  	protected function loadKeychain()
 108      {
 109          $keychain = $this->input->get('keychain', '', 'raw');
 110          $publicKeyFile = $this->input->get('public-key', '', 'raw');
 111          $passphraseFile = $this->input->get('passphrase', '', 'raw');
 112  
 113          $this->keychain = new JKeychain;
 114  
 115          if (file_exists($keychain))
 116          {
 117              if (file_exists($publicKeyFile))
 118              {
 119                  $this->keychain->loadKeychain($keychain, $passphraseFile, $publicKeyFile);
 120              }
 121              else
 122              {
 123                  $this->out('Public key not specified or missing!');
 124                  exit(1);
 125              }
 126          }
 127      }
 128  
 129      /**
 130       * Save this keychain to a file.
 131       *
 132       * @return  void
 133       *
 134       * @since   12.3
 135       */
 136  	protected function saveKeychain()
 137      {
 138          $keychain = $this->input->get('keychain', '', 'raw');
 139          $publicKeyFile = $this->input->get('public-key', '', 'raw');
 140          $passphraseFile = $this->input->get('passphrase', '', 'raw');
 141  
 142          if (!file_exists($publicKeyFile))
 143          {
 144              $this->out("Public key file specified doesn't exist: $publicKeyFile");
 145              exit(1);
 146          }
 147  
 148          $this->keychain->saveKeychain($keychain, $passphraseFile, $publicKeyFile);
 149      }
 150  
 151      /**
 152       * Initialise a new passphrase file.
 153       *
 154       * @return  void
 155       *
 156       * @since   12.3
 157       */
 158  	protected function initPassphraseFile()
 159      {
 160          $keychain = new JKeychain;
 161  
 162          $passphraseFile = $this->input->get('passphrase', '', 'raw');
 163          $privateKeyFile = $this->input->get('private-key', '', 'raw');
 164  
 165          if (!strlen($passphraseFile))
 166          {
 167              $this->out('A passphrase file must be specified with --passphrase');
 168              exit(1);
 169          }
 170  
 171          if (!file_exists($privateKeyFile))
 172          {
 173              $this->out("protected key file specified doesn't exist: $privateKeyFile");
 174              exit(1);
 175          }
 176  
 177          $this->out('Please enter the new passphrase:');
 178          $passphrase = $this->in();
 179  
 180          $this->out('Please enter the passphrase for the protected key:');
 181          $privateKeyPassphrase = $this->in();
 182  
 183          $keychain->createPassphraseFile($passphrase, $passphraseFile, $privateKeyFile, $privateKeyPassphrase);
 184      }
 185  
 186      /**
 187       * Create a new entry
 188       *
 189       * @return  void
 190       *
 191       * @since   12.3
 192       */
 193  	protected function create()
 194      {
 195          if (count($this->input->args) != 3)
 196          {
 197              $this->out("usage: {$this->input->executable} [options] create entry_name entry_value");
 198              exit(1);
 199          }
 200  
 201          if ($this->keychain->exists($this->input->args[1]))
 202          {
 203              $this->out('error: entry already exists. To change this entry, use "change"');
 204              exit(1);
 205          }
 206          $this->change();
 207      }
 208  
 209      /**
 210       * Change an existing entry to a new value or create an entry if missing.
 211       *
 212       * @return  void
 213       *
 214       * @since   12.3
 215       */
 216  	protected function change()
 217      {
 218          if (count($this->input->args) != 3)
 219          {
 220              $this->out("usage: {$this->input->executable} [options] change entry_name entry_value");
 221              exit(1);
 222          }
 223          $this->updated = true;
 224          $this->keychain->setValue($this->input->args[1], $this->input->args[2]);
 225      }
 226  
 227      /**
 228       * Read an entry from the keychain
 229       *
 230       * @return  void
 231       *
 232       * @since   12.3
 233       */
 234  	protected function read()
 235      {
 236          if (count($this->input->args) != 2)
 237          {
 238              $this->out("usage: {$this->input->executable} [options] read entry_name");
 239              exit(1);
 240          }
 241  
 242          $key = $this->input->args[1];
 243          $this->out($key . ': ' . $this->dumpVar($this->keychain->get($key)));
 244      }
 245  
 246      /**
 247       * Get the string from var_dump
 248       *
 249       * @param   mixed  $var  The variable you want to have dumped.
 250       *
 251       * @return  string  The result of var_dump
 252       *
 253       * @since   12.3
 254       */
 255  	private function dumpVar($var)
 256      {
 257          ob_start();
 258          var_dump($var);
 259          $result = trim(ob_get_contents());
 260          ob_end_clean();
 261  
 262          return $result;
 263      }
 264  
 265      /**
 266       * Delete an entry from the keychain
 267       *
 268       * @return  void
 269       *
 270       * @since   12.3
 271       */
 272  	protected function delete()
 273      {
 274          if (count($this->input->args) != 2)
 275          {
 276              $this->out("usage: {$this->input->executable} [options] delete entry_name");
 277              exit(1);
 278          }
 279  
 280          $this->updated = true;
 281          $this->keychain->deleteValue($this->input->args[1], null);
 282      }
 283  
 284      /**
 285       * List entries in the keychain
 286       *
 287       * @return  void
 288       *
 289       * @since   12.3
 290       */
 291  	protected function listEntries()
 292      {
 293          foreach ($this->keychain->toArray() as $key => $value)
 294          {
 295              $line = $key;
 296  
 297              if ($this->input->get('print-values'))
 298              {
 299                  $line .= ': ' . $this->dumpVar($value);
 300              }
 301              $this->out($line);
 302          }
 303      }
 304  
 305      /**
 306       * Display the help information
 307       *
 308       * @return  void
 309       *
 310       * @since   12.3
 311       */
 312  	protected function displayHelp()
 313      {
 314  /*
 315  COMMANDS
 316  
 317   - list
 318   - create entry_name entry_value
 319   - change entry_name entry_value
 320   - delete entry_name
 321   - read   entry_name
 322  */
 323  
 324          $help = <<<HELP
 325  Keychain Management Utility
 326  
 327  usage: {$this->input->executable} [--keychain=/path/to/keychain]
 328      [--passphrase=/path/to/passphrase.dat] [--public-key=/path/to/public.pem]
 329      [command] [<args>]
 330  
 331  OPTIONS
 332  
 333    --keychain=/path/to/keychain
 334      Path to a keychain file to manipulate.
 335  
 336    --passphrase=/path/to/passphrase.dat
 337      Path to a passphrase file containing the encryption/decryption key.
 338  
 339    --public-key=/path/to/public.pem
 340      Path to a public key file to decrypt the passphrase file.
 341  
 342  
 343  COMMANDS
 344  
 345    list:
 346      Usage: list [--print-values]
 347      Lists all entries in the keychain. Optionally pass --print-values to print the values as well.
 348  
 349    create:
 350      Usage: create entry_name entry_value
 351      Creates a new entry in the keychain called "entry_name" with the plaintext value "entry_value".
 352      NOTE: This is an alias for change.
 353  
 354    change:
 355      Usage: change entry_name entry_value
 356      Updates the keychain entry called "entry_name" with the value "entry_value".
 357  
 358    delete:
 359      Usage: delete entry_name
 360      Removes an entry called "entry_name" from the keychain.
 361  
 362    read:
 363      Usage: read entry_name
 364      Outputs the plaintext value of "entry_name" from the keychain.
 365  
 366    init:
 367      Usage: init
 368      Creates a new passphrase file and prompts for a new passphrase.
 369  
 370  HELP;
 371          $this->out($help);
 372      }
 373  }
 374  
 375  try
 376  {
 377      JApplicationCli::getInstance('KeychainManager')->execute();
 378  }
 379  catch (Exception $e)
 380  {
 381      echo $e->getMessage() . "\n";
 382      exit(1);
 383  }


Generated: Fri May 2 13:54:24 2014 Cross-referenced by PHPXref 0.7.1