From 88d9c9b51b41bf11cfb69f33b4e1df157281d6d4 Mon Sep 17 00:00:00 2001 From: Claudio Scafuri <claudio.scafuri@elettra.eu> Date: Fri, 2 Oct 2020 17:23:24 +0200 Subject: [PATCH] use enum for priveleges values --- src/SupervisedID.cpp | 30 +++++++++++++++++------------- src/SupervisedID.h | 10 ++++------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/SupervisedID.cpp b/src/SupervisedID.cpp index 15b77d2..bf455d1 100644 --- a/src/SupervisedID.cpp +++ b/src/SupervisedID.cpp @@ -314,7 +314,7 @@ void SupervisedID::add_dynamic_commands() * AUTHORIZED : can set gap/tapering when enabled * NORMAL : can only read; */ -int SupervisedID::caller_privileges(void) +privilege SupervisedID::caller_privileges(void) { //if no privileges are configured, run unimpeded if(hostMap.size() == 0)return PRIVILEGED; @@ -328,14 +328,6 @@ int SupervisedID::caller_privileges(void) loc=clientAddress.find(" ",0); clientAddress =clientAddress.substr(0,loc); INFO_STREAM<<"checking call from "<<clientAddress<<endl; - // std::set<std::string> client_ips; - // get_ips_from_host(clientAddress, client_ips); - // - // for (std::set<std::string>::const_iterator it = client_ips.begin(); it != client_ips.end(); ++it ) - // { - // if (std::find(allowed_ips.begin(), allowed_ips.end(), *it) != allowed_ips.end()) - // return; // found the IP in the allowed list of IPs - // } map<string,int>::iterator it; it=hostMap.find(clientAddress); if (it == hostMap.end()) { @@ -343,8 +335,20 @@ int SupervisedID::caller_privileges(void) return NORMAL; } else { - INFO_STREAM << "host: "<< clientAddress<< " privilges="<< it->second << endl; - return it->second; + INFO_STREAM << "host: " << clientAddress << " privilges=" + << it->second << endl; + switch (it->second) { + case 0: + return NORMAL; + case 1: + return AUTHORIZED; + case 2: + return PRIVILEGED; + default: + return NORMAL; + + } + } } @@ -366,9 +370,9 @@ int SupervisedID::caller_privileges(void) * NORMAL : can only read; */ -int SupervisedID::check_privileges(int level, string msg) +privilege SupervisedID::check_privileges(privilege level, string msg) { - int priv=caller_privileges(); + privilege priv=caller_privileges(); if (priv < level){ INFO_STREAM << msg << " from "<< clientAddress <<" REJECTED"<<endl; TangoSys_OMemStream o; diff --git a/src/SupervisedID.h b/src/SupervisedID.h index d794560..f4c2ba2 100644 --- a/src/SupervisedID.h +++ b/src/SupervisedID.h @@ -37,9 +37,7 @@ #include <tango.h> #include <map> -#define PRIVILEGED 2 -#define AUTHORIZED 1 -#define NORMAL 0 + /*----- PROTECTED REGION END -----*/ // SupervisedID.h /** @@ -56,7 +54,7 @@ namespace SupervisedID_ns // Additional Class Declarations /*----- PROTECTED REGION END -----*/ // SupervisedID::Additional Class Declarations - +enum privilege{NORMAL,AUTHORIZED,PRIVILEGED}; class SupervisedID : public TANGO_BASE_CLASS { @@ -64,11 +62,11 @@ class SupervisedID : public TANGO_BASE_CLASS // Add your own data members protected: - int caller_privileges(void); + privilege caller_privileges(void); bool is_privileged; //true is the caller has all privileges - must be handled in alawys_executed string clientAddress; map<string,int> hostMap; - int check_privileges(int, string); //check privileges , emits diagnostics and throw exception, returns actual privileges + privilege check_privileges(privilege, string); //check privileges , emits diagnostics and throw exception, returns actual privileges /*----- PROTECTED REGION END -----*/ // SupervisedID::Data Members -- GitLab