[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. |
---|
[21e5ee3] | 13 | * @note Route: /ogagent/started, Method: POST, Format: JSON |
---|
[a9140b0] | 14 | * @param string ip IP address |
---|
| 15 | * @param string mac MAC (Ethernet) address |
---|
| 16 | * @param string ostype OS type (Linux, Windows) |
---|
[21e5ee3] | 17 | * @param string osversion OS version |
---|
[a9140b0] | 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)); |
---|
[21e5ee3] | 32 | // Check sender agent type and IP address consistency (same as parameter value). |
---|
| 33 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT'])) or $ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 34 | throw new Exception("Bad OGAgent: ip=$ip, sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
---|
[a9140b0] | 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. |
---|
[f6045a5] | 43 | if (isset($input->secret)) $secret = htmlspecialchars($input->secret); |
---|
[2913439] | 44 | $cmd->texto = "UPDATE ordenadores |
---|
| 45 | SET agentkey='$secret' |
---|
[a9140b0] | 46 | WHERE ip='$ip' AND mac=UPPER(REPLACE('$mac',':','')) |
---|
| 47 | LIMIT 1"; |
---|
| 48 | if ($cmd->Ejecutar() !== true or mysql_affected_rows() !== 1) { |
---|
| 49 | // DB access error or not updated. |
---|
| 50 | throw new Exception("Cannot store new secret key: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
[2913439] | 51 | } |
---|
[210ee85] | 52 | } else { |
---|
| 53 | // Insecure agent exception. |
---|
[31970a0] | 54 | throw new Exception("Insecure OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
[210ee85] | 55 | } |
---|
[b1735a7] | 56 | // Default processing: log activity. |
---|
[31970a0] | 57 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
[b1735a7] | 58 | // Response. |
---|
[d98bc86] | 59 | $response = ""; |
---|
[b1735a7] | 60 | jsonResponse(200, $response); |
---|
| 61 | } catch (Exception $e) { |
---|
| 62 | // Comunication error. |
---|
| 63 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 64 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 65 | jsonResponse(400, $response); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | ); |
---|
| 69 | |
---|
[21e5ee3] | 70 | /** |
---|
| 71 | * @brief OGAgent notifies that its service is stopped on client. |
---|
| 72 | * @note Route: /ogagent/stopped, Method: POST, Format: JSON |
---|
| 73 | * @param string ip IP address |
---|
| 74 | * @param string mac MAC (Ethernet) address |
---|
| 75 | * @param string ostype OS type (Linux, Windows) |
---|
| 76 | * @param string osversion OS version |
---|
| 77 | * @return Null string if OK, else error message. |
---|
| 78 | */ |
---|
[b1735a7] | 79 | $app->post('/ogagent/stopped', |
---|
| 80 | function() use ($app) { |
---|
[2913439] | 81 | $osType = $osVersion = "none"; |
---|
[b1735a7] | 82 | try { |
---|
| 83 | // Reading POST parameters in JSON format. |
---|
| 84 | $input = json_decode($app->request()->getBody()); |
---|
| 85 | $ip = htmlspecialchars($input->ip); |
---|
| 86 | $mac = htmlspecialchars($input->mac); |
---|
[31970a0] | 87 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
| 88 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
[21e5ee3] | 89 | // Check sender agent type and IP address consistency (same as parameter value). |
---|
| 90 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT'])) or $ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 91 | throw new Exception("Bad OGAgent: ip=$ip, sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
---|
[a9140b0] | 92 | } |
---|
| 93 | // May check if client is included in the server database? |
---|
[b1735a7] | 94 | // Default processing: log activity. |
---|
[31970a0] | 95 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent stopped: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
[b1735a7] | 96 | // Response. |
---|
[d98bc86] | 97 | $response = ""; |
---|
[b1735a7] | 98 | jsonResponse(200, $response); |
---|
| 99 | } catch (Exception $e) { |
---|
| 100 | // Comunication error. |
---|
| 101 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 102 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 103 | jsonResponse(400, $response); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | ); |
---|
| 107 | |
---|
[21e5ee3] | 108 | /** |
---|
| 109 | * @brief OGAgent notifies that an user logs in. |
---|
| 110 | * @note Route: /ogagent/loggedin, Method: POST, Format: JSON |
---|
| 111 | * @param string ip IP address |
---|
| 112 | * @param string user username |
---|
| 113 | * @return Null string if OK, else error message. |
---|
| 114 | */ |
---|
[b1735a7] | 115 | $app->post('/ogagent/loggedin', |
---|
| 116 | function() use ($app) { |
---|
| 117 | try { |
---|
| 118 | // Reading POST parameters in JSON format. |
---|
| 119 | $input = json_decode($app->request()->getBody()); |
---|
| 120 | $ip = htmlspecialchars($input->ip); |
---|
| 121 | $user = htmlspecialchars($input->user); |
---|
[a9140b0] | 122 | // Check sender IP address consistency (same as parameter value). |
---|
[21e5ee3] | 123 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT'])) or $ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 124 | throw new Exception("Bad OGAgent: ip=$ip, sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
---|
[a9140b0] | 125 | } |
---|
| 126 | // May check if client is included in the server database? |
---|
[b1735a7] | 127 | // Default processing: log activity. |
---|
[0a767fc] | 128 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged in: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
[b1735a7] | 129 | // Response. |
---|
[d98bc86] | 130 | $response = ""; |
---|
[b1735a7] | 131 | jsonResponse(200, $response); |
---|
| 132 | } catch (Exception $e) { |
---|
| 133 | // Comunication error. |
---|
| 134 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 135 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 136 | jsonResponse(400, $response); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | ); |
---|
| 140 | |
---|
[21e5ee3] | 141 | /** |
---|
| 142 | * @brief OGAgent notifies that an user logs out. |
---|
| 143 | * @note Route: /ogagent/loggedout, Method: POST, Format: JSON |
---|
| 144 | * @param string ip IP address |
---|
| 145 | * @param string user username |
---|
| 146 | * @return Null string if OK, else error message. |
---|
| 147 | */ |
---|
[b1735a7] | 148 | $app->post('/ogagent/loggedout', |
---|
| 149 | function() use ($app) { |
---|
| 150 | try { |
---|
| 151 | // Reading POST parameters in JSON format. |
---|
| 152 | $input = json_decode($app->request()->getBody()); |
---|
| 153 | $ip = htmlspecialchars($input->ip); |
---|
| 154 | $user = htmlspecialchars($input->user); |
---|
[21e5ee3] | 155 | // Check sender agent type and IP address consistency (same as parameter value). |
---|
| 156 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT'])) or $ip !== $_SERVER['REMOTE_ADDR']) { |
---|
| 157 | throw new Exception("Bad OGAgent: ip=$ip, sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
---|
[a9140b0] | 158 | } |
---|
| 159 | // May check if client is included in the server database? |
---|
[b1735a7] | 160 | // Default processing: log activity. |
---|
[31970a0] | 161 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged out: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
[b1735a7] | 162 | // Response. |
---|
[d98bc86] | 163 | $response = ""; |
---|
[b1735a7] | 164 | jsonResponse(200, $response); |
---|
| 165 | } catch (Exception $e) { |
---|
| 166 | // Comunication error. |
---|
| 167 | $response["message"] = $e->getMessage(); |
---|
[a9140b0] | 168 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
[b1735a7] | 169 | jsonResponse(400, $response); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | ); |
---|
| 173 | |
---|
| 174 | ?> |
---|