2011-10-16 22:49:14 +04:00
|
|
|
<?php
|
2018-02-27 00:34:13 +03:00
|
|
|
declare(strict_types=1);
|
2011-10-16 22:49:14 +04:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Johannes Schlichenmaier <johannes@schlichenmaier.info>
|
|
|
|
* @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Thomas Pulzer <t.pulzer@kniel.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2011-10-16 22:49:14 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2015-04-27 21:06:24 +03:00
|
|
|
use InterfaSys\LogNormalizer\Normalizer;
|
|
|
|
|
2017-11-12 17:28:04 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Support\CrashReport\IRegistry;
|
2016-01-11 14:25:28 +03:00
|
|
|
use OCP\Util;
|
2014-05-07 03:55:06 +04:00
|
|
|
|
2011-10-16 22:49:14 +04:00
|
|
|
/**
|
2012-03-31 01:15:48 +04:00
|
|
|
* logging utilities
|
2011-10-16 22:49:14 +04:00
|
|
|
*
|
2013-03-10 00:00:48 +04:00
|
|
|
* This is a stand in, this should be replaced by a Psr\Log\LoggerInterface
|
|
|
|
* compatible logger. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
|
|
|
|
* for the full interface specification.
|
|
|
|
*
|
|
|
|
* MonoLog is an example implementing this interface.
|
2011-10-16 22:49:14 +04:00
|
|
|
*/
|
|
|
|
|
2014-05-07 03:55:06 +04:00
|
|
|
class Log implements ILogger {
|
2014-05-12 15:32:03 +04:00
|
|
|
|
2015-04-30 13:43:58 +03:00
|
|
|
/** @var string */
|
2014-05-07 03:55:06 +04:00
|
|
|
private $logger;
|
2016-01-11 14:25:28 +03:00
|
|
|
|
2015-04-30 13:43:58 +03:00
|
|
|
/** @var SystemConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var boolean|null cache the result of the log condition check for the request */
|
|
|
|
private $logConditionSatisfied = null;
|
2016-01-11 14:25:28 +03:00
|
|
|
|
2015-04-27 21:06:24 +03:00
|
|
|
/** @var Normalizer */
|
|
|
|
private $normalizer;
|
2014-05-07 03:55:06 +04:00
|
|
|
|
2017-11-12 17:28:04 +03:00
|
|
|
/** @var IRegistry */
|
|
|
|
private $crashReporters;
|
|
|
|
|
2016-06-08 17:32:58 +03:00
|
|
|
protected $methodsWithSensitiveParameters = [
|
|
|
|
// Session/User
|
2017-04-05 00:19:39 +03:00
|
|
|
'completeLogin',
|
2016-06-08 17:32:58 +03:00
|
|
|
'login',
|
|
|
|
'checkPassword',
|
2017-09-01 11:31:11 +03:00
|
|
|
'checkPasswordNoLogging',
|
2016-07-08 15:13:16 +03:00
|
|
|
'loginWithPassword',
|
2016-06-08 17:32:58 +03:00
|
|
|
'updatePrivateKeyPassword',
|
|
|
|
'validateUserPass',
|
2017-10-27 00:13:37 +03:00
|
|
|
'loginWithToken',
|
|
|
|
'\{closure\}',
|
2016-06-08 17:32:58 +03:00
|
|
|
|
|
|
|
// TokenProvider
|
|
|
|
'getToken',
|
|
|
|
'isTokenPassword',
|
|
|
|
'getPassword',
|
|
|
|
'decryptPassword',
|
|
|
|
'logClientIn',
|
|
|
|
'generateToken',
|
|
|
|
'validateToken',
|
|
|
|
|
|
|
|
// TwoFactorAuth
|
|
|
|
'solveChallenge',
|
|
|
|
'verifyChallenge',
|
|
|
|
|
2017-09-01 11:31:11 +03:00
|
|
|
// ICrypto
|
2016-06-08 17:32:58 +03:00
|
|
|
'calculateHMAC',
|
|
|
|
'encrypt',
|
|
|
|
'decrypt',
|
2016-08-22 15:33:23 +03:00
|
|
|
|
2017-09-01 11:31:11 +03:00
|
|
|
// LoginController
|
2017-02-07 14:16:11 +03:00
|
|
|
'tryLogin',
|
|
|
|
'confirmPassword',
|
2017-09-01 11:31:11 +03:00
|
|
|
|
|
|
|
// LDAP
|
|
|
|
'bind',
|
|
|
|
'areCredentialsValid',
|
|
|
|
'invokeLDAPMethod',
|
2017-10-27 00:13:37 +03:00
|
|
|
|
|
|
|
// Encryption
|
|
|
|
'storeKeyPair',
|
|
|
|
'setupUser',
|
2016-06-08 17:32:58 +03:00
|
|
|
];
|
|
|
|
|
2014-05-07 03:55:06 +04:00
|
|
|
/**
|
|
|
|
* @param string $logger The logger that should be used
|
2015-04-30 13:43:58 +03:00
|
|
|
* @param SystemConfig $config the system config object
|
2017-11-12 17:28:04 +03:00
|
|
|
* @param Normalizer|null $normalizer
|
|
|
|
* @param IRegistry|null $registry
|
2014-05-07 03:55:06 +04:00
|
|
|
*/
|
2017-11-12 17:28:04 +03:00
|
|
|
public function __construct($logger = null, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) {
|
2015-04-30 13:43:58 +03:00
|
|
|
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
|
|
|
|
if($config === null) {
|
|
|
|
$config = \OC::$server->getSystemConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
|
2014-05-07 03:55:06 +04:00
|
|
|
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
|
|
|
|
if($logger === null) {
|
2016-07-22 12:44:19 +03:00
|
|
|
$logType = $this->config->getValue('log_type', 'file');
|
2017-01-05 14:47:27 +03:00
|
|
|
$this->logger = static::getLogClass($logType);
|
2018-02-27 00:34:13 +03:00
|
|
|
call_user_func([$this->logger, 'init']);
|
2014-05-07 03:55:06 +04:00
|
|
|
} else {
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
2015-04-27 21:06:24 +03:00
|
|
|
if ($normalizer === null) {
|
|
|
|
$this->normalizer = new Normalizer();
|
|
|
|
} else {
|
|
|
|
$this->normalizer = $normalizer;
|
|
|
|
}
|
2017-11-12 17:28:04 +03:00
|
|
|
$this->crashReporters = $registry;
|
2014-05-07 03:55:06 +04:00
|
|
|
}
|
|
|
|
|
2011-10-16 22:49:14 +04:00
|
|
|
/**
|
2013-03-10 00:00:48 +04:00
|
|
|
* System is unusable.
|
|
|
|
*
|
2011-10-16 22:49:14 +04:00
|
|
|
* @param string $message
|
2013-03-10 00:00:48 +04:00
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2011-10-16 22:49:14 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function emergency(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::FATAL, $message, $context);
|
2011-10-16 22:49:14 +04:00
|
|
|
}
|
2013-01-14 23:30:39 +04:00
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
/**
|
|
|
|
* Action must be taken immediately.
|
|
|
|
*
|
|
|
|
* Example: Entire website down, database unavailable, etc. This should
|
|
|
|
* trigger the SMS alerts and wake you up.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function alert(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::ERROR, $message, $context);
|
2012-09-12 23:30:04 +04:00
|
|
|
}
|
2013-01-14 23:30:39 +04:00
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
/**
|
|
|
|
* Critical conditions.
|
|
|
|
*
|
|
|
|
* Example: Application component unavailable, unexpected exception.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function critical(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::ERROR, $message, $context);
|
2012-09-26 15:38:06 +04:00
|
|
|
}
|
2012-09-12 23:30:04 +04:00
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
/**
|
|
|
|
* Runtime errors that do not require immediate action but should typically
|
|
|
|
* be logged and monitored.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function error(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::ERROR, $message, $context);
|
2013-03-10 00:00:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exceptional occurrences that are not errors.
|
|
|
|
*
|
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things
|
|
|
|
* that are not necessarily wrong.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function warning(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::WARN, $message, $context);
|
2013-03-10 00:00:48 +04:00
|
|
|
}
|
2012-09-12 23:30:04 +04:00
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
/**
|
|
|
|
* Normal but significant events.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function notice(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::INFO, $message, $context);
|
2013-03-10 00:00:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interesting events.
|
|
|
|
*
|
|
|
|
* Example: User logs in, SQL logs.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function info(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::INFO, $message, $context);
|
2013-03-10 00:00:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detailed debug information.
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function debug(string $message, array $context = []) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$this->log(Util::DEBUG, $message, $context);
|
2013-03-10 00:00:48 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 22:10:45 +04:00
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
/**
|
|
|
|
* Logs with an arbitrary level.
|
|
|
|
*
|
|
|
|
* @param mixed $level
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
2016-01-11 14:25:28 +03:00
|
|
|
* @return void
|
2013-03-10 00:00:48 +04:00
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function log($level, string $message, array $context = []) {
|
2016-09-26 09:43:42 +03:00
|
|
|
$minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL);
|
2015-04-30 13:43:58 +03:00
|
|
|
$logCondition = $this->config->getValue('log.condition', []);
|
|
|
|
|
2015-04-27 21:06:24 +03:00
|
|
|
array_walk($context, [$this->normalizer, 'format']);
|
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
if (isset($context['app'])) {
|
|
|
|
$app = $context['app'];
|
2015-04-30 13:43:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check log condition based on the context of each log message
|
|
|
|
* once this is met -> change the required log level to debug
|
|
|
|
*/
|
|
|
|
if(!empty($logCondition)
|
|
|
|
&& isset($logCondition['apps'])
|
|
|
|
&& in_array($app, $logCondition['apps'], true)) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$minLevel = Util::DEBUG;
|
2015-04-30 13:43:58 +03:00
|
|
|
}
|
|
|
|
|
2013-03-10 00:00:48 +04:00
|
|
|
} else {
|
|
|
|
$app = 'no app in context';
|
|
|
|
}
|
2014-05-12 12:54:09 +04:00
|
|
|
// interpolate $message as defined in PSR-3
|
2018-02-27 00:34:13 +03:00
|
|
|
$replace = [];
|
2014-05-12 12:54:09 +04:00
|
|
|
foreach ($context as $key => $val) {
|
|
|
|
$replace['{' . $key . '}'] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interpolate replacement values into the message and return
|
|
|
|
$message = strtr($message, $replace);
|
|
|
|
|
2015-04-30 13:43:58 +03:00
|
|
|
/**
|
|
|
|
* check for a special log condition - this enables an increased log on
|
|
|
|
* a per request/user base
|
|
|
|
*/
|
|
|
|
if($this->logConditionSatisfied === null) {
|
|
|
|
// default to false to just process this once per request
|
|
|
|
$this->logConditionSatisfied = false;
|
|
|
|
if(!empty($logCondition)) {
|
|
|
|
|
|
|
|
// check for secret token in the request
|
|
|
|
if(isset($logCondition['shared_secret'])) {
|
|
|
|
$request = \OC::$server->getRequest();
|
|
|
|
|
|
|
|
// if token is found in the request change set the log condition to satisfied
|
2016-10-07 17:23:05 +03:00
|
|
|
if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret', ''))) {
|
2015-04-30 13:43:58 +03:00
|
|
|
$this->logConditionSatisfied = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for user
|
|
|
|
if(isset($logCondition['users'])) {
|
|
|
|
$user = \OC::$server->getUserSession()->getUser();
|
|
|
|
|
|
|
|
// if the user matches set the log condition to satisfied
|
|
|
|
if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
|
|
|
|
$this->logConditionSatisfied = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 13:06:52 +03:00
|
|
|
|
2015-04-30 13:43:58 +03:00
|
|
|
// if log condition is satisfied change the required log level to DEBUG
|
|
|
|
if($this->logConditionSatisfied) {
|
2016-01-11 14:25:28 +03:00
|
|
|
$minLevel = Util::DEBUG;
|
2015-04-30 13:43:58 +03:00
|
|
|
}
|
2015-04-30 13:06:52 +03:00
|
|
|
|
|
|
|
if ($level >= $minLevel) {
|
|
|
|
$logger = $this->logger;
|
2018-02-27 00:34:13 +03:00
|
|
|
call_user_func([$logger, 'write'], $app, $message, $level);
|
2015-04-30 13:06:52 +03:00
|
|
|
}
|
2012-09-12 23:30:04 +04:00
|
|
|
}
|
2015-09-24 18:01:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs an exception very detailed
|
|
|
|
*
|
2017-06-29 12:43:32 +03:00
|
|
|
* @param \Exception|\Throwable $exception
|
2015-09-24 18:01:31 +03:00
|
|
|
* @param array $context
|
|
|
|
* @return void
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public function logException($exception, array $context = []) {
|
2017-06-29 12:43:32 +03:00
|
|
|
$level = Util::ERROR;
|
|
|
|
if (isset($context['level'])) {
|
|
|
|
$level = $context['level'];
|
|
|
|
unset($context['level']);
|
|
|
|
}
|
2018-02-27 00:34:13 +03:00
|
|
|
$data = [
|
2015-09-24 18:01:31 +03:00
|
|
|
'Exception' => get_class($exception),
|
|
|
|
'Message' => $exception->getMessage(),
|
|
|
|
'Code' => $exception->getCode(),
|
|
|
|
'Trace' => $exception->getTraceAsString(),
|
|
|
|
'File' => $exception->getFile(),
|
|
|
|
'Line' => $exception->getLine(),
|
2018-02-27 00:34:13 +03:00
|
|
|
];
|
2017-06-29 12:43:32 +03:00
|
|
|
$data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']);
|
2017-09-26 12:21:39 +03:00
|
|
|
if ($exception instanceof HintException) {
|
|
|
|
$data['Hint'] = $exception->getHint();
|
|
|
|
}
|
2016-01-15 15:13:27 +03:00
|
|
|
$msg = isset($context['message']) ? $context['message'] : 'Exception';
|
2017-06-29 12:43:32 +03:00
|
|
|
$msg .= ': ' . json_encode($data);
|
|
|
|
$this->log($level, $msg, $context);
|
2017-11-27 17:35:29 +03:00
|
|
|
$context['level'] = $level;
|
2017-11-12 17:28:04 +03:00
|
|
|
if (!is_null($this->crashReporters)) {
|
2017-11-14 16:28:04 +03:00
|
|
|
$this->crashReporters->delegateReport($exception, $context);
|
2017-11-12 17:28:04 +03:00
|
|
|
}
|
2015-09-24 18:01:31 +03:00
|
|
|
}
|
2017-01-05 14:47:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $logType
|
|
|
|
* @return string
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-02-27 00:34:13 +03:00
|
|
|
public static function getLogClass(string $logType): string {
|
2017-01-05 14:47:27 +03:00
|
|
|
switch (strtolower($logType)) {
|
2017-01-10 14:58:23 +03:00
|
|
|
case 'errorlog':
|
|
|
|
return \OC\Log\Errorlog::class;
|
|
|
|
case 'syslog':
|
|
|
|
return \OC\Log\Syslog::class;
|
|
|
|
case 'file':
|
|
|
|
return \OC\Log\File::class;
|
|
|
|
|
|
|
|
// Backwards compatibility for old and fallback for unknown log types
|
2017-01-05 14:47:27 +03:00
|
|
|
case 'owncloud':
|
|
|
|
case 'nextcloud':
|
2017-01-10 14:58:23 +03:00
|
|
|
default:
|
|
|
|
return \OC\Log\File::class;
|
2017-01-05 14:47:27 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-16 22:49:14 +04:00
|
|
|
}
|