piler/util/sign.php

170 lines
4.2 KiB
PHP
Raw Normal View History

2015-04-22 12:26:04 +02:00
<?php
define('COUNT', 'count');
define('HASH_VALUE', 'hash_value');
define('RESPONSE_STRING', 'response_string');
define('RESPONSE_TIME', 'response_time');
define('START_ID', 'start_id');
define('STOP_ID', 'stop_id');
2015-04-22 12:26:04 +02:00
ini_set("session.save_path", "/tmp");
$webuidir = "";
$verbose = 0;
$mode = "unit";
$opts = 'h::v';
$lopts = array(
'webui:',
'mode:',
'verbose'
);
2015-04-22 12:26:04 +02:00
if ( $options = getopt( $opts, $lopts ) )
{
if ( isset($options['webui']) )
2015-04-22 12:26:04 +02:00
{
$webuidir = $options['webui'];
} else
{
echo "\nError: must provide path to WebUI directory\n\n";
2015-04-22 12:26:04 +02:00
display_help();
exit;
}
if ( isset($options['mode']) && $options['mode'] == 'time') {
$mode = $options['mode'];
2015-04-22 12:26:04 +02:00
}
if ( isset($options['h']) )
2015-04-22 12:26:04 +02:00
{
display_help();
exit;
}
2015-04-22 12:26:04 +02:00
if ( isset($options['verbose']) )
{
$verbose = 1;
}
} else {
display_help();
exit;
2015-04-22 12:26:04 +02:00
}
require_once($webuidir . "/config.php");
openlog("piler-timestamp", LOG_PID, LOG_MAIL);
2015-04-22 12:26:04 +02:00
require(DIR_SYSTEM . "/startup.php");
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PREFIX);
Registry::set('DB_DATABASE', DB_DATABASE);
Registry::set('db', $db);
Registry::set('DB_DRIVER', DB_DRIVER);
define('MODE', $mode);
$data = get_hash_values();
// check if we have enough messages
if(MODE == 'unit' && $data[COUNT] < TSA_STAMP_REQUEST_UNIT_SIZE) {
syslog(LOG_INFO, "not enough messages to sign yet (" . $data[COUNT] . ")");
2015-04-22 12:26:04 +02:00
exit;
}
if(MODE == 'time' && $data[COUNT] < 1) {
syslog(LOG_INFO, "not enough messages to sign yet (" . $data[COUNT] . ")");
2015-04-22 12:26:04 +02:00
exit;
}
try {
$requestfile_path = TrustedTimestamps::createRequestfile($data[HASH_VALUE]);
$response = TrustedTimestamps::signRequestfile($requestfile_path, TSA_URL);
} catch(Exception $e) {
die("Error: " . $e->getMessage() . "\n");
}
2015-04-22 12:26:04 +02:00
$data[RESPONSE_STRING] = $response[RESPONSE_STRING];
$data[RESPONSE_TIME] = $response[RESPONSE_TIME];
2015-04-22 12:26:04 +02:00
$rc = store_results($data);
if($rc == 1) { syslog(LOG_INFO, "signed " . $data[COUNT] . " messages (" . $data[START_ID] . "-" . $data[STOP_ID] . ") on @" . $data[RESPONSE_TIME]); }
else { syslog(LOG_INFO, "ERROR: failed to sign " . $data[COUNT] . " messages (" . $data[START_ID] . "-" . $data[STOP_ID] . ") on @" . $data[RESPONSE_TIME]); }
2015-04-22 12:26:04 +02:00
function get_last_entry_from_timestamp_table() {
$db = Registry::get('db');
$query = $db->query("SELECT start_id, stop_id FROM " . TABLE_TIMESTAMP . " WHERE id < 100000000000 ORDER BY id DESC LIMIT 1");
if($query->num_rows == 0) {
return 0;
}
return $query->row[STOP_ID];
2015-04-22 12:26:04 +02:00
}
function get_hash_values() {
$s = '';
$count = 0;
$db = Registry::get('db');
$last_id = get_last_entry_from_timestamp_table();
if($last_id == 0) {
$start_id = TSA_START_ID;
if(MODE == 'unit') { $stop_id = $start_id + TSA_STAMP_REQUEST_UNIT_SIZE - 1; }
else { $stop_id = 1000000000; }
}
else {
$start_id = $last_id + 1;
if(MODE == 'unit') { $stop_id = $start_id + TSA_STAMP_REQUEST_UNIT_SIZE - 1; }
else { $stop_id = 1000000000; }
}
$query = $db->query("SELECT id, digest FROM " . TABLE_META . " WHERE id >= ? AND id <= ? ORDER BY id", array($start_id, $stop_id));
2015-04-22 12:26:04 +02:00
foreach($query->rows as $q) {
$count++;
$s .= $q['digest'];
}
if(MODE == 'time') { $stop_id = $start_id + $count - 1; }
return [
START_ID => $start_id,
STOP_ID => $stop_id,
COUNT => $count,
HASH_VALUE => sha1($s)
];
2015-04-22 12:26:04 +02:00
}
function store_results($data = array()) {
$db = Registry::get('db');
$db->query("INSERT INTO " . TABLE_TIMESTAMP . " (start_id, stop_id, hash_value, `count`, response_time, response_string) VALUES(?,?,?,?,?,?)", array($data[START_ID], $data[STOP_ID], $data[HASH_VALUE], $data[COUNT], $data[RESPONSE_TIME], $data[RESPONSE_STRING]));
2015-04-22 12:26:04 +02:00
return $db->countAffected();
2015-04-22 12:26:04 +02:00
}
function display_help() {
$phpself = basename(__FILE__);
echo "\nUsage: $phpself --webui [PATH] [OPTIONS...]\n\n";
echo "\t--webui=\"[REQUIRED: path to the Piler WebUI Directory]\"\n\n";
echo "options:\n";
echo "\t--mode time|unit (default: unit)\n";
echo "\t-v Provide a verbose output\n";
echo "\t-h Prints this help screen and exits\n";
2015-04-22 12:26:04 +02:00
}