From 542d7384d4bc65089fb29a599f8f3896bb7a9fa0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 28 Jan 2017 18:58:49 +0100 Subject: [PATCH] New hash management (prepared next secret) : cyclic-hash/* --- lib/api/source/fetch.php | 2 +- lib/api/source/sync.php | 25 ++++++++++++++++--------- lib/cyclic-hash/source/decr.php | 13 +++++++------ lib/cyclic-hash/source/hash.php | 2 +- lib/cyclic-hash/source/new.php | 9 +++++---- lib/include/const | 4 ++-- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/lib/api/source/fetch.php b/lib/api/source/fetch.php index 917edaf..765c1c4 100755 --- a/lib/api/source/fetch.php +++ b/lib/api/source/fetch.php @@ -16,7 +16,7 @@ - + return json_encode(['a'=>1]); } echo api_fetch(); diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index 48507b0..032ab37 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -55,7 +55,8 @@ /* [2] Create httpRequest basis =========================================================*/ /* (1) Set URL */ - $curl = curl_init($url); + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $url); /* (2) Set HTTP method -> POST */ curl_setopt($curl, CURLOPT_POST, true); @@ -75,29 +76,30 @@ $postarray['renew'] = $new; /* (3) Parse postfiels to multipart format */ - #$postraw = "--$boundary"; + $postraw = "--$boundary"; - #foreach($postarray as $postkey=>$postvalue) - # $postraw .= "\r\nContent-Disposition: form-data; name=\"$postkey\"\r\n\r\n$postvalue\r\n--$boundary"; + foreach($postarray as $postkey=>$postvalue) + $postraw .= "\r\nContent-Disposition: form-data; name=\"$postkey\"\r\n\r\n$postvalue\r\n--$boundary"; - #$postraw .= "--"; + $postraw .= "--"; /* (4) Set postdata raw to curl */ - curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray); + curl_setopt($curl, CURLOPT_POSTFIELDS, $postraw); /* [4] Manage headers =========================================================*/ - #curl_setopt($curl, CURLOPT_HTTPHEADER, [ - # "Content-Type: multipart/form-data" - #]); + curl_setopt($curl, CURLOPT_HTTPHEADER, [ + "Content-Type: multipart/form-data; boundary=$boundary" + ]); /* [5] Send and catch request response =========================================================*/ /* (1) Send and catch response */ + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); /* (2) Close request */ @@ -109,6 +111,11 @@ return 127; } + + /* [6] Decrement cyclic-hash so request has ran successfully + =========================================================*/ + + diff --git a/lib/cyclic-hash/source/decr.php b/lib/cyclic-hash/source/decr.php index 1d7f1e8..5aaf1a8 100755 --- a/lib/cyclic-hash/source/decr.php +++ b/lib/cyclic-hash/source/decr.php @@ -33,10 +33,10 @@ $secret = @file_get_contents(SECRET_CONF); /* (2) Check secret file format */ - if( !is_string($secret) || !preg_match("/^(.{250}):(\d+)$/", $secret, $match) ){ + if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+):(.{".SECRET_SIZE."})$/", $secret, $match) ){ - // Generate new secret - $secret = generate_secret().':999'; + // Generate full secret + $secret = generate_secret().':999:'.generate_secret(); // Try to override the secret file if( @file_put_contents(SECRET_CONF, $secret) ){ @@ -51,6 +51,7 @@ /* (3) Extract data */ $key = (string) $match[1]; $depth = (int) $match[2]; + $next = (string) $match[3]; /* [3] If can decrement, decrement @@ -61,7 +62,7 @@ $depth--; /* (2) Try to override the secret file */ - if( @file_put_contents(SECRET_CONF, "$key:$depth") ){ + if( @file_put_contents(SECRET_CONF, "$key:$depth:$next") ){ slog("Secret depth decremented to $depth", 'cyclic-hash:decr'); return 0; }else{ @@ -70,12 +71,12 @@ } - /* [4] If cannot decrement, generate new password + /* [4] If cannot decrement, use new secret and generate next =========================================================*/ }else{ // Generate new secret - $secret = generate_secret().':999'; + $secret = $next.':999:'.generate_secret(); // Try to override the secret file if( @file_put_contents(SECRET_CONF, $secret) ){ diff --git a/lib/cyclic-hash/source/hash.php b/lib/cyclic-hash/source/hash.php index ffcb88d..4a4b6d0 100755 --- a/lib/cyclic-hash/source/hash.php +++ b/lib/cyclic-hash/source/hash.php @@ -13,7 +13,7 @@ $secret = @file_get_contents(SECRET_CONF); /* (2) Check secret file format */ - if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+)$/", $secret, $match) ) + if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+):.{".SECRET_SIZE."}$/", $secret, $match) ) return 127; /* (3) Extract data for hashing from @secret */ diff --git a/lib/cyclic-hash/source/new.php b/lib/cyclic-hash/source/new.php index fdf2ba4..5b8d030 100755 --- a/lib/cyclic-hash/source/new.php +++ b/lib/cyclic-hash/source/new.php @@ -13,7 +13,7 @@ $secret = @file_get_contents(SECRET_CONF); /* (2) Check secret file format */ - if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+)$/", $secret, $match) ){ + if( !is_string($secret) || !preg_match("/^(.{".SECRET_SIZE."}):(\d+):(.{".SECRET_SIZE."})$/", $secret, $match) ){ slog("Error while reading secret", 'cyclic-hash:new'); return 127; } @@ -21,10 +21,11 @@ /* (3) Extract data for hashing from @secret */ $key = (string) $match[1]; $depth = (int) $match[2]; + $next = (string) $match[3]; /* (4) Die if not token not changed */ - if( $depth < 999 ){ + if( $depth > 1 ){ slog("No new secret with $depth depth", 'cyclic-hash:new'); return 0; } @@ -32,8 +33,8 @@ /* [2] If hash have just been created (original depth = 1000) =========================================================*/ - /* (1) Return new hash */ - $newhash = $key; + /* (1) Return new hash (from @next) */ + $newhash = $next; /* (2) Hash @depth times = 1000 */ for( $d = 0 ; $d < 1000 ; $d++ ) diff --git a/lib/include/const b/lib/include/const index 42957f7..ba27cec 100755 --- a/lib/include/const +++ b/lib/include/const @@ -3,11 +3,11 @@ # RESET OUTPUT BUFFER # MAIN DIRECTORIES - define('ROOT_DIR', '/home/sats/satsd'); + define('ROOT_DIR', '/home/xdrm-brackets/SANDBOX/sats-local'); define('LOG_DIR', ROOT_DIR.'/log'); define('DATA_DIR', ROOT_DIR.'/data'); define('CONF_DIR', ROOT_DIR.'/conf'); - define('SOURCE_DIR', ROOT_DIR.'/source'); + define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats'); define('TMP_DIR', ROOT_DIR.'/tmp'); # CONFIGURATION FILES