1 | <?php |
---|
2 | |
---|
3 | // OpenGnsys REST routes for OGAgent communications. |
---|
4 | // Author: Ramón M. Gómez |
---|
5 | // Date: 2016-10-03 |
---|
6 | |
---|
7 | |
---|
8 | // OGAgent sessions log file. |
---|
9 | define('LOG_FILE', '/opt/opengnsys/log/ogagent.log'); |
---|
10 | |
---|
11 | /** |
---|
12 | * @brief OGAgent notifies that its service is started on a client. |
---|
13 | * @note Route: /ogagent/started, Method: POST, Format: JSON |
---|
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 version |
---|
18 | * @param string secret random secret key to access client's REST API |
---|
19 | * @return Null string if OK, else error message. |
---|
20 | */ |
---|
21 | $app->post('/ogagent/started', |
---|
22 | function() use ($app) { |
---|
23 | global $cmd; |
---|
24 | $osType = $osVersion = "none"; |
---|
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); |
---|
30 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
31 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
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']); |
---|
35 | } |
---|
36 | // Client secret key for secure communications. |
---|
37 | if (isset($input->secret)) { |
---|
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 | } |
---|
42 | // Store secret key in DB. |
---|
43 | if (isset($input->secret)) $secret = htmlspecialchars($input->secret); |
---|
44 | $cmd->texto = "UPDATE ordenadores |
---|
45 | SET agentkey='$secret' |
---|
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."); |
---|
51 | } |
---|
52 | } else { |
---|
53 | // Insecure agent exception. |
---|
54 | throw new Exception("Insecure OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
55 | } |
---|
56 | // Default processing: log activity. |
---|
57 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
58 | // Response. |
---|
59 | $response = ""; |
---|
60 | jsonResponse(200, $response); |
---|
61 | } catch (Exception $e) { |
---|
62 | // Comunication error. |
---|
63 | $response["message"] = $e->getMessage(); |
---|
64 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
65 | jsonResponse(400, $response); |
---|
66 | } |
---|
67 | } |
---|
68 | ); |
---|
69 | |
---|
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 | */ |
---|
79 | $app->post('/ogagent/stopped', |
---|
80 | function() use ($app) { |
---|
81 | $osType = $osVersion = "none"; |
---|
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); |
---|
87 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
88 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
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']); |
---|
92 | } |
---|
93 | // May check if client is included in the server database? |
---|
94 | // Default processing: log activity. |
---|
95 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent stopped: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
96 | // Response. |
---|
97 | $response = ""; |
---|
98 | jsonResponse(200, $response); |
---|
99 | } catch (Exception $e) { |
---|
100 | // Comunication error. |
---|
101 | $response["message"] = $e->getMessage(); |
---|
102 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
103 | jsonResponse(400, $response); |
---|
104 | } |
---|
105 | } |
---|
106 | ); |
---|
107 | |
---|
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 | */ |
---|
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); |
---|
122 | // Check sender IP address consistency (same as parameter value). |
---|
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']); |
---|
125 | } |
---|
126 | // May check if client is included in the server database? |
---|
127 | // Default processing: log activity. |
---|
128 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged in: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
129 | // Response. |
---|
130 | $response = ""; |
---|
131 | jsonResponse(200, $response); |
---|
132 | } catch (Exception $e) { |
---|
133 | // Comunication error. |
---|
134 | $response["message"] = $e->getMessage(); |
---|
135 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
136 | jsonResponse(400, $response); |
---|
137 | } |
---|
138 | } |
---|
139 | ); |
---|
140 | |
---|
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 | */ |
---|
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); |
---|
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']); |
---|
158 | } |
---|
159 | // May check if client is included in the server database? |
---|
160 | // Default processing: log activity. |
---|
161 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged out: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
162 | // Response. |
---|
163 | $response = ""; |
---|
164 | jsonResponse(200, $response); |
---|
165 | } catch (Exception $e) { |
---|
166 | // Comunication error. |
---|
167 | $response["message"] = $e->getMessage(); |
---|
168 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".$app->request()->getResourceUri().": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
169 | jsonResponse(400, $response); |
---|
170 | } |
---|
171 | } |
---|
172 | ); |
---|
173 | |
---|
174 | ?> |
---|