From d3ed049eb3945f3407b5cc2e2e3303aee0150a56 Mon Sep 17 00:00:00 2001
From: fermin831 <fermin.hernandez@artica.es>
Date: Tue, 22 Sep 2015 14:54:48 +0200
Subject: [PATCH] Added several UDP Server Auth IPs to Windows Agent

---
 pandora_agents/win32/udp_server/udp_server.cc | 60 ++++++++++++-------
 pandora_agents/win32/udp_server/udp_server.h  |  6 +-
 2 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/pandora_agents/win32/udp_server/udp_server.cc b/pandora_agents/win32/udp_server/udp_server.cc
index 09db298c95..4aede22ed6 100644
--- a/pandora_agents/win32/udp_server/udp_server.cc
+++ b/pandora_agents/win32/udp_server/udp_server.cc
@@ -38,16 +38,6 @@ 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.
  * 
@@ -90,9 +80,9 @@ UDP_Server::UDP_Server (Pandora_Windows_Service *service, string address, string
 		this->address = inet_addr (address.c_str ());
 	}
 	if (auth_address.empty ()) {
-		this->auth_address = INADDR_ANY;
+		this->auth_address.push_front(INADDR_ANY);
 	} else {
-	   this->auth_address = inet_addr (auth_address.c_str ());
+	   splitAuthAddress (auth_address);
 	}
 	this->port = port;
 	this->running = 0;
@@ -169,25 +159,21 @@ void Pandora::listen (UDP_Server *server) {
 	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);
+		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) {
+		if (server->isAddressAuth (cliaddr.sin_addr.s_addr)) {
+			mesg[n] = 0;
+			process_command (server->getService (), mesg);
+		} else {
 			pandoraLog ("UDP Server: Unauthorised access from %s", inet_ntoa (cliaddr.sin_addr));
-			continue;
 		}
-
-		mesg[n] = 0;
-		process_command (server->getService (), mesg);
 	}
 
 	WSACleanup ();
@@ -263,3 +249,35 @@ int Pandora::process_command (Pandora_Windows_Service *service, char *command) {
 
 	return 0;
 }
+
+void UDP_Server::splitAuthAddress (string all_address) {	
+	this->auth_address.clear();
+	size_t comma_pos;
+	string single_ip;
+	do {
+		single_ip.clear();
+		/*Splits ips with comma*/
+		comma_pos = all_address.find_first_of (',', 0);
+		if (comma_pos != string::npos){
+			single_ip = all_address.substr (0, comma_pos);
+		} else {
+			single_ip = all_address;
+		}
+		unsigned long single_ip_num = inet_addr (single_ip.c_str ());
+		if (single_ip_num != INADDR_NONE) {
+			this->auth_address.push_back (single_ip_num);
+		} else {
+			pandoraDebug ("Invalid UDP Server Auth Address: %s", single_ip.c_str ());
+		}
+		all_address = all_address.substr (comma_pos + 1, all_address.length ());
+	} while (comma_pos != string::npos);
+}
+
+bool UDP_Server::isAddressAuth (unsigned long ip){	
+	for (this->it=(this->auth_address).begin(); this->it != (this->auth_address).end(); ++it) {
+		if (*it == ip || *it == INADDR_ANY) {
+			return true;
+		}
+	}
+	return false;
+}
diff --git a/pandora_agents/win32/udp_server/udp_server.h b/pandora_agents/win32/udp_server/udp_server.h
index 32631d997b..6a920c8e15 100644
--- a/pandora_agents/win32/udp_server/udp_server.h
+++ b/pandora_agents/win32/udp_server/udp_server.h
@@ -37,20 +37,22 @@ namespace Pandora {
 			UDP_Server (Pandora_Windows_Service *service, string address, string auth_address, unsigned int port);
 			virtual ~UDP_Server ();
 			unsigned long getAddress ();
-			unsigned long getAuthAddress ();
 			unsigned long getPort ();
 			Pandora_Windows_Service *getService ();
 			unsigned char isRunning ();
+			bool isAddressAuth (unsigned long ip);
 
 			int start ();
 			int stop ();
 
 		private:
 			unsigned long address;
-			unsigned long auth_address;
+			list<unsigned long> auth_address;
+			list<unsigned long>::iterator it;
 			unsigned long port;
 			unsigned char running;
 			Pandora_Windows_Service *service;
+			void splitAuthAddress (string all_address);
 	};
 
 	void listen (UDP_Server *server);