/** * Comprueba si la clave temporal es válida * * @param string $pass clave a comprobar * @return bool */ public static function checkTempMasterPass($pass) { $passTime = ConfigDB::getValue('tempmaster_passtime'); $passMaxTime = ConfigDB::getValue('tempmaster_maxtime'); $attempts = ConfigDB::getValue('tempmaster_attempts'); // Comprobar si el tiempo de validez se ha superado if ($passTime !== false && time() - $passTime > $passMaxTime || $attempts >= 5) { ConfigDB::setCacheConfigValue('tempmaster_pass', ''); ConfigDB::setCacheConfigValue('tempmaster_passiv', ''); ConfigDB::setCacheConfigValue('tempmaster_passhash', ''); ConfigDB::writeConfig(); return false; } Crypt::checkHashPass($pass, ConfigDB::getValue('tempmaster_passhash')); $isValid = Crypt::checkHashPass($pass, ConfigDB::getValue('tempmaster_passhash')); if (!$isValid) { ConfigDB::setValue('tempmaster_attempts', $attempts + 1, false); } return $isValid; }
/** * Actualizar la clave maestra del usuario en la BBDD. * * @param string $masterPwd con la clave maestra * @return bool */ public function updateUserMPass($masterPwd) { $configHashMPass = ConfigDB::getValue('masterPwd'); if ($configHashMPass === false) { return false; } if (is_null($configHashMPass)) { $configHashMPass = Crypt::mkHashPassword($masterPwd); ConfigDB::setValue('masterPwd', $configHashMPass); } if (Crypt::checkHashPass($masterPwd, $configHashMPass, true)) { $cryptMPass = Crypt::mkCustomMPassEncrypt(self::getCypherPass(), $masterPwd); if (!$cryptMPass) { return false; } } else { return false; } $query = 'UPDATE usrData SET ' . 'user_mPass = :mPass,' . 'user_mIV = :mIV,' . 'user_lastUpdateMPass = UNIX_TIMESTAMP() ' . 'WHERE user_id = :id LIMIT 1'; $data['mPass'] = $cryptMPass[0]; $data['mIV'] = $cryptMPass[1]; $data['id'] = $this->_userId; return DB::getQuery($query, __FUNCTION__, $data); }
/** * Comprobar el hash de una clave. * * @param string $pwd con la clave a comprobar * @param string $checkedHash con el hash a comprobar * @param bool $isMPass si es la clave maestra * @return bool */ public static function checkHashPass($pwd, $checkedHash, $isMPass = false) { // Obtenemos el salt de la clave $salt = substr($checkedHash, 0, 72); // Obtenemos el hash SHA256 $validHash = substr($checkedHash, 72); // Re-hash de la clave a comprobar $testHash = crypt($pwd, $salt); // Comprobar si el hash está en formato anterior a 12002 if ($isMPass && strlen($checkedHash) === 128) { $check = hash("sha256", substr($checkedHash, 0, 64) . $pwd) == substr($checkedHash, 64, 64); if ($check) { ConfigDB::setValue('masterPwd', self::mkHashPassword($pwd)); Log::writeNewLog(_('Aviso'), _('Se ha regenerado el HASH de clave maestra. No es necesaria ninguna acción.')); } return $check; } // Si los hashes son idénticos, la clave es válida return $testHash == $validHash; }
/** * Iniciar instalación. * * @return array resultado del proceso */ public static function install() { $error = array(); if (!self::$_username) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar nombre de usuario admin'), 'hint' => _('Usuario admin para acceso a la aplicación')); } elseif (!self::$_password) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar la clave de admin'), 'hint' => _('Clave del usuario admin de la aplicación')); } elseif (!self::$_masterPassword) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar la clave maestra'), 'hint' => _('Clave maestra para encriptar las claves')); } elseif (strlen(self::$_masterPassword) < 11) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Clave maestra muy corta'), 'hint' => _('La longitud de la clave maestra ha de ser mayor de 11 caracteres')); } elseif (!self::$_dbuser) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar el usuario de la BBDD'), 'hint' => _('Usuario con permisos de administrador de la Base de Datos')); } elseif (!self::$_dbpass) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar la clave de la BBDD'), 'hint' => _('Clave del usuario administrador de la Base de Datos')); } elseif (!self::$_dbname) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar el nombre de la BBDD'), 'hint' => _('Nombre para la BBDD de la aplicación pej. syspass')); } elseif (substr_count(self::$_dbname, '.') >= 1) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('El nombre de la BBDD no puede contener "."'), 'hint' => _('Elimine los puntos del nombre de la Base de Datos')); } elseif (!self::$_dbhost) { $error[] = array('type' => SPException::SP_CRITICAL, 'description' => _('Indicar el servidor de la BBDD'), 'hint' => _('Servidor donde se instalará la Base de Datos')); } if (count($error) === 0) { //no errors, good // Generate a random salt that is used to salt the local user passwords Config::setValue('passwordsalt', Util::generate_random_bytes(30)); Config::setValue('version', implode(Util::getVersion(true))); if (preg_match('/(.*):(\\d{1,5})/', self::$_dbhost, $match)) { self::setDbhost($match[1]); $dbport = $match[2]; } else { $dbport = 3306; } // Save DB connection info Config::setValue('dbhost', self::$_dbhost); Config::setValue('dbname', self::$_dbname); // Set some basic configuration options Config::setDefaultValues(); try { self::checkDatabaseAdmin(self::$_dbhost, self::$_dbuser, self::$_dbpass, $dbport); self::setupMySQLDatabase(); self::createAdminAccount(); } catch (SPException $e) { $error[] = array('type' => $e->getType(), 'description' => $e->getMessage(), 'hint' => $e->getHint()); return $error; } ConfigDB::setValue('version', implode(Util::getVersion(true))); Config::setValue('installed', 1); } return $error; }