2012-06-28 17:24:12 +02:00
|
|
|
<?php
|
2013-01-04 16:48:46 +01:00
|
|
|
|
2012-06-28 17:24:12 +02:00
|
|
|
App::uses('AppModel', 'Model');
|
2013-01-04 16:48:46 +01:00
|
|
|
|
2012-06-28 17:24:12 +02:00
|
|
|
class Log extends AppModel {
|
2012-09-24 16:02:01 +02:00
|
|
|
|
2012-06-28 17:24:12 +02:00
|
|
|
public $validate = array(
|
|
|
|
'action' => array(
|
|
|
|
'rule' => array('inList', array(
|
|
|
|
'login',
|
2015-11-08 22:35:46 +01:00
|
|
|
'login_fail',
|
2012-06-28 17:24:12 +02:00
|
|
|
'logout',
|
2012-09-24 16:02:01 +02:00
|
|
|
'add',
|
|
|
|
'edit',
|
2012-12-17 15:51:30 +01:00
|
|
|
'change_pw',
|
2012-09-24 16:02:01 +02:00
|
|
|
'delete',
|
2014-01-28 16:27:58 +01:00
|
|
|
'publish',
|
|
|
|
'accept',
|
2014-01-29 15:52:09 +01:00
|
|
|
'discard',
|
|
|
|
'pull',
|
2014-02-05 14:30:59 +01:00
|
|
|
'push',
|
2014-12-12 10:42:12 +01:00
|
|
|
'blacklisted',
|
2015-04-20 11:46:55 +02:00
|
|
|
'admin_email',
|
|
|
|
'tag',
|
2015-04-26 11:40:54 +02:00
|
|
|
'publish alert',
|
|
|
|
'warning',
|
2015-06-10 18:07:48 +02:00
|
|
|
'error',
|
2015-06-09 09:55:22 +02:00
|
|
|
'email',
|
2015-07-12 21:45:11 +02:00
|
|
|
'serverSettingsEdit',
|
2015-08-05 00:11:16 +02:00
|
|
|
'remove_dead_workers',
|
2015-08-18 09:44:59 +02:00
|
|
|
'upload_sample',
|
2015-09-02 01:34:52 +02:00
|
|
|
'update_database',
|
2015-10-12 09:41:20 +02:00
|
|
|
'upgrade_24',
|
2015-10-09 15:59:25 +02:00
|
|
|
'version_warning',
|
2015-11-08 22:35:46 +01:00
|
|
|
'auth',
|
2015-11-16 00:22:58 +01:00
|
|
|
'auth_fail',
|
2015-11-24 03:27:14 +01:00
|
|
|
'reset_auth_key',
|
|
|
|
'update',
|
|
|
|
'enable',
|
2016-02-12 05:47:06 +01:00
|
|
|
'disable',
|
|
|
|
'accept_delegation',
|
2016-02-12 05:56:32 +01:00
|
|
|
'request_delegation',
|
2016-05-10 10:31:56 +02:00
|
|
|
'merge',
|
2016-08-10 12:06:36 +02:00
|
|
|
'undelete',
|
2016-08-23 00:24:54 +02:00
|
|
|
'file_upload',
|
2017-01-30 09:16:43 +01:00
|
|
|
'export',
|
|
|
|
'pruneUpdateLogs'
|
2012-09-24 16:02:01 +02:00
|
|
|
)),
|
2012-06-28 17:24:12 +02:00
|
|
|
'message' => 'Options : ...'
|
|
|
|
)
|
|
|
|
);
|
2012-09-24 16:02:01 +02:00
|
|
|
|
|
|
|
public $actionDefinitions = array(
|
2012-06-28 17:24:12 +02:00
|
|
|
'login' => array('desc' => 'Login action', 'formdesc' => "Login action"),
|
|
|
|
'logout' => array('desc' => 'Logout action', 'formdesc' => "Logout action"),
|
|
|
|
'add' => array('desc' => 'Add action', 'formdesc' => "Add action"),
|
|
|
|
'edit' => array('desc' => 'Edit action', 'formdesc' => "Edit action"),
|
2012-12-17 15:51:30 +01:00
|
|
|
'change_pw' => array('desc' => 'Change_pw action', 'formdesc' => "Change_pw action"),
|
2012-06-28 17:24:12 +02:00
|
|
|
'delete' => array('desc' => 'Delete action', 'formdesc' => "Delete action"),
|
|
|
|
'publish' => array('desc' => "Publish action", 'formdesc' => "Publish action")
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2016-05-15 19:50:12 +02:00
|
|
|
public $logMeta = array(
|
|
|
|
'email' => array('values' => array('email'), 'name' => 'Emails'),
|
|
|
|
'auth_issues' => array('values' => array('login_fail', 'auth_fail'), 'name' => 'Authentication issues')
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2016-05-15 19:50:12 +02:00
|
|
|
public $logMetaAdmin = array(
|
|
|
|
'update' => array('values' => array('update_database'), 'name' => 'MISP Update results'),
|
|
|
|
'settings' => array('values' => array('serverSettingsEdit', 'remove_dead_workers'), 'name' => 'Setting changes'),
|
|
|
|
'errors' => array('values' => array('warning', 'errors', 'version_warning'), 'name' => 'Warnings and errors'),
|
|
|
|
'email' => array('values' => array('admin_email'))
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2016-12-29 16:06:11 +01:00
|
|
|
public function beforeValidete() {
|
|
|
|
parent::beforeValidate();
|
|
|
|
if (!isset($this->data['Log']['org']) || empty($this->data['Log']['org'])) {
|
|
|
|
$this->data['Log']['org'] = 'SYSTEM';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 22:35:46 +01:00
|
|
|
public function beforeSave($options = array()) {
|
|
|
|
if (Configure::read('MISP.log_client_ip') && isset($_SERVER['REMOTE_ADDR'])) $this->data['Log']['ip'] = $_SERVER['REMOTE_ADDR'];
|
2015-11-27 14:10:25 +01:00
|
|
|
$setEmpty = array('title' => '', 'model' => '', 'model_id' => 0, 'action' => '', 'user_id' => 0, 'change' => '', 'email' => '', 'org' => '', 'description' => '');
|
|
|
|
foreach ($setEmpty as $field => $empty) {
|
2016-06-04 01:10:45 +02:00
|
|
|
if (!isset($this->data['Log'][$field]) || empty($this->data['Log'][$field])) $this->data['Log'][$field] = $empty;
|
2015-11-27 14:10:25 +01:00
|
|
|
}
|
2016-07-12 19:14:53 +02:00
|
|
|
if (!isset($this->data['Log']['created'])) $this->data['Log']['created'] = date('Y-m-d H:i:s');
|
2015-11-08 22:35:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2014-02-05 13:45:18 +01:00
|
|
|
public function returnDates($org = 'all') {
|
2016-08-18 06:30:05 +02:00
|
|
|
$dataSourceConfig = ConnectionManager::getDataSource('default')->config;
|
|
|
|
$dataSource = $dataSourceConfig['datasource'];
|
2014-02-05 13:45:18 +01:00
|
|
|
$conditions = array();
|
2015-11-28 23:49:52 +01:00
|
|
|
$this->Organisation = ClassRegistry::init('Organisation');
|
|
|
|
if ($org !== 'all') {
|
|
|
|
$org = $this->Organisation->find('first', array('fields' => array('name'), 'recursive' => -1, 'conditions' => array('UPPER(Organisation.name) LIKE' => strtoupper($org))));
|
|
|
|
if (empty($org)) return MethodNotAllowedException('Invalid organisation.');
|
2015-12-16 23:18:05 +01:00
|
|
|
$conditions['org'] = $org['Organisation']['name'];
|
2015-11-28 23:49:52 +01:00
|
|
|
}
|
2014-02-12 17:17:15 +01:00
|
|
|
$conditions['AND']['NOT'] = array('action' => array('login', 'logout', 'changepw'));
|
2016-08-18 06:30:05 +02:00
|
|
|
if ($dataSource == 'Database/Mysql') {
|
|
|
|
$validDates = $this->find('all', array(
|
|
|
|
'fields' => array('DISTINCT UNIX_TIMESTAMP(DATE(created)) AS Date', 'count(id) AS count'),
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'group' => array('Date'),
|
|
|
|
'order' => array('Date')
|
|
|
|
));
|
|
|
|
} else if ($dataSource == 'Database/Postgres') {
|
|
|
|
// manually generate the query for Postgres
|
|
|
|
// cakephp ORM would escape "DATE" datatype in CAST expression
|
|
|
|
$condnotinaction = "'" . implode("', '", $conditions['AND']['NOT']['action']) . "'";
|
|
|
|
if (!empty($conditions['org'])) $condOrg = ' AND org = "' . $conditions['org'] . '"';
|
|
|
|
else $condOrg = '';
|
|
|
|
$sql = 'SELECT DISTINCT EXTRACT(EPOCH FROM CAST(created AS DATE)) AS "Date",
|
|
|
|
COUNT(id) AS count
|
|
|
|
FROM logs
|
|
|
|
WHERE action NOT IN (' . $condnotinaction . ')
|
|
|
|
' . $condOrg . '
|
|
|
|
GROUP BY "Date" ORDER BY "Date"';
|
|
|
|
$validDates = $this->query($sql);
|
|
|
|
}
|
2014-02-05 13:45:18 +01:00
|
|
|
$data = array();
|
|
|
|
foreach ($validDates as $k => $date) {
|
|
|
|
$data[$date[0]['Date']] = intval($date[0]['count']);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2015-12-17 11:01:54 +01:00
|
|
|
public function createLogEntry($user = array('Organisation' => array('name' => 'SYSTEM'), 'email' => 'SYSTEM', 'id' => 0), $action, $model, $model_id = 0, $title = '', $change = '') {
|
2015-04-20 11:46:55 +02:00
|
|
|
$this->create();
|
|
|
|
$this->save(array(
|
|
|
|
'org' => $user['Organisation']['name'],
|
|
|
|
'email' =>$user['email'],
|
|
|
|
'user_id' => $user['id'],
|
|
|
|
'action' => $action,
|
|
|
|
'title' => $title,
|
|
|
|
'change' => $change,
|
|
|
|
'model' => $model,
|
|
|
|
'model_id' => $model_id,
|
|
|
|
));
|
|
|
|
}
|
2017-01-30 09:16:43 +01:00
|
|
|
|
|
|
|
// to combat a certain bug that causes the upgrade scripts to loop without being able to set the correct version
|
|
|
|
// this function remedies a fixed upgrade bug instance by eliminating the massive number of erroneous upgrade log entries
|
|
|
|
public function pruneUpdateLogs($jobId = false, $user) {
|
|
|
|
$max = $this->find('first', array('fields' => array('MAX(id) AS lastid')));
|
|
|
|
if (!empty($max)) {
|
|
|
|
$max = $max[0]['lastid'];
|
|
|
|
}
|
|
|
|
if ($jobId) {
|
|
|
|
$this->Job = ClassRegistry::init('Job');
|
|
|
|
$this->Job->id = $jobId;
|
|
|
|
if (!$this->Job->exists()) {
|
|
|
|
$jobId = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$iterations = ($max / 1000);
|
|
|
|
for ($i = 0; $i < $iterations; $i++) {
|
|
|
|
$this->deleteAll(array('action' => 'update_database', 'id >' => $i * 1000, 'id <' => ($i+1) * 1000));
|
|
|
|
if ($jobId) {
|
|
|
|
$this->Job->saveField('progress', $i * 100 / $iterations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->create();
|
|
|
|
$this->save(array(
|
|
|
|
'org' => $user['Organisation']['name'],
|
|
|
|
'email' =>$user['email'],
|
|
|
|
'user_id' => $user['id'],
|
|
|
|
'action' => 'pruneUpdateLogs',
|
|
|
|
'title' => 'Pruning updates',
|
|
|
|
'change' => 'Pruning completed in ' . $i . ' iteration(s).',
|
|
|
|
'model' => 'Log',
|
|
|
|
'model_id' => 0
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function pruneUpdateLogsRouter($user) {
|
|
|
|
if (Configure::read('MISP.background_jobs')) {
|
|
|
|
$job = ClassRegistry::init('Job');
|
|
|
|
$job->create();
|
|
|
|
$data = array(
|
|
|
|
'worker' => 'default',
|
|
|
|
'job_type' => 'prune_update_logs',
|
|
|
|
'job_input' => 'All update entries',
|
|
|
|
'status' => 0,
|
|
|
|
'retries' => 0,
|
|
|
|
'org_id' => $user['org_id'],
|
|
|
|
'org' => $user['Organisation']['name'],
|
|
|
|
'message' => 'Purging the heretic.',
|
|
|
|
);
|
|
|
|
$job->save($data);
|
|
|
|
$jobId = $job->id;
|
|
|
|
$process_id = CakeResque::enqueue(
|
|
|
|
'default',
|
|
|
|
'AdminShell',
|
|
|
|
array('prune_update_logs', $jobId, $user['id']),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$job->saveField('process_id', $process_id);
|
|
|
|
return $process_id;
|
|
|
|
} else {
|
|
|
|
$result = $this->pruneUpdateLogs(false, $user);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 10:09:55 +02:00
|
|
|
}
|