mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-30 01:05:39 +02:00
2008-12-11 Ramon Novoa <rnovoa@artica.es>
* win32/windows/pandora_wmi.cc, win32/pandora_windows_service.h, win32/PandoraAgent.dev, win32/pandora_windows_service.cc: Added an UDP Server. The agent can receive remote commands. * win32/udp_server, win32/udp_server/udp_server.cc, win32/udp_server/udp_server.h: Added to repository. UDP Server. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1287 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
parent
7d49c1e929
commit
26603c5166
@ -1,3 +1,20 @@
|
||||
2008-12-11 Ramon Novoa <rnovoa@artica.es>
|
||||
|
||||
* win32/windows/pandora_wmi.cc,
|
||||
win32/pandora_windows_service.h,
|
||||
win32/PandoraAgent.dev,
|
||||
win32/pandora_windows_service.cc: Added an UDP Server. The
|
||||
agent can receive remote commands.
|
||||
|
||||
* win32/udp_server,
|
||||
win32/udp_server/udp_server.cc,
|
||||
win32/udp_server/udp_server.h: Added to repository. UDP Server.
|
||||
|
||||
2008-12-10 Ramon Novoa <rnovoa@artica.es>
|
||||
|
||||
* linux/pandora_agent,
|
||||
linux/pandora_agent.conf: Added support for a secondary server.
|
||||
|
||||
2008-11-25 Sancho Lerena <slerena@gmail.com>
|
||||
|
||||
* linux/pandora_agent: Fixed problem parsing async_string.
|
||||
|
@ -1,7 +1,7 @@
|
||||
[Project]
|
||||
FileName=PandoraAgent.dev
|
||||
Name=PandoraAgent
|
||||
UnitCount=77
|
||||
UnitCount=79
|
||||
Type=1
|
||||
Ver=1
|
||||
ObjFiles=
|
||||
@ -20,7 +20,7 @@ ObjectOutput=
|
||||
OverrideOutput=0
|
||||
OverrideOutputName=PandoraAgent.exe
|
||||
HostApplication=
|
||||
Folders=FTP,Misc,Modules,Modules/Utils,SSH,SSH/libssh2,Windows,Windows/WMI,XML
|
||||
Folders=FTP,Misc,Modules,Modules/Utils,SSH,SSH/libssh2,"UDP Server",Windows,Windows/WMI,XML
|
||||
CommandLine=
|
||||
UseCustomMakefile=0
|
||||
CustomMakefile=
|
||||
@ -817,3 +817,23 @@ Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit78]
|
||||
FileName=udp_server\udp_server.h
|
||||
CompileCpp=1
|
||||
Folder=UDP Server
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
[Unit79]
|
||||
FileName=udp_server\udp_server.cc
|
||||
CompileCpp=1
|
||||
Folder=UDP Server
|
||||
Compile=1
|
||||
Link=1
|
||||
Priority=1000
|
||||
OverrideBuildCmd=0
|
||||
BuildCmd=
|
||||
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "ftp/pandora_ftp_client.h"
|
||||
#include "misc/pandora_file.h"
|
||||
#include "windows/pandora_windows_info.h"
|
||||
#include "udp_server/udp_server.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
@ -104,6 +105,8 @@ Pandora_Windows_Service::start () {
|
||||
void
|
||||
Pandora_Windows_Service::pandora_init () {
|
||||
string conf_file, interval, debug, transfer_interval;
|
||||
string udp_server_enabled, udp_server_port, udp_server_addr, udp_server_auth_addr;
|
||||
static UDP_Server *udp_server = NULL;
|
||||
|
||||
setPandoraDebug (true);
|
||||
|
||||
@ -144,6 +147,16 @@ Pandora_Windows_Service::pandora_init () {
|
||||
this->setSleepTime (this->interval);
|
||||
|
||||
pandoraLog ("Pandora agent started");
|
||||
|
||||
/* Launch UDP Server */
|
||||
udp_server_enabled = conf->getValue ("udp_server");
|
||||
if (udp_server == NULL && udp_server_enabled.compare ("1") == 0) {
|
||||
udp_server_port = conf->getValue ("udp_server_port");
|
||||
udp_server_addr = conf->getValue ("udp_server_address");
|
||||
udp_server_auth_addr = conf->getValue ("udp_server_auth_address");
|
||||
udp_server = new UDP_Server (this, udp_server_addr, udp_server_auth_addr, atoi (udp_server_port.c_str ()));
|
||||
udp_server->start ();
|
||||
}
|
||||
}
|
||||
|
||||
TiXmlElement *
|
||||
@ -732,3 +745,8 @@ Pandora_Windows_Service::pandora_run () {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Pandora_Agent_Conf *
|
||||
Pandora_Windows_Service::getConf () {
|
||||
return this->conf;
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ namespace Pandora {
|
||||
|
||||
void start ();
|
||||
void sendXml (Pandora_Module_List *modules);
|
||||
Pandora_Agent_Conf *getConf ();
|
||||
};
|
||||
}
|
||||
|
||||
|
241
pandora_agents/win32/udp_server/udp_server.cc
Normal file
241
pandora_agents/win32/udp_server/udp_server.cc
Normal file
@ -0,0 +1,241 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <WinSock2.h>
|
||||
|
||||
#include "udp_server.h"
|
||||
#include "../pandora.h"
|
||||
#include "../windows/pandora_wmi.h"
|
||||
|
||||
using namespace Pandora;
|
||||
|
||||
/**
|
||||
* Get the address of the server.
|
||||
*
|
||||
* @return Server address.
|
||||
*/
|
||||
unsigned long UDP_Server::getAddress () {
|
||||
return this->address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the address authorized to send commands to
|
||||
* the server.
|
||||
*
|
||||
* @return Authorized address.
|
||||
*/
|
||||
unsigned long UDP_Server::getAuthAddress () {
|
||||
return this->auth_address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the port of the server.
|
||||
*
|
||||
* @return Server port.
|
||||
*/
|
||||
unsigned long UDP_Server::getPort () {
|
||||
return this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the windows service associated to the server.
|
||||
*
|
||||
* @return Windows service associated to the server.
|
||||
*/
|
||||
Pandora_Windows_Service *UDP_Server::getService () {
|
||||
return this->service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the state of the server.
|
||||
*
|
||||
* @return 1 if the server is running, 0 if not.
|
||||
*/
|
||||
unsigned char UDP_Server::isRunning () {
|
||||
return this->running;
|
||||
}
|
||||
|
||||
/**
|
||||
* UDP_Server constructor.
|
||||
*
|
||||
* @param service Service associated to the server.
|
||||
* @param address Server address.
|
||||
* @param auth_address Authorized address.
|
||||
* @param port Server port.
|
||||
*/
|
||||
UDP_Server::UDP_Server (Pandora_Windows_Service *service, string address, string auth_address, unsigned int port) {
|
||||
if (address.empty ()) {
|
||||
this->address = INADDR_ANY;
|
||||
} else {
|
||||
this->address = inet_addr (address.c_str ());
|
||||
}
|
||||
if (auth_address.empty ()) {
|
||||
this->auth_address = INADDR_ANY;
|
||||
} else {
|
||||
this->auth_address = inet_addr (auth_address.c_str ());
|
||||
}
|
||||
this->port = port;
|
||||
this->running = 0;
|
||||
this->service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the server.
|
||||
*
|
||||
* @return 1 on error, 0 otherwise.
|
||||
*/
|
||||
int UDP_Server::start () {
|
||||
if (this->running != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Run in a new thread */
|
||||
this->running = 1;
|
||||
if (CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) listen, this, 0, NULL) == NULL) {
|
||||
this->running = 0;
|
||||
pandoraLog ("UDP Server: Error starting UDP Server thread");
|
||||
return 1;
|
||||
}
|
||||
|
||||
pandoraLog ("UDP Server: UDP Server started on port %d", this->port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the server.
|
||||
*
|
||||
* @return 1 on error, 0 otherwise.
|
||||
*/
|
||||
int UDP_Server::stop () {
|
||||
if (this->running != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
this->running = 0;
|
||||
pandoraLog ("UDP Server: UDP Server going down");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens for incoming packets.
|
||||
*
|
||||
* @param server UDP Server.
|
||||
*/
|
||||
void Pandora::listen (UDP_Server *server) {
|
||||
int sockfd,n;
|
||||
struct sockaddr_in servaddr, cliaddr;
|
||||
int len, err;
|
||||
char mesg[MAX_PACKET_SIZE];
|
||||
unsigned long auth_addr;
|
||||
WSADATA wsa;
|
||||
|
||||
err = WSAStartup (MAKEWORD (2,0), &wsa);
|
||||
if (err != 0) {
|
||||
/* Could not find a usable Winsock DLL */
|
||||
printf("UDP Server: WSAStartup failed with error: %d\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
sockfd = socket (AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
memset (&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = htonl (server->getAddress ());
|
||||
servaddr.sin_port = htons (server->getPort ());
|
||||
bind(sockfd, (struct sockaddr *)&servaddr, sizeof (servaddr));
|
||||
|
||||
/* Get authorised address */
|
||||
auth_addr = server->getAuthAddress ();
|
||||
|
||||
while (server->isRunning () == 1) {
|
||||
len = sizeof(cliaddr);
|
||||
n = recvfrom(sockfd, mesg, MAX_PACKET_SIZE, 0, (struct sockaddr *)&cliaddr, &len);
|
||||
if (n == SOCKET_ERROR) {
|
||||
pandoraLog ("UDP Server: Error %d", WSAGetLastError ());
|
||||
break;
|
||||
}
|
||||
|
||||
/* Authenticate client */
|
||||
if (auth_addr != INADDR_ANY && auth_addr != cliaddr.sin_addr.s_addr) {
|
||||
pandoraLog ("UDP Server: Unauthorised access from %s", inet_ntoa (cliaddr.sin_addr));
|
||||
continue;
|
||||
}
|
||||
|
||||
mesg[n] = 0;
|
||||
process_command (server->getService (), mesg);
|
||||
}
|
||||
|
||||
WSACleanup ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes and executes server commands.
|
||||
*
|
||||
* @param service Windows service associated to the server.
|
||||
* @param command Server command.
|
||||
*
|
||||
* @return 1 on error, 0 otherwise.
|
||||
*/
|
||||
int Pandora::process_command (Pandora_Windows_Service *service, char *command) {
|
||||
int rc;
|
||||
char operation[MAX_PACKET_SIZE], action[MAX_PACKET_SIZE], target[MAX_PACKET_SIZE];
|
||||
string var, value;
|
||||
Pandora_Agent_Conf *conf = NULL;
|
||||
|
||||
rc = sscanf (command, "%s %s %s", operation, action, target);
|
||||
if (rc < 3) {
|
||||
pandoraLog ("UDP Server: Received invalid data: %s", command);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Re-run */
|
||||
if (strcmp (operation, "REFRESH") == 0) {
|
||||
service->pandora_run ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
conf = service->getConf();
|
||||
|
||||
/* Service management */
|
||||
if (strcmp (action, "SERVICE") == 0) {
|
||||
var = "service_";
|
||||
var.append (target);
|
||||
std::transform(var.begin(), var.end(), var.begin(), ::tolower);
|
||||
value = conf->getValue (var);
|
||||
if (atoi (value.c_str ()) != 1) {
|
||||
pandoraLog ("UDP Server: Unauthorised access to service %s", target);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp (operation, "START") == 0) {
|
||||
Pandora_Wmi::startService (target);
|
||||
} else if (strcmp (operation, "STOP") == 0) {
|
||||
Pandora_Wmi::stopService (target);
|
||||
}
|
||||
}
|
||||
|
||||
/* Process management */
|
||||
if (strcmp (action, "PROCESS") == 0) {
|
||||
var = "process_";
|
||||
var.append (target);
|
||||
std::transform(var.begin(), var.end(), var.begin(), ::tolower);
|
||||
|
||||
if (strcmp (operation, "START") == 0) {
|
||||
var.append ("_start");
|
||||
} else if (strcmp (operation, "STOP") == 0) {
|
||||
var.append ("_stop");
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
|
||||
value = conf->getValue (var);
|
||||
if (value.empty ()) {
|
||||
pandoraLog ("UDP Server: Unauthorised access to process %s", target);
|
||||
return 1;
|
||||
}
|
||||
|
||||
system (value.c_str());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
38
pandora_agents/win32/udp_server/udp_server.h
Normal file
38
pandora_agents/win32/udp_server/udp_server.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef __UDP_SERVER_H__
|
||||
#define __UDP_SERVER_H__
|
||||
|
||||
#define MAX_PACKET_SIZE 1024
|
||||
|
||||
#include "../pandora_windows_service.h"
|
||||
|
||||
namespace Pandora {
|
||||
|
||||
/**
|
||||
* UDP Server class.
|
||||
*/
|
||||
class UDP_Server {
|
||||
public:
|
||||
UDP_Server (Pandora_Windows_Service *service, string address, string auth_address, unsigned int port);
|
||||
~UDP_Server ();
|
||||
unsigned long getAddress ();
|
||||
unsigned long getAuthAddress ();
|
||||
unsigned long getPort ();
|
||||
Pandora_Windows_Service *getService ();
|
||||
unsigned char isRunning ();
|
||||
|
||||
int start ();
|
||||
int stop ();
|
||||
|
||||
private:
|
||||
unsigned long address;
|
||||
unsigned long auth_address;
|
||||
unsigned long port;
|
||||
unsigned char running;
|
||||
Pandora_Windows_Service *service;
|
||||
};
|
||||
|
||||
void listen (UDP_Server *server);
|
||||
int process_command (Pandora_Windows_Service *service, char *command);
|
||||
}
|
||||
|
||||
#endif
|
@ -611,7 +611,7 @@ Pandora_Wmi::stopService (string service_name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
service = OpenService (manager, service_name.c_str (), GENERIC_EXECUTE);
|
||||
service = OpenService (manager, service_name.c_str (), SERVICE_STOP);
|
||||
if (service == NULL) {
|
||||
pandoraLog ("Could not access to service \"%s\" to stop.",
|
||||
service_name.c_str ());
|
||||
|
Loading…
x
Reference in New Issue
Block a user