[b1735a7] | 1 | <?php |
---|
| 2 | |
---|
| 3 | // OpenGnsys REST routes for OGAgent communications. |
---|
| 4 | // Author: Ramón M. Gómez |
---|
[a9140b0] | 5 | // Date: 2016-10-03 |
---|
[b1735a7] | 6 | |
---|
| 7 | |
---|
| 8 | // OGAgent sessions log file. |
---|
| 9 | define('LOG_FILE', '/opt/opengnsys/log/ogagent.log'); |
---|
| 10 | |
---|
[a9140b0] | 11 | /** |
---|
| 12 | * @brief OGAgent notifies that its service is started on a client. |
---|
| 13 | * @note Route: /ogagent/started, Method: POST |
---|
| 14 | * @param string ip IP address |
---|
| 15 | * @param string mac MAC (Ethernet) address |
---|
| 16 | * @param string ostype OS type (Linux, Windows) |
---|
| 17 | * @param string osversion OS name and version |
---|
| 18 | * @param string secret random secret key to access client's REST API |
---|
| 19 | * @return Null string if OK, else error message. |
---|
| 20 | */ |
---|
[b1735a7] | 21 | $app->post('/ogagent/started', |
---|
| 22 | function() use ($app) { |
---|
[2913439] | 23 | global $cmd; |
---|
| 24 | $osType = $osVersion = "none"; |
---|
[b1735a7] | 25 | try { |
---|
| 26 | // Reading POST parameters in JSON format. |
---|
| 27 | $input = json_decode($app->request()->getBody()); |
---|
| 28 | $ip = htmlspecialchars($input->ip); |
---|
| 29 | $mac = htmlspecialchars($input->mac); |
---|
[31970a0] | 30 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
| 31 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
[a9140b0] | 32 | // Check sender IP address consistency (same as parameter value). |
---|
| 33 | if ($ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 34 | throw new Exception("Bad IP address: agent=$ip, sender=".$_SERVER['REMOTE_ADDR']); |
---|
| 35 | } |
---|
[210ee85] | 36 | // Client secret key for secure communications. |
---|
| 37 | if (isset($input->secret)) { |
---|
[a9140b0] | 38 | // Check if secret key is valid (32 alphanumeric characters). |
---|
| 39 | if (! ctype_alnum($input->secret) or strlen($input->secret) !== 32) { |
---|
| 40 | throw new Exception("Bad secret key: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
| 41 | } |
---|
[210ee85] | 42 | // Store secret key in DB. |
---|
[2913439] | 43 | $cmd->texto = "UPDATE ordenadores |
---|
| 44 | SET agentkey='$secret' |
---|
[a9140b0] | 45 | WHERE ip='$ip' AND mac=UPPER(REPLACE('$mac',':','')) |
---|
| 46 | LIMIT 1"; |
---|
| 47 | if ($cmd->Ejecutar() !== true or mysql_affected_rows() !== 1) { |
---|
| 48 | // DB access error or not updated. |
---|
| 49 | throw new Exception("Cannot store new secret key: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
[2913439] | 50 | } |
---|
[210ee85] | 51 | } else { |
---|
| 52 | // Insecure agent exception. |
---|
[31970a0] | 53 | throw new Exception("Insecure OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
[210ee85] | 54 | } |
---|
[b1735a7] | 55 | // Default processing: log activity. |
---|
[31970a0] | 56 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
[b1735a7] | 57 | // Response. |
---|
[d98bc86] | 58 | $response = ""; |
---|
[b1735a7] | 59 | jsonResponse(200, $response); |
---|
| 60 | } catch (Exception $e) { |
---|
| 61 | // Comunication error. |
---|
| 62 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 63 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 64 | jsonResponse(400, $response); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | ); |
---|
| 68 | |
---|
| 69 | // OGAgent notifies that its service is stopped on client. |
---|
| 70 | $app->post('/ogagent/stopped', |
---|
| 71 | function() use ($app) { |
---|
[2913439] | 72 | $osType = $osVersion = "none"; |
---|
[b1735a7] | 73 | try { |
---|
| 74 | // Reading POST parameters in JSON format. |
---|
| 75 | $input = json_decode($app->request()->getBody()); |
---|
| 76 | $ip = htmlspecialchars($input->ip); |
---|
| 77 | $mac = htmlspecialchars($input->mac); |
---|
[31970a0] | 78 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
| 79 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
[a9140b0] | 80 | // Check sender IP address consistency (same as parameter value). |
---|
| 81 | if ($ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 82 | throw new Exception("Bad IP address: agent=$ip, sender=".$_SERVER['REMOTE_ADDR']); |
---|
| 83 | } |
---|
| 84 | // May check if client is included in the server database? |
---|
[b1735a7] | 85 | // Default processing: log activity. |
---|
[31970a0] | 86 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent stopped: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
[b1735a7] | 87 | // Response. |
---|
[d98bc86] | 88 | $response = ""; |
---|
[b1735a7] | 89 | jsonResponse(200, $response); |
---|
| 90 | } catch (Exception $e) { |
---|
| 91 | // Comunication error. |
---|
| 92 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 93 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 94 | jsonResponse(400, $response); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | ); |
---|
| 98 | |
---|
| 99 | // OGAgent notifies that an user logs in. |
---|
| 100 | $app->post('/ogagent/loggedin', |
---|
| 101 | function() use ($app) { |
---|
| 102 | try { |
---|
| 103 | // Reading POST parameters in JSON format. |
---|
| 104 | $input = json_decode($app->request()->getBody()); |
---|
| 105 | $ip = htmlspecialchars($input->ip); |
---|
| 106 | $user = htmlspecialchars($input->user); |
---|
[a9140b0] | 107 | // Check sender IP address consistency (same as parameter value). |
---|
| 108 | if ($ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 109 | throw new Exception("Bad IP address: agent=$ip, sender=".$_SERVER['REMOTE_ADDR']); |
---|
| 110 | } |
---|
| 111 | // May check if client is included in the server database? |
---|
[b1735a7] | 112 | // Default processing: log activity. |
---|
[0a767fc] | 113 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged in: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
[b1735a7] | 114 | // Response. |
---|
[d98bc86] | 115 | $response = ""; |
---|
[b1735a7] | 116 | jsonResponse(200, $response); |
---|
| 117 | } catch (Exception $e) { |
---|
| 118 | // Comunication error. |
---|
| 119 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 120 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 121 | jsonResponse(400, $response); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | ); |
---|
| 125 | |
---|
| 126 | // OGAgent notifies that an user logs out. |
---|
| 127 | $app->post('/ogagent/loggedout', |
---|
| 128 | function() use ($app) { |
---|
| 129 | try { |
---|
| 130 | // Reading POST parameters in JSON format. |
---|
| 131 | $input = json_decode($app->request()->getBody()); |
---|
| 132 | $ip = htmlspecialchars($input->ip); |
---|
| 133 | $user = htmlspecialchars($input->user); |
---|
[a9140b0] | 134 | // Check sender IP address consistency (same as parameter value). |
---|
| 135 | if ($ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 136 | throw new Exception("Bad IP address: agent=$ip, sender=".$_SERVER['REMOTE_ADDR']); |
---|
| 137 | } |
---|
| 138 | // May check if client is included in the server database? |
---|
[b1735a7] | 139 | // Default processing: log activity. |
---|
[31970a0] | 140 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged out: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
[b1735a7] | 141 | // Response. |
---|
[d98bc86] | 142 | $response = ""; |
---|
[b1735a7] | 143 | jsonResponse(200, $response); |
---|
| 144 | } catch (Exception $e) { |
---|
| 145 | // Comunication error. |
---|
| 146 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 147 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 148 | jsonResponse(400, $response); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | ); |
---|
| 152 | |
---|
| 153 | ?> |
---|
| 154 | |
---|