You are here

function security_review_drush_hash_setup in Security Review 6

Same name and namespace in other branches
  1. 7 security_review.drush.inc \security_review_drush_hash_setup()
1 string reference to 'security_review_drush_hash_setup'
security_review_drush_command in ./security_review.drush.inc
Implementation of hook_drush_command().

File

./security_review.drush.inc, line 143
Drush commands for Security Review module.

Code

function security_review_drush_hash_setup() {
  $args = func_get_args();
  if (empty($args)) {
    drush_set_error('SECURITY_REVIEW_ERROR', dt('Dictionary filename required'));
    return FALSE;
  }
  if (file_exists($args[0])) {
    $ret = array();

    // Create the rainbow table.
    if (!db_table_exists('security_review_rainbow')) {
      $schema = array(
        'fields' => array(
          'hash_id' => array(
            'type' => 'serial',
          ),
          'hash_word' => array(
            'type' => 'varchar',
            'length' => 20,
          ),
          'hash_hash' => array(
            'type' => 'varchar',
            'length' => 32,
          ),
        ),
        'primary key' => array(
          'hash_id',
        ),
        'indexes' => array(
          'hash_hash' => array(
            'hash_hash',
          ),
        ),
      );
      db_create_table($ret, 'security_review_rainbow', $schema);
    }

    // Put an index on users.pass.
    db_drop_index($ret, 'users', 'pass');

    // Drop in case this has already run.
    db_add_index($ret, 'users', 'pass', array(
      'pass',
    ));
    $handle = fopen($args[0], 'r');
    if ($handle) {
      $count = 0;
      while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        $word = trim($buffer);
        $hash = md5($word);
        $sql = "INSERT INTO {security_review_rainbow} (hash_word, hash_hash) VALUES ('%s', '%s')";
        db_query($sql, $word, $hash);
        $count++;
      }
      fclose($handle);
      drush_log(dt('!count records inserted into rainbow table', array(
        '!count' => $count,
      )), 'success');
    }
  }
  else {
    drush_die('File not found');
  }
}