From a3ff6b9cbea8fc4304951cd24f9153148ec8b3d2 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 19 Feb 2017 11:19:40 +0100 Subject: [PATCH 1/2] added sync+init (with new system for init a machine : unlock code) --- lib/api/init | 3 + lib/api/source/init.php | 150 ++++++++++++++++++++++++++++++++++++++++ lib/api/source/sync.php | 54 +++++++++------ lib/include/bash/const | 1 + lib/include/php/const | 3 +- 5 files changed, 190 insertions(+), 21 deletions(-) create mode 100755 lib/api/init create mode 100755 lib/api/source/init.php diff --git a/lib/api/init b/lib/api/init new file mode 100755 index 0000000..b35485c --- /dev/null +++ b/lib/api/init @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/init.php; diff --git a/lib/api/source/init.php b/lib/api/source/init.php new file mode 100755 index 0000000..b3bfcca --- /dev/null +++ b/lib/api/source/init.php @@ -0,0 +1,150 @@ + POST */ + curl_setopt($curl, CURLOPT_POST, true); + + /* (3) Additional options */ + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + + + + /* [3] Manage post data + =========================================================*/ + /* (1) Set post data */ + $postarray = [ + 'id_machine' => $id_machine, + 'token' => $hash, + 'unlock' => $unlock_code + ]; + + /* (2) Add new hash if renew */ + if( strlen($new) == 128 ) + $postarray['token'] = $new; + + /* (3) Set postdata raw to curl */ + curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray); + + + /* [4] Manage authentication + =========================================================*/ + /* (1) Fetch warehouse token */ + $wh_tok = @file_get_contents(WAREHOUSE_TOK); + + if( $wh_tok === false ){ + slog("Cannot find warehouse token", 'api:init','update'); + return 127; + } + + /* (2) Set Auth header digest */ + curl_setopt($curl, CURLOPT_HTTPHEADER, [ + "Authorization: Digest $wh_tok" + ]); + + + /* [5] Send and catch request response + =========================================================*/ + /* (1) Send and catch response */ + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($curl); + + /* (2) Close request */ + curl_close($curl); + + /* (3) Return response as result */ + if( $response === false ){ + slog("Request error", 'api:init','update'); + return 127; + } + + + + /* [6] Decrement cyclic-hash so request has ran successfully + =========================================================*/ + /* (1) Decrement the hash */ + $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); + + if( $decr === false ){ + slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:init','update'); + return 127; + } + + + /* [6] Response management + =========================================================*/ + /* (1) Writes request to temporary pipe */ + file_put_contents(TMP_DIR.'/api.response', $response); + + /* (2) Manage response deployement */ + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); + + /* (3) Return state */ + return ($deploy === true) ? 0 : 127; + + } + + echo api_init(); + + + +?> diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index 7254eea..cdf6845 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -15,13 +15,14 @@ } // remove unwanted spaces or linebreaks - $url = preg_replace('/\s/', '', $url); + $url = preg_replace('/\s/', '', $url).'/sync'; + /* (2) Fetch cyclic hash */ $hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash'); if( strlen($hash) != 128 ){ - slog("Wrong cyclic-hash:hash hash length (".strlen($hash).")", 'api:sync','update'); + slog("Wrong hash length (".strlen($hash).")", 'api:sync','update'); return 127; } @@ -33,21 +34,18 @@ return 127; } - /* (4) Generate the multipart boundary */ - $boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary'; - - /* (5) Fetch data */ + /* (4) Fetch data */ $data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch')); if( is_null($data) ){ slog("api:fetch returned unreadable content", 'api:sync','update'); $data = []; } - + - /* [2] Create httpRequest basis + /* [2] Create httpRequest base =========================================================*/ /* (1) Set URL */ $curl = curl_init(); @@ -56,14 +54,17 @@ /* (2) Set HTTP method -> POST */ curl_setopt($curl, CURLOPT_POST, true); + /* (3) Additional options */ + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + /* [3] Manage post data =========================================================*/ /* (1) Set post data */ $postarray = [ - 'token' => $hash, - 'data' => json_encode($data) + 'token' => $hash, + 'data' => json_encode($data) ]; /* (2) Add renew if renew */ @@ -74,14 +75,30 @@ curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray); - /* [4] Send and catch request response + /* [4] Manage authentication + =========================================================*/ + /* (1) Fetch warehouse token */ + $wh_tok = @file_get_contents(WAREHOUSE_TOK); + + if( $wh_tok === false ){ + slog("Cannot find warehouse token", 'api:sync','update'); + return 127; + } + + /* (2) Set Auth header digest */ + curl_setopt($curl, CURLOPT_HTTPHEADER, [ + "Authorization: Digest $wh_tok" + ]); + + + /* [5] Send and catch request response =========================================================*/ /* (1) Send and catch response */ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); /* (2) Close request */ - //curl_close($curl); + curl_close($curl); /* (3) Return response as result */ if( $response === false ){ @@ -90,7 +107,8 @@ } - /* [5] Decrement cyclic-hash so request has ran successfully + + /* [6] Decrement cyclic-hash so request has ran successfully =========================================================*/ /* (1) Decrement the hash */ $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); @@ -100,12 +118,8 @@ return 127; } - - - - - /* [5] Response management + /* [6] Response management =========================================================*/ /* (1) Writes request to temporary pipe */ file_put_contents(TMP_DIR.'/api.response', $response); @@ -114,8 +128,8 @@ $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); /* (3) Return state */ - return ($deploy === true); - + return ($deploy === true) ? 0 : 127; + } echo api_request(); diff --git a/lib/include/bash/const b/lib/include/bash/const index e288965..810980b 100755 --- a/lib/include/bash/const +++ b/lib/include/bash/const @@ -19,6 +19,7 @@ export BRANCH_CONF="$CONF_DIR/machine.branch"; export ID_CONF="$CONF_DIR/machine.id"; export URL_CONF="$CONF_DIR/api.url"; export AUTH_CONF="$CONF_DIR/auth.list"; +export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; # LOG FILES export BOOT_LOG="$LOG_DIR/boot.log"; diff --git a/lib/include/php/const b/lib/include/php/const index ca62ae7..60a3775 100755 --- a/lib/include/php/const +++ b/lib/include/php/const @@ -9,7 +9,7 @@ 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.xdr/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats'); + define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats'); define('TMP_DIR', ROOT_DIR.'/tmp'); # CONFIGURATION FILES @@ -19,6 +19,7 @@ define('ID_CONF', CONF_DIR.'/machine.id'); define('URL_CONF', CONF_DIR.'/api.url'); define('AUTH_CONF', CONF_DIR.'/auth.list'); + define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); # LOG FILES define('BOOT_LOG', LOG_DIR.'/boot.log'); From 5ff68d5c42856985a05ab517b509229633df1b1d Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 19 Feb 2017 12:31:36 +0100 Subject: [PATCH 2/2] [Done] init|sync works[Todo] Fetcher [Todo] Deployer --- lib/api/source/deploy.php | 40 +++++++++++++++++++++++++++++++++++++++ lib/api/source/init.php | 7 +++++-- lib/api/source/sync.php | 21 ++++++++++++++++---- lib/include/bash/const | 1 + lib/include/php/const | 1 + 5 files changed, 64 insertions(+), 6 deletions(-) create mode 100755 lib/api/source/deploy.php diff --git a/lib/api/source/deploy.php b/lib/api/source/deploy.php new file mode 100755 index 0000000..f2f4a6e --- /dev/null +++ b/lib/api/source/deploy.php @@ -0,0 +1,40 @@ + diff --git a/lib/api/source/init.php b/lib/api/source/init.php index b3bfcca..ff01337 100755 --- a/lib/api/source/init.php +++ b/lib/api/source/init.php @@ -45,13 +45,14 @@ } /* (5) Fetch machine unlock code */ - $unlock_code = @file_get_contents(ID_CONF); + $unlock_code = @file_get_contents(UNLOCK_CODE); if( $unlock_code === false ){ slog("Cannot find unlock code", 'api:init','update'); return 127; } + $unlock_code = preg_replace('/\s/', '', $unlock_code); @@ -96,6 +97,8 @@ return 127; } + $wh_tok = preg_replace('/\s/', '', $wh_tok); + /* (2) Set Auth header digest */ curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Digest $wh_tok" @@ -136,7 +139,7 @@ file_put_contents(TMP_DIR.'/api.response', $response); /* (2) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy init'); /* (3) Return state */ return ($deploy === true) ? 0 : 127; diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index cdf6845..c0768a4 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -85,6 +85,8 @@ return 127; } + $wh_tok = preg_replace('/\s/', '', $wh_tok); + /* (2) Set Auth header digest */ curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Digest $wh_tok" @@ -121,13 +123,24 @@ /* [6] Response management =========================================================*/ - /* (1) Writes request to temporary pipe */ + /* (1) Try to json_decode response */ + $response_arr = json_decode($response, true); + + // if cannot, abort + if( is_null($response_arr) ) + return 127; + + /* (2) Check response error */ + if( !isset($response_arr['error']) || $response_arr['error'] != 0 ) + return 127; + + /* (3) Writes request to temporary pipe */ file_put_contents(TMP_DIR.'/api.response', $response); - /* (2) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); + /* (4) Manage response deployement */ + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); - /* (3) Return state */ + /* (5) Return state */ return ($deploy === true) ? 0 : 127; } diff --git a/lib/include/bash/const b/lib/include/bash/const index 810980b..42d7fc6 100755 --- a/lib/include/bash/const +++ b/lib/include/bash/const @@ -20,6 +20,7 @@ export ID_CONF="$CONF_DIR/machine.id"; export URL_CONF="$CONF_DIR/api.url"; export AUTH_CONF="$CONF_DIR/auth.list"; export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; +export UNLOCK_CODE="$CONF_DIR/machine.unlock"; # LOG FILES export BOOT_LOG="$LOG_DIR/boot.log"; diff --git a/lib/include/php/const b/lib/include/php/const index 60a3775..189f15d 100755 --- a/lib/include/php/const +++ b/lib/include/php/const @@ -20,6 +20,7 @@ define('URL_CONF', CONF_DIR.'/api.url'); define('AUTH_CONF', CONF_DIR.'/auth.list'); define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); + define('UNLOCK_CODE', CONF_DIR.'/machine.unlock'); # LOG FILES define('BOOT_LOG', LOG_DIR.'/boot.log');