Source for file keychain.php

Documentation is available at keychain.php

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

Documentation generated on Tue, 19 Nov 2013 15:06:21 +0100 by phpDocumentor 1.4.3