source: admin/WebConsole/rest/ogagent.php @ 8b8e948

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 8b8e948 was 2913439, checked in by ramon <ramongomez@…>, 9 years ago

#718: Guardar en BD la clave de acceso a la API REST de OGAgent en la ruta REST ogagent/started del servidor.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@4984 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[b1735a7]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.
10define('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) {
[2913439]15        global $cmd;
16        $osType = $osVersion = "none";
[b1735a7]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);
[31970a0]22                if (isset($input->ostype))  $osType = htmlspecialchars($input->ostype);
23                if (isset($input->osversion))  $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion));
[210ee85]24                // Client secret key for secure communications.
25                if (isset($input->secret)) {
26                    // Store secret key in DB.
[2913439]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                    }
[210ee85]35                } else {
36                    // Insecure agent exception.
[31970a0]37                    throw new Exception("Insecure OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion.");
[210ee85]38                }
[b1735a7]39                // Default processing: log activity.
[31970a0]40                file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent started: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND);
[b1735a7]41                // Response.
[d98bc86]42                $response = "";
[b1735a7]43                jsonResponse(200, $response);
44        } catch (Exception $e) {
45                // Comunication error.
46                $response["message"] = $e->getMessage();
[4e12ae6]47                file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND);
[b1735a7]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) {
[2913439]56        $osType = $osVersion = "none";
[b1735a7]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);
[31970a0]62                if (isset($input->ostype))  $osType = htmlspecialchars($input->ostype);
63                if (isset($input->osversion))  $osVersion = str_replace(",", ";", htmlspecialchars($input->osversion));
[b1735a7]64                // May check that client is included in the server database?
65                // Default processing: log activity.
[31970a0]66                file_put_contents(LOG_FILE, date(DATE_RSS).": OGAgent stopped: ip=$ip, mac=$mac, os=$osType:$osVersion.\n", FILE_APPEND);
[b1735a7]67                // Response.
[d98bc86]68                $response = "";
[b1735a7]69                jsonResponse(200, $response);
70        } catch (Exception $e) {
71                // Comunication error.
72                $response["message"] = $e->getMessage();
[4e12ae6]73                file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND);
[b1735a7]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.
[0a767fc]89                file_put_contents(LOG_FILE, date(DATE_RSS).": User logged in: ip=$ip, user=$user.\n", FILE_APPEND);
[b1735a7]90                // Response.
[d98bc86]91                $response = "";
[b1735a7]92                jsonResponse(200, $response);
93        } catch (Exception $e) {
94                // Comunication error.
95                $response["message"] = $e->getMessage();
[4e12ae6]96                file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND);
[b1735a7]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.
[31970a0]112                file_put_contents(LOG_FILE, date(DATE_RSS).": User logged out: ip=$ip, user=$user.\n", FILE_APPEND);
[b1735a7]113                // Response.
[d98bc86]114                $response = "";
[b1735a7]115                jsonResponse(200, $response);
116        } catch (Exception $e) {
117                // Comunication error.
118                $response["message"] = $e->getMessage();
[4e12ae6]119                file_put_contents(LOG_FILE, date(DATE_RSS).": ".__FUNCTION__.": ERROR: ".$response["message"]."\n", FILE_APPEND);
[b1735a7]120                jsonResponse(400, $response);
121        }
122    }
123);
124
125?>
126
Note: See TracBrowser for help on using the repository browser.