1 | <?php |
---|
2 | |
---|
3 | // OpenGnsys REST routes for OGAgent communications. |
---|
4 | // Author: Ramón M. Gómez |
---|
5 | // Date: 2015-09-04 |
---|
6 | // Warning: authentication/authorisation not included. |
---|
7 | |
---|
8 | |
---|
9 | // OGAgent sessions log file. |
---|
10 | define('LOG_FILE', '/opt/opengnsys/log/ogagent.log'); |
---|
11 | |
---|
12 | // OGAgent notifies that its service is started on client. |
---|
13 | $app->post('/ogagent/started', |
---|
14 | function() use ($app) { |
---|
15 | global $cmd; |
---|
16 | $osType = $osVersion = "none"; |
---|
17 | try { |
---|
18 | // Reading POST parameters in JSON format. |
---|
19 | $input = json_decode($app->request()->getBody()); |
---|
20 | $ip = htmlspecialchars($input->ip); |
---|
21 | $mac = htmlspecialchars($input->mac); |
---|
22 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
23 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
24 | // Client secret key for secure communications. |
---|
25 | if (isset($input->secret)) { |
---|
26 | // Store secret key in DB. |
---|
27 | $secret = htmlspecialchars($input->secret); |
---|
28 | $cmd->texto = "UPDATE ordenadores |
---|
29 | SET agentkey='$secret' |
---|
30 | WHERE ip='$ip' AND mac=UPPER(REPLACE('$mac',':',''))"; |
---|
31 | if ($cmd->Ejecutar() !== true) { |
---|
32 | // DB access error. |
---|
33 | throw new Exception("Cannot store secret key: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
34 | } |
---|
35 | } else { |
---|
36 | // Insecure agent exception. |
---|
37 | throw new Exception("Insecure OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion."); |
---|
38 | } |
---|
39 | // Default processing: log activity. |
---|
40 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
41 | // Response. |
---|
42 | $response = ""; |
---|
43 | jsonResponse(200, $response); |
---|
44 | } catch (Exception $e) { |
---|
45 | // Comunication error. |
---|
46 | $response["message"] = $e->getMessage(); |
---|
47 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
48 | jsonResponse(400, $response); |
---|
49 | } |
---|
50 | } |
---|
51 | ); |
---|
52 | |
---|
53 | // OGAgent notifies that its service is stopped on client. |
---|
54 | $app->post('/ogagent/stopped', |
---|
55 | function() use ($app) { |
---|
56 | $osType = $osVersion = "none"; |
---|
57 | try { |
---|
58 | // Reading POST parameters in JSON format. |
---|
59 | $input = json_decode($app->request()->getBody()); |
---|
60 | $ip = htmlspecialchars($input->ip); |
---|
61 | $mac = htmlspecialchars($input->mac); |
---|
62 | if (isset($input->ostype)) $osType = htmlspecialchars($input->ostype); |
---|
63 | if (isset($input->osversion)) $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion)); |
---|
64 | // May check that client is included in the server database? |
---|
65 | // Default processing: log activity. |
---|
66 | file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent stopped: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND); |
---|
67 | // Response. |
---|
68 | $response = ""; |
---|
69 | jsonResponse(200, $response); |
---|
70 | } catch (Exception $e) { |
---|
71 | // Comunication error. |
---|
72 | $response["message"] = $e->getMessage(); |
---|
73 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
74 | jsonResponse(400, $response); |
---|
75 | } |
---|
76 | } |
---|
77 | ); |
---|
78 | |
---|
79 | // OGAgent notifies that an user logs in. |
---|
80 | $app->post('/ogagent/loggedin', |
---|
81 | function() use ($app) { |
---|
82 | try { |
---|
83 | // Reading POST parameters in JSON format. |
---|
84 | $input = json_decode($app->request()->getBody()); |
---|
85 | $ip = htmlspecialchars($input->ip); |
---|
86 | $user = htmlspecialchars($input->user); |
---|
87 | // May check that client is included in the server database? |
---|
88 | // Default processing: log activity. |
---|
89 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged in: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
90 | // Response. |
---|
91 | $response = ""; |
---|
92 | jsonResponse(200, $response); |
---|
93 | } catch (Exception $e) { |
---|
94 | // Comunication error. |
---|
95 | $response["message"] = $e->getMessage(); |
---|
96 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
97 | jsonResponse(400, $response); |
---|
98 | } |
---|
99 | } |
---|
100 | ); |
---|
101 | |
---|
102 | // OGAgent notifies that an user logs out. |
---|
103 | $app->post('/ogagent/loggedout', |
---|
104 | function() use ($app) { |
---|
105 | try { |
---|
106 | // Reading POST parameters in JSON format. |
---|
107 | $input = json_decode($app->request()->getBody()); |
---|
108 | $ip = htmlspecialchars($input->ip); |
---|
109 | $user = htmlspecialchars($input->user); |
---|
110 | // May check that client is included in the server database? |
---|
111 | // Default processing: log activity. |
---|
112 | file_put_contents(LOG_FILE, date(DATE_RSS).": User logged out: ip=$ip, user=$user.\n", FILE_APPEND); |
---|
113 | // Response. |
---|
114 | $response = ""; |
---|
115 | jsonResponse(200, $response); |
---|
116 | } catch (Exception $e) { |
---|
117 | // Comunication error. |
---|
118 | $response["message"] = $e->getMessage(); |
---|
119 | file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND); |
---|
120 | jsonResponse(400, $response); |
---|
121 | } |
---|
122 | } |
---|
123 | ); |
---|
124 | |
---|
125 | ?> |
---|
126 | |
---|